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/evaluators/InsertUntypedArc.h"
28 #include "smtk/graph/filter/Grammar.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)...);
213 Functor::end(
this, std::forward<Args>(args)...);
217 template<
typename Functor,
typename... Args>
218 void evaluateArcs(Args&&... args)
220 Functor::begin(
this, std::forward<Args>(args)...);
221 m_arcs.invoke<
typename detail::GraphTraits<Traits>::ArcTypes, Functor>(
222 this, std::forward<Args>(args)...);
223 Functor::end(
this, std::forward<Args>(args)...);
228 const std::set<smtk::string::Token>&
nodeTypes()
const override {
return m_nodeTypes; }
231 const std::set<smtk::string::Token>&
arcTypes()
const override {
return m_arcs.types(); }
235 template<
typename ResourceType>
242 if (!m_self || m_typeName.data().empty())
244 throw std::invalid_argument(
"Invalid resource or empty node type-name.");
249 void evaluate(std::size_t ii)
253 if (
typeName == m_typeName && !m_node)
255 m_node = m_self->template create<T>();
259 const std::shared_ptr<Component>& createdNode()
const {
return m_node; }
261 ResourceType* m_self;
262 std::shared_ptr<Component> m_node;
270 if (m_nodeTypes.find(nodeTypeName) == m_nodeTypes.end())
275 smtk::tuple_evaluate<typename Traits::NodeTypes>(creator);
276 return creator.createdNode().get();
292 this->evaluateArcs<evaluators::InsertUntypedArc>(from, to, arcType, result);
299 bool removedAnArc =
false;
300 this->evaluateArcs<evaluators::DeleteArcs>(node, explicitOnly, removedAnArc);
311 void dump(
const std::string& filename,
const std::string& mimeType =
"text/vnd.graphviz")
314 std::ostream* stream = filename.empty() ? &std::cout :
new std::ofstream(filename.c_str());
315 if (stream && stream->good())
318 this->evaluateArcs<evaluators::Dump>(*stream, dump);
320 if (!filename.empty())
327 std::size_t eraseNodes(
const smtk::graph::ComponentPtr& node)
override
329 return NodeContainer::eraseNodes(node) > 0;
333 bool insertNode(
const smtk::graph::ComponentPtr& node)
override
335 return NodeContainer::insertNode(node);
342 return m_nodeTypes.find(typeToken) != m_nodeTypes.end();
354 void evaluate(std::size_t ii)
358 m_names.insert(name);
361 std::set<smtk::string::Token>& m_names;
364 void initializeResource()
368 smtk::tuple_evaluate<typename Traits::NodeTypes>(nodeTypeNames);
372 : Superclass(manager)
373 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
375 this->initializeResource();
379 : Superclass(uid, manager)
380 , m_arcs(
identity<typename detail::GraphTraits<Traits>::ArcTypes>())
382 this->initializeResource();
386 std::set<smtk::string::Token> m_nodeTypes;
387 std::set<smtk::string::Token> m_arcTypes;
393 #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:339
A functor to create smtk::string::Tokens of accepted node types.
Definition: Resource.h:236
bool disconnect(Component *node, bool explicitOnly) override
Remove all arcs (or only explicit arcs) from the given node.
Definition: Resource.h:297
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:346
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
Component * createNodeOfType(smtk::string::Token nodeTypeName) override
Implement the generic (untyped) graph API for inserting nodes.
Definition: Resource.h:268
#define smtkTypedefs(...)
Used by smtkTypeMacro()
Definition: SharedFromThis.h:21
bool insertNode(const smtk::graph::ComponentPtr &node) override
Insert a node without performing any type-safety checks.
Definition: Resource.h:333
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:40
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:311
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:113
const ArcMap & arcs() const override
Access the arcs of the graph resource.
Definition: Resource.h:160
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:190
const std::set< smtk::string::Token > & nodeTypes() const override
Return the set of node types accepted by this resource.
Definition: Resource.h:228
Definition: Resource.h:48
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:87
std::string typeName()
Return the name of a class.
Definition: TypeName.h:286
const std::set< smtk::string::Token > & arcTypes() const override
Return the set of arc types accepted by this resource.
Definition: Resource.h:231
bool connect(Component *from, Component *to, smtk::string::Token arcType) override
Implement the generic (untyped) graph API for inserting arcs.
Definition: Resource.h:289
A string token identified by an integer.
Definition: Token.h:30
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
A container for arcs held by a resource.
Definition: ArcMap.h:37
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
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: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:208