]> ruin.nu Git - popboot.git/blob - node.cpp
b6793f6c73b804b78101eca68389e6d49db3e4e4
[popboot.git] / node.cpp
1 #include "node.h"
2 #include <algorithm>
3 #include <iostream>
4 using namespace std;
5
6 Node::Node(const Action* action){
7         _action = action;
8         _preconditions = _action->preconditions();
9         _executed = false;
10 }
11 Node::Node(){
12         _executed = false;
13 }
14
15 Node::Node(const Node& node){
16         _action = node._action;
17         _preconditions = node._preconditions;
18         _executed = node._executed;
19 }
20
21 const Action* Node::action() const{
22         return _action;
23 }
24
25
26 void Node::addChild(Node* node){
27         _children.push_back(node);
28 }
29
30 bool Node::executed() const{
31         return _executed;
32 }
33
34 const Literals& Node::effects() const{
35         return _effects;
36 }
37
38 bool Node::satisfyCondition(std::string effect){
39         _preconditions.erase(_preconditions.find(effect));
40         return _preconditions.size() == 0 && !_executed;
41 }
42
43 bool Node::satisfyConditions(const Literals& effects){
44         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
45                 cerr << "Satisfied effect: " << *effect << endl;
46                 _preconditions.erase(_preconditions.find(*effect));
47         }
48         cerr << "Number of preconditions left: " << _preconditions.size() << endl;
49         return _preconditions.size() == 0 && !_executed;
50 }
51
52 void Node::execute(){
53         cerr << "Executing: " << _action->name() << endl;
54         if(_executed)
55                 cerr << "Already executed" << endl;
56         if ((_preconditions.size() != 0) || _executed)
57                 return;
58
59         _executed = true;
60         int value = _action->execute();
61         _effects = _action->effects(value);
62
63         cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl;
64
65 }
66
67 const std::vector<Node*>& Node::children() const{
68         return _children;
69 }
70 StartNode::StartNode(const Literals& init){
71         EffectsMap initial;
72         initial[0] = init;
73         _action = new Action("start",Preconditions(),"", initial);
74 }
75
76 EndNode::EndNode(const Literals& goal){
77         Preconditions goalState;
78         for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
79                 goalState[*g] = false;
80         _action = new Action("finish",goalState,"",EffectsMap());
81         _preconditions = _action->preconditions();
82 }
83