aGrUM  0.16.0
gum::prm::o3prmr::O3prmrInterpreter Class Reference

Represents a O3PRMR context. More...

#include <O3prmrInterpreter.h>

+ Collaboration diagram for gum::prm::o3prmr::O3prmrInterpreter:

Public Member Functions

 O3prmrInterpreter ()
 This constructor create an empty context. More...
 
 ~O3prmrInterpreter ()
 Destructor. Delete current context. More...
 
bool interpretFile (const std::string &filename)
 Interpret the file or the command line. More...
 
bool interpretLine (const std::string &line)
 
const gum::prm::PRM< double > * prm () const
 Retrieve prm object. More...
 
const gum::prm::PRMInference< double > * inference () const
 Retrieve inference motor object. More...
 
const std::vector< QueryResult > & results () const
 Return a vector of QueryResults. Each QueryResults is a struct with query command, time and values, a vector of struct SingleResult, with pair label/value. More...
 
ErrorsContainer errorsContainer () const
 Return container with all errors. More...
 
O3prmrContext< double > * getContext () const
 Getter and setter for the context. More...
 
void setContext (O3prmrContext< double > *context)
 Setter for the context. More...
 
std::vector< std::string > getPaths () const
 Root paths to search from there packages. Default are working dir, request file dir if any and one is calculate from package if any. More...
 
void addPath (std::string path)
 Root paths to search from there packages. Default are './' and one is calculate from request package if any. More...
 
void clearPaths ()
 Root paths to search from there packages. Default are './' and one is calculate from request package if any. More...
 
bool isInSyntaxMode () const
 syntax mode don't process anything, just check syntax. Default is false. More...
 
void setSyntaxMode (bool f)
 syntax mode don't process anything, just check syntax. More...
 
bool isVerboseMode () const
 verbose mode show more details on the program execution. Default is false. More...
 
void setVerboseMode (bool f)
 verbose mode show more details on the program execution. More...
 
Size count () const
 En cas d'échec, l'API de gestion d'erreurs est présente. More...
 
Size errors () const
 

of errors

More...
 
Size warnings () const
 

of warnings

More...
 
ParseError error (Idx i) const
 throw a string error if i >= count More...
 
void showElegantErrors (std::ostream &o=std::cerr) const
 send on std::cerr the list of errors More...
 
void showElegantErrorsAndWarnings (std::ostream &o=std::cerr) const
 send on std::cerr the list of errors or warnings More...
 
void showErrorCounts (std::ostream &o=std::cerr) const
 send on std::cerr the number of errors and the number of warnings More...
 

Detailed Description

Represents a O3PRMR context.

O3prmr use the <double> version of the O3PRM templatized ecosystem

Definition at line 70 of file O3prmrInterpreter.h.

Constructor & Destructor Documentation

◆ O3prmrInterpreter()

gum::prm::o3prmr::O3prmrInterpreter::O3prmrInterpreter ( )

This constructor create an empty context.

Definition at line 54 of file O3prmrInterpreter.cpp.

54  :
56  m_reader(new o3prm::O3prmReader< double >()), m_bn(0), m_inf(0),
57  m_syntax_flag(false), m_verbose(false), m_log(std::cout),
58  m_current_line(-1) {}
gum::prm::o3prm::O3prmReader< double > * m_reader
gum::prm::PRMInference< double > * m_inf
O3prmrContext< double > * m_context

◆ ~O3prmrInterpreter()

gum::prm::o3prmr::O3prmrInterpreter::~O3prmrInterpreter ( )

Destructor. Delete current context.

Definition at line 61 of file O3prmrInterpreter.cpp.

References m_bn, m_context, m_inf_map, m_reader, and gum::prm::o3prm::O3prmReader< GUM_SCALAR >::prm().

61  {
62  delete m_context;
63  if (m_bn) { delete m_bn; }
64  for (auto p : m_inf_map) {
65  delete p.second;
66  }
67  delete m_reader->prm();
68  delete m_reader;
69  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:110
gum::prm::o3prm::O3prmReader< double > * m_reader
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map
O3prmrContext< double > * m_context
+ Here is the call graph for this function:

Member Function Documentation

◆ __readFile()

std::string gum::prm::o3prmr::O3prmrInterpreter::__readFile ( const std::string &  file)
private

Definition at line 188 of file O3prmrInterpreter.cpp.

References GUM_ERROR.

Referenced by interpretFile().

188  {
189  // read entire file into string
190  std::ifstream istream(file, std::ifstream::binary);
191  if (istream) {
192  // get length of file:
193  istream.seekg(0, istream.end);
194  int length = int(istream.tellg());
195  istream.seekg(0, istream.beg);
196 
197  std::string str;
198  str.resize(length, ' '); // reserve space
199  char* begin = &*str.begin();
200 
201  istream.read(begin, length);
202  istream.close();
203 
204  return str;
205  }
206  GUM_ERROR(OperationNotAllowed, "Could not open file");
207  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addError()

void gum::prm::o3prmr::O3prmrInterpreter::addError ( std::string  msg)
private

Definition at line 977 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::addError(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::filename(), gum::ErrorsContainer::last(), m_context, m_current_line, m_errors, m_log, m_verbose, and gum::ParseError::toString().

Referenced by checkObserve(), checkQuery(), checkSemantic(), checkUnobserve(), import(), interpret(), observe(), query(), and unobserve().

977  {
979 
980  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
981  }
std::string toString() const
Return a std::string representation of this gum::ParseError.
ParseError last() const
Returns the last added error.
void addError(const std::string &msg, const std::string &filename, Idx line, Idx col)
Adds an error.
O3prmrContext< double > * m_context
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addPath()

void gum::prm::o3prmr::O3prmrInterpreter::addPath ( std::string  path)

Root paths to search from there packages. Default are './' and one is calculate from request package if any.

Definition at line 97 of file O3prmrInterpreter.cpp.

References GUM_ERROR, gum::Directory::isDir(), and m_paths.

97  {
98  if (path.length() && path.back() != '/') { path = path + '/'; }
99  if (Directory::isDir(path)) {
100  m_paths.push_back(path);
101  } else {
102  GUM_ERROR(NotFound, "not a directory");
103  }
104  }
std::vector< std::string > m_paths
static bool isDir(const std::string &path)
&brief Return true if directory is a valid directory, false otherwise.
Definition: utils_dir.cpp:35
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ addWarning()

void gum::prm::o3prmr::O3prmrInterpreter::addWarning ( std::string  msg)
private

Definition at line 984 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::addWarning(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::filename(), gum::ErrorsContainer::last(), m_context, m_current_line, m_errors, m_log, m_verbose, and gum::ParseError::toString().

Referenced by generateInfEngine(), observe(), and unobserve().

984  {
986 
987  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
988  }
void addWarning(const std::string &msg, const std::string &filename, Idx line, Idx col)
Adds a warning.
std::string toString() const
Return a std::string representation of this gum::ParseError.
ParseError last() const
Returns the last added error.
O3prmrContext< double > * m_context
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkObserve()

bool gum::prm::o3prmr::O3prmrInterpreter::checkObserve ( ObserveCommand< double > *  command)
private

Definition at line 434 of file O3prmrInterpreter.cpp.

References addError(), gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::chain, gum::Instantiation::end(), gum::Exception::errorContent(), findAttributeName(), findInstanceName(), gum::prm::PRMSystem< GUM_SCALAR >::get(), gum::prm::PRMInstance< GUM_SCALAR >::get(), gum::Instantiation::inc(), gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::leftValue, gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::potentiel, gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::rightValue, gum::Instantiation::setFirst(), gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::system, system(), and gum::Instantiation::val().

Referenced by checkSemantic().

434  {
435  try {
436  std::string left_val = command->leftValue;
437  const std::string right_val = command->rightValue;
438 
439  // Contruct the pair (instance,attribut)
440  const PRMSystem< double >& sys = system(left_val);
441  const PRMInstance< double >& instance =
442  sys.get(findInstanceName(left_val, sys));
443  const PRMAttribute< double >& attr =
444  instance.get(findAttributeName(left_val, instance));
445  typename PRMInference< double >::Chain chain =
446  std::make_pair(&instance, &attr);
447 
448  command->system = &sys;
449  command->chain = std::make_pair(&instance, &attr);
450 
451  // Check label exists for this type.
452  // Potential<double> e;
453  command->potentiel.add(chain.second->type().variable());
454  Instantiation i(command->potentiel);
455  bool found = false;
456 
457  for (i.setFirst(); !i.end(); i.inc()) {
458  if (chain.second->type().variable().label(
459  i.val(chain.second->type().variable()))
460  == right_val) {
461  command->potentiel.set(i, (double)1.0);
462  found = true;
463  } else {
464  command->potentiel.set(i, (double)0.0);
465  }
466  }
467 
468  if (!found) addError(right_val + " is not a label of " + left_val);
469 
470  // else command->potentiel = e;
471 
472  return found;
473 
474  } catch (Exception& err) {
475  addError(err.errorContent());
476  } catch (std::string& err) { addError(err); }
477 
478  return false;
479  }
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
std::string findAttributeName(const std::string &s, const gum::prm::PRMInstance< double > &instance)
std::pair< const PRMInstance< double > *, const PRMAttribute< double > * > Chain
Code alias.
Definition: PRMInference.h:57
const PRMSystem< double > & system(std::string &ident)
std::string findInstanceName(std::string &s, const gum::prm::PRMSystem< double > &sys)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkQuery()

bool gum::prm::o3prmr::O3prmrInterpreter::checkQuery ( QueryCommand< double > *  command)
private

Definition at line 506 of file O3prmrInterpreter.cpp.

References addError(), gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::chain, gum::Exception::errorContent(), findAttributeName(), findInstanceName(), gum::prm::PRMSystem< GUM_SCALAR >::get(), gum::prm::PRMInstance< GUM_SCALAR >::get(), system(), gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::system, and gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::value.

Referenced by checkSemantic().

506  {
507  try {
508  std::string name = command->value;
509 
510  // Contruct the pair (instance,attribut)
511  const PRMSystem< double >& sys = system(name);
512  const PRMInstance< double >& instance =
513  sys.get(findInstanceName(name, sys));
514  const PRMAttribute< double >& attr =
515  instance.get(findAttributeName(name, instance));
516  // PRMInference<double>::Chain chain = std::make_pair(&instance,
517  // &attr);
518 
519  command->system = &sys;
520  command->chain = std::make_pair(&instance, &attr);
521 
522  return true;
523 
524  } catch (Exception& err) {
525  addError(err.errorContent());
526  } catch (std::string& err) { addError(err); }
527 
528  return false;
529  }
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
std::string findAttributeName(const std::string &s, const gum::prm::PRMInstance< double > &instance)
const PRMSystem< double > & system(std::string &ident)
std::string findInstanceName(std::string &s, const gum::prm::PRMSystem< double > &sys)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkSemantic()

bool gum::prm::o3prmr::O3prmrInterpreter::checkSemantic ( O3prmrContext< double > *  context)
private

Check semantic validity of context.

Import first all import, and check that systems, instances, attributes and labels exists. While checking, prepare data structures for interpretation. Return true if all is right, false otherwise.

Note : Stop checking at first error unless syntax mode is activated.

Definition at line 311 of file O3prmrInterpreter.cpp.

References gum::prm::o3prmr::O3prmrSession< GUM_SCALAR >::addCommand(), addError(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::addImport(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::addSession(), checkObserve(), checkQuery(), checkSetEngine(), checkSetGndEngine(), checkUnobserve(), gum::ErrorsContainer::elegantErrorsAndWarnings(), gum::Exception::errorContent(), errors(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::imports(), isInSyntaxMode(), isVerboseMode(), m_context, m_current_line, m_errors, m_log, m_verbose, gum::prm::o3prmr::O3prmrCommand::Observe, gum::prm::o3prmr::O3prmrCommand::Query, gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::sessions(), gum::prm::o3prmr::O3prmrCommand::SetEngine, gum::prm::o3prmr::O3prmrCommand::SetGndEngine, gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::toString(), and gum::prm::o3prmr::O3prmrCommand::Unobserve.

Referenced by interpretFile(), and interpretLine().

311  {
312  // Don't parse if any syntax errors.
313  if (errors() > 0) return false;
314 
315  // On importe tous les systèmes.
316  for (const auto command : context->imports()) {
317  m_current_line = command->line;
318  // if import doen't succed stop here unless syntax mode is activated.
319  bool succeed = import(context, command->value);
320 
321  if (!succeed && !isInSyntaxMode()) return false;
322 
323  // En cas de succès, on met à jour le contexte global
324  if (succeed) m_context->addImport(*command);
325  }
326 
327  if (m_verbose)
328  m_log << "## Check semantic for " << context->sessions().size()
329  << " sessions" << std::endl;
330 
331  // On vérifie chaque session
332  for (const auto session : context->sessions()) {
333  std::string sessionName = session->name();
334  O3prmrSession< double >* new_session =
335  new O3prmrSession< double >(sessionName);
336 
337  if (m_verbose)
338  m_log << "## Start session '" << sessionName << "'..." << std::endl
339  << std::endl;
340 
341  for (const auto command : session->commands()) {
342  if (m_verbose)
343  m_log << "# * Going to check command : " << command->toString()
344  << std::endl;
345 
346  // Update the current line (for warnings and errors)
347  m_current_line = command->line;
348 
349  // We check it.
350  bool result = true;
351 
352  try {
353  switch (command->type()) {
355  result = checkSetEngine((SetEngineCommand*)command);
356  break;
357 
359  result = checkSetGndEngine((SetGndEngineCommand*)command);
360  break;
361 
363  result = checkObserve((ObserveCommand< double >*)command);
364  break;
365 
367  result = checkUnobserve((UnobserveCommand< double >*)command);
368  break;
369 
371  result = checkQuery((QueryCommand< double >*)command);
372  break;
373 
374  default:
375  addError("Error : Unknow command : " + command->toString()
376  + "\n -> Command not processed.");
377  result = false;
378  }
379  } catch (Exception& err) {
380  result = false;
381  addError(err.errorContent());
382  } catch (std::string& err) {
383  result = false;
384  addError(err);
385  }
386 
387  // If there was a problem, skip the rest of this session,
388  // unless syntax mode is activated.
389  if (!result && !isInSyntaxMode()) {
390  if (m_verbose)
391  m_log << "Errors : skip the rest of this session." << std::endl;
392 
393  break;
394  }
395 
396  // On l'ajoute au contexte globale
397  if (result) new_session->addCommand((const O3prmrCommand*)command);
398  }
399 
400  // Ajoute la session au contexte global,
401  // ou à la dernière session.
402  if (sessionName == "default" && m_context->sessions().size() > 0)
403  *(m_context->sessions().back()) += *new_session;
404  else
405  m_context->addSession(*new_session);
406 
407  if (m_verbose)
408  m_log << std::endl
409  << "## Session '" << sessionName << "' finished." << std::endl
410  << std::endl
411  << std::endl;
412 
413  // todo : check memory leak
414  // delete new_session; ??
415  }
416 
417  if (isVerboseMode() && errors() != 0)
419 
420  return errors() == 0;
421  }
void addSession(const O3prmrSession< GUM_SCALAR > &session)
bool checkSetEngine(SetEngineCommand *command)
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
bool isVerboseMode() const
verbose mode show more details on the program execution. Default is false.
bool checkSetGndEngine(SetGndEngineCommand *command)
std::vector< ImportCommand *> imports() const
bool checkQuery(QueryCommand< double > *command)
bool checkUnobserve(UnobserveCommand< double > *command)
void addImport(int line, const std::string &import, const std::string &alias)
std::vector< O3prmrSession< GUM_SCALAR > *> sessions() const
bool checkObserve(ObserveCommand< double > *command)
bool isInSyntaxMode() const
syntax mode don&#39;t process anything, just check syntax. Default is false.
void addCommand(const O3prmrCommand *command)
O3prmrContext< double > * m_context
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkSetEngine()

bool gum::prm::o3prmr::O3prmrInterpreter::checkSetEngine ( SetEngineCommand command)
private

Definition at line 423 of file O3prmrInterpreter.cpp.

References m_engine, and gum::prm::o3prmr::SetEngineCommand::value.

Referenced by checkSemantic().

423  {
424  m_engine = command->value;
425  return m_engine == "SVED" || m_engine == "GRD" || m_engine == "SVE";
426  }
+ Here is the caller graph for this function:

◆ checkSetGndEngine()

bool gum::prm::o3prmr::O3prmrInterpreter::checkSetGndEngine ( SetGndEngineCommand command)
private

Definition at line 428 of file O3prmrInterpreter.cpp.

References m_bn_engine, and gum::prm::o3prmr::SetGndEngineCommand::value.

Referenced by checkSemantic().

428  {
429  m_bn_engine = command->value;
430  return m_bn_engine == "VE" || m_bn_engine == "VEBB"
431  || m_bn_engine == "lazy";
432  }
+ Here is the caller graph for this function:

◆ checkUnobserve()

bool gum::prm::o3prmr::O3prmrInterpreter::checkUnobserve ( UnobserveCommand< double > *  command)
private

Definition at line 481 of file O3prmrInterpreter.cpp.

References addError(), gum::prm::o3prmr::UnobserveCommand< GUM_SCALAR >::chain, gum::Exception::errorContent(), findAttributeName(), findInstanceName(), gum::prm::PRMSystem< GUM_SCALAR >::get(), gum::prm::PRMInstance< GUM_SCALAR >::get(), gum::prm::o3prmr::UnobserveCommand< GUM_SCALAR >::system, system(), and gum::prm::o3prmr::UnobserveCommand< GUM_SCALAR >::value.

Referenced by checkSemantic().

481  {
482  try {
483  std::string name = command->value;
484 
485  // Contruct the pair (instance,attribut)
486  const PRMSystem< double >& sys = system(name);
487  const PRMInstance< double >& instance =
488  sys.get(findInstanceName(name, sys));
489  const PRMAttribute< double >& attr =
490  instance.get(findAttributeName(name, instance));
491  // PRMInference<double>::Chain chain = std::make_pair(&instance,
492  // &attr);
493 
494  command->system = &sys;
495  command->chain = std::make_pair(&instance, &attr);
496 
497  return true;
498 
499  } catch (Exception& err) {
500  addError(err.errorContent());
501  } catch (std::string& err) { addError(err); }
502 
503  return false;
504  }
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
std::string findAttributeName(const std::string &s, const gum::prm::PRMInstance< double > &instance)
const PRMSystem< double > & system(std::string &ident)
std::string findInstanceName(std::string &s, const gum::prm::PRMSystem< double > &sys)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clearPaths()

void gum::prm::o3prmr::O3prmrInterpreter::clearPaths ( )

Root paths to search from there packages. Default are './' and one is calculate from request package if any.

Definition at line 108 of file O3prmrInterpreter.cpp.

References m_paths.

108 { m_paths.clear(); }
std::vector< std::string > m_paths

◆ count()

Size gum::prm::o3prmr::O3prmrInterpreter::count ( ) const

En cas d'échec, l'API de gestion d'erreurs est présente.

of errors + warnings

Definition at line 938 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::count(), and m_errors.

Referenced by error(), and import().

938 { return m_errors.count(); }
Size count() const
Returns the number of errors and warnings.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ error()

ParseError gum::prm::o3prmr::O3prmrInterpreter::error ( Idx  i) const

throw a string error if i >= count

Definition at line 947 of file O3prmrInterpreter.cpp.

References count(), gum::ErrorsContainer::error(), and m_errors.

947  {
948  if (i >= count()) throw "Index out of bound.";
949 
950  return m_errors.error(i);
951  }
ParseError error(Idx i) const
Returns the i-th error.
Size count() const
En cas d&#39;échec, l&#39;API de gestion d&#39;erreurs est présente.
+ Here is the call graph for this function:

◆ errors()

Size gum::prm::o3prmr::O3prmrInterpreter::errors ( ) const

of errors

Definition at line 941 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::error_count, and m_errors.

Referenced by checkSemantic(), import(), interpret(), interpretFile(), and interpretLine().

941 { return m_errors.error_count; }
Size error_count
Number of errors detected.
+ Here is the caller graph for this function:

◆ errorsContainer()

ErrorsContainer gum::prm::o3prmr::O3prmrInterpreter::errorsContainer ( ) const

Return container with all errors.

Definition at line 954 of file O3prmrInterpreter.cpp.

References m_errors.

954  {
955  return m_errors;
956  }

◆ findAttributeName()

std::string gum::prm::o3prmr::O3prmrInterpreter::findAttributeName ( const std::string &  s,
const gum::prm::PRMInstance< double > &  instance 
)
private

Definition at line 735 of file O3prmrInterpreter.cpp.

References gum::prm::PRMInstance< GUM_SCALAR >::exists(), and gum::prm::PRMObject::name().

Referenced by checkObserve(), checkQuery(), and checkUnobserve().

736  {
737  if (!instance.exists(s))
738  throw "'" + s + "' is not an attribute of instance '" + instance.name()
739  + "'.";
740 
741  return s;
742  }
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ findInstanceName()

std::string gum::prm::o3prmr::O3prmrInterpreter::findInstanceName ( std::string &  s,
const gum::prm::PRMSystem< double > &  sys 
)
private

Definition at line 721 of file O3prmrInterpreter.cpp.

References gum::prm::PRMSystem< GUM_SCALAR >::exists(), and gum::prm::PRMObject::name().

Referenced by checkObserve(), checkQuery(), and checkUnobserve().

722  {
723  // We have found system before, so 's' has been stripped.
724  size_t dot = s.find_first_of('.');
725  std::string name = s.substr(0, dot);
726 
727  if (!sys.exists(name))
728  throw "'" + name + "' is not an instance of system '" + sys.name()
729  + "'.";
730 
731  s = s.substr(dot + 1);
732  return name;
733  }
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
bool exists(const std::string &name) const
Retruns true either if name is an instance or an array in this PRMSystem.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ findSystemName()

std::string gum::prm::o3prmr::O3prmrInterpreter::findSystemName ( std::string &  s)
private

Definition at line 692 of file O3prmrInterpreter.cpp.

References gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::aliasToImport(), m_context, and prm().

Referenced by system().

692  {
693  size_t dot = s.find_first_of('.');
694  std::string name = s.substr(0, dot);
695 
696  // We look first for real system, next for alias.
697  if (prm()->isSystem(name)) {
698  s = s.substr(dot + 1);
699  return name;
700  }
701 
702  if (!m_context->aliasToImport(name).empty()) {
703  s = s.substr(dot + 1);
704  return m_context->aliasToImport(name);
705  }
706 
707  while (dot != std::string::npos) {
708  if (prm()->isSystem(name)) {
709  s = s.substr(dot + 1);
710  return name;
711  }
712 
713  dot = s.find('.', dot + 1);
714  name = s.substr(0, dot);
715  }
716 
717  throw "could not find any system in '" + s + "'.";
718  }
const gum::prm::PRM< double > * prm() const
Retrieve prm object.
std::string aliasToImport(const std::string &alias)
O3prmrContext< double > * m_context
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateInfEngine()

void gum::prm::o3prmr::O3prmrInterpreter::generateInfEngine ( const gum::prm::PRMSystem< double > &  sys)
private

Definition at line 894 of file O3prmrInterpreter.cpp.

References addWarning(), gum::prm::PRMSystem< GUM_SCALAR >::groundedBN(), m_bn, m_engine, m_inf, m_inf_map, m_log, m_verbose, and prm().

Referenced by observe(), and query().

894  {
895  if (m_verbose)
896  m_log << "# Building the inference engine... " << std::flush;
897 
898  //
899  if (m_engine == "SVED") {
900  m_inf = new SVED< double >(*(prm()), sys);
901 
902  //
903  } else if (m_engine == "SVE") {
904  m_inf = new SVE< double >(*(prm()), sys);
905 
906  } else {
907  if (m_engine != "GRD") {
908  addWarning("unkown engine '" + m_engine + "', use GRD insteed.");
909  }
910 
911  MarginalTargetedInference< double >* bn_inf = nullptr;
912  if (m_bn) { delete m_bn; }
913  m_bn = new BayesNet< double >();
914  BayesNetFactory< double > bn_factory(m_bn);
915 
916  if (m_verbose) m_log << "(Grounding the network... " << std::flush;
917 
918  sys.groundedBN(bn_factory);
919 
920  if (m_verbose) m_log << "Finished)" << std::flush;
921 
922  // bn_inf = new LazyPropagation<double>( *m_bn );
923  bn_inf = new VariableElimination< double >(m_bn);
924 
925  auto grd_inf = new GroundedInference< double >(*(prm()), sys);
926  grd_inf->setBNInference(bn_inf);
927  m_inf = grd_inf;
928  }
929 
930  m_inf_map.insert(&sys, m_inf);
931  if (m_verbose) m_log << "Finished." << std::endl;
932  }
const gum::prm::PRM< double > * prm() const
Retrieve prm object.
void groundedBN(BayesNetFactory< GUM_SCALAR > &factory) const
Returns the grounded Bayesian Network of this system.
Definition: PRMSystem_tpl.h:82
gum::prm::PRMInference< double > * m_inf
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getContext()

O3prmrContext< double > * gum::prm::o3prmr::O3prmrInterpreter::getContext ( ) const

Getter and setter for the context.

Getter for the context.

Definition at line 75 of file O3prmrInterpreter.cpp.

References m_context.

75  {
76  return m_context;
77  }
O3prmrContext< double > * m_context

◆ getPaths()

std::vector< std::string > gum::prm::o3prmr::O3prmrInterpreter::getPaths ( ) const

Root paths to search from there packages. Default are working dir, request file dir if any and one is calculate from package if any.

Root paths to search from there packages. Default are './' and one is calculate from request package if any.

Definition at line 91 of file O3prmrInterpreter.cpp.

References m_paths.

91  {
92  return m_paths;
93  }
std::vector< std::string > m_paths

◆ import()

bool gum::prm::o3prmr::O3prmrInterpreter::import ( O3prmrContext< double > *  context,
std::string  import 
)
private

Definition at line 534 of file O3prmrInterpreter.cpp.

References gum::Directory::absolutePath(), gum::ErrorsContainer::add(), addError(), count(), gum::ErrorsContainer::count(), gum::ErrorsContainer::error(), gum::Exception::errorContent(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::errors(), errors(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::errorsContainer(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::filename(), m_errors, m_log, m_paths, m_reader, m_verbose, gum::prm::o3prm::O3prmReader< GUM_SCALAR >::readFile(), and gum::replace().

535  {
536  try {
537  if (m_verbose) {
538  m_log << "# Loading system '" << import_name << "' => '" << std::flush;
539  }
540 
541  std::string import_package = import_name;
542 
543  std::replace(import_name.begin(), import_name.end(), '.', '/');
544  import_name += ".o3prm";
545 
546  if (m_verbose) {
547  m_log << import_name << "' ... " << std::endl << std::flush;
548  }
549 
550  std::ifstream file_test;
551  bool found = false;
552  std::string import_abs_filename;
553 
554  // Search in o3prmr file dir.
555  std::string o3prmrFilename = context->filename();
556 
557  if (!o3prmrFilename.empty()) {
558  size_t index = o3prmrFilename.find_last_of('/');
559 
560  if (index != std::string::npos) {
561  std::string dir = o3prmrFilename.substr(0, index + 1);
562  import_abs_filename = dir + import_name;
563 
564  if (m_verbose) {
565  m_log << "# Search from filedir '" << import_abs_filename
566  << "' ... " << std::flush;
567  }
568 
569  file_test.open(import_abs_filename.c_str());
570 
571  if (file_test.is_open()) {
572  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
573 
574  file_test.close();
575  found = true;
576  } else if (m_verbose) {
577  m_log << "not found." << std::endl << std::flush;
578  }
579  }
580  }
581 
582  // Deduce root path from package name.
583  std::string package = context->package();
584 
585  if (!found && !package.empty()) {
586  std::string root;
587 
588  // if filename is not empty, start from it.
589  std::string filename = context->filename();
590 
591  if (!filename.empty()) {
592  size_t size = filename.find_last_of('/');
593 
594  if (size != std::string::npos) {
595  root += filename.substr(0, size + 1); // take with the '/'
596  }
597  }
598 
599  //
600  root += "../";
601  int count = (int)std::count(package.begin(), package.end(), '.');
602 
603  for (int i = 0; i < count; i++)
604  root += "../";
605 
606  import_abs_filename = Directory(root).absolutePath() + import_name;
607 
608  if (m_verbose) {
609  m_log << "# Search from package '" << package << "' => '"
610  << import_abs_filename << "' ... " << std::flush;
611  }
612 
613  file_test.open(import_abs_filename.c_str());
614 
615  if (file_test.is_open()) {
616  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
617 
618  file_test.close();
619  found = true;
620  } else if (m_verbose) {
621  m_log << "not found." << std::endl << std::flush;
622  }
623  }
624 
625  // Search import in all paths.
626  for (const auto& path : m_paths) {
627  import_abs_filename = path + import_name;
628 
629  if (m_verbose) {
630  m_log << "# Search from classpath '" << import_abs_filename
631  << "' ... " << std::flush;
632  }
633 
634  file_test.open(import_abs_filename.c_str());
635 
636  if (file_test.is_open()) {
637  if (m_verbose) { m_log << " found !" << std::endl << std::flush; }
638 
639  file_test.close();
640  found = true;
641  break;
642  } else if (m_verbose) {
643  m_log << " not found." << std::endl << std::flush;
644  }
645  }
646 
647  if (!found) {
648  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
649 
650  addError("import not found.");
651  return false;
652  }
653 
654  // May throw std::IOError if file does't exist
655  Size previousO3prmError = m_reader->errors();
656  Size previousO3prmrError = errors();
657 
658  try {
659  m_reader->readFile(import_abs_filename, import_package);
660 
661  // Show errors and warning
662  if (m_verbose
663  && (m_reader->errors() > (unsigned int)previousO3prmError
664  || errors() > previousO3prmrError)) {
665  m_log << "Finished with errors." << std::endl;
666  } else if (m_verbose) {
667  m_log << "Finished." << std::endl;
668  }
669 
670  } catch (const IOError& err) {
671  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
672 
673  addError(err.errorContent());
674  }
675 
676  // Add o3prm errors and warnings to o3prmr errors
677  for (; previousO3prmError < m_reader->errorsContainer().count();
678  previousO3prmError++) {
679  m_errors.add(m_reader->errorsContainer().error(previousO3prmError));
680  }
681 
682  return errors() == previousO3prmrError;
683 
684  } catch (const Exception& err) {
685  if (m_verbose) { m_log << "Finished with exceptions." << std::endl; }
686 
687  addError(err.errorContent());
688  return false;
689  }
690  }
Size readFile(const std::string &file, const std::string &module="")
Read file and load its content using a PRMFactory. The package parameter set the file&#39;s content packa...
Size count() const
Returns the number of errors and warnings.
const ErrorsContainer & errorsContainer() const
publishing Errors API
void add(ParseError error)
Add an error object to the container.
gum::prm::o3prm::O3prmReader< double > * m_reader
Size errors() const
publishing Errors API
std::vector< std::string > m_paths
std::string replace(const std::string &s, const std::string &val, const std::string &new_val)
not usable for gcc 4.8 std::vector<std::string> split( const std::string& orig, const std::string& de...
ParseError error(Idx i) const
Returns the i-th error.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size count() const
En cas d&#39;échec, l&#39;API de gestion d&#39;erreurs est présente.
+ Here is the call graph for this function:

◆ inference()

const PRMInference< double > * gum::prm::o3prmr::O3prmrInterpreter::inference ( ) const

Retrieve inference motor object.

Definition at line 128 of file O3prmrInterpreter.cpp.

References m_inf.

128  {
129  return m_inf;
130  }
gum::prm::PRMInference< double > * m_inf

◆ interpret()

bool gum::prm::o3prmr::O3prmrInterpreter::interpret ( O3prmrContext< double > *  c)
private

Crée le prm correspondant au contexte courant.

Renvoie true en cas de succès, ou false en cas échéant d'échec de l'interprétation du contexte (import introuvable ou non défini, etc).

Definition at line 237 of file O3prmrInterpreter.cpp.

References addError(), gum::Exception::errorContent(), errors(), isVerboseMode(), m_log, m_verbose, gum::prm::o3prmr::O3prmrCommand::Observe, observe(), gum::prm::o3prmr::O3prmrCommand::Query, query(), gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::sessions(), gum::prm::o3prmr::O3prmrCommand::SetEngine, setEngine(), gum::prm::o3prmr::O3prmrCommand::SetGndEngine, setGndEngine(), gum::prm::o3prmr::O3prmrCommand::Unobserve, and unobserve().

Referenced by interpretFile(), and interpretLine().

237  {
238  if (isVerboseMode())
239  m_log << "## Start interpretation." << std::endl << std::flush;
240 
241  // Don't parse if any syntax errors.
242  if (errors() > 0) return false;
243 
244  // For each session
245  std::vector< O3prmrSession< double >* > sessions = c->sessions();
246 
247  for (const auto session : sessions)
248  for (auto command : session->commands()) {
249  // We process it.
250  bool result = true;
251 
252  try {
253  switch (command->type()) {
255  result = observe((ObserveCommand< double >*)command);
256  break;
257 
259  result = unobserve((UnobserveCommand< double >*)command);
260  break;
261 
263  setEngine((SetEngineCommand*)command);
264  break;
265 
267  setGndEngine((SetGndEngineCommand*)command);
268  break;
269 
271  query((QueryCommand< double >*)command);
272  break;
273  }
274  } catch (Exception& err) {
275  result = false;
276  addError(err.errorContent());
277  } catch (std::string& err) {
278  result = false;
279  addError(err);
280  }
281 
282  // If there was a problem, skip the rest of this session,
283  // unless syntax mode is activated.
284  if (!result) {
285  if (m_verbose)
286  m_log << "Errors : skip the rest of this session." << std::endl;
287 
288  break;
289  }
290  }
291 
292  if (isVerboseMode())
293  m_log << "## End interpretation." << std::endl << std::flush;
294 
295  return errors() == 0;
296  }
bool observe(const ObserveCommand< double > *command)
void query(const QueryCommand< double > *command)
bool isVerboseMode() const
verbose mode show more details on the program execution. Default is false.
std::vector< O3prmrSession< GUM_SCALAR > *> sessions() const
void setGndEngine(const SetGndEngineCommand *command)
void setEngine(const SetEngineCommand *command)
bool unobserve(const UnobserveCommand< double > *command)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interpretFile()

bool gum::prm::o3prmr::O3prmrInterpreter::interpretFile ( const std::string &  filename)

Interpret the file or the command line.

Parse the file or the command line.

If syntax mode is activated, check only syntax and semantic. If errors occured, return false. Errors messages can be retrieve be errorsContainer() methods. If any errors occured, return true. Requests results can be retrieve be results() methods.

If errors occured, return false. Errors messages can be retrieve be getErrorsContainer() methods. If any errors occured, return true. Requests results can be retrieve be results() methods.

Definition at line 146 of file O3prmrInterpreter.cpp.

References __readFile(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::addClassPath(), checkSemantic(), errors(), gum::ErrorsContainer::errors, interpret(), isInSyntaxMode(), m_context, m_errors, m_paths, m_reader, m_results, and gum::prm::o3prm::O3prmReader< GUM_SCALAR >::prm().

146  {
147  m_results.clear();
148 
149  try {
150  std::string file_content = __readFile(filename);
151 
152  delete m_context;
153  m_context = new O3prmrContext< double >(filename);
154  O3prmrContext< double > c(filename);
155 
156  // On vérifie la syntaxe
157  unsigned char* buffer = new unsigned char[file_content.length() + 1];
158  strcpy((char*)buffer, file_content.c_str());
159  Scanner s(buffer, int(file_content.length() + 1));
160  Parser p(&s);
161  p.setO3prmrContext(&c);
162  p.Parse();
163 
164  m_errors = p.errors();
165 
166  if (errors() > 0) { return false; }
167 
168  // Set paths to search from.
169  delete m_reader->prm();
170  delete m_reader;
171  m_reader = new o3prm::O3prmReader< double >();
172 
173  for (size_t i = 0; i < m_paths.size(); i++) {
175  }
176 
177  // On vérifie la sémantique.
178  if (!checkSemantic(&c)) { return false; }
179 
180  if (isInSyntaxMode()) {
181  return true;
182  } else {
183  return interpret(&c);
184  }
185  } catch (gum::Exception&) { return false; }
186  }
std::vector< ParseError > errors
The list of gum::ParseError contained in this gum::ErrorsContainer.
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:110
bool checkSemantic(O3prmrContext< double > *context)
Check semantic validity of context.
gum::prm::o3prm::O3prmReader< double > * m_reader
bool interpret(O3prmrContext< double > *c)
Crée le prm correspondant au contexte courant.
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
std::vector< std::string > m_paths
void addClassPath(const std::string &class_path)
Add a list of paths to look for o3prm files.
bool isInSyntaxMode() const
syntax mode don&#39;t process anything, just check syntax. Default is false.
std::string __readFile(const std::string &file)
std::vector< QueryResult > m_results
O3prmrContext< double > * m_context
+ Here is the call graph for this function:

◆ interpretLine()

bool gum::prm::o3prmr::O3prmrInterpreter::interpretLine ( const std::string &  line)

Definition at line 209 of file O3prmrInterpreter.cpp.

References checkSemantic(), errors(), gum::ErrorsContainer::errors, interpret(), isInSyntaxMode(), m_errors, and m_results.

209  {
210  m_results.clear();
211 
212  // On vérifie la syntaxe
214  Scanner s((unsigned char*)line.c_str(), (int)line.length());
215  Parser p(&s);
216  p.setO3prmrContext(&c);
217  p.Parse();
218  m_errors = p.errors();
219 
220  if (errors() > 0) return false;
221 
222  // On vérifie la sémantique.
223  if (!checkSemantic(&c)) return false;
224 
225  if (isInSyntaxMode())
226  return true;
227  else
228  return interpret(&c);
229  }
std::vector< ParseError > errors
The list of gum::ParseError contained in this gum::ErrorsContainer.
bool checkSemantic(O3prmrContext< double > *context)
Check semantic validity of context.
bool interpret(O3prmrContext< double > *c)
Crée le prm correspondant au contexte courant.
bool isInSyntaxMode() const
syntax mode don&#39;t process anything, just check syntax. Default is false.
std::vector< QueryResult > m_results
+ Here is the call graph for this function:

◆ isInSyntaxMode()

bool gum::prm::o3prmr::O3prmrInterpreter::isInSyntaxMode ( ) const

syntax mode don't process anything, just check syntax. Default is false.

syntax mode don't process anything, just check syntax.

Definition at line 111 of file O3prmrInterpreter.cpp.

References m_syntax_flag.

Referenced by checkSemantic(), interpretFile(), and interpretLine().

+ Here is the caller graph for this function:

◆ isVerboseMode()

bool gum::prm::o3prmr::O3prmrInterpreter::isVerboseMode ( ) const

verbose mode show more details on the program execution. Default is false.

verbose mode show more details on the program execution.

Definition at line 117 of file O3prmrInterpreter.cpp.

References m_verbose.

Referenced by checkSemantic(), and interpret().

+ Here is the caller graph for this function:

◆ observe()

bool gum::prm::o3prmr::O3prmrInterpreter::observe ( const ObserveCommand< double > *  command)
private

Definition at line 761 of file O3prmrInterpreter.cpp.

References addError(), gum::prm::PRMInference< GUM_SCALAR >::addEvidence(), addWarning(), gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::chain, gum::Exception::errorContent(), generateInfEngine(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::leftValue, m_inf, m_log, m_verbose, gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::potentiel, gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::rightValue, and gum::prm::o3prmr::ObserveCommand< GUM_SCALAR >::system.

Referenced by interpret().

761  {
762  const typename PRMInference< double >::Chain& chain = command->chain;
763 
764  // Generate the inference engine if it doesn't exist.
765  if (!m_inf) { generateInfEngine(*(command->system)); }
766 
767  // Prevent from something
768  if (m_inf->hasEvidence(chain))
769  addWarning(command->leftValue + " is already observed");
770 
771  m_inf->addEvidence(chain, command->potentiel);
772 
773  if (m_verbose)
774  m_log << "# Added evidence " << command->rightValue << " over attribute "
775  << command->leftValue << std::endl;
776 
777  return true;
778 
779  } catch (OperationNotAllowed& ex) {
780  addError("something went wrong when adding evidence " + command->rightValue
781  + " over " + command->leftValue + " : " + ex.errorContent());
782  return false;
783 
784  } catch (const std::string& msg) {
785  addError(msg);
786  return false;
787  }
void generateInfEngine(const gum::prm::PRMSystem< double > &sys)
gum::prm::PRMInference< double > * m_inf
std::pair< const PRMInstance< double > *, const PRMAttribute< double > * > Chain
Code alias.
Definition: PRMInference.h:57
void addEvidence(const Chain &chain, const Potential< GUM_SCALAR > &p)
Add an evidence to the given instance&#39;s elt.
bool hasEvidence(const PRMInstance< GUM_SCALAR > &i) const
Returns true if i has evidence.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ prm()

const PRM< double > * gum::prm::o3prmr::O3prmrInterpreter::prm ( ) const

Retrieve prm object.

Definition at line 123 of file O3prmrInterpreter.cpp.

References m_reader, and gum::prm::o3prm::O3prmReader< GUM_SCALAR >::prm().

Referenced by findSystemName(), generateInfEngine(), and system().

123  {
124  return m_reader->prm();
125  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:110
gum::prm::o3prm::O3prmReader< double > * m_reader
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query()

void gum::prm::o3prmr::O3prmrInterpreter::query ( const QueryCommand< double > *  command)
private

Definition at line 814 of file O3prmrInterpreter.cpp.

References addError(), gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::chain, gum::prm::o3prmr::QueryResult::command, gum::Instantiation::end(), gum::Exception::errorContent(), generateInfEngine(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_SHOWERROR, gum::Instantiation::inc(), gum::prm::o3prmr::SingleResult::label, gum::DiscreteVariable::label(), m_inf, m_inf_map, m_log, m_results, m_verbose, gum::prm::PRMInference< GUM_SCALAR >::marginal(), gum::Timer::reset(), gum::Instantiation::setFirst(), gum::Timer::step(), gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::system, gum::prm::o3prmr::QueryResult::time, gum::prm::PRMAttribute< GUM_SCALAR >::type(), gum::Instantiation::val(), gum::prm::o3prmr::QueryCommand< GUM_SCALAR >::value, gum::prm::o3prmr::QueryResult::values, and gum::prm::PRMType::variable().

Referenced by interpret().

814  {
815  const std::string& query = command->value;
816 
817  if (m_inf_map.exists(command->system)) {
818  m_inf = m_inf_map[command->system];
819  } else {
820  m_inf = nullptr;
821  }
822 
823  // Create inference engine if it has not been already created.
824  if (!m_inf) { generateInfEngine(*(command->system)); }
825 
826  // Inference
827  if (m_verbose) {
828  m_log << "# Starting inference over query: " << query << "... "
829  << std::endl;
830  }
831 
832  Timer timer;
833  timer.reset();
834 
835  Potential< double > m;
836  m_inf->marginal(command->chain, m);
837 
838  // Compute spent time
839  double t = timer.step();
840 
841  if (m_verbose) { m_log << "Finished." << std::endl; }
842 
843  if (m_verbose) {
844  m_log << "# Time in seconds (accuracy ~0.001): " << t << std::endl;
845  }
846 
847  // Show results
848 
849  if (m_verbose) { m_log << std::endl; }
850 
851  QueryResult result;
852  result.command = query;
853  result.time = t;
854 
855  Instantiation j(m);
856  const PRMAttribute< double >& attr = *(command->chain.second);
857 
858  for (j.setFirst(); !j.end(); j.inc()) {
859  // auto label_value = j.val ( attr.type().variable() );
860  auto label_value = j.val(0);
861  std::string label = attr.type().variable().label(label_value);
862  float value = float(m.get(j));
863 
864  SingleResult singleResult;
865  singleResult.label = label;
866  singleResult.p = value;
867 
868  result.values.push_back(singleResult);
869 
870  if (m_verbose) { m_log << label << " : " << value << std::endl; }
871  }
872 
873  m_results.push_back(result);
874 
875  if (m_verbose) { m_log << std::endl; }
876 
877  } catch (Exception& e) {
878  GUM_SHOWERROR(e);
879  throw "something went wrong while infering: " + e.errorContent();
880 
881  } catch (const std::string& msg) { addError(msg); }
void generateInfEngine(const gum::prm::PRMSystem< double > &sys)
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
void query(const QueryCommand< double > *command)
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
virtual PRMType & type()=0
See gum::PRMClassElement::type().
void marginal(const Chain &chain, Potential< GUM_SCALAR > &m)
Compute the marginal of the formal attribute pointed by chain and stores it in m. ...
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
gum::prm::PRMInference< double > * m_inf
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map
std::vector< QueryResult > m_results
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ results()

const std::vector< QueryResult > & gum::prm::o3prmr::O3prmrInterpreter::results ( ) const

Return a vector of QueryResults. Each QueryResults is a struct with query command, time and values, a vector of struct SingleResult, with pair label/value.

Return a std::vector of QueryResults. Each QueryResults is a struct with query command, time and values, a std::vector of struct SingleResult, with pair label/value.

Definition at line 135 of file O3prmrInterpreter.cpp.

References m_results.

135  {
136  return m_results;
137  }
std::vector< QueryResult > m_results

◆ setContext()

void gum::prm::o3prmr::O3prmrInterpreter::setContext ( O3prmrContext< double > *  context)

Setter for the context.

Definition at line 80 of file O3prmrInterpreter.cpp.

References m_context.

80  {
81  delete m_context;
82 
83  if (context == 0)
85  else
86  m_context = context;
87  }
O3prmrContext< double > * m_context

◆ setEngine()

void gum::prm::o3prmr::O3prmrInterpreter::setEngine ( const SetEngineCommand command)
private

Definition at line 884 of file O3prmrInterpreter.cpp.

References m_engine, and gum::prm::o3prmr::SetEngineCommand::value.

Referenced by interpret().

884  {
885  m_engine = command->value;
886  }
+ Here is the caller graph for this function:

◆ setGndEngine()

void gum::prm::o3prmr::O3prmrInterpreter::setGndEngine ( const SetGndEngineCommand command)
private

Definition at line 889 of file O3prmrInterpreter.cpp.

References m_bn_engine, and gum::prm::o3prmr::SetGndEngineCommand::value.

Referenced by interpret().

889  {
890  m_bn_engine = command->value;
891  }
+ Here is the caller graph for this function:

◆ setSyntaxMode()

void gum::prm::o3prmr::O3prmrInterpreter::setSyntaxMode ( bool  f)

syntax mode don't process anything, just check syntax.

Definition at line 114 of file O3prmrInterpreter.cpp.

References m_syntax_flag.

◆ setVerboseMode()

void gum::prm::o3prmr::O3prmrInterpreter::setVerboseMode ( bool  f)

verbose mode show more details on the program execution.

Definition at line 120 of file O3prmrInterpreter.cpp.

References m_verbose.

◆ showElegantErrors()

void gum::prm::o3prmr::O3prmrInterpreter::showElegantErrors ( std::ostream &  o = std::cerr) const

send on std::cerr the list of errors

Definition at line 959 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::elegantErrors(), and m_errors.

959  {
961  }
void elegantErrors(std::ostream &o) const
Print errors on output stream.
+ Here is the call graph for this function:

◆ showElegantErrorsAndWarnings()

void gum::prm::o3prmr::O3prmrInterpreter::showElegantErrorsAndWarnings ( std::ostream &  o = std::cerr) const

send on std::cerr the list of errors or warnings

Definition at line 964 of file O3prmrInterpreter.cpp.

References gum::ErrorsContainer::elegantErrorsAndWarnings(), and m_errors.

964  {
966  }
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
+ Here is the call graph for this function:

◆ showErrorCounts()

void gum::prm::o3prmr::O3prmrInterpreter::showErrorCounts ( std::ostream &  o = std::cerr) const

send on std::cerr the number of errors and the number of warnings

Definition at line 969 of file O3prmrInterpreter.cpp.

References m_errors, and gum::ErrorsContainer::syntheticResults().

969  {
971  }
void syntheticResults(std::ostream &o) const
Print errors on output stream.
+ Here is the call graph for this function:

◆ system()

const PRMSystem< double > & gum::prm::o3prmr::O3prmrInterpreter::system ( std::string &  ident)
private

Definition at line 745 of file O3prmrInterpreter.cpp.

References findSystemName(), gum::prm::PRM< GUM_SCALAR >::getSystem(), gum::prm::PRM< GUM_SCALAR >::isSystem(), m_context, gum::prm::o3prmr::O3prmrContext< GUM_SCALAR >::mainImport(), prm(), and gum::prm::o3prmr::ImportCommand::value.

Referenced by checkObserve(), checkQuery(), and checkUnobserve().

745  {
746  try {
747  return prm()->getSystem(findSystemName(ident));
748  } catch (const std::string&) {}
749 
750  if ((m_context->mainImport() != 0)
752  return prm()->getSystem(m_context->mainImport()->value);
753 
754  throw "could not find any system or alias in '" + ident
755  + "' and no default alias has been set.";
756  }
const ImportCommand * mainImport() const
const gum::prm::PRM< double > * prm() const
Retrieve prm object.
std::string findSystemName(std::string &s)
bool isSystem(const std::string &name) const
Definition: PRM_tpl.h:89
PRMSystem< GUM_SCALAR > & getSystem(const std::string &name)
Returns a constant reference on a PRMSystem<GUM_SCALAR> given it&#39;s name.
Definition: PRM_tpl.h:146
O3prmrContext< double > * m_context
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unobserve()

bool gum::prm::o3prmr::O3prmrInterpreter::unobserve ( const UnobserveCommand< double > *  command)
private

Definition at line 791 of file O3prmrInterpreter.cpp.

References addError(), addWarning(), gum::prm::o3prmr::UnobserveCommand< GUM_SCALAR >::chain, gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), m_inf, m_log, m_verbose, gum::prm::PRMInference< GUM_SCALAR >::removeEvidence(), and gum::prm::o3prmr::UnobserveCommand< GUM_SCALAR >::value.

Referenced by interpret().

792  {
793  std::string name = command->value;
794  typename PRMInference< double >::Chain chain = command->chain;
795 
796  // Prevent from something
797  if (!m_inf || !m_inf->hasEvidence(chain)) {
798  addWarning(name + " was not observed");
799  } else {
800  m_inf->removeEvidence(chain);
801 
802  if (m_verbose)
803  m_log << "# Removed evidence over attribute " << name << std::endl;
804  }
805 
806  return true;
807 
808  } catch (const std::string& msg) {
809  addError(msg);
810  return false;
811  }
gum::prm::PRMInference< double > * m_inf
std::pair< const PRMInstance< double > *, const PRMAttribute< double > * > Chain
Code alias.
Definition: PRMInference.h:57
void removeEvidence(const Chain &chain)
Remove evidence on the given instance&#39;s elt.
bool hasEvidence(const PRMInstance< GUM_SCALAR > &i) const
Returns true if i has evidence.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ warnings()

Size gum::prm::o3prmr::O3prmrInterpreter::warnings ( ) const

of warnings

Definition at line 944 of file O3prmrInterpreter.cpp.

References m_errors, and gum::ErrorsContainer::warning_count.

Member Data Documentation

◆ m_bn

gum::BayesNet< double >* gum::prm::o3prmr::O3prmrInterpreter::m_bn
private

Definition at line 171 of file O3prmrInterpreter.h.

Referenced by generateInfEngine(), and ~O3prmrInterpreter().

◆ m_bn_engine

std::string gum::prm::o3prmr::O3prmrInterpreter::m_bn_engine
private

Definition at line 176 of file O3prmrInterpreter.h.

Referenced by checkSetGndEngine(), and setGndEngine().

◆ m_context

O3prmrContext< double >* gum::prm::o3prmr::O3prmrInterpreter::m_context
private

◆ m_current_line

int gum::prm::o3prmr::O3prmrInterpreter::m_current_line
private

Definition at line 182 of file O3prmrInterpreter.h.

Referenced by addError(), addWarning(), and checkSemantic().

◆ m_engine

std::string gum::prm::o3prmr::O3prmrInterpreter::m_engine
private

Definition at line 175 of file O3prmrInterpreter.h.

Referenced by checkSetEngine(), generateInfEngine(), and setEngine().

◆ m_errors

◆ m_inf

gum::prm::PRMInference< double >* gum::prm::o3prmr::O3prmrInterpreter::m_inf
private

Definition at line 173 of file O3prmrInterpreter.h.

Referenced by generateInfEngine(), inference(), observe(), query(), and unobserve().

◆ m_inf_map

HashTable< const PRMSystem< double >*, PRMInference< double >* > gum::prm::o3prmr::O3prmrInterpreter::m_inf_map
private

Definition at line 174 of file O3prmrInterpreter.h.

Referenced by generateInfEngine(), query(), and ~O3prmrInterpreter().

◆ m_log

std::ostream& gum::prm::o3prmr::O3prmrInterpreter::m_log
private

◆ m_paths

std::vector< std::string > gum::prm::o3prmr::O3prmrInterpreter::m_paths
private

Definition at line 170 of file O3prmrInterpreter.h.

Referenced by addPath(), clearPaths(), getPaths(), import(), and interpretFile().

◆ m_reader

gum::prm::o3prm::O3prmReader< double >* gum::prm::o3prmr::O3prmrInterpreter::m_reader
private

Definition at line 169 of file O3prmrInterpreter.h.

Referenced by import(), interpretFile(), prm(), and ~O3prmrInterpreter().

◆ m_results

std::vector< QueryResult > gum::prm::o3prmr::O3prmrInterpreter::m_results
private

Definition at line 177 of file O3prmrInterpreter.h.

Referenced by interpretFile(), interpretLine(), query(), and results().

◆ m_syntax_flag

bool gum::prm::o3prmr::O3prmrInterpreter::m_syntax_flag
private

Definition at line 179 of file O3prmrInterpreter.h.

Referenced by isInSyntaxMode(), and setSyntaxMode().

◆ m_verbose

bool gum::prm::o3prmr::O3prmrInterpreter::m_verbose
private

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