Skip to content

Commit e7fb0cd

Browse files
committed
Decomposition nodes now have reference to parent.
Gringo/Clasp now also gets parent bag as input.
1 parent 56a25a1 commit e7fb0cd

File tree

7 files changed

+36
-22
lines changed

7 files changed

+36
-22
lines changed

src/Decomposition.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ along with D-FLAT. If not, see <http://www.gnu.org/licenses/>.
2121
#include "Decomposition.h"
2222
#include "SolverFactory.h"
2323

24-
Decomposition::Decomposition(Node&& leaf, const SolverFactory& solverFactory)
25-
: DirectedAcyclicGraph(std::move(leaf))
24+
Decomposition::Decomposition(Node&& node, const SolverFactory& solverFactory)
25+
: DirectedAcyclicGraph(std::move(node))
2626
, solverFactory(solverFactory)
27-
, root(false)
27+
, parent(nullptr)
2828
, postJoinNode(false)
2929
{
3030
}
@@ -44,14 +44,9 @@ bool Decomposition::isJoinNode() const
4444
});
4545
}
4646

47-
void Decomposition::setRoot(bool root)
48-
{
49-
this->root = root;
50-
}
51-
5247
bool Decomposition::isRoot() const
5348
{
54-
return root;
49+
return parent == nullptr;
5550
}
5651

5752
void Decomposition::setPostJoinNode(bool postJoinNode)
@@ -64,6 +59,16 @@ bool Decomposition::isPostJoinNode() const
6459
return postJoinNode;
6560
}
6661

62+
const Decomposition* Decomposition::getParent() const
63+
{
64+
return parent;
65+
}
66+
67+
void Decomposition::setParent(const Decomposition* parent)
68+
{
69+
this->parent = parent;
70+
}
71+
6772
int Decomposition::getWidth() const
6873
{
6974
int width = node.getBag().size() - 1;

src/Decomposition.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ class Decomposition : public DirectedAcyclicGraph<DecompositionNode, std::vector
4343
// Returns true if the node is a join node, i.e., has at least two children and all children have equal bags
4444
bool isJoinNode() const;
4545

46-
// Mark this node to be the root.
47-
// Only do this if this is not a child of any node.
48-
void setRoot(bool root = true);
49-
50-
// Returns true if setRoot(true) has been called.
46+
// Returns true if setParent() has not been called (with non-null argument)
5147
bool isRoot() const;
5248

5349
// Mark this node to be a node added for post-processing join nodes.
@@ -56,6 +52,9 @@ class Decomposition : public DirectedAcyclicGraph<DecompositionNode, std::vector
5652
// Returns true if setPostJoinNode(true) has been called.
5753
bool isPostJoinNode() const;
5854

55+
const Decomposition* getParent() const;
56+
void setParent(const Decomposition*);
57+
5958
// Traverses the decomposition and returns its width
6059
int getWidth() const;
6160

@@ -67,6 +66,6 @@ class Decomposition : public DirectedAcyclicGraph<DecompositionNode, std::vector
6766

6867
const SolverFactory& solverFactory;
6968
std::unique_ptr<Solver> solver;
70-
bool root;
69+
const Decomposition* parent;
7170
bool postJoinNode;
7271
};

src/asp_utils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ void declareDecomposition(const Decomposition& decomposition, std::ostream& out)
4646

4747
if(decomposition.isRoot())
4848
out << "final." << std::endl;
49+
else {
50+
assert(decomposition.getParent() != nullptr);
51+
const Decomposition& parent = *decomposition.getParent();
52+
out << "parentNode(" << parent.getNode().getGlobalId() << ")." << std::endl;
53+
for(const auto& v : parent.getNode().getBag())
54+
out << "bag(" << parent.getNode().getGlobalId() << ',' << v << ")." << std::endl;
55+
}
4956

5057
if(decomposition.isPostJoinNode())
5158
out << "postJoin." << std::endl;

src/decomposer/Dummy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ DecompositionPtr Dummy::decompose(const Instance& instance) const
3939
vertices.insert(arguments.begin(), arguments.end());
4040

4141
DecompositionPtr result(new Decomposition(vertices, app.getSolverFactory()));
42-
result->setRoot();
4342
return result;
4443
}
4544

src/decomposer/GraphMl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace {
6161
DecompositionNode::Bag bag = decomposition.getNode().getBag();
6262
bag.erase(bag.begin());
6363
DecompositionPtr child(new Decomposition(bag, app.getSolverFactory()));
64+
child->setParent(&decomposition);
6465
decomposition.addChild(std::move(child));
6566
}
6667

@@ -74,6 +75,7 @@ namespace {
7475
DecompositionNode::Bag bag = oldRoot->getNode().getBag();
7576
bag.erase(bag.begin());
7677
DecompositionPtr newNode(new Decomposition(bag, app.getSolverFactory()));
78+
oldRoot->setParent(newNode.get());
7779
newNode->addChild(std::move(oldRoot));
7880
return addEmptyRoot(std::move(newNode), app);
7981
}
@@ -140,6 +142,7 @@ DecompositionPtr GraphMl::decompose(const Instance& instance) const
140142
const DecompositionPtr to = nodes.top();
141143
nodes.pop();
142144
const DecompositionPtr& from = nodes.top();
145+
to->setParent(from.get());
143146
from->addChild(std::move(to));
144147
}
145148
else
@@ -155,7 +158,6 @@ DecompositionPtr GraphMl::decompose(const Instance& instance) const
155158
if(optAddEmptyRoot.isUsed())
156159
root = addEmptyRoot(std::move(root), app);
157160

158-
root->setRoot();
159161
return root;
160162
}
161163

src/decomposer/TreeDecomposer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ namespace {
147147
Decomposition* parentOrPostJoinNode = parent.get();
148148

149149
DecompositionPtr child{new Decomposition{childBag, app.getSolverFactory()}};
150+
child->setParent(parentOrPostJoinNode);
150151
parentOrPostJoinNode->addChild(child);
151152
stack.push({htdChild, child});
152153
}
@@ -275,7 +276,6 @@ DecompositionPtr TreeDecomposer::decompose(const Instance& instance) const
275276

276277
// Transform htd's tree decomposition into our format
277278
DecompositionPtr result = transformTd(*decomposition, graph, app);
278-
result->setRoot();
279279
return result;
280280
}
281281

test/Decomposition.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ TEST_F(DecompositionTest, IdentifiesJoinNodes)
7272
EXPECT_FALSE(child->isJoinNode());
7373
}
7474

75-
TEST_F(DecompositionTest, IsNotRootByDefault)
75+
TEST_F(DecompositionTest, SetsParent)
7676
{
7777
Decomposition d = {DecompositionNode{{}}, solverFactory};
78-
EXPECT_FALSE(d.isRoot());
79-
d.setRoot();
80-
EXPECT_TRUE(d.isRoot());
78+
EXPECT_EQ(nullptr, d.getParent());
79+
DecompositionPtr c{new Decomposition{DecompositionNode{{}}, solverFactory}};
80+
c->setParent(&d);
81+
d.addChild(std::move(c));
82+
EXPECT_EQ(&d, (*d.getChildren().begin())->getParent());
8183
}

0 commit comments

Comments
 (0)