]> ruin.nu Git - popboot.git/blob - node.cpp
Spelling fix in inittab
[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                 _preconditions.erase(_preconditions.find(*effect));
46         }
47         return _preconditions.size() == 0 && !_executed;
48 }
49
50 void Node::execute(){
51         if(_executed)
52                 cerr << "Already executed " << _action->name() << endl;
53         if ((_preconditions.size() != 0) || _executed)
54                 return;
55
56         _executed = true;
57         int value = _action->execute();
58         _effects = _action->effects(value);
59
60 }
61
62 const std::vector<Node*>& Node::children() const{
63         return _children;
64 }
65 StartNode::StartNode(const Literals& init){
66         EffectsMap initial;
67         initial[0] = init;
68         _action = new Action("start",Preconditions(),"", initial);
69         _executed = true;
70         _effects = init;
71 }
72
73 const Preconditions& Node::preconditions() const{
74         return _preconditions;
75 }
76
77 EndNode::EndNode(const Literals& goal){
78         Preconditions goalState;
79         for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
80                 goalState[*g] = false;
81         _action = new Action("finish",goalState,"",EffectsMap());
82         _preconditions = _action->preconditions();
83 }
84