CPN
Computational Process Networks
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CPN::VariantCPNLoader Class Reference

#include <VariantCPNLoader.h>

+ Collaboration diagram for CPN::VariantCPNLoader:

Public Member Functions

 VariantCPNLoader ()
 
 VariantCPNLoader (Variant conf)
 
 ~VariantCPNLoader ()
 
Variant & GetConfig ()
 
void MergeConfig (Variant v)
 
void KernelName (const std::string &name)
 
void KernelHost (const std::string &host)
 
void KernelPort (const std::string &port)
 
void AddLib (const std::string &filename)
 
void AddLibList (const std::string &filename)
 
void AddLibPath (const std::string &path)
 
void UseD4R (bool value)
 
void GrowQueueMaxThreshold (bool value)
 
void SwallowBrokenQueueExceptions (bool value)
 
void LogLevel (int i)
 
void ContextHost (const std::string &host)
 
void ContextPort (const std::string &port)
 
void AddNode (Variant v)
 
void AddQueue (Variant v)
 
void AddNodeMapping (const std::string &noden, const std::string &kernn)
 
std::pair< bool, std::string > Validate ()
 
CPN::KernelAttr GetKernelAttr ()
 
void Setup (CPN::Kernel *kernel)
 

Static Public Member Functions

static CPN::shared_ptr
< CPN::Context
LoadContext (Variant v)
 
static CPN::KernelAttr GetKernelAttr (Variant args)
 
static void Setup (CPN::Kernel *kernel, Variant args)
 
static void LoadNodes (CPN::Kernel *kernel, Variant nodelist, Variant nodemap)
 
static void LoadNode (CPN::Kernel *kernel, Variant attr, Variant nodemap)
 
static void LoadQueues (CPN::Kernel *kernel, Variant queuelist)
 
static void LoadQueue (CPN::Kernel *kernel, Variant attr)
 
static std::pair< bool,
std::string > 
Validate (Variant conf)
 

Private Attributes

Variant config
 

Detailed Description

Load a CPN network from a specially defined Variant

Definition at line 36 of file VariantCPNLoader.h.

Constructor & Destructor Documentation

CPN::VariantCPNLoader::VariantCPNLoader ( )

Definition at line 37 of file VariantCPNLoader.cc.

37 { }
CPN::VariantCPNLoader::VariantCPNLoader ( Variant  conf)

Definition at line 38 of file VariantCPNLoader.cc.

38 : config(conf) { }
CPN::VariantCPNLoader::~VariantCPNLoader ( )

Definition at line 39 of file VariantCPNLoader.cc.

39 { }

Member Function Documentation

void CPN::VariantCPNLoader::AddLib ( const std::string &  filename)

Definition at line 53 of file VariantCPNLoader.cc.

References config.

53  {
54  config["libs"].Append(filename);
55  }
void CPN::VariantCPNLoader::AddLibList ( const std::string &  filename)

Definition at line 57 of file VariantCPNLoader.cc.

References config.

57  {
58  config["liblist"].Append(filename);
59  }
void CPN::VariantCPNLoader::AddLibPath ( const std::string &  path)

Definition at line 61 of file VariantCPNLoader.cc.

References config.

61  {
62  config["libpaths"].Append(path);
63  }
void CPN::VariantCPNLoader::AddNode ( Variant  v)

Definition at line 102 of file VariantCPNLoader.cc.

References config.

Referenced by MergeConfig().

102  {
103  if (!config.Contains("nodes")) {
104  config["nodes"] = Variant::ListType;
105  }
106  Variant nodes = config["nodes"];
107  Variant::ListIterator itr = nodes.ListBegin(), end = nodes.ListEnd();
108  for (; itr != end; ++itr) {
109  if (v["name"] == itr->At("name")) {
110  for (Variant::MapIterator mitr = v.MapBegin(),
111  mend = v.MapEnd(); mitr != mend; ++mitr) {
112  itr->At(mitr->first) = mitr->second.Copy();
113  }
114  return;
115  }
116  }
117  nodes.Append(v.Copy());
118  }

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::AddNodeMapping ( const std::string &  noden,
const std::string &  kernn 
)

