knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
PrologTests.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_PROLOG_TESTS_H_
7 #define KNOWROB_PROLOG_TESTS_H_
8 
9 #include <gtest/gtest.h>
10 
11 #include "PrologReasoner.h"
12 #include "knowrob/reasoner/ReasonerManager.h"
13 #include "knowrob/Logger.h"
14 #include "knowrob/knowrob.h"
15 
16 namespace knowrob {
20  class PrologTestsBase : public testing::Test {
21  protected:
22  static void runPrologTests(
23  const std::shared_ptr<knowrob::PrologReasoner> &reasoner,
24  const std::string &target);
25  };
26 
27  template<class ReasonerType, class BackendType>
28  class PrologTests : public PrologTestsBase {
29  protected:
30  static std::shared_ptr<ReasonerType> reasoner_;
31  static std::shared_ptr<KnowledgeBase> kb_;
32  static std::shared_ptr<BackendType> db_;
33 
34  static std::shared_ptr<BackendType> createBackend(const std::string &name, const std::shared_ptr<KnowledgeBase> &kb) {
35  auto db = std::make_shared<BackendType>();
36  kb->backendManager()->addPlugin(name, PluginLanguage::CPP, db);
37  db->initializeBackend(knowrob::PropertyTree());
38  return db;
39  }
40 
41  static std::shared_ptr<ReasonerType> createReasoner(const std::string &name, const std::shared_ptr<KnowledgeBase> &kb, const std::shared_ptr<BackendType> &db) {
42  auto r = std::make_shared<ReasonerType>();
43  kb->reasonerManager()->addPlugin(name, PluginLanguage::CPP, r);
44  r->setStorage(db);
45  r->initializeReasoner(knowrob::PropertyTree());
46  return r;
47  }
48 
49  // Per-test-suite set-up.
50  static void SetUpTestSuite() {
51  // Initialize the reasoner
52  try {
53  reasoner();
54  } catch (std::exception &e) {
55  FAIL() << "SetUpTestSuite failed: " << e.what();
56  }
57  }
58 
59  // Per-test-suite tear-down.
60  static void TearDownTestSuite() {
61  kb_ = nullptr;
62  db_ = nullptr;
63  reasoner_ = nullptr;
64  }
65 
66  static void runTests(const std::string &t) {
67  try {
69  } catch (std::exception &e) {
70  FAIL() << "runTests failed: " << e.what();
71  }
72  }
73 
74  static std::shared_ptr<ReasonerType> reasoner() {
75  if(!reasoner_) {
76  std::stringstream ss;
77  ss << "prolog_";
78  insertUnique(ss);
79 
81  db_ = createBackend(ss.str(), kb_);
82  reasoner_ = createReasoner(ss.str(), kb_, db_);
83 
84  kb_->loadCommon();
85  kb_->init();
86  }
87  return reasoner_;
88  }
89  };
90 
91  template <class ReasonerType, class BackendType>
92  std::shared_ptr<ReasonerType> PrologTests<ReasonerType,BackendType>::reasoner_;
93 
94  template <class ReasonerType, class BackendType>
95  std::shared_ptr<KnowledgeBase> PrologTests<ReasonerType,BackendType>::kb_;
96 
97  template <class ReasonerType, class BackendType>
98  std::shared_ptr<BackendType> PrologTests<ReasonerType,BackendType>::db_;
99 }
100 
101 #endif //KNOWROB_PROLOG_TESTS_H_
static std::shared_ptr< KnowledgeBase > create()
static void runPrologTests(const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
Definition: PrologTests.cpp:23
static void runPrologTests(const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
static std::shared_ptr< ReasonerType > reasoner()
Definition: PrologTests.h:74
static void TearDownTestSuite()
Definition: PrologTests.h:60
static void runTests(const std::string &t)
Definition: PrologTests.h:66
static std::shared_ptr< BackendType > db_
Definition: PrologTests.h:32
static void SetUpTestSuite()
Definition: PrologTests.h:50
static std::shared_ptr< BackendType > createBackend(const std::string &name, const std::shared_ptr< KnowledgeBase > &kb)
Definition: PrologTests.h:34
static std::shared_ptr< ReasonerType > reasoner_
Definition: PrologTests.h:30
static std::shared_ptr< KnowledgeBase > kb_
Definition: PrologTests.h:31
static std::shared_ptr< ReasonerType > createReasoner(const std::string &name, const std::shared_ptr< KnowledgeBase > &kb, const std::shared_ptr< BackendType > &db)
Definition: PrologTests.h:41
TermRule & string()
Definition: terms.cpp:63
void insertUnique(std::ostream &os)
Definition: knowrob.cpp:44