]> ruin.nu Git - popboot.git/blob - node.cpp
acc5bd7090144cf31ffd4b536e2a5a68f1a72f18
[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(find(_preconditions.begin(),_preconditions.end(), *effect));
40         }
41         if (_preconditions.size() != 0 || _executed)
42                 return;
43
44         _executed = true;
45         int value = _action.execute();
46         _effects = _action.effects(value);
47
48         for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
49                 (*child)->execute(effects);
50         }
51 }
52
53 StartNode::StartNode(const literals& init){
54         effectsMap initial;
55         initial[0] = init;
56         Node(Action("start",preconditionsVector(), initial));
57 }
58
59 EndNode::EndNode(const literals& goal){
60         preconditionsVector goalState;
61         pair<string,literals> goalPair;
62         goalPair.first = "";
63         goalPair.second = goal;
64         goalState.push_back(goalPair);
65         Node(Action("finish",goalState,effectsMap()));
66 }
67