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  virtual bool setUnits(const std::string& 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  bool isDiscreteIndexValid(int index, const std::set<std::string>& categories) const;
118 
120  std::size_t numberOfChildrenItemDefinitions() const { return m_itemDefs.size(); }
121 
122  const std::map<std::string, smtk::attribute::ItemDefinitionPtr>& childrenItemDefinitions() const
123  {
124  return m_itemDefs;
125  }
126 
127  // returns true if this item has a child item definition of itemName
128  bool hasChildItemDefinition(const std::string& itemName) const
129  {
130  return (m_itemDefs.find(itemName) != m_itemDefs.end());
131  }
132 
133  // returns true if valueName has a child item definition of itemName
134  bool hasChildItemDefinition(const std::string& valueName, const std::string& itemName);
135 
136  bool addChildItemDefinition(smtk::attribute::ItemDefinitionPtr cdef);
137 
140  void setEnumCategories(
141  const std::string& enumValue,
143  void addEnumCategory(const std::string& enumValue, const std::string& cat);
144  const smtk::attribute::Categories::Expression& enumCategories(const std::string& enumValue) const;
146 
149  void setEnumAdvanceLevel(const std::string& enumValue, unsigned int level);
150  void unsetEnumAdvanceLevel(const std::string& enumValue);
151  unsigned int enumAdvanceLevel(const std::string& enumValue) const;
152  bool hasEnumAdvanceLevel(const std::string& enumValue) const;
153  const std::map<std::string, unsigned int> enumAdvanceLevelInfo() const
154  {
155  return m_valueToAdvanceLevelAssociations;
156  }
158 
159  // Description:
160  // Create an item definition based on a given idName. If an item
161  // with that name already exists then return a shared_pointer
162  // that points to nullptr.
163  bool addItemDefinition(smtk::attribute::ItemDefinitionPtr cdef);
164  template<typename T>
165  typename smtk::internal::shared_ptr_type<T>::SharedPointerType addItemDefinition(
166  const std::string& idName)
167  {
168  typedef smtk::internal::shared_ptr_type<T> SharedTypes;
169  typename SharedTypes::SharedPointerType item;
170 
171  // First see if there is a item by the same name
172  if (this->hasChildItemDefinition(idName))
173  {
174  // Already has an item of this name - do nothing
175  return item;
176  }
177  item = SharedTypes::RawPointerType::New(idName);
178  m_itemDefs[item->name()] = item;
179  // We need to get a pointer to the base Item class to set the unitsSystem
180  static_cast<ItemDefinition*>(item.get())->setUnitsSystem(m_unitsSystem);
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  void setUnitsSystem(const shared_ptr<units::System>& unitsSystem) override;
204 
205  virtual void updateDiscreteValue() = 0;
206  bool m_hasDefault;
207  bool m_useCommonLabel;
208  std::vector<std::string> m_valueLabels;
209  std::vector<std::string> m_discreteValueEnums;
210  int m_defaultDiscreteIndex;
211  std::size_t m_numberOfRequiredValues;
212  std::size_t m_maxNumberOfValues;
213  bool m_isExtensible;
214  std::string m_units;
215  std::string m_expressionType;
216  smtk::attribute::ComponentItemDefinitionPtr m_expressionDefinition;
217  std::map<std::string, smtk::attribute::ItemDefinitionPtr> m_itemDefs;
218  std::map<std::string, std::set<std::string>> m_itemToValueAssociations;
219  std::map<std::string, std::vector<std::string>> m_valueToItemAssociations;
220  std::map<std::string, smtk::attribute::Categories::Expression> m_valueToCategoryAssociations;
221  std::map<std::string, unsigned int> m_valueToAdvanceLevelAssociations;
222 
223 private:
224 };
225 } // namespace attribute
226 } // namespace smtk
227 
228 // returns true if valueName has a child item definition of itemName
229 inline bool smtk::attribute::ValueItemDefinition::hasChildItemDefinition(
230  const std::string& valueName,
231  const std::string& itemName)
232 {
233  // First we need to check to see if we have this child item
234  if (!this->hasChildItemDefinition(itemName))
235  {
236  return false;
237  }
238  return (
239  m_itemToValueAssociations[itemName].find(valueName) !=
240  m_itemToValueAssociations[itemName].end());
241 }
242 
243 #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:457
smtk::attribute::Categories::Stack
Definition: Categories.h:225
smtk::attribute::ValueItemDefinition::numberOfChildrenItemDefinitions
std::size_t numberOfChildrenItemDefinitions() const
For conditional children items based on the item's current discrete value.
Definition: ValueItemDefinition.h:120
smtk::attribute::ItemDefinition
Definition: ItemDefinition.h:42
smtk::internal::shared_ptr_type
Definition: PublicPointerDefs.h:756
smtk::attribute::Categories::Expression
Definition: Categories.h:166
smtk::attribute::ItemDefinition::CopyInfo
Definition: ItemDefinition.h:52
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:128
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:153
smtk::attribute::AttributePtr
smtk::shared_ptr< smtk::attribute::Attribute > AttributePtr
Definition: PublicPointerDefs.h:463
smtk::attribute::ComponentItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ComponentItemDefinition > ComponentItemDefinitionPtr
Definition: PublicPointerDefs.h:548
smtk::attribute::ValueItemDefinition
Definition: ValueItemDefinition.h:41
smtk::attribute::ValueItem
A concrete base class for items that store a vector of plain-old-data (POD) as values.
Definition: ValueItem.h:37
smtk::attribute::Categories
Represents the category constraints associated with an Attribute, Attribute Definition,...
Definition: Categories.h:33
smtk::attribute::ValueItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ValueItemDefinition > ValueItemDefinitionPtr
Definition: PublicPointerDefs.h:491
smtk::attribute::ItemDefinitionPtr
smtk::shared_ptr< smtk::attribute::ItemDefinition > ItemDefinitionPtr
Definition: PublicPointerDefs.h:480
smtk::attribute::ResourcePtr
smtk::shared_ptr< smtk::attribute::Resource > ResourcePtr
Definition: PublicPointerDefs.h:611