SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_graph_Resource_h
12 #define smtk_graph_Resource_h
16 #include "smtk/common/CompilerInformation.h"
17 #include "smtk/common/TypeMap.h"
18 #include "smtk/common/TypeTraits.h"
20 #include "smtk/geometry/Manager.h"
21 #include "smtk/geometry/Resource.h"
23 #include "smtk/graph/Component.h"
24 #include "smtk/graph/NodeSet.h"
25 #include "smtk/graph/evaluators/DeleteArcs.h"
26 #include "smtk/graph/evaluators/Dump.h"
27 #include "smtk/graph/filter/Grammar.h"
29 #include "smtk/resource/CopyOptions.h"
30 #include "smtk/resource/filter/Filter.h"
47 template<
typename T,
typename =
void>
52 template<
class T,
class... Args>
55 smtk::common::void_t<decltype(std::declval<T>().initialize(std::declval<Args>()...))>>
60 template<
class T,
class... Args>
65 t.initialize(std::forward<Args>(args)...);
68 template<
class T,
class... Args>
85 template<
typename Traits>
86 class SMTK_ALWAYS_EXPORT Resource
88 ,
public detail::GraphTraits<Traits>::NodeContainer
92 using NodeContainer =
typename detail::GraphTraits<Traits>::NodeContainer;
93 using TypeTraits = Traits;
95 template<
typename NodeType>
97 template<
typename ArcType>
106 return "smtk::graph::Resource<" + smtk::common::typeName<Traits>() +
">";
124 template<
typename NodeType,
typename... T>
125 typename std::enable_if<is_node<NodeType>::value, std::shared_ptr<NodeType>>::type
create(
128 std::shared_ptr<smtk::graph::Component> created(
new NodeType(this->shared_from_this()));
130 auto node = std::static_pointer_cast<NodeType>(created);
131 detail::initialize(*node, std::forward<T>(parameters)...);
139 template<
typename NodeType>
140 typename std::enable_if<is_node<NodeType>::value,
bool>::type
add(
141 const std::shared_ptr<NodeType>& node)
145 throw std::invalid_argument(
146 "Cannot add nodes that reference a different (or null) resource.");
148 return NodeContainer::insertNode(node);
152 template<
typename NodeType>
153 typename std::enable_if<is_node<NodeType>::value,
bool>::type
remove(
154 const std::shared_ptr<NodeType>& node)
156 return NodeContainer::eraseNodes(node) > 0;
161 ArcMap& arcs()
override {
return m_arcs; }
165 const std::string& filterString)
const override
167 return smtk::resource::filter::Filter<smtk::graph::filter::Grammar>(filterString);
176 return NodeContainer::find(uuid);
182 return NodeContainer::component(uuid);
188 NodeContainer::visit(v);
207 template<
typename Functor,
typename... Args>
210 Functor::begin(
this, std::forward<Args>(args)...);
211 m_arcs.invoke<
typename detail::GraphTraits<Traits>::ArcTypes, Functor>(
212 this, std::forward<Args>(args)...);
215 m_arcs.invokeRuntime<Functor>(
this, std::forward<Args>(args)...);
216 Functor::end(
this, std::forward<Args>(args)...);
220 template<
typename Functor,
typename... Args>
221 void evaluateArcs(Args&&... args)
223 Functor::begin(
this, std::forward<Args>(args)...);
224 m_arcs.invoke<
typename detail::GraphTraits<Traits>::ArcTypes, Functor>(
225 this, std::forward<Args>(args)...);
228 m_arcs.invokeRuntime<Functor>(
this, std::forward<Args>(args)...);
229 Functor::end(
this, std::forward<Args>(args)...);
234 const std::set<smtk::string::Token>&
nodeTypes()
const override {
return m_nodeTypes; }
237 const std::set<smtk::string::Token>&
arcTypes()
const override {
return m_arcs.types(); }
245 bool setTemplateVersion(std::size_t templateVersion)
override;
252 std::shared_ptr<smtk::resource::Resource> clone(
257 const std::shared_ptr<const smtk::resource::Resource>& source,
263 const std::shared_ptr<const smtk::resource::Resource>& source,
268 template<
typename ResourceType>
275 if (!m_self || m_typeName.data().empty())
277 throw std::invalid_argument(
"Invalid resource or empty node type-name.");
282 void evaluate(std::size_t ii)
286 if (
typeName == m_typeName && !m_node)
288 m_node = m_self->template create<T>();
292 const std::shared_ptr<Component>& createdNode()
const {
return m_node; }
294 ResourceType* m_self;
295 std::shared_ptr<Component> m_node;
303 if (m_nodeTypes.find(nodeTypeName) == m_nodeTypes.end())
308 smtk::tuple_evaluate<typename Traits::NodeTypes>(creator);
309 return creator.createdNode().get();
324 bool result = from->
outgoing(arcType).connect(to);
331 bool removedAnArc =
false;
332 this->evaluateArcs<evaluators::DeleteArcs>(node, explicitOnly, removedAnArc);
343 void dump(
const std::string& filename,
const std::string& mimeType =
"text/vnd.graphviz")
346 std::ostream* stream = filename.empty() ? &std::cout :
new std::ofstream(filename.c_str());
347 if (stream && stream->good())
350 this->evaluateArcs<evaluators::Dump>(*stream, dump);
352 if (!filename.empty())
359 std::size_t eraseNodes(
const smtk::graph::ComponentPtr& node)
override
361 return NodeContainer::eraseNodes(node) > 0;
365 bool insertNode(
const smtk::graph::ComponentPtr& node)
override
367 return NodeContainer::insertNode(node);
374 return m_nodeTypes.find(typeToken) != m_nodeTypes.end();
386 void evaluate(std::size_t ii)
390 m_names.insert(name);
393 std::set<smtk::string::Token>& m_names;
396 void initializeResource()
400 smtk::tuple_evaluate<typename Traits::NodeTypes>(nodeTypeNames);
404 : Superclass(manager)
405 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
407 this->initializeResource();
411 : Superclass(uid, manager)
412 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
414 this->initializeResource();
418 std::size_t m_templateVersion{ 0 };
420 std::set<smtk::string::Token> m_nodeTypes;
421 std::set<smtk::string::Token> m_arcTypes;
424 template<
typename Traits>
427 if (m_templateType == templateType)
431 m_templateType = templateType;
435 template<
typename Traits>
436 bool Resource<Traits>::setTemplateVersion(std::size_t templateVersion)
438 if (m_templateVersion == templateVersion || templateVersion == 0)
442 m_templateVersion = templateVersion;
446 template<
typename Traits>
452 std::shared_ptr<smtk::graph::ResourceBase> result;
453 if (
auto rsrcMgr = this->manager())
455 result = std::dynamic_pointer_cast<ResourceBase>(rsrcMgr->create(this->typeName()));
466 if (this->isNameSet())
471 if (options.copyLocation())
478 if (options.copyTemplateData() || options.copyComponents())
480 if (options.copyTemplateData())
483 auto version = this->templateVersion();
494 template<
typename Traits>
496 const std::shared_ptr<const smtk::resource::Resource>& source,
499 const auto& graphSource = std::dynamic_pointer_cast<const Resource<Traits>>(source);
504 "Resource types do not match (" << source->typeName() <<
" vs " << this->typeName() <<
").");
513 if (options.copyComponents())
515 smtk::resource::Component::Visitor copyComponent =
516 [&](
const std::shared_ptr<smtk::resource::Component>& comp) {
517 auto node = this->createNodeOfType(comp->typeToken());
520 source->
visit(copyComponent);
524 this->copyProperties(source, options);
527 this->copyGeometry(graphSource, options);
530 this->arcs().copyArcs(graphSource.get(), options,
this);
535 template<
typename Traits>
537 const std::shared_ptr<const smtk::resource::Resource>& source,
541 this->copyLinks(source, options);
544 if (options.copyComponents())
546 smtk::resource::Component::Visitor assignComponent =
547 [&](
const std::shared_ptr<smtk::resource::Component>& comp) {
548 auto sourceNode = std::dynamic_pointer_cast<smtk::graph::Component>(comp);
549 if (sourceNode && !options.
shouldOmitId(sourceNode->id()))
555 targetNode->assign(sourceNode, options);
561 "Source node mapping for " << source->
id() <<
", \"" << sourceNode->name()
562 <<
"\", did not produce a target.");
566 source->
visit(assignComponent);
574 #endif // smtk_graph_Resource_h
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
Graph Component represents a node in the graph resource.
Definition: Component.h:66
bool isNodeTypeAcceptable(const smtk::graph::ComponentPtr &node) override
Perform a run-time check to validate that a node is acceptable to this resource.
Definition: Resource.h:371
A functor to create smtk::string::Tokens of accepted node types.
Definition: Resource.h:269
bool disconnect(Component *node, bool explicitOnly) override
Remove all arcs (or only explicit arcs) from the given node.
Definition: Resource.h:329
std::enable_if< is_node< NodeType >::value, bool >::type add(const std::shared_ptr< NodeType > &node)
Add a node of type NodeType to the resource.
Definition: Resource.h:140
std::shared_ptr< smtk::resource::Component > find(const smtk::common::UUID &uuid) const override
Return a shared pointer to a node (component) given its UUID.
Definition: Resource.h:174
A functor to create smtk::string::Tokens of accepted node types.
Definition: Resource.h:378
virtual bool setLocation(const std::string &location)
Set/get the location (a URL) where the resource is stored persistently.
Definition: Resource.cxx:176
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
ArcEndpointInterface< ArcType, ConstArc, OutgoingArc > outgoing() const
Return an endpoint-interface object for arcs of ArcType outgoing from this node.
Definition: Component.h:100
Component * createNodeOfType(smtk::string::Token nodeTypeName) override
Implement the generic (untyped) graph API for inserting nodes.
Definition: Resource.h:301
const smtk::common::UUID & id() const override
Set/get the UUID of a resource.
Definition: Resource.h:143
#define smtkTypedefs(...)
Used by smtkTypeMacro()
Definition: SharedFromThis.h:24
#define smtkErrorMacro(logger, x)
Write the expression x to logger as an error message.
Definition: Logger.h:37
bool insertNode(const smtk::graph::ComponentPtr &node) override
Insert a node without performing any type-safety checks.
Definition: Resource.h:365
virtual void visit(std::function< void(const ComponentPtr &)> &v) const =0
visit all components in a resource.
smtk::resource::Component * component(const smtk::common::UUID &uuid) const override
Return a raw pointer to a node (component) given its UUID.
Definition: Resource.h:180
This is a functor that prints nodes and arcs in plain-text or graphviz format.
Definition: Dump.h:41
void dump(const std::string &filename, const std::string &mimeType="text/vnd.graphviz") const override
Dump the resource nodes and arcs to a file.
Definition: Resource.h:343
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:173
const ArcMap & arcs() const override
Access the arcs of the graph resource.
Definition: Resource.h:160
ObjectType * targetObjectFromSourceId(const smtk::common::UUID &sourceId) const
A convenience to fetch an entry from objectMapping(), casting it to the given type.
Definition: CopyOptions.h:144
const smtk::resource::ResourcePtr resource() const override
Access the containing resource.
Definition: Component.cxx:36
#define smtkSharedFromThisMacro(...)
A macro to help with derived classes whose bases enable shared_from_this().
Definition: SharedFromThis.h:250
const std::set< smtk::string::Token > & nodeTypes() const override
Return the set of node types accepted by this resource.
Definition: Resource.h:234
smtk::io::Logger & log() const
Return a logger to be used to provide feedback on copying.
Definition: CopyOptions.h:181
virtual bool setName(const std::string &name)
Set/get the user-assigned name of the resource.
Definition: Resource.cxx:212
ObjectMapType & objectMapping()
Provide a map from source-resource object-IDs to copied-resource object-pointers.
Definition: CopyOptions.h:135
virtual bool setTemplateVersion(std::size_t templateVersion)
Set/get the version number of the template this instance of the resource is based upon.
Definition: Resource.cxx:246
Definition: CopyOptions.h:31
void copyUnitSystem(const std::shared_ptr< const Resource > &rsrc, const CopyOptions &options)
Copy the units system from rsrc into this resource as specified by options.
Definition: Resource.cxx:278
virtual bool setTemplateType(const smtk::string::Token &templateType)
Set/get the "type" of a resource's template.
Definition: Resource.cxx:234
Definition: Resource.h:48
bool shouldOmitId(const smtk::common::UUID &sourceId) const
Return whether a sourceId should be omitted from the copy.
Definition: CopyOptions.cxx:93
std::string typeName() const override
Return the resource's type, which is used by the resource manager and for persistent storage.
Definition: Resource.h:104
std::enable_if< is_node< NodeType >::value, std::shared_ptr< NodeType > >::type create(T &&... parameters)
Create a node of type NodeType with additional constructor arguments.
Definition: Resource.h:125
#define smtkSuperclassMacro(...)
Add a typedef to the superclass of this class.
Definition: SharedFromThis.h:147
std::string typeName()
Return the name of a class.
Definition: TypeName.h:289
bool copyFinalize(const std::shared_ptr< const smtk::resource::Resource > &source, smtk::resource::CopyOptions &options) override
Implement copyFinalize() to copy any external links from a non-empty resource of the same type.
Definition: Resource.h:536
const std::set< smtk::string::Token > & arcTypes() const override
Return the set of arc types accepted by this resource.
Definition: Resource.h:237
bool connect(Component *from, Component *to, smtk::string::Token arcType) override
Implement the generic (untyped) graph API for inserting arcs.
Definition: Resource.h:322
std::shared_ptr< smtk::resource::Resource > clone(smtk::resource::CopyOptions &options) const override
Implement clone() to make a copy of a graph resource.
Definition: Resource.h:447
A string token identified by an integer.
Definition: Token.h:29
bool copyInitialize(const std::shared_ptr< const smtk::resource::Resource > &source, smtk::resource::CopyOptions &options) override
Implement copyInitialize() to copy arcs and nodes from a non-empty resource of the same type.
Definition: Resource.h:495
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
void visit(std::function< void(const smtk::resource::ComponentPtr &)> &v) const override
Visit all the components (nodes.
Definition: Resource.h:186
std::size_t templateVersion() const override
Override the base resource to store and retrieve a template version number.
Definition: Resource.h:244
A container for arcs held by a resource.
Definition: ArcMap.h:48
std::enable_if< is_node< NodeType >::value, bool >::type remove(const std::shared_ptr< NodeType > &node)
Remove a node from the resource. Return true if the removal took place.
Definition: Resource.h:153
A non-templated base class for graph resources.
Definition: ResourceBase.h:35
smtk::string::Token templateType() const override
Override the base resource to store and retrieve a template type.
Definition: Resource.h:240
An abstract base class for SMTK resources and their components.
Definition: PersistentObject.h:39
std::function< bool(const smtk::resource::Component &)> queryOperation(const std::string &filterString) const override
Return a functor that tests a node (component) against a string query.
Definition: Resource.h:164
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:288
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:304
A resource for conceptual modeling of geometric components.
Definition: PublicPointerDefs.h:59
Embeds a type in another class so its type information can be passed as a parameter.
Definition: TupleTraits.h:340
Takes a type and a tuple of types and returns a bool indicating whether or not the type is in the tup...
Definition: TupleTraits.h:317
void evaluateArcs(Args &&... args) const
Invoke a Functor on each arc-type's implementation.
Definition: Resource.h:208