SPH
Node.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "io/Output.h"
11 #include "objects/wrappers/Any.h"
12 #include "quantities/Storage.h"
13 #include "run/IRun.h"
14 #include "run/Job.h"
15 #include "run/VirtualSettings.h"
16 #include "system/Statistics.h"
17 #include <set>
18 
20 
21 class ConfigNode;
22 class JobNode;
23 
25 class IJobCallbacks : public IRunCallbacks {
26 public:
28  virtual void onStart(const IJob& job) = 0;
29 
34  virtual void onEnd(const Storage& storage, const Statistics& stats) = 0;
35 };
36 
38 public:
39  virtual void onStart(const IJob& UNUSED(job)) override {}
40 
41  virtual void onEnd(const Storage& UNUSED(storage), const Statistics& UNUSED(stats)) override {}
42 
43  virtual void onSetUp(const Storage& UNUSED(storage), Statistics& UNUSED(stats)) override {}
44 
45  virtual void onTimeStep(const Storage& UNUSED(storage), Statistics& UNUSED(stats)) override {}
46 
47  virtual bool shouldAbortRun() const override {
48  return false;
49  }
50 };
51 
53 class INode : public Polymorphic {
54 public:
55  virtual void run(const RunSettings& global, IJobCallbacks& callbacks) = 0;
56 };
57 
58 struct SlotData {
60  std::string name;
61 
64 
69  bool used;
70 
75 };
76 
83 };
84 
88 class JobNode : public ShareFromThis<JobNode>, public INode {
89 public:
90  using Accessor = Function<void(JobNotificationType, const Any& value)>;
91 
92 private:
95 
97  Array<WeakPtr<JobNode>> dependents;
98 
100  AutoPtr<IJob> job;
101 
103  CallbackSet<Accessor> accessors;
104 
105 public:
107  explicit JobNode(AutoPtr<IJob>&& job);
108 
110  std::string instanceName() const;
111 
113  std::string className() const;
114 
117 
119  RawPtr<IJob> getJob() const;
120 
122  void addAccessor(const SharedToken& owner, const Accessor& accessor);
123 
126 
132  void connect(SharedPtr<JobNode> dependent, const std::string& slotName);
133 
138  void disconnect(SharedPtr<JobNode> dependent);
139 
141  void disconnectAll();
142 
144  Size getSlotCnt() const;
145 
147  SlotData getSlot(const Size index) const;
148 
150  Size getDependentCnt() const;
151 
153  SharedPtr<JobNode> getDependent(const Size index) const;
154 
159  void enumerate(Function<void(const SharedPtr<JobNode>& job)> func);
160 
164  void enumerate(Function<void(const SharedPtr<JobNode>& job, Size depth)> func);
165 
171  virtual void run(const RunSettings& global, IJobCallbacks& callbacks) override;
172 
177  virtual void prepare(const RunSettings& global, IJobCallbacks& callbacks);
178 
179 private:
180  void enumerate(Function<void(const SharedPtr<JobNode>& job, Size depth)> func,
181  Size depth,
182  std::set<JobNode*>& visited);
183 
184  void run(const RunSettings& global, IJobCallbacks& callbacks, std::set<JobNode*>& visited);
185 
186  void prepare(const RunSettings& global, IJobCallbacks& callbacks, std::set<JobNode*>& visited);
187 };
188 
190 template <typename TJob, typename... TArgs>
191 SharedPtr<JobNode> makeNode(TArgs&&... args) {
192  return makeShared<JobNode>(makeAuto<TJob>(std::forward<TArgs>(args)...));
193 }
194 
200 AutoPtr<JobNode> cloneNode(const JobNode& node, const std::string& name = "");
201 
208 
Object that can store value of any type.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Container of callbacks.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Basic interface defining a single run.
JobNotificationType
Definition: Node.h:77
@ ENTRY_CHANGED
Definition: Node.h:78
@ DEPENDENT_DISCONNECTED
Definition: Node.h:82
@ PROVIDER_DISCONNECTED
Definition: Node.h:80
@ DEPENDENT_CONNECTED
Definition: Node.h:81
@ PROVIDER_CONNECTED
Definition: Node.h:79
AutoPtr< JobNode > cloneNode(const JobNode &node, const std::string &name="")
Clones a single node.
Definition: Node.cpp:269
SharedPtr< JobNode > makeNode(TArgs &&... args)
Helper function for creating job nodes.
Definition: Node.h:191
SharedPtr< JobNode > cloneHierarchy(JobNode &node, const Optional< std::string > &prefix=NOTHING)
Clones all nodes in the hierarchy.
Definition: Node.cpp:282
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
const NothingType NOTHING
Definition: Optional.h:16
Saving and loading particle data.
Statistics gathered and periodically displayed during the run.
Container for storing particle quantities and materials.
Key-value associative container.
Object providing connection between component parameters and values exposed to the user.
Type-safe object that can store value of any type, similar to std::any.
Definition: Any.h:15
Generic dynamically allocated resizable storage.
Definition: Array.h:43
Represents a single node in the hierarchy written into config file.
Definition: Config.h:198
Interface used during job evaluation.
Definition: Node.h:25
virtual void onEnd(const Storage &storage, const Statistics &stats)=0
Notifies the caller that the current job ended.
virtual void onStart(const IJob &job)=0
Notifies the caller that a new job started running.
Base class for all object performing an operation in a simulation hierarchy.
Definition: Job.h:96
Provides an interface for running a simulation.
Definition: Node.h:53
virtual void run(const RunSettings &global, IJobCallbacks &callbacks)=0
Callbacks executed by the simulation to provide feedback to the user.
Definition: IRun.h:27
Building block of a simulation hierarchy.
Definition: Node.h:88
Size getSlotCnt() const
Returns the number of provider slots of this node.
Definition: Node.cpp:132
virtual void run(const RunSettings &global, IJobCallbacks &callbacks) override
Evaluates the node and all its providers.
Definition: Node.cpp:184
void addAccessor(const SharedToken &owner, const Accessor &accessor)
Adds an accessor for entries returned by the getSettings function.
Definition: Node.cpp:52
Size getDependentCnt() const
Returns the number of dependent nodes.
Definition: Node.cpp:154
void enumerate(Function< void(const SharedPtr< JobNode > &job, Size depth)> func)
Enumerates all nodes in the hierarchy.
std::string instanceName() const
Returns the instance name of the job.
Definition: Node.cpp:14
std::string className() const
Returns the class name of the job.
Definition: Node.cpp:10
Optional< ExtJobType > provides() const
Returns the type of the job.
Definition: Node.cpp:56
RawPtr< IJob > getJob() const
Returns the underlying job.
Definition: Node.cpp:48
SlotData getSlot(const Size index) const
Returns the information about given slot.
Definition: Node.cpp:136
virtual void prepare(const RunSettings &global, IJobCallbacks &callbacks)
Evaluates all provides, without executing the node itself.
Definition: Node.cpp:189
void disconnect(SharedPtr< JobNode > dependent)
Disconnects this node from given dependent node.
Definition: Node.cpp:90
void disconnectAll()
Disconnects all dependent nodes from this node.
Definition: Node.cpp:126
void connect(SharedPtr< JobNode > dependent, const std::string &slotName)
Connects this node to given dependent node.
Definition: Node.cpp:60
SharedPtr< JobNode > getDependent(const Size index) const
Returns a dependent node with given index.
Definition: Node.cpp:158
JobNode(AutoPtr< IJob > &&job)
Creates a new node, given a job object.
Definition: Node.cpp:7
void enumerate(Function< void(const SharedPtr< JobNode > &job)> func)
Enumerates all nodes in the hierarchy.
VirtualSettings getSettings() const
Returns settings object allowing to access and modify the state of the job.
Definition: Node.cpp:39
virtual void onStart(const IJob &UNUSED(job)) override
Definition: Node.h:39
virtual bool shouldAbortRun() const override
Returns whether current run should be aborted or not.
Definition: Node.h:47
virtual void onEnd(const Storage &UNUSED(storage), const Statistics &UNUSED(stats)) override
Definition: Node.h:41
virtual void onSetUp(const Storage &UNUSED(storage), Statistics &UNUSED(stats)) override
Definition: Node.h:43
virtual void onTimeStep(const Storage &UNUSED(storage), Statistics &UNUSED(stats)) override
Definition: Node.h:45
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Holds a map of virtual entries, associated with a unique name.
Base class for all polymorphic objects.
Definition: Object.h:88
Definition: Node.h:58
bool used
Whether the node is used by the job.
Definition: Node.h:69
ExtJobType type
Specifies the type of the slot, or the type of the node connecting to it.
Definition: Node.h:63
std::string name
Identifier of the slot, used by the job to obtain the provided data.
Definition: Node.h:60
SharedPtr< JobNode > provider
Node currently connected to the slot.
Definition: Node.h:74