Definition at line 147 of file VariantCPNLoader.cc.

References config.

147  {
148  config.At("nodemap", Variant::MapType).Set(noden, kernn);
149  }
void CPN::VariantCPNLoader::AddQueue ( Variant  v)

Definition at line 120 of file VariantCPNLoader.cc.

References config.

Referenced by MergeConfig().

120  {
121  if (!config.Contains("queues")) {
122  config["queues"] = Variant::ListType;
123  }
124  Variant queues = config["queues"];
125  Variant::ListIterator qitr = queues.ListBegin(), qend = queues.ListEnd();
126  for (;qitr != qend; ++qitr) {
127  if (
128  (
129  v["readernode"] == qitr->At("readernode") &&
130  v["readerport"] == qitr->At("readerport")
131  ) ||
132  (
133  v["writernode"] == qitr->At("writernode") &&
134  v["writerport"] == qitr->At("writerport")
135  )
136  ) {
137  for (Variant::MapIterator mitr = v.MapBegin(),
138  mend = v.MapEnd(); mitr != mend; ++mitr) {
139  qitr->At(mitr->first) = mitr->second.Copy();
140  }
141  return;
142  }
143  }
144  queues.Append(v.Copy());
145  }

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::ContextHost ( const std::string &  host)

Definition at line 65 of file VariantCPNLoader.cc.

References config.

65  {
66  config["context"]["host"] = host;
67  }
void CPN::VariantCPNLoader::ContextPort ( const std::string &  port)

Definition at line 69 of file VariantCPNLoader.cc.

References config.

69  {
70  config["context"]["port"] = port;
71  }
Variant& CPN::VariantCPNLoader::GetConfig ( )
inline

Definition at line 42 of file VariantCPNLoader.h.

References config.

42 { return config; }
CPN::KernelAttr CPN::VariantCPNLoader::GetKernelAttr ( )
inline

Definition at line 66 of file VariantCPNLoader.h.

References config, and GetKernelAttr().

Referenced by GetKernelAttr().

66 { return GetKernelAttr(config); }
CPN::KernelAttr GetKernelAttr()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

CPN::KernelAttr CPN::VariantCPNLoader::GetKernelAttr ( Variant  args)
static

Definition at line 186 of file VariantCPNLoader.cc.

References CPN::KernelAttr::AddNodeList(), CPN::KernelAttr::AddNodeSearchPath(), CPN::KernelAttr::AddSharedLib(), CPN::KernelAttr::GrowQueueMaxThreshold(), LoadContext(), CPN::KernelAttr::SetContext(), CPN::KernelAttr::SetHostName(), CPN::KernelAttr::SetServName(), CPN::KernelAttr::SwallowBrokenQueueExceptions(), and CPN::KernelAttr::UseD4R().

186  {
187  CPN::KernelAttr attr(args["name"].AsString());
188  if (args.Contains("context")) {
189  attr.SetContext(LoadContext(args["context"]));
190  }
191  if (args.Contains("host")) {
192  attr.SetHostName(args["host"].AsString());
193  }
194  if (args.Contains("port")) {
195  attr.SetServName(args["port"].AsString());
196  }
197 
198  if (args.Contains("d4r")) {
199  attr.UseD4R(args["d4r"].AsBool());
200  }
201  if (args.Contains("swallow-broken-queue-exceptions")) {
202  attr.SwallowBrokenQueueExceptions(args["swallow-broken-queue-exceptions"].AsBool());
203  }
204  if (args.Contains("grow-queue-max-threshold")) {
205  attr.GrowQueueMaxThreshold(args["grow-queue-max-threshold"].AsBool());
206  }
207  if (args.Contains("libs") && args["libs"].IsList()) {
208  for (Variant::ListIterator itr = args["libs"].ListBegin(); itr != args["libs"].ListEnd(); ++itr) {
209  attr.AddSharedLib(itr->AsString());
210  }
211  }
212  if (args.Contains("liblist") && args["liblist"].IsList()) {
213  for (Variant::ListIterator itr = args["liblist"].ListBegin(); itr != args["liblist"].ListEnd(); ++itr) {
214  attr.AddNodeList(itr->AsString());
215  }
216  }
217  if (args.Contains("libpaths") && args["libpaths"].IsList()) {
218  for (Variant::ListIterator itr = args["libpaths"].ListBegin(); itr != args["libpaths"].ListEnd(); ++itr) {
219  attr.AddNodeSearchPath(itr->AsString());
220  }
221  }
222  return attr;
223  }
The attribute for the Kernel.
Definition: KernelAttr.h:40
static CPN::shared_ptr< CPN::Context > LoadContext(Variant v)

