6 #include <gtest/gtest.h>
7 #include "knowrob/formulas/DependencyGraph.h"
8 #include "knowrob/queries/QueryParser.h"
12 static inline bool has_intersection(
const std::set<std::string_view> &a,
const std::set<std::string_view> &b)
14 const std::set<std::string_view> &smaller = (a.size()<b.size() ? a : b);
15 const std::set<std::string_view> &larger = ((&smaller == &a) ? b : a);
16 return std::any_of(smaller.cbegin(), smaller.cend(),
17 [larger](
auto &v){ return larger.count(v)>0; });
22 for (
auto &node :
nodes_) {
23 node->neighbors_.clear();
36 for(
auto &l : literals)
37 insert(std::make_shared<DependencyNode>(l));
42 insert(std::make_shared<DependencyNode>(literal));
50 std::list<std::list<DependencyGroup>::iterator > dependencies;
52 if(has_intersection(it->variables_, newNode->variables())) {
53 dependencies.push_back(it);
58 if(dependencies.empty()) {
61 auto &newGroup =
groups_.emplace_back();
62 newNodeGroup = &newGroup;
64 else if(dependencies.size()==1) {
66 auto &group = *dependencies.front();
67 newNodeGroup = &group;
71 auto &newGroup =
groups_.emplace_back();
72 for(
auto groupIterator : dependencies) {
73 newGroup += *groupIterator;
76 newNodeGroup = &newGroup;
79 for(
auto &x : newNodeGroup->
member_) {
80 if(has_intersection(x->variables(), newNode->variables())) {
81 x->neighbors_.push_back(newNode);
82 newNode->neighbors_.push_back(x);
86 newNodeGroup->
member_.push_back(newNode);
87 newNodeGroup->
variables_.insert(newNode->variables().begin(), newNode->variables().end());
std::list< DependencyGroup > groups_
void insert(const DependencyNodePtr &node)
void operator+=(const DependencyNodePtr &node)
std::list< DependencyNodePtr > nodes_
void addDependency(const std::shared_ptr< DependencyNode > &other)
std::list< std::shared_ptr< DependencyNode > > neighbors_
DependencyNode(const FirstOrderLiteralPtr &literal)
std::shared_ptr< DependencyNode > DependencyNodePtr
std::shared_ptr< FirstOrderLiteral > FirstOrderLiteralPtr
std::list< DependencyNodePtr > member_
std::set< std::string_view > variables_