aGrUM  0.15.2
O3prmrInterpreter.cpp
Go to the documentation of this file.
1 
30 #include <agrum/agrum.h>
31 
32 #include <agrum/BN/BayesNet.h>
36 
41 
42 #include <agrum/PRM/o3prmr/cocoR/Parser.h>
43 
44 namespace gum {
45 
46  namespace prm {
47 
48  namespace o3prmr {
49 
50  /* **************************************************************************
51  */
52 
55  m_context(new O3prmrContext< double >()),
56  m_reader(new o3prm::O3prmReader< double >()), m_bn(0), m_inf(0),
57  m_syntax_flag(false), m_verbose(false), m_log(std::cout),
58  m_current_line(-1) {}
59 
62  delete m_context;
63  if (m_bn) { delete m_bn; }
64  for (auto p : m_inf_map) {
65  delete p.second;
66  }
67  delete m_reader->prm();
68  delete m_reader;
69  }
70 
71  /* **************************************************************************
72  */
73 
76  return m_context;
77  }
78 
81  delete m_context;
82 
83  if (context == 0)
85  else
86  m_context = context;
87  }
88 
91  std::vector< std::string > O3prmrInterpreter::getPaths() const {
92  return m_paths;
93  }
94 
97  void O3prmrInterpreter::addPath(std::string path) {
98  if (path.length() && path.back() != '/') { path = path + '/'; }
99  if (Directory::isDir(path)) {
100  m_paths.push_back(path);
101  } else {
102  GUM_ERROR(NotFound, "not a directory");
103  }
104  }
105 
109 
112 
115 
118 
121 
124  return m_reader->prm();
125  }
126 
129  return m_inf;
130  }
131 
135  const std::vector< QueryResult >& O3prmrInterpreter::results() const {
136  return m_results;
137  }
138 
146  bool O3prmrInterpreter::interpretFile(const std::string& filename) {
147  m_results.clear();
148 
149  try {
150  std::string file_content = __readFile(filename);
151 
152  delete m_context;
153  m_context = new O3prmrContext< double >(filename);
154  O3prmrContext< double > c(filename);
155 
156  // On vérifie la syntaxe
157  unsigned char* buffer = new unsigned char[file_content.length() + 1];
158  strcpy((char*)buffer, file_content.c_str());
159  Scanner s(buffer, int(file_content.length() + 1));
160  Parser p(&s);
161  p.setO3prmrContext(&c);
162  p.Parse();
163 
164  m_errors = p.errors();
165 
166  if (errors() > 0) { return false; }
167 
168  // Set paths to search from.
169  delete m_reader->prm();
170  delete m_reader;
172 
173  for (size_t i = 0; i < m_paths.size(); i++) {
175  }
176 
177  // On vérifie la sémantique.
178  if (!checkSemantic(&c)) { return false; }
179 
180  if (isInSyntaxMode()) {
181  return true;
182  } else {
183  return interpret(&c);
184  }
185  } catch (gum::Exception&) { return false; }
186  }
187 
188  std::string O3prmrInterpreter::__readFile(const std::string& file) {
189  // read entire file into string
190  std::ifstream istream(file, std::ifstream::binary);
191  if (istream) {
192  // get length of file:
193  istream.seekg(0, istream.end);
194  int length = int(istream.tellg());
195  istream.seekg(0, istream.beg);
196 
197  std::string str;
198  str.resize(length, ' '); // reserve space
199  char* begin = &*str.begin();
200 
201  istream.read(begin, length);
202  istream.close();
203 
204  return str;
205  }
206  GUM_ERROR(OperationNotAllowed, "Could not open file");
207  }
208 
209  bool O3prmrInterpreter::interpretLine(const std::string& line) {
210  m_results.clear();
211 
212  // On vérifie la syntaxe
214  Scanner s((unsigned char*)line.c_str(), (int)line.length());
215  Parser p(&s);
216  p.setO3prmrContext(&c);
217  p.Parse();
218  m_errors = p.errors();
219 
220  if (errors() > 0) return false;
221 
222  // On vérifie la sémantique.
223  if (!checkSemantic(&c)) return false;
224 
225  if (isInSyntaxMode())
226  return true;
227  else
228  return interpret(&c);
229  }
230 
238  if (isVerboseMode())
239  m_log << "## Start interpretation." << std::endl << std::flush;
240 
241  // Don't parse if any syntax errors.
242  if (errors() > 0) return false;
243 
244  // For each session
245  std::vector< O3prmrSession< double >* > sessions = c->sessions();
246 
247  for (const auto session : sessions)
248  for (auto command : session->commands()) {
249  // We process it.
250  bool result = true;
251 
252  try {
253  switch (command->type()) {
255  result = observe((ObserveCommand< double >*)command);
256  break;
257 
259  result = unobserve((UnobserveCommand< double >*)command);
260  break;
261 
263  setEngine((SetEngineCommand*)command);
264  break;
265 
268  break;
269 
271  query((QueryCommand< double >*)command);
272  break;
273  }
274  } catch (Exception& err) {
275  result = false;
276  addError(err.errorContent());
277  } catch (std::string& err) {
278  result = false;
279  addError(err);
280  }
281 
282  // If there was a problem, skip the rest of this session,
283  // unless syntax mode is activated.
284  if (!result) {
285  if (m_verbose)
286  m_log << "Errors : skip the rest of this session." << std::endl;
287 
288  break;
289  }
290  }
291 
292  if (isVerboseMode())
293  m_log << "## End interpretation." << std::endl << std::flush;
294 
295  return errors() == 0;
296  }
297 
298  /* **************************************************************************
299  */
300 
312  // Don't parse if any syntax errors.
313  if (errors() > 0) return false;
314 
315  // On importe tous les systèmes.
316  for (const auto command : context->imports()) {
317  m_current_line = command->line;
318  // if import doen't succed stop here unless syntax mode is activated.
319  bool succeed = import(context, command->value);
320 
321  if (!succeed && !isInSyntaxMode()) return false;
322 
323  // En cas de succès, on met à jour le contexte global
324  if (succeed) m_context->addImport(*command);
325  }
326 
327  if (m_verbose)
328  m_log << "## Check semantic for " << context->sessions().size()
329  << " sessions" << std::endl;
330 
331  // On vérifie chaque session
332  for (const auto session : context->sessions()) {
333  std::string sessionName = session->name();
334  O3prmrSession< double >* new_session =
335  new O3prmrSession< double >(sessionName);
336 
337  if (m_verbose)
338  m_log << "## Start session '" << sessionName << "'..." << std::endl
339  << std::endl;
340 
341  for (const auto command : session->commands()) {
342  if (m_verbose)
343  m_log << "# * Going to check command : " << command->toString()
344  << std::endl;
345 
346  // Update the current line (for warnings and errors)
347  m_current_line = command->line;
348 
349  // We check it.
350  bool result = true;
351 
352  try {
353  switch (command->type()) {
355  result = checkSetEngine((SetEngineCommand*)command);
356  break;
357 
359  result = checkSetGndEngine((SetGndEngineCommand*)command);
360  break;
361 
363  result = checkObserve((ObserveCommand< double >*)command);
364  break;
365 
367  result = checkUnobserve((UnobserveCommand< double >*)command);
368  break;
369 
371  result = checkQuery((QueryCommand< double >*)command);
372  break;
373 
374  default:
375  addError("Error : Unknow command : " + command->toString()
376  + "\n -> Command not processed.");
377  result = false;
378  }
379  } catch (Exception& err) {
380  result = false;
381  addError(err.errorContent());
382  } catch (std::string& err) {
383  result = false;
384  addError(err);
385  }
386 
387  // If there was a problem, skip the rest of this session,
388  // unless syntax mode is activated.
389  if (!result && !isInSyntaxMode()) {
390  if (m_verbose)
391  m_log << "Errors : skip the rest of this session." << std::endl;
392 
393  break;
394  }
395 
396  // On l'ajoute au contexte globale
397  if (result) new_session->addCommand((const O3prmrCommand*)command);
398  }
399 
400  // Ajoute la session au contexte global,
401  // ou à la dernière session.
402  if (sessionName == "default" && m_context->sessions().size() > 0)
403  *(m_context->sessions().back()) += *new_session;
404  else
405  m_context->addSession(*new_session);
406 
407  if (m_verbose)
408  m_log << std::endl
409  << "## Session '" << sessionName << "' finished." << std::endl
410  << std::endl
411  << std::endl;
412 
413  // todo : check memory leak
414  // delete new_session; ??
415  }
416 
417  if (isVerboseMode() && errors() != 0)
419 
420  return errors() == 0;
421  }
422 
424  m_engine = command->value;
425  return m_engine == "SVED" || m_engine == "GRD" || m_engine == "SVE";
426  }
427 
429  m_bn_engine = command->value;
430  return m_bn_engine == "VE" || m_bn_engine == "VEBB"
431  || m_bn_engine == "lazy";
432  }
433 
435  try {
436  std::string left_val = command->leftValue;
437  const std::string right_val = command->rightValue;
438 
439  // Contruct the pair (instance,attribut)
440  const PRMSystem< double >& sys = system(left_val);
441  const PRMInstance< double >& instance =
442  sys.get(findInstanceName(left_val, sys));
443  const PRMAttribute< double >& attr =
444  instance.get(findAttributeName(left_val, instance));
445  typename PRMInference< double >::Chain chain =
446  std::make_pair(&instance, &attr);
447 
448  command->system = &sys;
449  command->chain = std::make_pair(&instance, &attr);
450 
451  // Check label exists for this type.
452  // Potential<double> e;
453  command->potentiel.add(chain.second->type().variable());
454  Instantiation i(command->potentiel);
455  bool found = false;
456 
457  for (i.setFirst(); !i.end(); i.inc()) {
458  if (chain.second->type().variable().label(
459  i.val(chain.second->type().variable()))
460  == right_val) {
461  command->potentiel.set(i, (double)1.0);
462  found = true;
463  } else {
464  command->potentiel.set(i, (double)0.0);
465  }
466  }
467 
468  if (!found) addError(right_val + " is not a label of " + left_val);
469 
470  // else command->potentiel = e;
471 
472  return found;
473 
474  } catch (Exception& err) {
475  addError(err.errorContent());
476  } catch (std::string& err) { addError(err); }
477 
478  return false;
479  }
480 
482  try {
483  std::string name = command->value;
484 
485  // Contruct the pair (instance,attribut)
486  const PRMSystem< double >& sys = system(name);
487  const PRMInstance< double >& instance =
488  sys.get(findInstanceName(name, sys));
489  const PRMAttribute< double >& attr =
490  instance.get(findAttributeName(name, instance));
491  // PRMInference<double>::Chain chain = std::make_pair(&instance,
492  // &attr);
493 
494  command->system = &sys;
495  command->chain = std::make_pair(&instance, &attr);
496 
497  return true;
498 
499  } catch (Exception& err) {
500  addError(err.errorContent());
501  } catch (std::string& err) { addError(err); }
502 
503  return false;
504  }
505 
507  try {
508  std::string name = command->value;
509 
510  // Contruct the pair (instance,attribut)
511  const PRMSystem< double >& sys = system(name);
512  const PRMInstance< double >& instance =
513  sys.get(findInstanceName(name, sys));
514  const PRMAttribute< double >& attr =
515  instance.get(findAttributeName(name, instance));
516  // PRMInference<double>::Chain chain = std::make_pair(&instance,
517  // &attr);
518 
519  command->system = &sys;
520  command->chain = std::make_pair(&instance, &attr);
521 
522  return true;
523 
524  } catch (Exception& err) {
525  addError(err.errorContent());
526  } catch (std::string& err) { addError(err); }
527 
528  return false;
529  }
530 
531  // Import the system o3prm file
532  // Return false if any error.
533 
535  std::string import_name) {
536  try {
537  if (m_verbose) {
538  m_log << "# Loading system '" << import_name << "' => '" << std::flush;
539  }
540 
541  std::string import_package = import_name;
542 
543  std::replace(import_name.begin(), import_name.end(), '.', '/');
544  import_name += ".o3prm";
545 
546  if (m_verbose) {
547  m_log << import_name << "' ... " << std::endl << std::flush;
548  }
549 
550  std::ifstream file_test;
551  bool found = false;
552  std::string import_abs_filename;
553 
554  // Search in o3prmr file dir.
555  std::string o3prmrFilename = context->filename();
556 
557  if (!o3prmrFilename.empty()) {
558  size_t index = o3prmrFilename.find_last_of('/');
559 
560  if (index != std::string::npos) {
561  std::string dir = o3prmrFilename.substr(0, index + 1);
562  import_abs_filename = dir + import_name;
563 
564  if (m_verbose) {
565  m_log << "# Search from filedir '" << import_abs_filename
566  << "' ... " << std::flush;
567  }
568 
569  file_test.open(import_abs_filename.c_str());
570 
571  if (file_test.is_open()) {
572  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
573 
574  file_test.close();
575  found = true;
576  } else if (m_verbose) {
577  m_log << "not found." << std::endl << std::flush;
578  }
579  }
580  }
581 
582  // Deduce root path from package name.
583  std::string package = context->package();
584 
585  if (!found && !package.empty()) {
586  std::string root;
587 
588  // if filename is not empty, start from it.
589  std::string filename = context->filename();
590 
591  if (!filename.empty()) {
592  size_t size = filename.find_last_of('/');
593 
594  if (size != std::string::npos) {
595  root += filename.substr(0, size + 1); // take with the '/'
596  }
597  }
598 
599  //
600  root += "../";
601  int count = (int)std::count(package.begin(), package.end(), '.');
602 
603  for (int i = 0; i < count; i++)
604  root += "../";
605 
606  import_abs_filename = Directory(root).absolutePath() + import_name;
607 
608  if (m_verbose) {
609  m_log << "# Search from package '" << package << "' => '"
610  << import_abs_filename << "' ... " << std::flush;
611  }
612 
613  file_test.open(import_abs_filename.c_str());
614 
615  if (file_test.is_open()) {
616  if (m_verbose) { m_log << "found !" << std::endl << std::flush; }
617 
618  file_test.close();
619  found = true;
620  } else if (m_verbose) {
621  m_log << "not found." << std::endl << std::flush;
622  }
623  }
624 
625  // Search import in all paths.
626  for (const auto& path : m_paths) {
627  import_abs_filename = path + import_name;
628 
629  if (m_verbose) {
630  m_log << "# Search from classpath '" << import_abs_filename
631  << "' ... " << std::flush;
632  }
633 
634  file_test.open(import_abs_filename.c_str());
635 
636  if (file_test.is_open()) {
637  if (m_verbose) { m_log << " found !" << std::endl << std::flush; }
638 
639  file_test.close();
640  found = true;
641  break;
642  } else if (m_verbose) {
643  m_log << " not found." << std::endl << std::flush;
644  }
645  }
646 
647  if (!found) {
648  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
649 
650  addError("import not found.");
651  return false;
652  }
653 
654  // May throw std::IOError if file does't exist
655  Size previousO3prmError = m_reader->errors();
656  Size previousO3prmrError = errors();
657 
658  try {
659  m_reader->readFile(import_abs_filename, import_package);
660 
661  // Show errors and warning
662  if (m_verbose
663  && (m_reader->errors() > (unsigned int)previousO3prmError
664  || errors() > previousO3prmrError)) {
665  m_log << "Finished with errors." << std::endl;
666  } else if (m_verbose) {
667  m_log << "Finished." << std::endl;
668  }
669 
670  } catch (const IOError& err) {
671  if (m_verbose) { m_log << "Finished with errors." << std::endl; }
672 
673  addError(err.errorContent());
674  }
675 
676  // Add o3prm errors and warnings to o3prmr errors
677  for (; previousO3prmError < m_reader->errorsContainer().count();
678  previousO3prmError++) {
679  m_errors.add(m_reader->errorsContainer().error(previousO3prmError));
680  }
681 
682  return errors() == previousO3prmrError;
683 
684  } catch (const Exception& err) {
685  if (m_verbose) { m_log << "Finished with exceptions." << std::endl; }
686 
687  addError(err.errorContent());
688  return false;
689  }
690  }
691 
692  std::string O3prmrInterpreter::findSystemName(std::string& s) {
693  size_t dot = s.find_first_of('.');
694  std::string name = s.substr(0, dot);
695 
696  // We look first for real system, next for alias.
697  if (prm()->isSystem(name)) {
698  s = s.substr(dot + 1);
699  return name;
700  }
701 
702  if (!m_context->aliasToImport(name).empty()) {
703  s = s.substr(dot + 1);
704  return m_context->aliasToImport(name);
705  }
706 
707  while (dot != std::string::npos) {
708  if (prm()->isSystem(name)) {
709  s = s.substr(dot + 1);
710  return name;
711  }
712 
713  dot = s.find('.', dot + 1);
714  name = s.substr(0, dot);
715  }
716 
717  throw "could not find any system in '" + s + "'.";
718  }
719 
720  std::string
722  const PRMSystem< double >& sys) {
723  // We have found system before, so 's' has been stripped.
724  size_t dot = s.find_first_of('.');
725  std::string name = s.substr(0, dot);
726 
727  if (!sys.exists(name))
728  throw "'" + name + "' is not an instance of system '" + sys.name()
729  + "'.";
730 
731  s = s.substr(dot + 1);
732  return name;
733  }
734 
736  const std::string& s, const PRMInstance< double >& instance) {
737  if (!instance.exists(s))
738  throw "'" + s + "' is not an attribute of instance '" + instance.name()
739  + "'.";
740 
741  return s;
742  }
743 
744  // After this method, ident doesn't contains the system name anymore.
745  const PRMSystem< double >& O3prmrInterpreter::system(std::string& ident) {
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  }
757 
759 
760  bool
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  }
788 
790 
792  const UnobserveCommand< double >* command) try {
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  }
812 
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 
836  m_inf->marginal(command->chain, m);
837 
838  // Compute spent time
839  double t = timer.step();
840 
841  if (m_verbose) { m_log << "Finished." << std::endl; }
842 
843  if (m_verbose) {
844  m_log << "# Time in seconds (accuracy ~0.001): " << t << std::endl;
845  }
846 
847  // Show results
848 
849  if (m_verbose) { m_log << std::endl; }
850 
851  QueryResult result;
852  result.command = query;
853  result.time = t;
854 
855  Instantiation j(m);
856  const PRMAttribute< double >& attr = *(command->chain.second);
857 
858  for (j.setFirst(); !j.end(); j.inc()) {
859  // auto label_value = j.val ( attr.type().variable() );
860  auto label_value = j.val(0);
861  std::string label = attr.type().variable().label(label_value);
862  float value = float(m.get(j));
863 
864  SingleResult singleResult;
865  singleResult.label = label;
866  singleResult.p = value;
867 
868  result.values.push_back(singleResult);
869 
870  if (m_verbose) { m_log << label << " : " << value << std::endl; }
871  }
872 
873  m_results.push_back(result);
874 
875  if (m_verbose) { m_log << std::endl; }
876 
877  } catch (Exception& e) {
878  GUM_SHOWERROR(e);
879  throw "something went wrong while infering: " + e.errorContent();
880 
881  } catch (const std::string& msg) { addError(msg); }
882 
885  m_engine = command->value;
886  }
887 
890  m_bn_engine = command->value;
891  }
892 
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  }
933 
934  /* **************************************************************************
935  */
936 
939 
942 
945 
948  if (i >= count()) throw "Index out of bound.";
949 
950  return m_errors.error(i);
951  }
952 
955  return m_errors;
956  }
957 
959  void O3prmrInterpreter::showElegantErrors(std::ostream& o) const {
961  }
962 
966  }
967 
969  void O3prmrInterpreter::showErrorCounts(std::ostream& o) const {
971  }
972 
973  /* **************************************************************************
974  */
975 
977  void O3prmrInterpreter::addError(std::string msg) {
979 
980  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
981  }
982 
984  void O3prmrInterpreter::addWarning(std::string msg) {
986 
987  if (m_verbose) m_log << m_errors.last().toString() << std::endl;
988  }
989 
990  } // namespace o3prmr
991  } // namespace prm
992 } // namespace gum
void showElegantErrors(std::ostream &o=std::cerr) const
send on std::cerr the list of errors
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
const PRMSystem< GUM_SCALAR > * system
bool observe(const ObserveCommand< double > *command)
void addPath(std::string path)
Root paths to search from there packages. Default are &#39;./&#39; and one is calculate from request package ...
void generateInfEngine(const gum::prm::PRMSystem< double > &sys)
Cross-platform directory utility.
Definition: utils_dir.h:62
Potential< GUM_SCALAR > potentiel
const ImportCommand * mainImport() const
void addSession(const O3prmrSession< GUM_SCALAR > &session)
const gum::prm::PRM< double > * prm() const
Retrieve prm object.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:42
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
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...
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
void query(const QueryCommand< double > *command)
void addWarning(const std::string &msg, const std::string &filename, Idx line, Idx col)
Adds a warning.
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
Size count() const
Returns the number of errors and warnings.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
bool checkSetEngine(SetEngineCommand *command)
const PRMSystem< GUM_SCALAR > * system
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size error_count
Number of errors detected.
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
virtual PRMType & type()=0
See gum::PRMClassElement::type().
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
PRMInference< GUM_SCALAR >::Chain chain
std::vector< ParseError > errors
The list of gum::ParseError contained in this gum::ErrorsContainer.
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:110
bool checkSemantic(O3prmrContext< double > *context)
Check semantic validity of context.
STL namespace.
This class is used to represent parsing errors for the different parser implemented in aGrUM...
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
<agrum/BN/inference/variableElimination.h>
const ErrorsContainer & errorsContainer() const
publishing Errors API
void syntheticResults(std::ostream &o) const
Print errors on output stream.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
void add(ParseError error)
Add an error object to the container.
bool isVerboseMode() const
verbose mode show more details on the program execution. Default is false.
This class is used contain and manipulate gum::ParseError.
bool checkSetGndEngine(SetGndEngineCommand *command)
<agrum/BN/inference/marginalTargetedInference.h>
std::vector< ImportCommand *> imports() const
const gum::prm::PRMInference< double > * inference() const
Retrieve inference motor object.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::string toString() const
Return a std::string representation of this gum::ParseError.
bool checkQuery(QueryCommand< double > *command)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string aliasToImport(const std::string &alias)
bool checkUnobserve(UnobserveCommand< double > *command)
std::string findSystemName(std::string &s)
Idx val(Idx i) const
Returns the current value of the variable at position i.
void addImport(int line, const std::string &import, const std::string &alias)
void marginal(const Chain &chain, Potential< GUM_SCALAR > &m)
Compute the marginal of the formal attribute pointed by chain and stores it in m. ...
gum::prm::o3prm::O3prmReader< double > * m_reader
void reset()
Reset the timer.
Definition: timer_inl.h:32
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const PRMSystem< GUM_SCALAR > * system
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr) const
send on std::cerr the list of errors or warnings
Size errors() const
publishing Errors API
ParseError error(Idx i) const
throw a string error if i >= count
PRMInstance< GUM_SCALAR > & get(NodeId id)
Returns an PRMInstance given it&#39;s NodeId in the relational skeleton.
void inc()
Operator increment.
std::string toString() const
bool import(O3prmrContext< double > *context, std::string import)
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
void groundedBN(BayesNetFactory< GUM_SCALAR > &factory) const
Returns the grounded Bayesian Network of this system.
Definition: PRMSystem_tpl.h:82
This class is an implementation of the Structured Value Elimination algorithm on PRM<GUM_SCALAR>.
Definition: SVED.h:63
std::vector< SingleResult > values
bool isSystem(const std::string &name) const
Definition: PRM_tpl.h:89
PRMSystem< GUM_SCALAR > & getSystem(const std::string &name)
Returns a constant reference on a PRMSystem<GUM_SCALAR> given it&#39;s name.
Definition: PRM_tpl.h:146
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool interpret(O3prmrContext< double > *c)
Crée le prm correspondant au contexte courant.
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
PRMInference< GUM_SCALAR >::Chain chain
std::string findAttributeName(const std::string &s, const gum::prm::PRMInstance< double > &instance)
void elegantErrors(std::ostream &o) const
Print errors on output stream.
gum::prm::PRMInference< double > * m_inf
std::vector< std::string > m_paths
bool interpretLine(const std::string &line)
void clearPaths()
Root paths to search from there packages. Default are &#39;./&#39; and one is calculate from request package ...
void addClassPath(const std::string &class_path)
Add a list of paths to look for o3prm files.
void setSyntaxMode(bool f)
syntax mode don&#39;t process anything, just check syntax.
This is an abstract class.
Definition: O3prmrContext.h:50
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
ErrorsContainer errorsContainer() const
Return container with all errors.
bool interpretFile(const std::string &filename)
Interpret the file or the command line.
Represent a o3prmr context, with an import, and some sequencials commands.
std::vector< O3prmrSession< GUM_SCALAR > *> sessions() const
bool checkObserve(ObserveCommand< double > *command)
bool exists(const std::string &name) const
Retruns true either if name is an instance or an array in this PRMSystem.
std::string absolutePath() const
Returns directory absolute path.
Definition: utils_dir.cpp:91
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setGndEngine(const SetGndEngineCommand *command)
void setFirst()
Assign the first values to the tuple of the Instantiation.
void addEvidence(const Chain &chain, const Potential< GUM_SCALAR > &p)
Add an evidence to the given instance&#39;s elt.
void setEngine(const SetEngineCommand *command)
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...
static bool isDir(const std::string &path)
&brief Return true if directory is a valid directory, false otherwise.
Definition: utils_dir.cpp:35
Class used to compute response times for benchmark purposesThis class represents a classic timer...
Definition: timer.h:51
Size Idx
Type for indexes.
Definition: types.h:53
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...
~O3prmrInterpreter()
Destructor. Delete current context.
ParseError error(Idx i) const
Returns the i-th error.
bool unobserve(const UnobserveCommand< double > *command)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
bool isInSyntaxMode() const
syntax mode don&#39;t process anything, just check syntax. Default is false.
O3prmrContext< double > * getContext() const
Getter and setter for the context.
void showErrorCounts(std::ostream &o=std::cerr) const
send on std::cerr the number of errors and the number of warnings
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.
Size count() const
En cas d&#39;échec, l&#39;API de gestion d&#39;erreurs est présente.
PRMInference< GUM_SCALAR >::Chain chain
const std::string errorContent() const
Returns the message content.
Definition: exceptions.h:135
std::string __readFile(const std::string &file)
ParseError last() const
Returns the last added error.
HashTable< const PRMSystem< double > *, PRMInference< double > *> m_inf_map
void addError(const std::string &msg, const std::string &filename, Idx line, Idx col)
Adds an error.
const PRMSystem< double > & system(std::string &ident)
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.
void setContext(O3prmrContext< double > *context)
Setter for the context.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
<agrum/PRM/groundedInference.h>
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
This class is an implementation of the Structured Variable Elimination algorithm on PRM<GUM_SCALAR>...
Definition: SVE.h:66
A factory class to ease BayesNet construction.
Definition: BayesNet.h:45
O3prmrInterpreter()
This constructor create an empty context.
bool end() const
Returns true if the Instantiation reached the end.
std::vector< QueryResult > m_results
std::string findInstanceName(std::string &s, const gum::prm::PRMSystem< double > &sys)
void setVerboseMode(bool f)
verbose mode show more details on the program execution.
void addCommand(const O3prmrCommand *command)
O3prmrContext< double > * m_context