+ Here is the call graph for this function:

void CPN::VariantCPNLoader::GrowQueueMaxThreshold ( bool  value)

Definition at line 45 of file VariantCPNLoader.cc.

References config.

45  {
46  config["grow-queue-max-threshold"] = value;
47  }
void CPN::VariantCPNLoader::KernelHost ( const std::string &  host)

Definition at line 178 of file VariantCPNLoader.cc.

References config.

178  {
179  config["host"] = host;
180  }
void CPN::VariantCPNLoader::KernelName ( const std::string &  name)

Definition at line 174 of file VariantCPNLoader.cc.

References config.

174  {
175  config["name"] = name;
176  }
void CPN::VariantCPNLoader::KernelPort ( const std::string &  port)

Definition at line 182 of file VariantCPNLoader.cc.

References config.

182  {
183  config["port"] = port;
184  }
shared_ptr< Context > CPN::VariantCPNLoader::LoadContext ( Variant  v)
static

Definition at line 151 of file VariantCPNLoader.cc.

References SocketAddress::CreateIP(), and CPN::Context::Local().

Referenced by GetKernelAttr().

151  {
152  shared_ptr<Context> context;
153  if (v.IsNull()) {
154  context = Context::Local();
155  } else {
156  if (!v.Contains("host") && !v.Contains("port")) {
157  context = Context::Local();
158  } else {
160  v["host"].AsString(),
161  v["port"].AsString()
162  );
163  shared_ptr<RemoteContext> ctx = shared_ptr<RemoteContext>(new RemoteContext(addrs));
164  context = ctx;
165  }
166 
167  if (v.Contains("loglevel")) {
168  context->LogLevel(v["loglevel"].AsInt());
169  }
170  }
171  return context;
172  }
static shared_ptr< Context > Local()
Create a local context.
Definition: Context.cc:29
static SockAddrList CreateIP(unsigned serv)
Return a list of valid socket address for the given service number or port number.
std::vector< SocketAddress > SockAddrList
Definition: SocketAddress.h:35

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::LoadNode ( CPN::Kernel kernel,
Variant  attr,
Variant  nodemap 
)
static

Definition at line 245 of file VariantCPNLoader.cc.

References CPN::Kernel::CreateNode(), CPN::NodeAttr::GetName(), CPN::NodeAttr::SetKernel(), and CPN::NodeAttr::SetParam().

Referenced by LoadNodes().

245  {
246  CPN::NodeAttr nattr(attr["name"].AsString(), attr["type"].AsString());
247  if (nodemap.IsMap() && nodemap.Contains(nattr.GetName())) {
248  nattr.SetKernel(nodemap.At(nattr.GetName()).AsString());
249  } else if (attr.Contains("kernel")) {
250  nattr.SetKernel(attr["kernel"].AsString());
251  }
252  Variant param = attr["param"];
253  if (param.IsMap()) {
254  for (Variant::MapIterator i = param.MapBegin(), e = param.MapEnd();
255  i != e; ++i)
256  {
257  nattr.SetParam(i->first, i->second.AsString());
258  }
259  }
260  kernel->CreateNode(nattr);
261  }
Attributes for a node.
Definition: NodeAttr.h:58
Key_t CreateNode(const NodeAttr &attr)
Definition: Kernel.cc:139

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::LoadNodes ( CPN::Kernel kernel,
Variant  nodelist,
Variant  nodemap 
)
static

Definition at line 230 of file VariantCPNLoader.cc.

References LoadNode().

Referenced by Setup().

