SPH
ScriptNode.cpp
Go to the documentation of this file.
1 #include "run/ScriptNode.h"
2 #include "run/ScriptUtils.h"
3 
5 
6 #ifdef SPH_USE_CHAISCRIPT
7 
8 namespace Chai {
9 
10 class Node {
11 private:
12  SharedPtr<JobNode> node;
13  VirtualSettings settings;
14 
15  const RunSettings& global;
16  IJobCallbacks& callbacks;
17 
18 public:
19  Node(SharedPtr<JobNode> node, const RunSettings& global, IJobCallbacks& callbacks)
20  : node(node)
21  , global(global)
22  , callbacks(callbacks) {
23  settings = node->getSettings();
24  }
25 
26  template <typename T>
27  void setParam(const std::string& key, const T value) {
28  IVirtualEntry::Value current = settings.get(key);
29  if (!current.has<T>()) {
30  throw InvalidSetup("Type mismatch when setting parameter '" + key + "'");
31  }
32  settings.set(key, value);
33  }
34 
35  void run() {
36  if (!callbacks.shouldAbortRun()) {
37  node->run(global, callbacks);
38  }
39  }
40 };
41 
42 
43 // either string, Path, or EnumWrapper
44 template <>
45 void Node::setParam(const std::string& key, const std::string value) {
46  IVirtualEntry::Value current = settings.get(key);
47  if (current.has<std::string>()) {
48  settings.set(key, value);
49  } else if (current.has<Path>()) {
50  settings.set(key, Path(value));
51  } else if (current.has<EnumWrapper>()) {
52  EnumWrapper ew = current.get<EnumWrapper>();
53  Optional<int> enumValue = EnumMap::fromString(value, ew.index);
54  if (!enumValue) {
55  throw InvalidSetup("Unknown value of parameter '" + key + "': " + value);
56  }
57  ew.value = enumValue.value();
58  settings.set(key, ew);
59  } else {
60  throw InvalidSetup("Type mismatch when setting parameter '" + key + "'");
61  }
62 }
63 
64 } // namespace Chai
65 
66 void ScriptNode::run(const RunSettings& global, IJobCallbacks& callbacks) {
67  chaiscript::ChaiScript chai;
68  Chai::registerBindings(chai);
69 
70  chai.add(chaiscript::user_type<Chai::Node>(), "Node");
71  chai.add(chaiscript::fun(&Chai::Node::setParam<Float>), "setParam");
72  chai.add(chaiscript::fun(&Chai::Node::setParam<int>), "setParam");
73  chai.add(chaiscript::fun(&Chai::Node::setParam<std::string>), "setParam");
74  chai.add(chaiscript::fun(&Chai::Node::run), "run");
75 
76  chai.add(chaiscript::fun<std::function<Chai::Node(std::string)>>([&](std::string name) {
77  for (const auto& node : nodes) {
78  if (node->instanceName() == name) {
79  return Chai::Node(node, global, callbacks);
80  }
81  }
82  throw InvalidSetup("Unknown node '" + name + "'");
83  }),
84  "getNode");
85 
86  chai.eval_file(file.native());
87 }
88 
89 #endif
90 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define NAMESPACE_SPH_END
Definition: Object.h:12
Utility functions and classes exposed to the embedded scripting language.
static Optional< TEnum > fromString(const std::string &value)
Definition: EnumMap.h:101
Interface used during job evaluation.
Definition: Node.h:25
virtual bool shouldAbortRun() const =0
Returns whether current run should be aborted or not.
Thrown when components of the run are mutually incompatible.
Definition: Exceptions.h:24
virtual void run(const RunSettings &global, IJobCallbacks &callbacks) override
Evaluates the node and all its providers.
Definition: Node.cpp:184
std::string instanceName() const
Returns the instance name of the job.
Definition: Node.cpp:14
VirtualSettings getSettings() const
Returns settings object allowing to access and modify the state of the job.
Definition: Node.cpp:39
Object representing a path on a filesystem.
Definition: Path.h:17
Variant, an implementation of type-safe union, similar to std::variant or boost::variant.
Definition: Variant.h:171
INLINE bool has() const
Checks if the variant currently hold value of given type.
Definition: Variant.h:316
INLINE T & get()
Returns the stored value.
Definition: Variant.h:335
Holds a map of virtual entries, associated with a unique name.
IVirtualEntry::Value get(const std::string &key) const
Returns a value of an entry.
void set(const std::string &key, const IVirtualEntry::Value &value)
Modifies an existing entry in the settings.
Wrapper of an enum.
Definition: Settings.h:37
int value
Definition: Settings.h:38
EnumIndex index
Definition: Settings.h:40