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