230  {
231  if (!nodelist.IsList()) {
232  return;
233  }
234  Variant::ConstListIterator itr, end;
235  itr = nodelist.ListBegin();
236  end = nodelist.ListEnd();
237  while (itr != end) {
238  if (itr->IsMap()) {
239  LoadNode(kernel, *itr, nodemap);
240  }
241  ++itr;
242  }
243  }
static void LoadNode(CPN::Kernel *kernel, Variant attr, Variant nodemap)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::LoadQueue ( CPN::Kernel kernel,
Variant  attr 
)
static

Definition at line 278 of file VariantCPNLoader.cc.

References CPN::Kernel::CreateQueue(), CPN::QueueAttr::SetAlpha(), CPN::QueueAttr::SetDatatype(), CPN::QueueAttr::SetHint(), CPN::QueueAttr::SetMaxWriteThreshold(), CPN::QueueAttr::SetName(), CPN::QueueAttr::SetNumChannels(), CPN::QueueAttr::SetReader(), and CPN::QueueAttr::SetWriter().

Referenced by LoadQueues().

278  {
279  CPN::QueueAttr qattr(attr["size"].AsUnsigned(), attr["threshold"].AsUnsigned());
280  qattr.SetReader(attr["readernode"].AsString(), attr["readerport"].AsString());
281  qattr.SetWriter(attr["writernode"].AsString(), attr["writerport"].AsString());
282  if (attr.Contains("type")) {
283  qattr.SetHint(attr["type"].AsString());
284  }
285  if (attr.Contains("datatype")) {
286  qattr.SetDatatype(attr["datatype"].AsString());
287  }
288  if (attr.Contains("numchannels")) {
289  qattr.SetNumChannels(attr["numchannels"].AsUnsigned());
290  }
291  if (attr.Contains("alpha")) {
292  qattr.SetAlpha(attr["alpha"].AsDouble());
293  }
294  if (attr.Contains("name")) {
295  qattr.SetName(attr["name"].AsString());
296  }
297  if (attr.Contains("maxwritethreshold")) {
298  qattr.SetMaxWriteThreshold(attr["maxwritethreshold"].AsUnsigned());
299  }
300  kernel->CreateQueue(qattr);
301  }
void CreateQueue(const QueueAttr &attr)
Create a new queue.
Definition: Kernel.cc:251
The attributes for a queue.
Definition: QueueAttr.h:55

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::LoadQueues ( CPN::Kernel kernel,
Variant  queuelist 
)
static

Definition at line 263 of file VariantCPNLoader.cc.

References LoadQueue().

Referenced by Setup().

