knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
DependencyGraph.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_DEPENDENCY_GRAPH_H
7 #define KNOWROB_DEPENDENCY_GRAPH_H
8 
9 #include <list>
10 #include "knowrob/formulas/FirstOrderLiteral.h"
11 
12 namespace knowrob {
16  class DependencyNode {
17  public:
19 
23  const std::set<std::string_view> &variables() const { return literal_->predicate()->variables(); }
24 
28  auto numVariables() const { return variables().size(); }
29 
33  auto numNeighbors() const { return neighbors_.size(); }
34 
38  const auto &neighbors() const { return neighbors_; }
39 
40  void addDependency(const std::shared_ptr<DependencyNode> &other);
41 
45  const auto &literal() const { return literal_; }
46 
47  protected:
48  std::list<std::shared_ptr<DependencyNode>> neighbors_;
50 
51  friend class DependencyGraph;
52  };
53 
54  using DependencyNodePtr = std::shared_ptr<DependencyNode>;
55 
60  struct DependencyGroup {
61  std::set<std::string_view> variables_;
62  std::list<DependencyNodePtr> member_;
63 
64  void operator+=(const DependencyGroup &other) {
65  member_.insert(other.member_.end(), other.member_.begin(), other.member_.end());
66  variables_.insert(other.variables_.begin(), other.variables_.end());
67  }
68  };
69 
75  class DependencyGraph {
76  public:
77  DependencyGraph() = default;
78 
80 
81  DependencyGraph(const DependencyGraph &other) = delete;
82 
87  void operator+=(const DependencyNodePtr &node);
88 
94  void insert(const DependencyNodePtr &node);
95 
101  void insert(const FirstOrderLiteralPtr &literal);
102 
108  void insert(const std::vector<FirstOrderLiteralPtr> &literals);
109 
116  template<typename Iterator>
117  void insert(Iterator begin, Iterator end) {
118  while (begin != end) {
119  auto next = *begin;
120  insert(next);
121  ++begin;
122  }
123  }
124 
128  auto begin() const { return groups_.begin(); }
129 
133  auto end() const { return groups_.end(); }
134 
138  auto numNodes() const { return nodes_.size(); }
139 
143  auto numGroups() const { return groups_.size(); }
144 
145  protected:
146  std::list<DependencyNodePtr> nodes_;
147  std::list<DependencyGroup> groups_;
148  };
149 
150 } // knowrob
151 
152 #endif //KNOWROB_DEPENDENCY_GRAPH_H
void insert(const std::vector< FirstOrderLiteralPtr > &literals)
void insert(const DependencyNodePtr &node)
std::list< DependencyGroup > groups_
void insert(const DependencyNodePtr &node)
void insert(Iterator begin, Iterator end)
DependencyGraph(const DependencyGraph &other)=delete
void operator+=(const DependencyNodePtr &node)
void insert(const FirstOrderLiteralPtr &literal)
std::list< DependencyNodePtr > nodes_
const FirstOrderLiteralPtr literal_
const auto & neighbors() const
void addDependency(const std::shared_ptr< DependencyNode > &other)
std::list< std::shared_ptr< DependencyNode > > neighbors_
const std::set< std::string_view > & variables() const
const auto & literal() const
DependencyNode(const FirstOrderLiteralPtr &literal)
std::shared_ptr< DependencyNode > DependencyNodePtr
std::shared_ptr< FirstOrderLiteral > FirstOrderLiteralPtr
std::list< DependencyNodePtr > member_
void operator+=(const DependencyGroup &other)
std::set< std::string_view > variables_