knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
QueryTree.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_QUERY_TREE_H
7 #define KNOWROB_QUERY_TREE_H
8 
9 #include <memory>
10 #include <list>
11 #include "knowrob/formulas/Formula.h"
12 #include "knowrob/formulas/Predicate.h"
13 #include "knowrob/formulas/Conjunction.h"
14 #include "knowrob/formulas/Disjunction.h"
15 #include "knowrob/formulas/Implication.h"
16 #include "knowrob/formulas/Negation.h"
17 #include "knowrob/formulas/ModalFormula.h"
18 #include "knowrob/formulas/FirstOrderLiteral.h"
19 
20 namespace knowrob {
24  class QueryTree {
25  public:
26  explicit QueryTree(const FormulaPtr &query);
27 
29 
30  QueryTree(const QueryTree &) = delete;
31 
35  auto numPaths() const { return paths_.size(); }
36 
40  const auto &paths() const { return paths_; }
41 
45  auto begin() const { return paths_.begin(); }
46 
50  auto end() const { return paths_.end(); }
51 
55  class Node {
56  public:
58 
59  Node *parent;
60  const FormulaPtr formula;
61  bool isNegated;
62  bool isOpen;
63  std::list<Node *> successors;
64 
65  int priority() const;
66  };
67 
71  class Path {
72  public:
73  Path() = default;
74 
78  auto numNodes() const { return nodes_.size(); }
79 
83  const auto &nodes() const { return nodes_; }
84 
88  auto begin() const { return nodes_.begin(); }
89 
93  auto end() const { return nodes_.end(); }
94 
95  std::shared_ptr<Formula> toFormula() const;
96 
97  protected:
98  std::vector<FormulaPtr> nodes_;
99 
100  friend class QueryTree;
101  };
102 
103  protected:
104  const FormulaPtr query_;
105  std::list<Path> paths_;
106 
107  Node *rootNode_;
108 
109  struct NodeComparator {
110  bool operator()(const Node *a, const Node *b) const;
111  };
112 
113  std::priority_queue<Node *, std::vector<Node *>, NodeComparator> openNodes_;
114 
115  Node *createNode(Node *parent, const FormulaPtr &phi, bool isNegated);
116 
117  static std::list<QueryTree::Node *> getLeafs(Node *n);
118 
119  static bool hasCompletePath(Node *leaf);
120 
121  static void constructPath(Node *leaf, Path &path);
122 
124  };
125 
126 } // knowrob
127 
128 #endif //KNOWROB_QUERY_TREE_H
const FormulaPtr formula
Definition: QueryTree.h:60
std::list< Node * > successors
Definition: QueryTree.h:63
Node(Node *parent, FormulaPtr formula, bool isNegated)
std::vector< FormulaPtr > nodes_
Definition: QueryTree.h:98
auto begin() const
Definition: QueryTree.h:88
const auto & nodes() const
Definition: QueryTree.h:83
std::shared_ptr< Formula > toFormula() const
auto numNodes() const
Definition: QueryTree.h:78
static std::list< QueryTree::Node * > getLeafs(Node *n)
std::priority_queue< Node *, std::vector< Node * >, NodeComparator > openNodes_
Definition: QueryTree.h:113
auto end() const
Definition: QueryTree.h:50
static bool hasCompletePath(Node *leaf)
std::list< Path > paths_
Definition: QueryTree.h:105
Node * createNode(Node *parent, const FormulaPtr &phi, bool isNegated)
auto begin() const
Definition: QueryTree.h:45
QueryTree(const QueryTree &)=delete
QueryTree(const FormulaPtr &query)
const auto & paths() const
Definition: QueryTree.h:40
static void constructPath(Node *leaf, Path &path)
auto numPaths() const
Definition: QueryTree.h:35
const FormulaPtr query_
Definition: QueryTree.h:104
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
bool operator()(const Node *a, const Node *b) const