knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Conjunction.cpp
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 #include <iostream>
7 #include <knowrob/formulas/Conjunction.h>
8 #include "knowrob/integration/python/utils.h"
9 #include "knowrob/formulas/SimpleConjunction.h"
10 
11 using namespace knowrob;
12 
13 Conjunction::Conjunction(const std::vector<FormulaPtr> &formulae)
15 }
16 
17 bool Conjunction::isEqual(const Formula &other) const {
18  const auto &x = static_cast<const Conjunction &>(other); // NOLINT
19  if (formulae_.size() != x.formulae_.size()) return false;
20  for (auto &y1: formulae_) {
21  bool hasEqual = false;
22  for (auto &y2: x.formulae_) {
23  if (*y1 == *y2) {
24  hasEqual = true;
25  break;
26  }
27  }
28  if (!hasEqual) return false;
29  }
30  return true;
31 }
32 
33 namespace knowrob {
34  FormulaPtr operator&(const FormulaPtr &phi, const FormulaPtr &psi) {
35  if (phi->isBottom()) return phi;
36  else if (psi->isBottom()) return psi;
37  else if (phi->isTop()) return psi;
38  else if (psi->isTop()) return phi;
39  else if (phi->type() == FormulaType::CONJUNCTION) {
40  auto formulae = ((CompoundFormula *) phi.get())->formulae();
41  if (psi->type() == FormulaType::CONJUNCTION) {
42  auto &psi0 = ((CompoundFormula *) psi.get())->formulae();
43  formulae.insert(formulae.end(), psi0.begin(), psi0.end());
44  } else {
45  formulae.push_back(psi);
46  }
47  return std::make_shared<Conjunction>(formulae);
48  } else if (psi->type() == FormulaType::CONJUNCTION) {
49  auto &psi0 = ((CompoundFormula *) psi.get())->formulae();
50  std::vector<FormulaPtr> formulae = {phi};
51  formulae.insert(formulae.end(), psi0.begin(), psi0.end());
52  return std::make_shared<Conjunction>(formulae);
53  } else {
54  return std::make_shared<Conjunction>(std::vector<FormulaPtr>({phi, psi}));
55  }
56  }
57 }
58 
59 namespace knowrob::py {
60  template<>
62  using namespace boost::python;
63  class_<Conjunction, std::shared_ptr<Conjunction>, bases<CompoundFormula>>
64  ("Conjunction", init<const std::vector<FormulaPtr> &>());
65 
67  }
68 }
const std::vector< FormulaPtr > formulae_
Conjunction(const std::vector< FormulaPtr > &formulae)
Definition: Conjunction.cpp:13
bool isEqual(const Formula &other) const override
Definition: Conjunction.cpp:17
void createType< SimpleConjunction >()
void createType< Conjunction >()
Definition: Conjunction.cpp:61
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
FormulaType
Definition: Formula.h:17
FormulaPtr operator&(const FormulaPtr &phi, const FormulaPtr &psi)
Definition: Conjunction.cpp:34