263  {
264  if (!queuelist.IsList()) {
265  return;
266  }
267  Variant::ConstListIterator itr, end;
268  itr = queuelist.ListBegin();
269  end = queuelist.ListEnd();
270  while (itr != end) {
271  if (itr->IsMap()) {
272  LoadQueue(kernel, *itr);
273  }
274  ++itr;
275  }
276  }
static void LoadQueue(CPN::Kernel *kernel, Variant attr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::LogLevel ( int  i)

Definition at line 73 of file VariantCPNLoader.cc.

References config.

73  {
74  config["context"]["loglevel"] = i;
75  }
void CPN::VariantCPNLoader::MergeConfig ( Variant  v)

Definition at line 77 of file VariantCPNLoader.cc.

References AddNode(), AddQueue(), and config.

77  {
78  Variant::MapIterator topitr = v.MapBegin(), topend = v.MapEnd();
79  for (;topitr != topend; ++topitr) {
80  if (topitr->first == "context") {
81  Variant::MapIterator mitr = v["context"].MapBegin(), mend = v["context"].MapEnd();
82  for (;mitr != mend; ++mitr) {
83  config["context"][mitr->first] = mitr->second;
84  }
85  } else if (topitr->first == "nodes") {
86  Variant::ListIterator litr = v["nodes"].ListBegin(), lend = v["nodes"].ListEnd();
87  for (;litr != lend; ++litr) {
88  AddNode(*litr);
89  }
90  } else if (topitr->first == "queues") {
91  Variant::ListIterator litr = v["queues"].ListBegin(), lend = v["queues"].ListEnd();
92  for (;litr != lend; ++litr) {
93  AddQueue(*litr);
94  }
95  } else {
96  config[topitr->first] = topitr->second;
97  }
98  }
99  }
void AddQueue(Variant v)

+ Here is the call graph for this function:

void CPN::VariantCPNLoader::Setup ( CPN::Kernel kernel)
inline

Definition at line 67 of file VariantCPNLoader.h.

References config, and Setup().

Referenced by Setup().

67 { Setup(kernel, config); }
void Setup(CPN::Kernel *kernel)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::VariantCPNLoader::Setup ( CPN::Kernel kernel,
Variant  args 
)
static

Definition at line 225 of file VariantCPNLoader.cc.

References LoadNodes(), and LoadQueues().

225  {
226  LoadNodes(kernel, args.Get("nodes", Variant::ListType), args.Get("nodemap", Variant::MapType));
227  LoadQueues(kernel, args.Get("queues", Variant::ListType));
228  }
static void LoadNodes(CPN::Kernel *kernel, Variant nodelist, Variant nodemap)
static void LoadQueues(CPN::Kernel *kernel, Variant queuelist)

+ Here is the call graph for this function:

void CPN::VariantCPNLoader::SwallowBrokenQueueExceptions ( bool  value)

Definition at line 49 of file VariantCPNLoader.cc.

References config.

49  {
50  config["swallow-broken-queue-exceptions"] = value;
51  }
void CPN::VariantCPNLoader::UseD4R ( bool  value)

Definition at line 41 of file VariantCPNLoader.cc.

References config.

41  {
42  config["d4r"] = value;
43  }
std::pair<bool, std::string> CPN::VariantCPNLoader::Validate ( )
inline

Definition at line 64 of file VariantCPNLoader.h.

References config, and Validate().

Referenced by Validate().

64 { return Validate(config); }
std::pair< bool, std::string > Validate()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::pair< bool, std::string > CPN::VariantCPNLoader::Validate ( Variant  conf)
static

Definition at line 303 of file VariantCPNLoader.cc.

303  {
304  set<string> nodeset;
305  if (!conf.Contains("name")) {
306  return make_pair(false, "Kernels must have a name.");
307  }
308 
309  if (conf.Contains("nodes")) {
310  Variant nodes = conf["nodes"];
311  Variant::ListIterator nitr = nodes.ListBegin(), nend = nodes.ListEnd();
312  for (; nitr != nend; ++nitr) {
313  if (!nitr->Contains("name")) {
314  return make_pair(false, "Nodes must have a name");
315  }
316  if (!nitr->Contains("type")) {
317  return make_pair(false, "Nodes must have a type");
318  }
319  if (!nodeset.insert(nitr->At("name").AsString()).second) {
320  return make_pair(false, "Duplicate node");
321  }
322  }
323  }
324  if (conf.Contains("queues")) {
325  map<string, set<string> > readports, writeports;
326  Variant queues = conf["queues"];
327  Variant::ListIterator qitr = queues.ListBegin(), qend = queues.ListEnd();
328  for (;qitr != qend; ++qitr) {
329  if (!qitr->Contains("size")) {
330  return make_pair(false, "Queues must have a size");
331  }
332  if (!qitr->Contains("threshold")) {
333  return make_pair(false, "Queues must have a threshold");
334  }
335  if (!qitr->Contains("readernode")) {
336  return make_pair(false, "Queues must have a readernode");
337  }
338  if (!qitr->Contains("readerport")) {
339  return make_pair(false, "Queues must have a readerport");
340  }
341  if (!readports[qitr->At("readernode").AsString()].insert(qitr->At("readerport").AsString()).second) {
342  return make_pair(false, "Two queues connected to the same reader port.");
343  }
344  if (!qitr->Contains("writernode")) {
345  return make_pair(false, "Queues must have a writernode");
346  }
347  if (!qitr->Contains("writerport")) {
348  return make_pair(false, "Queues must have a writerport");
349  }
350  if (!writeports[qitr->At("writernode").AsString()].insert(qitr->At("writerport").AsString()).second) {
351  return make_pair(false, "Two queues connected to the same writer port.");
352  }
353  }
354  }
355  return make_pair(true, "");
356  }

Member Data Documentation

Variant CPN::VariantCPNLoader::config
private

The documentation for this class was generated from the following files: