aGrUM  0.14.1
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 68 of file O3prmrInterpreter.h.

Constructor & Destructor Documentation

◆ O3prmrInterpreter()

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

This constructor create an empty context.

Definition at line 51 of file O3prmrInterpreter.cpp.

51  :
53  m_reader(new o3prm::O3prmReader< double >()), m_bn(0), m_inf(0),
54  m_syntax_flag(false), m_verbose(false), m_log(std::cout),
55  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 58 of file O3prmrInterpreter.cpp.

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

58  {
59  delete m_context;
60  if (m_bn) { delete m_bn; }
61  for (auto p : m_inf_map) {
62  delete p.second;
63  }
64  delete m_reader->prm();
65  delete m_reader;
66  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:108
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 185 of file O3prmrInterpreter.cpp.

References GUM_ERROR.

Referenced by interpretFile().

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

◆ addError()

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

Definition at line 974 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().

974  {
976 
977  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
978  }
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 94 of file O3prmrInterpreter.cpp.

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

94  {
95  if (path.length() && path.back() != '/') { path = path + '/'; }
96  if (Directory::isDir(path)) {
97  m_paths.push_back(path);
98  } else {
99  GUM_ERROR(NotFound, "not a directory");
100  }
101  }
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:32
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

◆ addWarning()

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

Definition at line 981 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().

981  {
983 
984  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
985  }
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 431 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().

431  {
432  try {
433  std::string left_val = command->leftValue;
434  const std::string right_val = command->rightValue;
435 
436  // Contruct the pair (instance,attribut)
437  const PRMSystem< double >& sys = system(left_val);
438  const PRMInstance< double >& instance =
439  sys.get(findInstanceName(left_val, sys));
440  const PRMAttribute< double >& attr =
441  instance.get(findAttributeName(left_val, instance));
442  typename PRMInference< double >::Chain chain =
443  std::make_pair(&instance, &attr);
444 
445  command->system = &sys;
446  command->chain = std::make_pair(&instance, &attr);
447 
448  // Check label exists for this type.
449  // Potential<double> e;
450  command->potentiel.add(chain.second->type().variable());
451  Instantiation i(command->potentiel);
452  bool found = false;
453 
454  for (i.setFirst(); !i.end(); i.inc()) {
455  if (chain.second->type().variable().label(
456  i.val(chain.second->type().variable()))
457  == right_val) {
458  command->potentiel.set(i, (double)1.0);
459  found = true;
460  } else {
461  command->potentiel.set(i, (double)0.0);
462  }
463  }
464 
465  if (!found) addError(right_val + " is not a label of " + left_val);
466 
467  // else command->potentiel = e;
468 
469  return found;
470 
471  } catch (Exception& err) {
472  addError(err.errorContent());
473  } catch (std::string& err) { addError(err); }
474 
475  return false;
476  }
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:54
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 503 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().

503  {
504  try {
505  std::string name = command->value;
506 
507  // Contruct the pair (instance,attribut)
508  const PRMSystem< double >& sys = system(name);
509  const PRMInstance< double >& instance =
510  sys.get(findInstanceName(name, sys));
511  const PRMAttribute< double >& attr =
512  instance.get(findAttributeName(name, instance));
513  // PRMInference<double>::Chain chain = std::make_pair(&instance,
514  // &attr);
515 
516  command->system = &sys;
517  command->chain = std::make_pair(&instance, &attr);
518 
519  return true;
520 
521  } catch (Exception& err) {
522  addError(err.errorContent());
523  } catch (std::string& err) { addError(err); }
524 
525  return false;
526  }
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 308 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().

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

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

Referenced by checkSemantic().

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

◆ checkSetGndEngine()

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

Definition at line 425 of file O3prmrInterpreter.cpp.

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

Referenced by checkSemantic().

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

◆ checkUnobserve()

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

Definition at line 478 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().

478  {
479  try {
480  std::string name = command->value;
481 
482  // Contruct the pair (instance,attribut)
483  const PRMSystem< double >& sys = system(name);
484  const PRMInstance< double >& instance =
485  sys.get(findInstanceName(name, sys));
486  const PRMAttribute< double >& attr =
487  instance.get(findAttributeName(name, instance));
488  // PRMInference<double>::Chain chain = std::make_pair(&instance,
489  // &attr);
490 
491  command->system = &sys;
492  command->chain = std::make_pair(&instance, &attr);
493 
494  return true;
495 
496  } catch (Exception& err) {
497  addError(err.errorContent());
498  } catch (std::string& err) { addError(err); }
499 
500  return false;
501  }
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 105 of file O3prmrInterpreter.cpp.

References m_paths.

105 { 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 935 of file O3prmrInterpreter.cpp.

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

Referenced by error(), and import().

935 { 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 944 of file O3prmrInterpreter.cpp.

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

944  {
945  if (i >= count()) throw "Index out of bound.";
946 
947  return m_errors.error(i);
948  }
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 938 of file O3prmrInterpreter.cpp.

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

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

938 { 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 951 of file O3prmrInterpreter.cpp.

References m_errors.

951  {
952  return m_errors;
953  }

◆ findAttributeName()

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

Definition at line 732 of file O3prmrInterpreter.cpp.

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

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

733  {
734  if (!instance.exists(s))
735  throw "'" + s + "' is not an attribute of instance '" + instance.name()
736  + "'.";
737 
738  return s;
739  }
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:32
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 718 of file O3prmrInterpreter.cpp.

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

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

719  {
720  // We have found system before, so 's' has been stripped.
721  size_t dot = s.find_first_of('.');
722  std::string name = s.substr(0, dot);
723 
724  if (!sys.exists(name))
725  throw "'" + name + "' is not an instance of system '" + sys.name()
726  + "'.";
727 
728  s = s.substr(dot + 1);
729  return name;
730  }
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:32
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 689 of file O3prmrInterpreter.cpp.

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

Referenced by system().

689  {
690  size_t dot = s.find_first_of('.');
691  std::string name = s.substr(0, dot);
692 
693  // We look first for real system, next for alias.
694  if (prm()->isSystem(name)) {
695  s = s.substr(dot + 1);
696  return name;
697  }
698 
699  if (!m_context->aliasToImport(name).empty()) {
700  s = s.substr(dot + 1);
701  return m_context->aliasToImport(name);
702  }
703 
704  while (dot != std::string::npos) {
705  if (prm()->isSystem(name)) {
706  s = s.substr(dot + 1);
707  return name;
708  }
709 
710  dot = s.find('.', dot + 1);
711  name = s.substr(0, dot);
712  }
713 
714  throw "could not find any system in '" + s + "'.";
715  }
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 891 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().

891  {
892  if (m_verbose)
893  m_log << "# Building the inference engine... " << std::flush;
894 
895  //
896  if (m_engine == "SVED") {
897  m_inf = new SVED< double >(*(prm()), sys);
898 
899  //
900  } else if (m_engine == "SVE") {
901  m_inf = new SVE< double >(*(prm()), sys);
902 
903  } else {
904  if (m_engine != "GRD") {
905  addWarning("unkown engine '" + m_engine + "', use GRD insteed.");
906  }
907 
908  MarginalTargetedInference< double >* bn_inf = nullptr;
909  if (m_bn) { delete m_bn; }
910  m_bn = new BayesNet< double >();
911  BayesNetFactory< double > bn_factory(m_bn);
912 
913  if (m_verbose) m_log << "(Grounding the network... " << std::flush;
914 
915  sys.groundedBN(bn_factory);
916 
917  if (m_verbose) m_log << "Finished)" << std::flush;
918 
919  // bn_inf = new LazyPropagation<double>( *m_bn );
920  bn_inf = new VariableElimination< double >(m_bn);
921 
922  auto grd_inf = new GroundedInference< double >(*(prm()), sys);
923  grd_inf->setBNInference(bn_inf);
924  m_inf = grd_inf;
925  }
926 
927  m_inf_map.insert(&sys, m_inf);
928  if (m_verbose) m_log << "Finished." << std::endl;
929  }
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:79
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 72 of file O3prmrInterpreter.cpp.

References m_context.

72  {
73  return m_context;
74  }
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 88 of file O3prmrInterpreter.cpp.

References m_paths.

88  {
89  return m_paths;
90  }
std::vector< std::string > m_paths

◆ import()

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

Definition at line 531 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().

532  {
533  try {
534  if (m_verbose) {
535  m_log << "# Loading system '" << import_name << "' => '" << std::flush;
536  }
537 
538  std::string import_package = import_name;
539 
540  std::replace(import_name.begin(), import_name.end(), '.', '/');
541  import_name += ".o3prm";
542 
543  if (m_verbose) {
544  m_log << import_name << "' ... " << std::endl << std::flush;
545  }
546 
547  std::ifstream file_test;
548  bool found = false;
549  std::string import_abs_filename;
550 
551  // Search in o3prmr file dir.
552  std::string o3prmrFilename = context->filename();
553 
554  if (!o3prmrFilename.empty()) {
555  size_t index = o3prmrFilename.find_last_of('/');
556 
557  if (index != std::string::npos) {
558  std::string dir = o3prmrFilename.substr(0, index + 1);
559  import_abs_filename = dir + import_name;
560 
561  if (m_verbose) {
562  m_log << "# Search from filedir '" << import_abs_filename
563  << "' ... " << std::flush;
564  }
565 
566  file_test.open(import_abs_filename.c_str());
567 
568  if (file_test.is_open()) {
569  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
570 
571  file_test.close();
572  found = true;
573  } else if (m_verbose) {
574  m_log << "not found." << std::endl << std::flush;
575  }
576  }
577  }
578 
579  // Deduce root path from package name.
580  std::string package = context->package();
581 
582  if (!found && !package.empty()) {
583  std::string root;
584 
585  // if filename is not empty, start from it.
586  std::string filename = context->filename();
587 
588  if (!filename.empty()) {
589  size_t size = filename.find_last_of('/');
590 
591  if (size != std::string::npos) {
592  root += filename.substr(0, size + 1); // take with the '/'
593  }
594  }
595 
596  //
597  root += "../";
598  int count = (int)std::count(package.begin(), package.end(), '.');
599 
600  for (int i = 0; i < count; i++)
601  root += "../";
602 
603  import_abs_filename = Directory(root).absolutePath() + import_name;
604 
605  if (m_verbose) {
606  m_log << "# Search from package '" << package << "' => '"
607  << import_abs_filename << "' ... " << std::flush;
608  }
609 
610  file_test.open(import_abs_filename.c_str());
611 
612  if (file_test.is_open()) {
613  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
614 
615  file_test.close();
616  found = true;
617  } else if (m_verbose) {
618  m_log << "not found." << std::endl << std::flush;
619  }
620  }
621 
622  // Search import in all paths.
623  for (const auto& path : m_paths) {
624  import_abs_filename = path + import_name;
625 
626  if (m_verbose) {
627  m_log << "# Search from classpath '" << import_abs_filename
628  << "' ... " << std::flush;
629  }
630 
631  file_test.open(import_abs_filename.c_str());
632 
633  if (file_test.is_open()) {
634  if (m_verbose) { m_log << " found !" << std::endl << std::flush; }
635 
636  file_test.close();
637  found = true;
638  break;
639  } else if (m_verbose) {
640  m_log << " not found." << std::endl << std::flush;
641  }
642  }
643 
644  if (!found) {
645  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
646 
647  addError("import not found.");
648  return false;
649  }
650 
651  // May throw std::IOError if file does't exist
652  Size previousO3prmError = m_reader->errors();
653  Size previousO3prmrError = errors();
654 
655  try {
656  m_reader->readFile(import_abs_filename, import_package);
657 
658  // Show errors and warning
659  if (m_verbose
660  && (m_reader->errors() > (unsigned int)previousO3prmError
661  || errors() > previousO3prmrError)) {
662  m_log << "Finished with errors." << std::endl;
663  } else if (m_verbose) {
664  m_log << "Finished." << std::endl;
665  }
666 
667  } catch (const IOError& err) {
668  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
669 
670  addError(err.errorContent());
671  }
672 
673  // Add o3prm errors and warnings to o3prmr errors
674  for (; previousO3prmError < m_reader->errorsContainer().count();
675  previousO3prmError++) {
676  m_errors.add(m_reader->errorsContainer().error(previousO3prmError));
677  }
678 
679  return errors() == previousO3prmrError;
680 
681  } catch (const Exception& err) {
682  if (m_verbose) { m_log << "Finished with exceptions." << std::endl; }
683 
684  addError(err.errorContent());
685  return false;
686  }
687  }
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:45
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 125 of file O3prmrInterpreter.cpp.

References m_inf.

125  {
126  return m_inf;
127  }
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 234 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().

234  {
235  if (isVerboseMode())
236  m_log << "## Start interpretation." << std::endl << std::flush;
237 
238  // Don't parse if any syntax errors.
239  if (errors() > 0) return false;
240 
241  // For each session
242  std::vector< O3prmrSession< double >* > sessions = c->sessions();
243 
244  for (const auto session : sessions)
245  for (auto command : session->commands()) {
246  // We process it.
247  bool result = true;
248 
249  try {
250  switch (command->type()) {
252  result = observe((ObserveCommand< double >*)command);
253  break;
254 
256  result = unobserve((UnobserveCommand< double >*)command);
257  break;
258 
260  setEngine((SetEngineCommand*)command);
261  break;
262 
264  setGndEngine((SetGndEngineCommand*)command);
265  break;
266 
268  query((QueryCommand< double >*)command);
269  break;
270  }
271  } catch (Exception& err) {
272  result = false;
273  addError(err.errorContent());
274  } catch (std::string& err) {
275  result = false;
276  addError(err);
277  }
278 
279  // If there was a problem, skip the rest of this session,
280  // unless syntax mode is activated.
281  if (!result) {
282  if (m_verbose)
283  m_log << "Errors : skip the rest of this session." << std::endl;
284 
285  break;
286  }
287  }
288 
289  if (isVerboseMode())
290  m_log << "## End interpretation." << std::endl << std::flush;
291 
292  return errors() == 0;
293  }
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 143 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().

143  {
144  m_results.clear();
145 
146  try {
147  std::string file_content = __readFile(filename);
148 
149  delete m_context;
150  m_context = new O3prmrContext< double >(filename);
151  O3prmrContext< double > c(filename);
152 
153  // On vérifie la syntaxe
154  unsigned char* buffer = new unsigned char[file_content.length() + 1];
155  strcpy((char*)buffer, file_content.c_str());
156  Scanner s(buffer, int(file_content.length() + 1));
157  Parser p(&s);
158  p.setO3prmrContext(&c);
159  p.Parse();
160 
161  m_errors = p.errors();
162 
163  if (errors() > 0) { return false; }
164 
165  // Set paths to search from.
166  delete m_reader->prm();
167  delete m_reader;
168  m_reader = new o3prm::O3prmReader< double >();
169 
170  for (size_t i = 0; i < m_paths.size(); i++) {
172  }
173 
174  // On vérifie la sémantique.
175  if (!checkSemantic(&c)) { return false; }
176 
177  if (isInSyntaxMode()) {
178  return true;
179  } else {
180  return interpret(&c);
181  }
182  } catch (gum::Exception&) { return false; }
183  }
std::vector< ParseError > errors
The list of gum::ParseError contained in this gum::ErrorsContainer.
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:108
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:103
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 206 of file O3prmrInterpreter.cpp.

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

206  {
207  m_results.clear();
208 
209  // On vérifie la syntaxe
211  Scanner s((unsigned char*)line.c_str(), (int)line.length());
212  Parser p(&s);
213  p.setO3prmrContext(&c);
214  p.Parse();
215  m_errors = p.errors();
216 
217  if (errors() > 0) return false;
218 
219  // On vérifie la sémantique.
220  if (!checkSemantic(&c)) return false;
221 
222  if (isInSyntaxMode())
223  return true;
224  else
225  return interpret(&c);
226  }
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 108 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 114 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 758 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().

758  {
759  const typename PRMInference< double >::Chain& chain = command->chain;
760 
761  // Generate the inference engine if it doesn't exist.
762  if (!m_inf) { generateInfEngine(*(command->system)); }
763 
764  // Prevent from something
765  if (m_inf->hasEvidence(chain))
766  addWarning(command->leftValue + " is already observed");
767 
768  m_inf->addEvidence(chain, command->potentiel);
769 
770  if (m_verbose)
771  m_log << "# Added evidence " << command->rightValue << " over attribute "
772  << command->leftValue << std::endl;
773 
774  return true;
775 
776  } catch (OperationNotAllowed& ex) {
777  addError("something went wrong when adding evidence " + command->rightValue
778  + " over " + command->leftValue + " : " + ex.errorContent());
779  return false;
780 
781  } catch (const std::string& msg) {
782  addError(msg);
783  return false;
784  }
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:54
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 120 of file O3prmrInterpreter.cpp.

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

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

120  {
121  return m_reader->prm();
122  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:108
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 811 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().

811  {
812  const std::string& query = command->value;
813 
814  if (m_inf_map.exists(command->system)) {
815  m_inf = m_inf_map[command->system];
816  } else {
817  m_inf = nullptr;
818  }
819 
820  // Create inference engine if it has not been already created.
821  if (!m_inf) { generateInfEngine(*(command->system)); }
822 
823  // Inference
824  if (m_verbose) {
825  m_log << "# Starting inference over query: " << query << "... "
826  << std::endl;
827  }
828 
829  Timer timer;
830  timer.reset();
831 
832  Potential< double > m;
833  m_inf->marginal(command->chain, m);
834 
835  // Compute spent time
836  double t = timer.step();
837 
838  if (m_verbose) { m_log << "Finished." << std::endl; }
839 
840  if (m_verbose) {
841  m_log << "# Time in seconds (accuracy ~0.001): " << t << std::endl;
842  }
843 
844  // Show results
845 
846  if (m_verbose) { m_log << std::endl; }
847 
848  QueryResult result;
849  result.command = query;
850  result.time = t;
851 
852  Instantiation j(m);
853  const PRMAttribute< double >& attr = *(command->chain.second);
854 
855  for (j.setFirst(); !j.end(); j.inc()) {
856  // auto label_value = j.val ( attr.type().variable() );
857  auto label_value = j.val(0);
858  std::string label = attr.type().variable().label(label_value);
859  float value = float(m.get(j));
860 
861  SingleResult singleResult;
862  singleResult.label = label;
863  singleResult.p = value;
864 
865  result.values.push_back(singleResult);
866 
867  if (m_verbose) { m_log << label << " : " << value << std::endl; }
868  }
869 
870  m_results.push_back(result);
871 
872  if (m_verbose) { m_log << std::endl; }
873 
874  } catch (Exception& e) {
875  GUM_SHOWERROR(e);
876  throw "something went wrong while infering: " + e.errorContent();
877 
878  } 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:42
void query(const QueryCommand< double > *command)
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
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 132 of file O3prmrInterpreter.cpp.

References m_results.

132  {
133  return m_results;
134  }
std::vector< QueryResult > m_results

◆ setContext()

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

Setter for the context.

Definition at line 77 of file O3prmrInterpreter.cpp.

References m_context.

77  {
78  delete m_context;
79 
80  if (context == 0)
82  else
83  m_context = context;
84  }
O3prmrContext< double > * m_context

◆ setEngine()

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

Definition at line 881 of file O3prmrInterpreter.cpp.

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

Referenced by interpret().

881  {
882  m_engine = command->value;
883  }
+ Here is the caller graph for this function:

◆ setGndEngine()

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

Definition at line 886 of file O3prmrInterpreter.cpp.

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

Referenced by interpret().

886  {
887  m_bn_engine = command->value;
888  }
+ 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 111 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 117 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 956 of file O3prmrInterpreter.cpp.

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

956  {
958  }
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 961 of file O3prmrInterpreter.cpp.

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

961  {
963  }
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 966 of file O3prmrInterpreter.cpp.

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

966  {
968  }
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 742 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().

742  {
743  try {
744  return prm()->getSystem(findSystemName(ident));
745  } catch (const std::string&) {}
746 
747  if ((m_context->mainImport() != 0)
749  return prm()->getSystem(m_context->mainImport()->value);
750 
751  throw "could not find any system or alias in '" + ident
752  + "' and no default alias has been set.";
753  }
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:86
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:143
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 788 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().

789  {
790  std::string name = command->value;
791  typename PRMInference< double >::Chain chain = command->chain;
792 
793  // Prevent from something
794  if (!m_inf || !m_inf->hasEvidence(chain)) {
795  addWarning(name + " was not observed");
796  } else {
797  m_inf->removeEvidence(chain);
798 
799  if (m_verbose)
800  m_log << "# Removed evidence over attribute " << name << std::endl;
801  }
802 
803  return true;
804 
805  } catch (const std::string& msg) {
806  addError(msg);
807  return false;
808  }
gum::prm::PRMInference< double > * m_inf
std::pair< const PRMInstance< double > *, const PRMAttribute< double > * > Chain
Code alias.
Definition: PRMInference.h:54
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 941 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 169 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 174 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 180 of file O3prmrInterpreter.h.

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

◆ m_engine

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

Definition at line 173 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 171 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 172 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 168 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 167 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 175 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 177 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: