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/filter/Filter.h"
46 template<
typename T,
typename =
void>
51 template<
class T,
class... Args>
54 smtk::common::void_t<decltype(std::declval<T>().initialize(std::declval<Args>()...))>>
59 template<
class T,
class... Args>
64 t.initialize(std::forward<Args>(args)...);
67 template<
class T,
class... Args>
84 template<
typename Traits>
85 class SMTK_ALWAYS_EXPORT Resource
87 ,
public detail::GraphTraits<Traits>::NodeContainer
91 using NodeContainer =
typename detail::GraphTraits<Traits>::NodeContainer;
92 using TypeTraits = Traits;
94 template<
typename NodeType>
96 template<
typename ArcType>
105 return "smtk::graph::Resource<" + smtk::common::typeName<Traits>() +
">";
123 template<
typename NodeType,
typename... T>
124 typename std::enable_if<is_node<NodeType>::value, std::shared_ptr<NodeType>>::type
create(
127 std::shared_ptr<smtk::graph::Component> created(
new NodeType(this->shared_from_this()));
129 auto node = std::static_pointer_cast<NodeType>(created);
130 detail::initialize(*node, std::forward<T>(parameters)...);
138 template<
typename NodeType>
139 typename std::enable_if<is_node<NodeType>::value,
bool>::type
add(
140 const std::shared_ptr<NodeType>& node)
144 throw std::invalid_argument(
145 "Cannot add nodes that reference a different (or null) resource.");
147 return NodeContainer::insertNode(node);
151 template<
typename NodeType>
152 typename std::enable_if<is_node<NodeType>::value,
bool>::type
remove(
153 const std::shared_ptr<NodeType>& node)
155 return NodeContainer::eraseNodes(node) > 0;
160 ArcMap& arcs()
override {
return m_arcs; }
164 const std::string& filterString)
const override
166 return smtk::resource::filter::Filter<smtk::graph::filter::Grammar>(filterString);
175 return NodeContainer::find(uuid);
181 return NodeContainer::component(uuid);
187 NodeContainer::visit(v);
206 template<
typename Functor,
typename... Args>
209 Functor::begin(
this, std::forward<Args>(args)...);
210 m_arcs.invoke<
typename detail::GraphTraits<Traits>::ArcTypes, Functor>(
211 this, std::forward<Args>(args)...);
214 m_arcs.invokeRuntime<Functor>(
this, std::forward<Args>(args)...);
215 Functor::end(
this, std::forward<Args>(args)...);
219 template<
typename Functor,
typename... Args>
220 void evaluateArcs(Args&&... args)
222 Functor::begin(
this, std::forward<Args>(args)...);
223 m_arcs.invoke<
typename detail::GraphTraits<Traits>::ArcTypes, Functor>(
224 this, std::forward<Args>(args)...);
227 m_arcs.invokeRuntime<Functor>(
this, std::forward<Args>(args)...);
228 Functor::end(
this, std::forward<Args>(args)...);
233 const std::set<smtk::string::Token>&
nodeTypes()
const override {
return m_nodeTypes; }
236 const std::set<smtk::string::Token>&
arcTypes()
const override {
return m_arcs.types(); }
240 template<
typename ResourceType>
247 if (!m_self || m_typeName.data().empty())
249 throw std::invalid_argument(
"Invalid resource or empty node type-name.");
254 void evaluate(std::size_t ii)
258 if (
typeName == m_typeName && !m_node)
260 m_node = m_self->template create<T>();
264 const std::shared_ptr<Component>& createdNode()
const {
return m_node; }
266 ResourceType* m_self;
267 std::shared_ptr<Component> m_node;
275 if (m_nodeTypes.find(nodeTypeName) == m_nodeTypes.end())
280 smtk::tuple_evaluate<typename Traits::NodeTypes>(creator);
281 return creator.createdNode().get();
296 bool result = from->
outgoing(arcType).connect(to);
303 bool removedAnArc =
false;
304 this->evaluateArcs<evaluators::DeleteArcs>(node, explicitOnly, removedAnArc);
315 void dump(
const std::string& filename,
const std::string& mimeType =
"text/vnd.graphviz")
318 std::ostream* stream = filename.empty() ? &std::cout :
new std::ofstream(filename.c_str());
319 if (stream && stream->good())
322 this->evaluateArcs<evaluators::Dump>(*stream, dump);
324 if (!filename.empty())
331 std::size_t eraseNodes(
const smtk::graph::ComponentPtr& node)
override
333 return NodeContainer::eraseNodes(node) > 0;
337 bool insertNode(
const smtk::graph::ComponentPtr& node)
override
339 return NodeContainer::insertNode(node);
346 return m_nodeTypes.find(typeToken) != m_nodeTypes.end();
358 void evaluate(std::size_t ii)
362 m_names.insert(name);
365 std::set<smtk::string::Token>& m_names;
368 void initializeResource()
372 smtk::tuple_evaluate<typename Traits::NodeTypes>(nodeTypeNames);
376 : Superclass(manager)
377 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
379 this->initializeResource();
383 : Superclass(uid, manager)
384 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
386 this->initializeResource();
390 std::set<smtk::string::Token> m_nodeTypes;
391 std::set<smtk::string::Token> m_arcTypes;
397 #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:62
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:343
A functor to create smtk::string::Tokens of accepted node types.
Definition: Resource.h:241
bool disconnect(Component *node, bool explicitOnly) override
Remove all arcs (or only explicit arcs) from the given node.
Definition: Resource.h:301
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:139
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:173
A functor to create smtk::string::Tokens of accepted node types.
Definition: Resource.h:350
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:96
Component * createNodeOfType(smtk::string::Token nodeTypeName) override
Implement the generic (untyped) graph API for inserting nodes.
Definition: Resource.h:273
#define smtkTypedefs(...)
Used by smtkTypeMacro()
Definition: SharedFromThis.h:24
bool insertNode(const smtk::graph::ComponentPtr &node) override
Insert a node without performing any type-safety checks.
Definition: Resource.h:337
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:179
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:315
#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:159
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:233
Definition: Resource.h:47
std::string typeName() const override
Return the resource's type, which is used by the resource manager and for persistent storage.
Definition: Resource.h:103
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:124
#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
const std::set< smtk::string::Token > & arcTypes() const override
Return the set of arc types accepted by this resource.
Definition: Resource.h:236
bool connect(Component *from, Component *to, smtk::string::Token arcType) override
Implement the generic (untyped) graph API for inserting arcs.
Definition: Resource.h:294
A string token identified by an integer.
Definition: Token.h:29
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:185
A container for arcs held by a resource.
Definition: ArcMap.h:44
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:152
A non-templated base class for graph resources.
Definition: ResourceBase.h:35
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:163
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
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:207