]> ruin.nu Git - popboot.git/blob - node.cpp
seems to be working now
[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 void Node::execute(const Literals& effects){
39         cerr << "Executing: " << _action.name() << endl;
40         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
41                 cerr << "Satisfied effect: " << *effect << endl;
42                 _preconditions.erase(_preconditions.find(*effect));
43         }
44         cerr << "Number of preconditions left: " << _preconditions.size() << endl;
45         if(_executed)
46                 cerr << "Already executed" << endl;
47         if ((_preconditions.size() != 0) || _executed)
48                 return;
49
50         _executed = true;
51         int value = _action.execute();
52         _effects = _action.effects(value);
53
54         cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl;
55
56         for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
57                 (*child)->execute(_effects);
58         }
59 }
60
61 StartNode::StartNode(const Literals& init){
62         EffectsMap initial;
63         initial[0] = init;
64         _action = Action("start",Preconditions(),"", initial);
65 }
66
67 EndNode::EndNode(const Literals& goal){
68         Preconditions goalState;
69         for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
70                 goalState[*g] = true;
71         _action = Action("finish",goalState,"",EffectsMap());
72         _preconditions = _action.preconditions();
73 }
74