aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
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 69 of file O3prmrInterpreter.h.

Constructor & Destructor Documentation

◆ O3prmrInterpreter()

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

This constructor create an empty context.

Definition at line 53 of file O3prmrInterpreter.cpp.

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

60  {
61  delete m_context;
62  if (m_bn) { delete m_bn; }
63  for (auto p: m_inf_map) {
64  delete p.second;
65  }
66  delete m_reader->prm();
67  delete m_reader;
68  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:109
gum::prm::o3prm::O3prmReader< double > * m_reader
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map
O3prmrContext< double > * m_context

Member Function Documentation

◆ addError()

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

Definition at line 977 of file O3prmrInterpreter.cpp.

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

◆ 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 96 of file O3prmrInterpreter.cpp.

96  {
97  if (path.length() && path.back() != '/') { path = path + '/'; }
98  if (Directory::isDir(path)) {
99  m_paths.push_back(path);
100  } else {
101  GUM_ERROR(NotFound, "not a directory");
102  }
103  }
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:34
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addWarning()

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

Definition at line 984 of file O3prmrInterpreter.cpp.

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

◆ checkObserve()

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

Definition at line 433 of file O3prmrInterpreter.cpp.

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

◆ checkQuery()

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

Definition at line 505 of file O3prmrInterpreter.cpp.

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

◆ 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 310 of file O3prmrInterpreter.cpp.

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

◆ checkSetEngine()

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

Definition at line 422 of file O3prmrInterpreter.cpp.

422  {
423  m_engine = command->value;
424  return m_engine == "SVED" || m_engine == "GRD" || m_engine == "SVE";
425  }

◆ checkSetGndEngine()

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

Definition at line 427 of file O3prmrInterpreter.cpp.

427  {
428  m_bn_engine = command->value;
429  return m_bn_engine == "VE" || m_bn_engine == "VEBB"
430  || m_bn_engine == "lazy";
431  }

◆ checkUnobserve()

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

Definition at line 480 of file O3prmrInterpreter.cpp.

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

◆ 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 107 of file O3prmrInterpreter.cpp.

107 { 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.

938 { return m_errors.count(); }
Size count() const
Returns the number of errors and warnings.

◆ 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.

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.

◆ errors()

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

of errors

Definition at line 941 of file O3prmrInterpreter.cpp.

941 { return m_errors.error_count; }
Size error_count
Number of errors detected.

◆ errorsContainer()

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

Return container with all errors.

Definition at line 954 of file O3prmrInterpreter.cpp.

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 734 of file O3prmrInterpreter.cpp.

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:34
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.

◆ findInstanceName()

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

Definition at line 720 of file O3prmrInterpreter.cpp.

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

◆ findSystemName()

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

Definition at line 691 of file O3prmrInterpreter.cpp.

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

◆ generateInfEngine()

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

Definition at line 894 of file O3prmrInterpreter.cpp.

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:90
gum::prm::PRMInference< double > * m_inf
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map

◆ getContext()

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

Getter and setter for the context.

Getter for the context.

Definition at line 74 of file O3prmrInterpreter.cpp.

74  {
75  return m_context;
76  }
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 90 of file O3prmrInterpreter.cpp.

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

◆ import()

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

Definition at line 533 of file O3prmrInterpreter.cpp.

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

◆ inference()

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

Retrieve inference motor object.

Definition at line 127 of file O3prmrInterpreter.cpp.

127  {
128  return m_inf;
129  }
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 236 of file O3prmrInterpreter.cpp.

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

◆ 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 145 of file O3prmrInterpreter.cpp.

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

◆ interpretLine()

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

Definition at line 208 of file O3prmrInterpreter.cpp.

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

◆ 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 110 of file O3prmrInterpreter.cpp.

◆ 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 116 of file O3prmrInterpreter.cpp.

◆ observe()

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

Definition at line 761 of file O3prmrInterpreter.cpp.

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:56
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.

◆ prm()

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

Retrieve prm object.

Definition at line 122 of file O3prmrInterpreter.cpp.

122  {
123  return m_reader->prm();
124  }
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:109
gum::prm::o3prm::O3prmReader< double > * m_reader

◆ query()

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

Definition at line 814 of file O3prmrInterpreter.cpp.

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->posterior(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)
void posterior(const Chain &chain, Potential< GUM_SCALAR > &m)
Compute the posterior of the formal attribute pointed by chain and stores it in m.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:44
void query(const QueryCommand< double > *command)
#define GUM_SHOWERROR(e)
Definition: exceptions.h:60
virtual PRMType & type()=0
See gum::PRMClassElement::type().
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

◆ readFile__()

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

Definition at line 187 of file O3prmrInterpreter.cpp.

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

◆ 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 134 of file O3prmrInterpreter.cpp.

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

◆ setContext()

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

Setter for the context.

Definition at line 79 of file O3prmrInterpreter.cpp.

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

◆ setEngine()

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

Definition at line 884 of file O3prmrInterpreter.cpp.

884  {
885  m_engine = command->value;
886  }

◆ setGndEngine()

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

Definition at line 889 of file O3prmrInterpreter.cpp.

889  {
890  m_bn_engine = command->value;
891  }

◆ setSyntaxMode()

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

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

Definition at line 113 of file O3prmrInterpreter.cpp.

◆ setVerboseMode()

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

verbose mode show more details on the program execution.

Definition at line 119 of file O3prmrInterpreter.cpp.

◆ 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.

959  {
961  }
void elegantErrors(std::ostream &o) const
Print errors on output stream.

◆ 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.

964  {
966  }
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.

◆ 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.

969  {
971  }
void syntheticResults(std::ostream &o) const
Print errors on output stream.

◆ system()

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

Definition at line 745 of file O3prmrInterpreter.cpp.

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:88
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:145
O3prmrContext< double > * m_context

◆ unobserve()

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

Definition at line 791 of file O3prmrInterpreter.cpp.

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:56
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.

◆ warnings()

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

of warnings

Definition at line 944 of file O3prmrInterpreter.cpp.

Member Data Documentation

◆ m_bn

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

Definition at line 170 of file O3prmrInterpreter.h.

◆ m_bn_engine

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

Definition at line 175 of file O3prmrInterpreter.h.

◆ m_context

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

Definition at line 167 of file O3prmrInterpreter.h.

◆ m_current_line

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

Definition at line 181 of file O3prmrInterpreter.h.

◆ m_engine

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

Definition at line 174 of file O3prmrInterpreter.h.

◆ m_errors

gum::ErrorsContainer gum::prm::o3prmr::O3prmrInterpreter::m_errors
private

Definition at line 177 of file O3prmrInterpreter.h.

◆ m_inf

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

Definition at line 172 of file O3prmrInterpreter.h.

◆ m_inf_map

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

Definition at line 173 of file O3prmrInterpreter.h.

◆ m_log

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

Definition at line 180 of file O3prmrInterpreter.h.

◆ m_paths

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

Definition at line 169 of file O3prmrInterpreter.h.

◆ m_reader

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

Definition at line 168 of file O3prmrInterpreter.h.

◆ m_results

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

Definition at line 176 of file O3prmrInterpreter.h.

◆ m_syntax_flag

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

Definition at line 178 of file O3prmrInterpreter.h.

◆ m_verbose

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

Definition at line 179 of file O3prmrInterpreter.h.


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