SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
ValueItemDefinition.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 // .NAME ValueItemDefinition.h -
11 // .SECTION Description
12 // The base class for attribute items that have an input value.
13 // It can have a specified number of values (m_numberOfRequiredValues > 0)
14 // or an unbounded number of values (m_numberOfRequredValues == 0).
15 // m_valueLabels is used to store labels for individual component values
16 // but can hold a common label that should be used for all components.
17 // m_valueLabels should only be used if m_numberOfRequiredValues is not 1.
18 // .SECTION See Also
19 
20 #ifndef smtk_attribute_ValueItemDefinition_h
21 #define smtk_attribute_ValueItemDefinition_h
22 
23 #include <cassert>
24 #include <map>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 #include "smtk/CoreExports.h"
30 #include "smtk/PublicPointerDefs.h"
31 #include "smtk/attribute/ItemDefinition.h"
32 #include "smtk/attribute/VoidItemDefinition.h"
33 
34 namespace smtk
35 {
36 namespace attribute
37 {
38 class Attribute;
39 class Cluster;
40 class ValueItem;
42 {
43 public:
45  ValueItemDefinition(const std::string& myname);
46  ~ValueItemDefinition() override;
47 
48  const std::string& units() const { return m_units; }
49  void setUnits(const std::string& newUnits) { m_units = newUnits; }
50 
51  bool isDiscrete() const { return !m_discreteValueEnums.empty(); }
52  std::size_t numberOfDiscreteValues() const { return m_discreteValueEnums.size(); }
53  const std::string& discreteEnum(std::size_t ith) const
54  {
55  assert(m_discreteValueEnums.size() > ith);
56  return m_discreteValueEnums[ith];
57  }
58  bool getEnumIndex(const std::string& enumVal, std::size_t& index) const;
59  int defaultDiscreteIndex() const { return m_defaultDiscreteIndex; }
60  void setDefaultDiscreteIndex(int discreteIndex);
61 
62  bool allowsExpressions() const;
63  bool isValidExpression(const smtk::attribute::AttributePtr& exp) const;
64  std::string expressionType() const { return m_expressionType; }
65  void setExpressionType(const std::string& etype) { m_expressionType = etype; }
66  void setExpressionDefinition(const smtk::attribute::DefinitionPtr& exp);
67  smtk::attribute::DefinitionPtr expressionDefinition(
68  const smtk::attribute::ResourcePtr& attResource) const;
69  // Should only be called internally by the ValueItem
70  void buildExpressionItem(ValueItem* vitem) const;
71  void buildChildrenItems(ValueItem* vitem) const;
72 
73  bool hasDefault() const { return m_hasDefault; }
74 
75  virtual bool hasRange() const = 0;
76 
77  // Returns or Sets the minimum number of values that items from this def can have
78  // Default value is 1
79  std::size_t numberOfRequiredValues() const { return m_numberOfRequiredValues; }
80 
81  // Returns false if esize is greater than max number of values (and max number > 0)
82  bool setNumberOfRequiredValues(std::size_t esize);
83 
84  // Returns or Sets the maximum number of values that items from this def can have.
85  // if 0 is returned then there is no max limit. Default value is 0
86  // Note that this is used only when the def is extensible
87  std::size_t maxNumberOfValues() const { return m_maxNumberOfValues; }
88 
89  // Returns false if esize is less than number of required values (and esize > 0)
90  bool setMaxNumberOfValues(std::size_t esize);
91 
92  // Returns or Sets the def's extensiblity property. If true then items from this def
93  // can have a variable number of values. The number of values is always <= to number of
94  // required values and max number of values (provided max number of values > 0)
95  // Default value is false.
96  bool isExtensible() const { return m_isExtensible; }
97  void setIsExtensible(bool mode);
98 
99  // Description:
100  // Return whether or not there are labels for components.
101  // There should only be labels if there is more than a single
102  // component (i.e. m_numberOfRequiredValues != 1).
103  bool hasValueLabels() const { return !m_valueLabels.empty(); }
104 
105  // Description:
106  // Specify whether the components label is coming from a common
107  // label that is repeated.
108  bool usingCommonLabel() const { return m_useCommonLabel; }
109  void setValueLabel(std::size_t element, const std::string& elabel);
110  void setCommonValueLabel(const std::string& elabel);
111  // Description:
112  // Get the component label for specified element component. This
113  // takes into account whether to use the common label or specific
114  // component label.
115  std::string valueLabel(std::size_t element) const;
116  bool isDiscreteIndexValid(int index) const
117  {
118  return ((index > -1) && (static_cast<unsigned int>(index) < m_discreteValueEnums.size()));
119  }
120 
122  std::size_t numberOfChildrenItemDefinitions() const { return m_itemDefs.size(); }
123 
124  const std::map<std::string, smtk::attribute::ItemDefinitionPtr>& childrenItemDefinitions() const
125  {
126  return m_itemDefs;
127  }
128 
129  // returns true if this item has a child item definition of itemName
130  bool hasChildItemDefinition(const std::string& itemName) const
131  {
132  return (m_itemDefs.find(itemName) != m_itemDefs.end());
133  }
134 
135  // returns true if valueName has a child item definition of itemName
136  bool hasChildItemDefinition(const std::string& valueName, const std::string& itemName);
137 
138  bool addChildItemDefinition(smtk::attribute::ItemDefinitionPtr cdef);
139 
142  void setEnumCategories(
143  const std::string& enumValue,
145  void addEnumCategory(const std::string& enumValue, const std::string& cat);
146  const smtk::attribute::Categories::Set& enumCategories(const std::string& enumValue) const;
148 
151  void setEnumAdvanceLevel(const std::string& enumValue, unsigned int level);
152  void unsetEnumAdvanceLevel(const std::string& enumValue);
153  unsigned int enumAdvanceLevel(const std::string& enumValue) const;
154  bool hasEnumAdvanceLevel(const std::string& enumValue) const;
155  const std::map<std::string, unsigned int> enumAdvanceLevelInfo() const
156  {
157  return m_valueToAdvanceLevelAssociations;
158  }
160 
161  // Description:
162  // Create an item definition based on a given idName. If an item
163  // with that name already exists then return a shared_pointer
164  // that points to nullptr.
165  bool addItemDefinition(smtk::attribute::ItemDefinitionPtr cdef);
166  template<typename T>
167  typename smtk::internal::shared_ptr_type<T>::SharedPointerType addItemDefinition(
168  const std::string& idName)
169  {
170  typedef smtk::internal::shared_ptr_type<T> SharedTypes;
171  typename SharedTypes::SharedPointerType item;
172 
173  // First see if there is a item by the same name
174  if (this->hasChildItemDefinition(idName))
175  {
176  // Already has an item of this name - do nothing
177  return item;
178  }
179  item = SharedTypes::RawPointerType::New(idName);
180  m_itemDefs[item->name()] = item;
181  return item;
182  }
183 
184  bool addConditionalItem(const std::string& enumValue, const std::string& itemName);
185  std::vector<std::string> conditionalItems(const std::string& enumValue) const;
186 
188  std::vector<std::string> relevantEnums(
189  bool includeCategories,
190  const std::set<std::string>& testCategories,
191  bool includeReadAccess,
192  unsigned int readAccessLevel) const;
193 
194 protected:
196  void applyCategories(
197  const smtk::attribute::Categories::Stack& inheritedFromParent,
198  smtk::attribute::Categories& inheritedToParent) override;
199  void applyAdvanceLevels(
200  const unsigned int& readLevelFromParent,
201  const unsigned int& writeLevelFromParent) override;
202 
203  virtual void updateDiscreteValue() = 0;
204  bool m_hasDefault;
205  bool m_useCommonLabel;
206  std::vector<std::string> m_valueLabels;
207  std::vector<std::string> m_discreteValueEnums;
208  int m_defaultDiscreteIndex;
209  std::size_t m_numberOfRequiredValues;
210  std::size_t m_maxNumberOfValues;
211  bool m_isExtensible;
212  std::string m_units;
213  std::string m_expressionType;
214  smtk::attribute::ComponentItemDefinitionPtr m_expressionDefinition;
215  std::map<std::string, smtk::attribute::ItemDefinitionPtr> m_itemDefs;
216  std::map<std::string, std::set<std::string>> m_itemToValueAssociations;
217  std::map<std::string, std::vector<std::string>> m_valueToItemAssociations;
218  std::map<std::string, smtk::attribute::Categories::Set> m_valueToCategoryAssociations;
219  std::map<std::string, unsigned int> m_valueToAdvanceLevelAssociations;
220 
221 private:
222 };
223 } // namespace attribute
224 } // namespace smtk
225 
226 // returns true if valueName has a child item definition of itemName
227 inline bool smtk::attribute::ValueItemDefinition::hasChildItemDefinition(
228  const std::string& valueName,
229  const std::string& itemName)
230 {
231  // First we need to check to see if we have this child item
232  if (!this->hasChildItemDefinition(itemName))
233  {
234  return false;
235  }
236  return (
237  m_itemToValueAssociations[itemName].find(valueName) !=
238  m_itemToValueAssociations[itemName].end());
239 }
240 
241 #endif /* smtk_attribute_ValueItemDefinition_h */
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
PublicPointerDefs.h
smtk::attribute::DefinitionPtr
smtk::shared_ptr< smtk::attribute::Definition > DefinitionPtr
Definition: PublicPointerDefs.h:450
smtk::attribute::Categories::Stack
Definition: Categories.h:167
smtk::attribute::ValueItemDefinition::numberOfChildrenItemDefinitions
std::size_t numberOfChildrenItemDefinitions() const
For conditional children items based on the item's current discrete value.
Definition: ValueItemDefinition.h:122
smtk::attribute::ItemDefinition
Definition: ItemDefinition.h:37
smtk::internal::shared_ptr_type
Definition: PublicPointerDefs.h:729
smtk::attribute::ItemDefinition::CopyInfo
Definition: ItemDefinition.h:47
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::attribute::ValueItemDefinition::enumAdvanceLevelInfo
const std::map< std::string, unsigned int > enumAdvanceLevelInfo() const
Set/Get advance level associated with associated with a value Enum.
Definition: ValueItemDefinition.h:155
smtk::attribute::AttributePtr
smtk::shared_ptr< smtk::attribute::Attribute > AttributePtr
Definition: PublicPointerDefs.h:456
smtk::attribute::ComponentItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ComponentItemDefinition > ComponentItemDefinitionPtr
Definition: PublicPointerDefs.h:541
smtk::attribute::ValueItemDefinition
Definition: ValueItemDefinition.h:41
smtk::attribute::ValueItem
Definition: ValueItem.h:31
smtk::attribute::Categories
Represents the category constraints associated with an Attribute, Attribute Definition,...
Definition: Categories.h:34
smtk::attribute::Categories::Set
Categories::Set represents a single category constraint used by the Categories class.
Definition: Categories.h:75
smtk::attribute::ValueItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ValueItemDefinition > ValueItemDefinitionPtr
Definition: PublicPointerDefs.h:484
smtk::attribute::ItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ItemDefinition > ItemDefinitionPtr
Definition: PublicPointerDefs.h:473
smtk::attribute::ResourcePtr
smtk::shared_ptr< smtk::attribute::Resource > ResourcePtr
Definition: PublicPointerDefs.h:604