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/CopyAssignmentOptions.h"
29 #include "smtk/attribute/Definition.h"
30 #include "smtk/attribute/DirectoryInfo.h"
31 #include "smtk/attribute/Evaluator.h"
32 #include "smtk/attribute/EvaluatorFactory.h"
33 #include "smtk/attribute/Item.h"
34 #include "smtk/attribute/ItemDefinition.h"
35 #include "smtk/attribute/SymbolDependencyStorage.h"
36 
37 #include "smtk/string/Token.h"
38 
39 #include "smtk/view/Configuration.h"
40 
41 #include <map>
42 #include <mutex>
43 #include <set>
44 #include <string>
45 #include <vector>
46 
47 namespace smtk
48 {
49 
50 namespace io
51 {
52 class Logger;
53 };
54 
55 namespace attribute
56 {
57 class Attribute;
58 class Definition;
59 
60 typedef smtk::common::Factory<ItemDefinition, std::string> CustomItemDefinitionFactory;
61 
76 class SMTKCORE_EXPORT Resource
77  : public smtk::resource::DerivedFrom<Resource, smtk::geometry::Resource>
78 {
79 public:
83 
84  // typedef referring to the parent resource.
86 
89  static constexpr smtk::resource::Links::RoleType AssociationRole = -1;
90 
93  static constexpr smtk::resource::Links::RoleType ReferenceRole = -2;
94 
95  ~Resource() override;
96 
97  bool setUnitsSystem(const shared_ptr<units::System>& unitsSystem) override;
98 
99  smtk::attribute::DefinitionPtr createDefinition(
100  const std::string& typeName,
101  const std::string& baseTypeName = "");
102  smtk::attribute::DefinitionPtr createDefinition(
103  const std::string& name,
104  attribute::DefinitionPtr baseDefiniiton);
105  // Description:
106  // For simplicity, only Definitions without any children can be currently
107  // removed (external nodes).
108  bool removeDefinition(smtk::attribute::DefinitionPtr def);
109 
110  // Description:
111  // Provide a way to mark a resource enabled/disabled
112  // so that we can hide it in certain contexts
113  void setIsPrivate(bool isPrivateValue) { m_isPrivate = isPrivateValue; }
114  bool isPrivate() const { return m_isPrivate; };
115 
118  const std::string& defaultNameSeparator() const;
121  void resetDefaultNameSeparator();
124  bool setDefaultNameSeparator(const std::string& separator);
125 
126  smtk::attribute::AttributePtr createAttribute(const std::string& name, const std::string& type);
128  smtk::attribute::AttributePtr createAttribute(const std::string& type);
129  smtk::attribute::AttributePtr createAttribute(
130  const std::string& name,
132  bool removeAttribute(smtk::attribute::AttributePtr att);
133  smtk::attribute::AttributePtr findAttribute(const std::string& name) const;
134  smtk::attribute::AttributePtr findAttribute(const smtk::common::UUID& id) const;
135  smtk::attribute::AttributePtr findAttribute(
136  const smtk::resource::ComponentPtr& comp,
137  const smtk::resource::Links::RoleType& role) const;
138 
139  void addUniqueRoles(const std::set<smtk::resource::Links::RoleType>& roles);
140  void addUniqueRole(const smtk::resource::Links::RoleType& role);
141  const std::set<smtk::resource::Links::RoleType>& uniqueRoles() const;
142  bool isRoleUnique(const smtk::resource::Links::RoleType& role) const;
143 
144  // given a resource component's UUID, return the resource component.
145  smtk::resource::ComponentPtr find(const smtk::common::UUID& id) const override;
146 
160  std::function<bool(const smtk::resource::Component&)> queryOperation(
161  const std::string&) const override;
163  static std::string createAttributeQuery(const smtk::attribute::DefinitionPtr& def);
164  static std::string createAttributeQuery(const std::string& defType);
165  // visit all components in the resource.
166  void visit(smtk::resource::Component::Visitor&) const override;
167 
168  void findAttributes(const std::string& type, std::vector<smtk::attribute::AttributePtr>& result)
169  const;
170  std::vector<smtk::attribute::AttributePtr> findAttributes(const std::string& type) const;
171  void findAttributes(
173  std::vector<smtk::attribute::AttributePtr>& result) const;
174 
175  smtk::attribute::DefinitionPtr findDefinition(const std::string& type) const;
176 
178  bool hasDefinition(const std::string& type) const;
179 
180  // Return a list of definitions that are not derived from another definition
181  void findBaseDefinitions(std::vector<smtk::attribute::DefinitionPtr>& result) const;
182 
183  void derivedDefinitions(
185  std::vector<smtk::attribute::DefinitionPtr>& result) const;
186 
187  void findAllDerivedDefinitions(
189  bool concreteOnly,
190  std::vector<smtk::attribute::DefinitionPtr>& result) const;
191 
192  void findDefinitionAttributes(
193  const std::string& type,
194  std::vector<smtk::attribute::AttributePtr>& result) const;
195  void findDefinitions(unsigned long mask, std::vector<smtk::attribute::DefinitionPtr>& result)
196  const;
197 
198  smtk::attribute::ConstDefinitionPtr findIsUniqueBaseClass(
200 
201  bool rename(AttributePtr att, const std::string& newName);
202 
203  // Access Analysis Information
204  smtk::attribute::Analyses& analyses() { return m_analyses; }
205 
206  std::size_t numberOfAdvanceLevels() const { return m_advLevels.size(); }
207  void addAdvanceLevel(int level, std::string label, const double* l_color = nullptr);
208  const std::map<int, std::string>& advanceLevels() const { return m_advLevels; }
209  // the color is expected in the format of double[4] - rgba
210  const double* advanceLevelColor(int level) const;
211  void setAdvanceLevelColor(int level, const double* l_color);
212 
213  // For Reader classes
215  createAttribute(const std::string& name, const std::string& type, const smtk::common::UUID& id);
216  smtk::attribute::AttributePtr createAttribute(
217  const std::string& name,
219  const smtk::common::UUID& id);
220  std::string createUniqueName(const std::string& type) const;
221 
222  void finalizeDefinitions();
223 
229  std::size_t numberOfCategories() const { return m_categories.size(); }
230  const std::set<std::string>& categories() const { return m_categories; }
232 
238  void setActiveCategoriesEnabled(bool mode);
239  bool activeCategoriesEnabled() const { return m_activeCategoriesEnabled; }
240  void setActiveCategories(const std::set<std::string>& cats);
241  const std::set<std::string>& activeCategories() const { return m_activeCategories; }
243 
244  bool passActiveCategoryCheck(const smtk::attribute::Categories::Expression& cats) const;
245  bool passActiveCategoryCheck(const smtk::attribute::Categories& cats) const;
246 
247  void addView(smtk::view::ConfigurationPtr);
248  smtk::view::ConfigurationPtr findView(const std::string& name) const;
249  smtk::view::ConfigurationPtr findViewByType(const std::string& vtype) const;
250  smtk::view::ConfigurationPtr findTopLevelView() const;
251  std::vector<smtk::view::ConfigurationPtr> findTopLevelViews() const;
252  const std::map<std::string, smtk::view::ConfigurationPtr>& views() const { return m_views; }
253 
259  void addStyle(const std::string& definitionType, smtk::view::Configuration::Component style);
260  const smtk::view::Configuration::Component& findStyle(
262  const std::string& styleName = "") const;
263  const std::map<std::string, smtk::view::Configuration::Component>& findStyles(
264  const smtk::attribute::DefinitionPtr& def) const;
265  const std::map<std::string, std::map<std::string, smtk::view::Configuration::Component>>& styles()
266  const
267  {
268  return m_styles;
269  }
271 
273  smtk::resource::ResourceSet associations() const;
274 
277  bool associate(const smtk::resource::ResourcePtr& resource);
278 
281  bool disassociate(const smtk::resource::ResourcePtr& resource);
282 
284  bool hasAssociations() const;
285 
286  // Return the attributes that are associated on a PersistentObject
287  std::set<AttributePtr> attributes(const smtk::resource::ConstPersistentObjectPtr& object) const;
288 
289  // true if the PersistentObject has attributes associated with it
290  bool hasAttributes(const smtk::resource::ConstPersistentObjectPtr& object) const;
291 
292  bool hasAttributes() const { return !m_attributes.empty(); }
293 
294  void disassociateAllAttributes(const smtk::resource::PersistentObjectPtr& object);
295 
296  // When a definition's items has changed use this method to update derived def
297  // item offsets which is used by the find item method
298  void updateDerivedDefinitionIndexOffsets(smtk::attribute::DefinitionPtr def);
299 
300  // Copies definition from another Resource
301  smtk::attribute::DefinitionPtr copyDefinition(
303  unsigned int options = 0);
304 
315  smtk::attribute::AttributePtr copyAttribute(
317  const CopyAssignmentOptions& options,
318  smtk::io::Logger& logger);
319 
320  smtk::attribute::AttributePtr copyAttribute(
322  const CopyAssignmentOptions& options = CopyAssignmentOptions());
324 
325  //Get a list of all definitions in the Resource
326  void definitions(std::vector<smtk::attribute::DefinitionPtr>& result, bool sortList = false)
327  const;
328  //Get a list of all attributes in the Resource
329  void attributes(std::vector<smtk::attribute::AttributePtr>& result) const;
330 
331  smtk::attribute::EvaluatorFactory& evaluatorFactory() { return m_evaluatorFactory; }
332 
333  const smtk::attribute::EvaluatorFactory& evaluatorFactory() const { return m_evaluatorFactory; }
334 
335  bool canEvaluate(smtk::attribute::ConstAttributePtr att)
336  {
337  return !!m_evaluatorFactory.createEvaluator(att);
338  }
339 
340  std::unique_ptr<smtk::attribute::Evaluator> createEvaluator(
342  {
343  return m_evaluatorFactory.createEvaluator(att);
344  }
345 
346  smtk::attribute::SymbolDependencyStorage& symbolDependencyStorage()
347  {
348  return queries().cache<smtk::attribute::SymbolDependencyStorage>();
349  }
350 
351  // Set/Get the directory structure of the resource on disk
352  void setDirectoryInfo(const DirectoryInfo& dinfo) { m_directoryInfo = dinfo; }
353  const DirectoryInfo& directoryInfo() const { return m_directoryInfo; }
354 
355  // Access the factory for generating custom item defnitions
356  CustomItemDefinitionFactory& customItemDefinitionFactory()
357  {
358  return m_customItemDefinitionFactory;
359  }
360  const CustomItemDefinitionFactory& customItemDefinitionFactory() const
361  {
362  return m_customItemDefinitionFactory;
363  }
364 
365  AssociationRules& associationRules() { return m_associationRules; }
366  const AssociationRules& associationRules() const { return m_associationRules; }
367 
369  {
370  public:
371  GuardedLinks(std::mutex& mutex, const smtk::resource::Resource::Links& links)
372  : m_guard(mutex)
373  , m_links(links)
374  {
375  }
376 
377  const smtk::resource::Resource::Links* operator->() const { return &m_links; }
378 
379  smtk::resource::Resource::Links* operator->()
380  {
381  return const_cast<smtk::resource::Resource::Links*>(&m_links);
382  }
383 
384  private:
385  std::unique_lock<std::mutex> m_guard;
386  const smtk::resource::Resource::Links& m_links;
387  };
388 
389  // Attributes are uniquely used outside of an operation context, where they
390  // are not guarded from concurrency issues. Specifically, ReferenceItems use
391  // ResourceLinks to store references to other resources, and the
392  // Resource::Links and Component::Links API is not thread-safe. This API
393  // ensures thread safety when manipulating smtk::attribute::(Resource,Attribute
394  // Links.
395  const GuardedLinks guardedLinks() const;
396  GuardedLinks guardedLinks();
397 
398  std::mutex& mutex() const { return m_mutex; }
399 
404  bool setTemplateType(const smtk::string::Token& templateType) override;
405  smtk::string::Token templateType() const override { return m_templateType; }
406 
413  bool setTemplateVersion(std::size_t templateVersion) override;
414  std::size_t templateVersion() const override { return m_templateVersion; }
415 
420  std::shared_ptr<smtk::resource::Resource> clone(
421  smtk::resource::CopyOptions& options) const override;
422 
424  bool copyInitialize(
425  const std::shared_ptr<const smtk::resource::Resource>& other,
426  smtk::resource::CopyOptions& options) override;
427 
430  bool copyFinalize(
431  const std::shared_ptr<const smtk::resource::Resource>& source,
432  smtk::resource::CopyOptions& options) override;
433 
436  void copyViews(
437  const std::shared_ptr<const smtk::attribute::Resource>& source,
438  smtk::resource::CopyOptions& options);
443  void updateViewComponentIdAttributes(
445  smtk::resource::CopyOptions& options);
446 
447 protected:
449  Resource(smtk::resource::ManagerPtr manager = nullptr);
450  void internalFindAllDerivedDefinitions(
452  bool onlyConcrete,
453  std::vector<smtk::attribute::DefinitionPtr>& result) const;
454  void internalFindAttributes(
456  std::vector<smtk::attribute::AttributePtr>& result) const;
457  bool copyDefinitionImpl(
460 
461  std::map<std::string, smtk::attribute::DefinitionPtr> m_definitions;
462  std::map<std::string, std::set<smtk::attribute::AttributePtr, Attribute::CompareByName>>
463  m_attributeClusters;
464  std::map<std::string, smtk::attribute::AttributePtr> m_attributes;
465  std::map<smtk::common::UUID, smtk::attribute::AttributePtr> m_attributeIdMap;
466 
467  std::map<
469  std::set<smtk::attribute::WeakDefinitionPtr, Definition::WeakDefinitionPtrCompare>>
470  m_derivedDefInfo;
471  std::set<std::string> m_categories;
472  std::set<std::string> m_activeCategories;
473  bool m_activeCategoriesEnabled = false;
474  smtk::attribute::Analyses m_analyses;
475  std::map<std::string, smtk::view::ConfigurationPtr> m_views;
476  std::map<std::string, std::map<std::string, smtk::view::Configuration::Component>> m_styles;
477 
478  // Advance levels, <int-level, <string-label, color[4]>
479  // higher level means more advanced.
480  std::map<int, std::string> m_advLevels;
481  std::map<int, std::vector<double>> m_advLevelColors;
482  DirectoryInfo m_directoryInfo;
483  std::set<smtk::resource::Links::RoleType> m_roles;
484 
485  CustomItemDefinitionFactory m_customItemDefinitionFactory;
486 
487  AssociationRules m_associationRules;
488 
489  bool m_isPrivate = false;
490 
491  EvaluatorFactory m_evaluatorFactory;
492 
493  std::string m_defaultAttNameSeparator = "-";
494 
495  smtk::string::Token m_templateType;
496  std::size_t m_templateVersion = 0;
497 
498 private:
499  mutable std::mutex m_mutex;
500 };
501 
502 inline smtk::view::ConfigurationPtr Resource::findView(const std::string& name) const
503 {
504  std::map<std::string, smtk::view::ConfigurationPtr>::const_iterator it;
505  it = m_views.find(name);
506  return (it == m_views.end()) ? smtk::view::ConfigurationPtr() : it->second;
507 }
508 
509 inline smtk::attribute::AttributePtr Resource::findAttribute(const std::string& name) const
510 {
511  std::map<std::string, AttributePtr>::const_iterator it;
512  it = m_attributes.find(name);
513  return (it == m_attributes.end()) ? smtk::attribute::AttributePtr() : it->second;
514 }
515 
516 inline smtk::attribute::AttributePtr Resource::findAttribute(const smtk::common::UUID& attId) const
517 {
518  std::map<smtk::common::UUID, AttributePtr>::const_iterator it;
519  it = m_attributeIdMap.find(attId);
520  return (it == m_attributeIdMap.end()) ? smtk::attribute::AttributePtr() : it->second;
521 }
522 
523 inline smtk::attribute::DefinitionPtr Resource::findDefinition(const std::string& typeName) const
524 {
525  std::map<std::string, smtk::attribute::DefinitionPtr>::const_iterator it;
526  it = m_definitions.find(typeName);
527  return (it == m_definitions.end()) ? smtk::attribute::DefinitionPtr() : it->second;
528 }
529 
530 inline bool Resource::hasDefinition(const std::string& typeName) const
531 {
532  auto it = m_definitions.find(typeName);
533  return (it != m_definitions.end());
534 }
535 
536 inline void Resource::findDefinitionAttributes(
537  const std::string& typeName,
538  std::vector<smtk::attribute::AttributePtr>& result) const
539 {
540  result.clear();
541  auto it = m_attributeClusters.find(typeName);
542  if (it != m_attributeClusters.end())
543  {
544  result.insert(result.end(), it->second.begin(), it->second.end());
545  }
546 }
547 
548 inline void Resource::findAttributes(
549  const std::string& type,
550  std::vector<smtk::attribute::AttributePtr>& result) const
551 {
552  result.clear();
553  smtk::attribute::DefinitionPtr def = this->findDefinition(type);
554  if (def)
555  {
556  this->internalFindAttributes(def, result);
557  }
558 }
559 
560 inline std::vector<smtk::attribute::AttributePtr> Resource::findAttributes(
561  const std::string& type) const
562 {
563  std::vector<smtk::attribute::AttributePtr> result;
564  this->findAttributes(type, result);
565  return result;
566 }
567 
568 } // namespace attribute
569 } // namespace smtk
570 
571 #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:457
smtk::resource::DerivedFrom
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
smtk::attribute::Resource::templateVersion
std::size_t templateVersion() const override
Set/get the "type" of a resource's template.
Definition: Resource.h:414
smtk::resource::Resource::name
std::string name() const override
Set/get the user-assigned name of the resource.
Definition: Resource.cxx:203
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:265
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:239
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:241
smtk::resource::ConstPersistentObjectPtr
smtk::shared_ptr< const smtk::resource::PersistentObject > ConstPersistentObjectPtr
Definition: PublicPointerDefs.h:294
smtk::io::Logger
Log messages for later presentation to a user or a file.
Definition: Logger.h:94
smtk::resource::ResourceSet
std::set< smtk::resource::ResourcePtr > ResourceSet
Definition: PublicPointerDefs.h:324
smtkCreateMacro
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:173
smtk::attribute::Categories::Expression
Definition: Categories.h:166
smtkSharedFromThisMacro
#define smtkSharedFromThisMacro(...)
A macro to help with derived classes whose bases enable shared_from_this().
Definition: SharedFromThis.h:250
smtk::attribute::ItemDefinition::CopyInfo
Definition: ItemDefinition.h:52
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:128
smtk::resource::CopyOptions
Definition: CopyOptions.h:31
smtk::attribute::AttributePtr
smtk::shared_ptr< smtk::attribute::Attribute > AttributePtr
Definition: PublicPointerDefs.h:463
smtk::common::Factory< ItemDefinition, std::string >
smtk::attribute::Resource::templateType
smtk::string::Token templateType() const override
Set/get the "type" of a resource's template.
Definition: Resource.h:405
smtk::attribute::SymbolDependencyStorage
Definition: SymbolDependencyStorage.h:30
smtk::view::ConfigurationPtr
smtk::shared_ptr< smtk::view::Configuration > ConfigurationPtr
Definition: PublicPointerDefs.h:649
smtk::attribute::Resource
Store information about attribute definitions and instances.
Definition: Resource.h:76
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:289
smtk::attribute::Categories
Represents the category constraints associated with an Attribute, Attribute Definition,...
Definition: Categories.h:33
smtk::attribute::Resource::categories
const std::set< std::string > & categories() const
API for accessing Category information.
Definition: Resource.h:230
smtk::attribute::ConstAttributePtr
smtk::shared_ptr< const smtk::attribute::Attribute > ConstAttributePtr
Definition: PublicPointerDefs.h:465
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::string::Token
A string token identified by an integer.
Definition: Token.h:29
smtk::attribute::Resource::hasDefinition
bool hasDefinition(const std::string &type) const
Return true if the Resource has a Definition with the requested type.
Definition: Resource.h:530
smtk::attribute::Analyses::size
std::size_t size() const
Return the number of Analysis Instances.
Definition: Analyses.h:130
smtk::resource::Component
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
smtk::resource::PersistentObjectPtr
smtk::shared_ptr< smtk::resource::PersistentObject > PersistentObjectPtr
Definition: PublicPointerDefs.h:292
smtk::attribute::Resource::numberOfCategories
std::size_t numberOfCategories() const
API for accessing Category information.
Definition: Resource.h:229
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:288
smtk::resource::ComponentPtr
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:304
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:302
smtk::attribute::ConstDefinitionPtr
smtk::shared_ptr< const smtk::attribute::Definition > ConstDefinitionPtr
Definition: PublicPointerDefs.h:459