SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Resource.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 
11 #ifndef smtk_attribute_Resource_h
12 #define smtk_attribute_Resource_h
13 
14 #include "smtk/common/Factory.h"
15 #include "smtk/common/UUID.h"
16 
17 #include "smtk/geometry/Resource.h"
18 
19 #include "smtk/resource/Component.h"
20 #include "smtk/resource/DerivedFrom.h"
21 #include "smtk/resource/Links.h"
22 
23 #include "smtk/CoreExports.h"
24 #include "smtk/PublicPointerDefs.h"
25 #include "smtk/attribute/Analyses.h"
26 #include "smtk/attribute/AssociationRules.h"
27 #include "smtk/attribute/Attribute.h"
28 #include "smtk/attribute/Definition.h"
29 #include "smtk/attribute/DirectoryInfo.h"
30 #include "smtk/attribute/Evaluator.h"
31 #include "smtk/attribute/EvaluatorFactory.h"
32 #include "smtk/attribute/Item.h"
33 #include "smtk/attribute/ItemDefinition.h"
34 #include "smtk/attribute/SymbolDependencyStorage.h"
35 
36 #include "smtk/view/Configuration.h"
37 
38 #include <map>
39 #include <mutex>
40 #include <set>
41 #include <string>
42 #include <vector>
43 
44 namespace smtk
45 {
46 namespace attribute
47 {
48 class Attribute;
49 class Definition;
50 
51 typedef smtk::common::Factory<ItemDefinition, std::string> CustomItemDefinitionFactory;
52 
67 class SMTKCORE_EXPORT Resource
68  : public smtk::resource::DerivedFrom<Resource, smtk::geometry::Resource>
69 {
70 public:
74 
75  // typedef referring to the parent resource.
77 
79  {
80  COPY_ASSOCIATIONS =
81  0x00000001,
82  FORCE_COPY_ASSOCIATIONS =
83  0x00000003
84  };
85 
86  // Associations and references to other resources and components are managed
87  // internally using smtk::resource::Links. The concepts of association and
88  // reference are internally very similar, but to the outward-facing API they
89  // are treated separately and serve different functions. The storage for these
90  // values are therefore logically separated by different role values.
91  static constexpr smtk::resource::Links::RoleType AssociationRole = -1;
92  static constexpr smtk::resource::Links::RoleType ReferenceRole = -2;
93 
94  ~Resource() override;
95 
96  smtk::attribute::DefinitionPtr createDefinition(
97  const std::string& typeName,
98  const std::string& baseTypeName = "");
99  smtk::attribute::DefinitionPtr createDefinition(
100  const std::string& name,
101  attribute::DefinitionPtr baseDefiniiton);
102  // Description:
103  // For simplicity, only Definitions without any children can be currently
104  // removed (external nodes).
105  bool removeDefinition(smtk::attribute::DefinitionPtr def);
106 
107  // Description:
108  // Provide a way to mark a resource enabled/disabled
109  // so that we can hide it in certain contexts
110  void setIsPrivate(bool isPrivateValue) { m_isPrivate = isPrivateValue; }
111  bool isPrivate() const { return m_isPrivate; };
112 
115  const std::string& defaultNameSeparator() const;
118  void resetDefaultNameSeparator();
121  bool setDefaultNameSeparator(const std::string& separator);
122 
123  smtk::attribute::AttributePtr createAttribute(const std::string& name, const std::string& type);
125  smtk::attribute::AttributePtr createAttribute(const std::string& type);
126  smtk::attribute::AttributePtr createAttribute(
127  const std::string& name,
129  bool removeAttribute(smtk::attribute::AttributePtr att);
130  smtk::attribute::AttributePtr findAttribute(const std::string& name) const;
131  smtk::attribute::AttributePtr findAttribute(const smtk::common::UUID& id) const;
132  smtk::attribute::AttributePtr findAttribute(
133  const smtk::resource::ComponentPtr& comp,
134  const smtk::resource::Links::RoleType& role) const;
135 
136  void addUniqueRoles(const std::set<smtk::resource::Links::RoleType>& roles);
137  void addUniqueRole(const smtk::resource::Links::RoleType& role);
138  const std::set<smtk::resource::Links::RoleType>& uniqueRoles() const;
139  bool isRoleUnique(const smtk::resource::Links::RoleType& role) const;
140 
141  // given a resource component's UUID, return the resource component.
142  smtk::resource::ComponentPtr find(const smtk::common::UUID& id) const override;
143 
157  std::function<bool(const smtk::resource::Component&)> queryOperation(
158  const std::string&) const override;
160  static std::string createAttributeQuery(const smtk::attribute::DefinitionPtr& def);
161  static std::string createAttributeQuery(const std::string& defType);
162  // visit all components in the resource.
163  void visit(smtk::resource::Component::Visitor&) const override;
164 
165  void findAttributes(const std::string& type, std::vector<smtk::attribute::AttributePtr>& result)
166  const;
167  std::vector<smtk::attribute::AttributePtr> findAttributes(const std::string& type) const;
168  void findAttributes(
170  std::vector<smtk::attribute::AttributePtr>& result) const;
171  smtk::attribute::DefinitionPtr findDefinition(const std::string& type) const;
172 
173  // Return a list of definitions that are not derived from another definition
174  void findBaseDefinitions(std::vector<smtk::attribute::DefinitionPtr>& result) const;
175 
176  void derivedDefinitions(
178  std::vector<smtk::attribute::DefinitionPtr>& result) const;
179 
180  void findAllDerivedDefinitions(
182  bool concreteOnly,
183  std::vector<smtk::attribute::DefinitionPtr>& result) const;
184 
185  void findDefinitionAttributes(
186  const std::string& type,
187  std::vector<smtk::attribute::AttributePtr>& result) const;
188  void findDefinitions(unsigned long mask, std::vector<smtk::attribute::DefinitionPtr>& result)
189  const;
190 
191  smtk::attribute::ConstDefinitionPtr findIsUniqueBaseClass(
193 
194  bool rename(AttributePtr att, const std::string& newName);
195 
196  // Access Analysis Information
197  smtk::attribute::Analyses& analyses() { return m_analyses; }
198 
199  std::size_t numberOfAdvanceLevels() const { return m_advLevels.size(); }
200  void addAdvanceLevel(int level, std::string label, const double* l_color = nullptr);
201  const std::map<int, std::string>& advanceLevels() const { return m_advLevels; }
202  // the color is expected in the format of double[4] - rgba
203  const double* advanceLevelColor(int level) const;
204  void setAdvanceLevelColor(int level, const double* l_color);
205 
206  // For Reader classes
208  createAttribute(const std::string& name, const std::string& type, const smtk::common::UUID& id);
209  smtk::attribute::AttributePtr createAttribute(
210  const std::string& name,
212  const smtk::common::UUID& id);
213  std::string createUniqueName(const std::string& type) const;
214 
215  void finalizeDefinitions();
216 
222  std::size_t numberOfCategories() const { return m_categories.size(); }
223  const std::set<std::string>& categories() const { return m_categories; }
225 
231  void setActiveCategoriesEnabled(bool mode);
232  bool activeCategoriesEnabled() const { return m_activeCategoriesEnabled; }
233  void setActiveCategories(const std::set<std::string>& cats);
234  const std::set<std::string>& activeCategories() const { return m_activeCategories; }
236 
237  bool passActiveCategoryCheck(const smtk::attribute::Categories::Set& cats) const;
238  bool passActiveCategoryCheck(const smtk::attribute::Categories& cats) const;
239 
240  void addView(smtk::view::ConfigurationPtr);
241  smtk::view::ConfigurationPtr findView(const std::string& name) const;
242  smtk::view::ConfigurationPtr findViewByType(const std::string& vtype) const;
243  smtk::view::ConfigurationPtr findTopLevelView() const;
244  std::vector<smtk::view::ConfigurationPtr> findTopLevelViews() const;
245  const std::map<std::string, smtk::view::ConfigurationPtr>& views() const { return m_views; }
246 
252  void addStyle(const std::string& definitionType, smtk::view::Configuration::Component style);
253  const smtk::view::Configuration::Component& findStyle(
255  const std::string& styleName = "") const;
256  const std::map<std::string, smtk::view::Configuration::Component>& findStyles(
257  const smtk::attribute::DefinitionPtr& def) const;
258  const std::map<std::string, std::map<std::string, smtk::view::Configuration::Component>>& styles()
259  const
260  {
261  return m_styles;
262  }
264 
266  smtk::resource::ResourceSet associations() const;
267 
270  bool associate(const smtk::resource::ResourcePtr& resource);
271 
274  bool disassociate(const smtk::resource::ResourcePtr& resource);
275 
277  bool hasAssociations() const;
278 
279  // Return the attributes that are associated on a PersistentObject
280  std::set<AttributePtr> attributes(const smtk::resource::ConstPersistentObjectPtr& object) const;
281 
282  // true if the PersistentObject has attributes associated with it
283  bool hasAttributes(const smtk::resource::ConstPersistentObjectPtr& object) const;
284 
285  bool hasAttributes() const { return !m_attributes.empty(); }
286 
287  void disassociateAllAttributes(const smtk::resource::PersistentObjectPtr& object);
288 
289  // When a definition's items has changed use this method to update derived def
290  // item offsets which is used by the find item method
291  void updateDerivedDefinitionIndexOffsets(smtk::attribute::DefinitionPtr def);
292 
293  // Copies definition from another Resource
294  smtk::attribute::DefinitionPtr copyDefinition(
296  unsigned int options = 0);
297  // Copies attribute from another Resource
298  // Note: that if the attribute is unique (meaning only 1 attribute of this type can be asociated
299  // to a model entity, the copyModelAssociations flag is ignored since it would violate this constraint.
300  // In terms of options - these are item assignment options - see Item.h for documentation.
301  smtk::attribute::AttributePtr copyAttribute(
303  const bool& copyModelAssociations = false,
304  const unsigned int& options = 0);
305 
306  //Get a list of all definitions in the Resource
307  void definitions(std::vector<smtk::attribute::DefinitionPtr>& result, bool sortList = false)
308  const;
309  //Get a list of all attributes in the Resource
310  void attributes(std::vector<smtk::attribute::AttributePtr>& result) const;
311 
312  smtk::attribute::EvaluatorFactory& evaluatorFactory() { return m_evaluatorFactory; }
313 
314  const smtk::attribute::EvaluatorFactory& evaluatorFactory() const { return m_evaluatorFactory; }
315 
316  bool canEvaluate(smtk::attribute::ConstAttributePtr att)
317  {
318  return !!m_evaluatorFactory.createEvaluator(att);
319  }
320 
321  std::unique_ptr<smtk::attribute::Evaluator> createEvaluator(
323  {
324  return m_evaluatorFactory.createEvaluator(att);
325  }
326 
327  smtk::attribute::SymbolDependencyStorage& symbolDependencyStorage()
328  {
329  return queries().cache<smtk::attribute::SymbolDependencyStorage>();
330  }
331 
332  // Set/Get the directory structure of the resource on disk
333  void setDirectoryInfo(const DirectoryInfo& dinfo) { m_directoryInfo = dinfo; }
334  const DirectoryInfo& directoryInfo() const { return m_directoryInfo; }
335 
336  // Access the factory for generating custom item defnitions
337  CustomItemDefinitionFactory& customItemDefinitionFactory()
338  {
339  return m_customItemDefinitionFactory;
340  }
341  const CustomItemDefinitionFactory& customItemDefinitionFactory() const
342  {
343  return m_customItemDefinitionFactory;
344  }
345 
346  AssociationRules& associationRules() { return m_associationRules; }
347  const AssociationRules& associationRules() const { return m_associationRules; }
348 
350  {
351  public:
352  GuardedLinks(std::mutex& mutex, const smtk::resource::Resource::Links& links)
353  : m_guard(mutex)
354  , m_links(links)
355  {
356  }
357 
358  const smtk::resource::Resource::Links* operator->() const { return &m_links; }
359 
360  smtk::resource::Resource::Links* operator->()
361  {
362  return const_cast<smtk::resource::Resource::Links*>(&m_links);
363  }
364 
365  private:
366  std::unique_lock<std::mutex> m_guard;
367  const smtk::resource::Resource::Links& m_links;
368  };
369 
370  // Attributes are uniquely used outside of an operation context, where they
371  // are not guarded from concurrency issues. Specifically, ReferenceItems use
372  // ResourceLinks to store references to other resources, and the
373  // Resource::Links and Component::Links API is not thread-safe. This API
374  // ensures thread safety when manipulating smtk::attribute::(Resource,Attribute
375  // Links.
376  const GuardedLinks guardedLinks() const;
377  GuardedLinks guardedLinks();
378 
379  std::mutex& mutex() const { return m_mutex; }
380 
381 protected:
383  Resource(smtk::resource::ManagerPtr manager = nullptr);
384  void internalFindAllDerivedDefinitions(
386  bool onlyConcrete,
387  std::vector<smtk::attribute::DefinitionPtr>& result) const;
388  void internalFindAttributes(
390  std::vector<smtk::attribute::AttributePtr>& result) const;
391  bool copyDefinitionImpl(
394 
395  std::map<std::string, smtk::attribute::DefinitionPtr> m_definitions;
396  std::map<std::string, std::set<smtk::attribute::AttributePtr, Attribute::CompareByName>>
397  m_attributeClusters;
398  std::map<std::string, smtk::attribute::AttributePtr> m_attributes;
399  std::map<smtk::common::UUID, smtk::attribute::AttributePtr> m_attributeIdMap;
400 
401  std::map<
403  std::set<smtk::attribute::WeakDefinitionPtr, Definition::WeakDefinitionPtrCompare>>
404  m_derivedDefInfo;
405  std::set<std::string> m_categories;
406  std::set<std::string> m_activeCategories;
407  bool m_activeCategoriesEnabled = false;
408  smtk::attribute::Analyses m_analyses;
409  std::map<std::string, smtk::view::ConfigurationPtr> m_views;
410  std::map<std::string, std::map<std::string, smtk::view::Configuration::Component>> m_styles;
411 
412  // Advance levels, <int-level, <string-label, color[4]>
413  // higher level means more advanced.
414  std::map<int, std::string> m_advLevels;
415  std::map<int, std::vector<double>> m_advLevelColors;
416  DirectoryInfo m_directoryInfo;
417  std::set<smtk::resource::Links::RoleType> m_roles;
418 
419  CustomItemDefinitionFactory m_customItemDefinitionFactory;
420 
421  AssociationRules m_associationRules;
422 
423  bool m_isPrivate = false;
424 
425  EvaluatorFactory m_evaluatorFactory;
426 
427  std::string m_defaultAttNameSeparator = "-";
428 
429 private:
430  mutable std::mutex m_mutex;
431 };
432 
433 inline smtk::view::ConfigurationPtr Resource::findView(const std::string& name) const
434 {
435  std::map<std::string, smtk::view::ConfigurationPtr>::const_iterator it;
436  it = m_views.find(name);
437  return (it == m_views.end()) ? smtk::view::ConfigurationPtr() : it->second;
438 }
439 
440 inline smtk::attribute::AttributePtr Resource::findAttribute(const std::string& name) const
441 {
442  std::map<std::string, AttributePtr>::const_iterator it;
443  it = m_attributes.find(name);
444  return (it == m_attributes.end()) ? smtk::attribute::AttributePtr() : it->second;
445 }
446 
447 inline smtk::attribute::AttributePtr Resource::findAttribute(const smtk::common::UUID& attId) const
448 {
449  std::map<smtk::common::UUID, AttributePtr>::const_iterator it;
450  it = m_attributeIdMap.find(attId);
451  return (it == m_attributeIdMap.end()) ? smtk::attribute::AttributePtr() : it->second;
452 }
453 
454 inline smtk::attribute::DefinitionPtr Resource::findDefinition(const std::string& typeName) const
455 {
456  std::map<std::string, smtk::attribute::DefinitionPtr>::const_iterator it;
457  it = m_definitions.find(typeName);
458  return (it == m_definitions.end()) ? smtk::attribute::DefinitionPtr() : it->second;
459 }
460 
461 inline void Resource::findDefinitionAttributes(
462  const std::string& typeName,
463  std::vector<smtk::attribute::AttributePtr>& result) const
464 {
465  result.clear();
466  auto it = m_attributeClusters.find(typeName);
467  if (it != m_attributeClusters.end())
468  {
469  result.insert(result.end(), it->second.begin(), it->second.end());
470  }
471 }
472 
473 inline void Resource::findAttributes(
474  const std::string& type,
475  std::vector<smtk::attribute::AttributePtr>& result) const
476 {
477  result.clear();
478  smtk::attribute::DefinitionPtr def = this->findDefinition(type);
479  if (def)
480  {
481  this->internalFindAttributes(def, result);
482  }
483 }
484 
485 inline std::vector<smtk::attribute::AttributePtr> Resource::findAttributes(
486  const std::string& type) const
487 {
488  std::vector<smtk::attribute::AttributePtr> result;
489  this->findAttributes(type, result);
490  return result;
491 }
492 
493 } // namespace attribute
494 } // namespace smtk
495 
496 #endif /* smtk_attribute_Resource_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::resource::DerivedFrom
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
smtk::resource::Resource::name
std::string name() const override
Return the user-assigned name of the resource.
Definition: Resource.cxx:198
smtk::attribute::Resource::styles
const std::map< std::string, std::map< std::string, smtk::view::Configuration::Component > > & styles() const
API for setting and accessing style information.
Definition: Resource.h:258
smtk::attribute::Analyses
Represents a set of Analyses defined within an Attribute Resource.
Definition: Analyses.h:26
smtk::attribute::Resource::activeCategoriesEnabled
bool activeCategoriesEnabled() const
API for setting and accessing Active Category information.
Definition: Resource.h:232
smtk::attribute::EvaluatorFactory
Definition: EvaluatorFactory.h:35
smtk::common::UUID
Definition: UUID.h:38
smtk::attribute::Resource::activeCategories
const std::set< std::string > & activeCategories() const
API for setting and accessing Active Category information.
Definition: Resource.h:234
smtk::resource::ConstPersistentObjectPtr
smtk::shared_ptr< const smtk::resource::PersistentObject > ConstPersistentObjectPtr
Definition: PublicPointerDefs.h:287
smtk::resource::ResourceSet
std::set< smtk::resource::ResourcePtr > ResourceSet
Definition: PublicPointerDefs.h:317
smtkCreateMacro
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:113
smtkSharedFromThisMacro
#define smtkSharedFromThisMacro(...)
A macro to help with derived classes whose bases enable shared_from_this().
Definition: SharedFromThis.h:190
smtk::attribute::ItemDefinition::CopyInfo
Definition: ItemDefinition.h:47
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::attribute::Resource::CopyOptions
CopyOptions
Definition: Resource.h:78
smtk::attribute::AttributePtr
smtk::shared_ptr< smtk::attribute::Attribute > AttributePtr
Definition: PublicPointerDefs.h:456
smtk::common::Factory< ItemDefinition, std::string >
smtk::attribute::SymbolDependencyStorage
Definition: SymbolDependencyStorage.h:30
smtk::view::ConfigurationPtr
smtk::shared_ptr< smtk::view::Configuration > ConfigurationPtr
Definition: PublicPointerDefs.h:642
smtk::attribute::Resource
Store information about attribute definitions and instances.
Definition: Resource.h:67
smtk::view::Configuration::Component
Configure one item in a view, which may contain children.
Definition: Configuration.h:32
smtk::common::typeName
std::string typeName()
Return the name of a class.
Definition: TypeName.h:276
smtk::attribute::Categories
Represents the category constraints associated with an Attribute, Attribute Definition,...
Definition: Categories.h:34
smtk::attribute::Resource::categories
const std::set< std::string > & categories() const
API for accessing Category information.
Definition: Resource.h:223
smtk::attribute::ConstAttributePtr
smtk::shared_ptr< const smtk::attribute::Attribute > ConstAttributePtr
Definition: PublicPointerDefs.h:458
smtk::attribute::AssociationRules
A class for holding the state and encapsulating the logic behind custom association rules for an attr...
Definition: AssociationRules.h:26
smtk::attribute::Analyses::size
std::size_t size() const
Return the number of Analysis Instances.
Definition: Analyses.h:133
smtk::resource::Component
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
smtk::attribute::Categories::Set
Categories::Set represents a single category constraint used by the Categories class.
Definition: Categories.h:75
smtk::resource::PersistentObjectPtr
smtk::shared_ptr< smtk::resource::PersistentObject > PersistentObjectPtr
Definition: PublicPointerDefs.h:285
smtk::attribute::Resource::numberOfCategories
std::size_t numberOfCategories() const
API for accessing Category information.
Definition: Resource.h:222
smtk::resource::PersistentObject
An abstract base class for SMTK resources and their components.
Definition: PersistentObject.h:39
smtk::resource::ManagerPtr
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281
smtk::resource::ComponentPtr
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
smtk::geometry::Resource
An abstract base class for SMTK resources that provide geometry for themselves and/or their component...
Definition: Resource.h:33
smtk::resource::ResourcePtr
smtk::shared_ptr< smtk::resource::Resource > ResourcePtr
Definition: PublicPointerDefs.h:295
smtk::attribute::ConstDefinitionPtr
smtk::shared_ptr< const smtk::attribute::Definition > ConstDefinitionPtr
Definition: PublicPointerDefs.h:452