aGrUM  0.16.0
O3SystemFactory_tpl.h
Go to the documentation of this file.
1 
32 
33 namespace gum {
34  namespace prm {
35  namespace o3prm {
36 
37  template < typename GUM_SCALAR >
39  PRM< GUM_SCALAR >& prm,
40  O3PRM& o3_prm,
42  ErrorsContainer& errors) :
43  __prm(&prm),
44  __o3_prm(&o3_prm), __solver(&solver), __errors(&errors) {
45  GUM_CONSTRUCTOR(O3SystemFactory);
46  }
47 
48  template < typename GUM_SCALAR >
50  const O3SystemFactory< GUM_SCALAR >& src) :
51  __prm(src.__prm),
53  __nameMap(src.__nameMap) {
54  GUM_CONS_CPY(O3SystemFactory);
55  }
56 
57  template < typename GUM_SCALAR >
60  __prm(std::move(src.__prm)),
61  __o3_prm(std::move(src.__o3_prm)), __solver(std::move(src.__solver)),
62  __errors(std::move(src.__errors)), __nameMap(std::move(src.__nameMap)) {
63  GUM_CONS_MOV(O3SystemFactory);
64  }
65 
66  template < typename GUM_SCALAR >
68  GUM_DESTRUCTOR(O3SystemFactory);
69  }
70 
71  template < typename GUM_SCALAR >
74  if (this == &src) { return *this; }
75  __prm = src.__prm;
76  __o3_prm = src.__o3_prm;
77  __solver = src.__solver;
78  __errors = src.__errors;
79  return *this;
80  }
81 
82  template < typename GUM_SCALAR >
85  if (this == &src) { return *this; }
86  __prm = std::move(src.__prm);
87  __o3_prm = std::move(src.__o3_prm);
88  __solver = std::move(src.__solver);
89  __errors = std::move(src.__errors);
90  return *this;
91  }
92 
93  template < typename GUM_SCALAR >
96 
97  for (auto& sys : __o3_prm->systems()) {
98  // Reseting name map for each system
100 
101  if (__checkSystem(*sys)) {
102  factory.startSystem(sys->name().label());
103 
104  __addInstances(factory, *sys);
105  __addAssignments(factory, *sys);
106  __addIncrements(factory, *sys);
107 
108  try {
109  factory.endSystem();
110  } catch (FatalError&) {
111  O3PRM_SYSTEM_INSTANTIATION_FAILED(*sys, *__errors);
112  }
113  }
114  }
115  }
116 
117  template < typename GUM_SCALAR >
119  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
120  for (auto& i : sys.instances()) {
121  if (i.parameters().size() > 0) {
122  auto params = HashTable< std::string, double >();
123  for (auto& p : i.parameters()) {
124  params.insert(p.name().label(), (double)p.value().value());
125  }
126  factory.addInstance(i.type().label(), i.name().label(), params);
127 
128  } else {
129  if (i.size().value() > 1) {
130  factory.addArray(
131  i.type().label(), i.name().label(), i.size().value());
132  } else {
133  factory.addInstance(i.type().label(), i.name().label());
134  }
135  }
136  }
137  }
138 
139  template < typename GUM_SCALAR >
141  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
142  const auto& real_sys = __prm->getSystem(sys.name().label());
143 
144  for (auto& ass : sys.assignments()) {
145  auto leftInstance = ass.leftInstance().label();
146  auto leftReference = ass.leftReference().label();
147  auto rightInstance = ass.rightInstance().label();
148 
149  if (ass.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
150  std::stringstream sBuff;
151  sBuff << leftInstance << "[" << ass.leftIndex().value() << "]";
152  leftInstance = sBuff.str();
153  }
154 
155  if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
156  std::stringstream sBuff;
157  sBuff << rightInstance << "[" << ass.rightIndex().value() << "]";
158  rightInstance = sBuff.str();
159  }
160 
161  factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
162  }
163  }
164 
165  template < typename GUM_SCALAR >
167  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
168  const auto& real_sys = __prm->getSystem(sys.name().label());
169  for (auto& inc : sys.increments()) {
170  auto leftInstance = inc.leftInstance().label();
171  auto leftReference = inc.leftReference().label();
172  auto rightInstance = inc.rightInstance().label();
173 
174  if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
175  std::stringstream sBuff;
176  sBuff << leftInstance << "[" << inc.leftIndex().value() << "]";
177  leftInstance = sBuff.str();
178  }
179 
180  if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
181  std::stringstream sBuff;
182  sBuff << rightInstance << "[" << inc.rightIndex().value() << "]";
183  rightInstance = sBuff.str();
184  }
185 
186  factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
187  }
188  }
189 
190  template < typename GUM_SCALAR >
192  if (__checkInstance(sys) && __checkAssignments(sys)
193  && __checkIncrements(sys)) {
194  return true;
195  }
196 
197  return false;
198  }
199 
200  template < typename GUM_SCALAR >
202  for (auto& i : sys.instances()) {
203  if (!__solver->resolveClass(i.type())) { return false; }
204 
205  const auto& type = __prm->getClass(i.type().label());
206  if (type.parameters().size() > 0) {
207  if (!__checkParameters(type, i)) { return false; }
208  }
209 
210  if (__nameMap.exists(i.name().label())) {
211  O3PRM_SYSTEM_DUPLICATE_INSTANCE(i, *__errors);
212  return false;
213  }
214 
215  __nameMap.insert(i.name().label(), &i);
216  }
217 
218  return true;
219  }
220 
221  template < typename GUM_SCALAR >
223  const PRMClass< GUM_SCALAR >& type, const O3Instance& inst) {
224  for (const auto& param : inst.parameters()) {
225  if (!type.exists(param.name().label())) {
226  O3PRM_SYSTEM_PARAMETER_NOT_FOUND(param, *__errors);
227  return false;
228  }
229 
231  type.get(param.name().label()))) {
232  O3PRM_SYSTEM_NOT_A_PARAMETER(param, *__errors);
233  return false;
234  }
235 
236  const auto& type_param =
237  static_cast< const PRMParameter< GUM_SCALAR >& >(
238  type.get(param.name().label()));
239 
240  switch (type_param.valueType()) {
242  if (!param.isInteger()) {
243  O3PRM_SYSTEM_PARAMETER_NOT_INT(param, *__errors);
244  return false;
245  }
246  break;
247  }
248 
250  if (param.isInteger()) {
251  O3PRM_SYSTEM_PARAMETER_NOT_FLOAT(param, *__errors);
252  return false;
253  }
254  break;
255  }
256 
257  default: {
258  GUM_ERROR(FatalError, "unknown parameter type");
259  }
260  }
261  }
262  return true;
263  }
264 
265  template < typename GUM_SCALAR >
266  INLINE bool
268  for (auto& ass : sys.assignments()) {
269  // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
270  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), *__errors );
271  // return false;
272  //}
273 
274  if (!__nameMap.exists(ass.leftInstance().label())) {
275  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.leftInstance(), *__errors);
276  return false;
277  }
278 
279  auto i = __nameMap[ass.leftInstance().label()];
280  const auto& type = __prm->getClass(i->type().label());
281  const auto& ref = ass.leftReference().label();
282 
283  if (!(type.exists(ass.leftReference().label())
285  type.get(ref)))) {
286  O3PRM_SYSTEM_REFERENCE_NOT_FOUND(
287  ass.leftReference(), type.name(), *__errors);
288  return false;
289  }
290 
291  const auto& real_ref =
292  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
293 
294  if (!__nameMap.exists(ass.rightInstance().label())) {
295  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.rightInstance(), *__errors);
296  return false;
297  }
298 
299  if (real_ref.isArray()
300  && __nameMap[ass.rightInstance().label()]->size().value() == 0) {
301  O3PRM_SYSTEM_NOT_AN_ARRAY(ass.rightInstance(), *__errors);
302  return false;
303  }
304 
305  if ((!real_ref.isArray())
306  && __nameMap[ass.rightInstance().label()]->size().value() > 0
307  && ass.rightIndex().value() == -1) {
308  O3PRM_SYSTEM_NOT_AN_ARRAY(ass.leftReference(), *__errors);
309  return false;
310  }
311  }
312  return true;
313  }
314 
315  template < typename GUM_SCALAR >
317  for (auto& inc : sys.increments()) {
318  // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
319  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), *__errors );
320  // return false;
321  //}
322 
323  if (!__nameMap.exists(inc.leftInstance().label())) {
324  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(inc.leftInstance(), *__errors);
325  return false;
326  }
327 
328  auto i = __nameMap[inc.leftInstance().label()];
329  const auto& type = __prm->getClass(i->type().label());
330  const auto& ref = inc.leftReference().label();
331 
332  if (!(type.exists(inc.leftReference().label())
334  type.get(ref)))) {
335  O3PRM_SYSTEM_REFERENCE_NOT_FOUND(
336  inc.leftReference(), type.name(), *__errors);
337  return false;
338  }
339 
340  const auto& real_ref =
341  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
342 
343  if (!real_ref.isArray()) {
344  O3PRM_SYSTEM_NOT_AN_ARRAY(inc.leftReference(), *__errors);
345  return false;
346  }
347  }
348 
349  return true;
350  }
351  } // namespace o3prm
352  } // namespace prm
353 } // namespace gum
O3IncrementList & increments()
Definition: O3prm.cpp:1313
PRMParameter is a member of a Class in a PRM.
Definition: PRMParameter.h:52
virtual void setReferenceSlot(const std::string &left_instance, const std::string &left_reference, const std::string &right_instance) override
Instantiate a reference in the current model.
bool __checkParameters(const PRMClass< GUM_SCALAR > &type, const O3Instance &inst)
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
virtual void addArray(const std::string &type, const std::string &name, Size size) override
Creates an array with the given number of instances of the given type.
void __addIncrements(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
STL namespace.
Abstract class representing an element of PRM class.
O3InstanceParameterList & parameters()
Definition: O3prm.cpp:1254
O3InstanceList & instances()
Definition: O3prm.cpp:1301
O3NameSolver< GUM_SCALAR > * __solver
This class is used contain and manipulate gum::ParseError.
virtual bool exists(const std::string &name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Builds gum::prm::PRMSystem from gum::prm::o3prm::O3System.
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
Definition: PRMObject.h:223
The class for generic Hash Tables.
Definition: hashTable.h:679
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
std::string & label()
Definition: O3prm.cpp:242
Resolves names for the different O3PRM factories.
Definition: O3NameSolver.h:57
Factory which builds a PRM<GUM_SCALAR>.
Definition: PRMType.h:50
The O3Instance is part of the AST of the O3PRM language.
Definition: O3prm.h:790
O3AssignmentList & assignments()
Definition: O3prm.cpp:1307
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
void __addAssignments(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
The O3System is part of the AST of the O3PRM language.
Definition: O3prm.h:828
O3SystemList & systems()
Definition: O3prm.cpp:507
HashTable< std::string, O3Instance *> __nameMap
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
void __addInstances(PRMFactory< GUM_SCALAR > &factory, O3System &sys)
O3SystemFactory< GUM_SCALAR > & operator=(const O3SystemFactory< GUM_SCALAR > &src)
O3SystemFactory(PRM< GUM_SCALAR > &prm, O3PRM &o3_prm, O3NameSolver< GUM_SCALAR > &solver, ErrorsContainer &errors)
The O3PRM is part of the AST of the O3PRM language.
Definition: O3prm.h:892
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
virtual void endSystem() override
Tells the factory that we finished declaring a model.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55