aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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  const O3SystemFactory< GUM_SCALAR >& src) {
75  if (this == &src) { return *this; }
76  __prm = src.__prm;
77  __o3_prm = src.__o3_prm;
78  __solver = src.__solver;
79  __errors = src.__errors;
80  return *this;
81  }
82 
83  template < typename GUM_SCALAR >
87  if (this == &src) { return *this; }
88  __prm = std::move(src.__prm);
89  __o3_prm = std::move(src.__o3_prm);
90  __solver = std::move(src.__solver);
91  __errors = std::move(src.__errors);
92  return *this;
93  }
94 
95  template < typename GUM_SCALAR >
98 
99  for (auto& sys: __o3_prm->systems()) {
100  // Reseting name map for each system
102 
103  if (__checkSystem(*sys)) {
104  factory.startSystem(sys->name().label());
105 
106  __addInstances(factory, *sys);
107  __addAssignments(factory, *sys);
108  __addIncrements(factory, *sys);
109 
110  try {
111  factory.endSystem();
112  } catch (FatalError&) {
113  O3PRM_SYSTEM_INSTANTIATION_FAILED(*sys, *__errors);
114  }
115  }
116  }
117  }
118 
119  template < typename GUM_SCALAR >
121  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
122  for (auto& i: sys.instances()) {
123  if (i.parameters().size() > 0) {
124  auto params = HashTable< std::string, double >();
125  for (auto& p: i.parameters()) {
126  params.insert(p.name().label(), (double)p.value().value());
127  }
128  factory.addInstance(i.type().label(), i.name().label(), params);
129 
130  } else {
131  if (i.size().value() > 1) {
132  factory.addArray(
133  i.type().label(), i.name().label(), i.size().value());
134  } else {
135  factory.addInstance(i.type().label(), i.name().label());
136  }
137  }
138  }
139  }
140 
141  template < typename GUM_SCALAR >
143  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
144  const auto& real_sys = __prm->getSystem(sys.name().label());
145 
146  for (auto& ass: sys.assignments()) {
147  auto leftInstance = ass.leftInstance().label();
148  auto leftReference = ass.leftReference().label();
149  auto rightInstance = ass.rightInstance().label();
150 
151  if (ass.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
152  std::stringstream sBuff;
153  sBuff << leftInstance << "[" << ass.leftIndex().value() << "]";
154  leftInstance = sBuff.str();
155  }
156 
157  if (ass.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
158  std::stringstream sBuff;
159  sBuff << rightInstance << "[" << ass.rightIndex().value() << "]";
160  rightInstance = sBuff.str();
161  }
162 
163  factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
164  }
165  }
166 
167  template < typename GUM_SCALAR >
169  PRMFactory< GUM_SCALAR >& factory, O3System& sys) {
170  const auto& real_sys = __prm->getSystem(sys.name().label());
171  for (auto& inc: sys.increments()) {
172  auto leftInstance = inc.leftInstance().label();
173  auto leftReference = inc.leftReference().label();
174  auto rightInstance = inc.rightInstance().label();
175 
176  if (inc.leftIndex().value() > -1 && real_sys.isArray(leftInstance)) {
177  std::stringstream sBuff;
178  sBuff << leftInstance << "[" << inc.leftIndex().value() << "]";
179  leftInstance = sBuff.str();
180  }
181 
182  if (inc.rightIndex().value() > -1 && real_sys.isArray(rightInstance)) {
183  std::stringstream sBuff;
184  sBuff << rightInstance << "[" << inc.rightIndex().value() << "]";
185  rightInstance = sBuff.str();
186  }
187 
188  factory.setReferenceSlot(leftInstance, leftReference, rightInstance);
189  }
190  }
191 
192  template < typename GUM_SCALAR >
194  if (__checkInstance(sys) && __checkAssignments(sys)
195  && __checkIncrements(sys)) {
196  return true;
197  }
198 
199  return false;
200  }
201 
202  template < typename GUM_SCALAR >
204  for (auto& i: sys.instances()) {
205  if (!__solver->resolveClass(i.type())) { return false; }
206 
207  const auto& type = __prm->getClass(i.type().label());
208  if (type.parameters().size() > 0) {
209  if (!__checkParameters(type, i)) { return false; }
210  }
211 
212  if (__nameMap.exists(i.name().label())) {
213  O3PRM_SYSTEM_DUPLICATE_INSTANCE(i, *__errors);
214  return false;
215  }
216 
217  __nameMap.insert(i.name().label(), &i);
218  }
219 
220  return true;
221  }
222 
223  template < typename GUM_SCALAR >
225  const PRMClass< GUM_SCALAR >& type, const O3Instance& inst) {
226  for (const auto& param: inst.parameters()) {
227  if (!type.exists(param.name().label())) {
228  O3PRM_SYSTEM_PARAMETER_NOT_FOUND(param, *__errors);
229  return false;
230  }
231 
233  type.get(param.name().label()))) {
234  O3PRM_SYSTEM_NOT_A_PARAMETER(param, *__errors);
235  return false;
236  }
237 
238  const auto& type_param =
239  static_cast< const PRMParameter< GUM_SCALAR >& >(
240  type.get(param.name().label()));
241 
242  switch (type_param.valueType()) {
244  if (!param.isInteger()) {
245  O3PRM_SYSTEM_PARAMETER_NOT_INT(param, *__errors);
246  return false;
247  }
248  break;
249  }
250 
252  if (param.isInteger()) {
253  O3PRM_SYSTEM_PARAMETER_NOT_FLOAT(param, *__errors);
254  return false;
255  }
256  break;
257  }
258 
259  default: {
260  GUM_ERROR(FatalError, "unknown parameter type");
261  }
262  }
263  }
264  return true;
265  }
266 
267  template < typename GUM_SCALAR >
268  INLINE bool
270  for (auto& ass: sys.assignments()) {
271  // if ( ass.leftInstance().label() == ass.leftReference().label() ) {
272  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( ass.leftInstance(), *__errors );
273  // return false;
274  //}
275 
276  if (!__nameMap.exists(ass.leftInstance().label())) {
277  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.leftInstance(), *__errors);
278  return false;
279  }
280 
281  auto i = __nameMap[ass.leftInstance().label()];
282  const auto& type = __prm->getClass(i->type().label());
283  const auto& ref = ass.leftReference().label();
284 
285  if (!(type.exists(ass.leftReference().label())
287  type.get(ref)))) {
288  O3PRM_SYSTEM_REFERENCE_NOT_FOUND(
289  ass.leftReference(), type.name(), *__errors);
290  return false;
291  }
292 
293  const auto& real_ref =
294  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
295 
296  if (!__nameMap.exists(ass.rightInstance().label())) {
297  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(ass.rightInstance(), *__errors);
298  return false;
299  }
300 
301  if (real_ref.isArray()
302  && __nameMap[ass.rightInstance().label()]->size().value() == 0) {
303  O3PRM_SYSTEM_NOT_AN_ARRAY(ass.rightInstance(), *__errors);
304  return false;
305  }
306 
307  if ((!real_ref.isArray())
308  && __nameMap[ass.rightInstance().label()]->size().value() > 0
309  && ass.rightIndex().value() == -1) {
310  O3PRM_SYSTEM_NOT_AN_ARRAY(ass.leftReference(), *__errors);
311  return false;
312  }
313  }
314  return true;
315  }
316 
317  template < typename GUM_SCALAR >
319  for (auto& inc: sys.increments()) {
320  // if ( inc.leftInstance().label() == inc.leftReference().label() ) {
321  // O3PRM_SYSTEM_INVALID_LEFT_VALUE( inc.leftInstance(), *__errors );
322  // return false;
323  //}
324 
325  if (!__nameMap.exists(inc.leftInstance().label())) {
326  O3PRM_SYSTEM_INSTANCE_NOT_FOUND(inc.leftInstance(), *__errors);
327  return false;
328  }
329 
330  auto i = __nameMap[inc.leftInstance().label()];
331  const auto& type = __prm->getClass(i->type().label());
332  const auto& ref = inc.leftReference().label();
333 
334  if (!(type.exists(inc.leftReference().label())
336  type.get(ref)))) {
337  O3PRM_SYSTEM_REFERENCE_NOT_FOUND(
338  inc.leftReference(), type.name(), *__errors);
339  return false;
340  }
341 
342  const auto& real_ref =
343  static_cast< const PRMReferenceSlot< GUM_SCALAR >& >(type.get(ref));
344 
345  if (!real_ref.isArray()) {
346  O3PRM_SYSTEM_NOT_AN_ARRAY(inc.leftReference(), *__errors);
347  return false;
348  }
349  }
350 
351  return true;
352  }
353  } // namespace o3prm
354  } // namespace prm
355 } // 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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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