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