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