]> ruin.nu Git - popboot.git/blobdiff - node.cpp
delete remaining actions
[popboot.git] / node.cpp
index dbb099cf47bc5c30fe14b03fe83aad6cd5d53ca9..b4d22fb9569c28cea70176abce6ce585c2b8b8c1 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -1,13 +1,24 @@
 #include "node.h"
 #include <algorithm>
+#include <iostream>
 using namespace std;
 
-Node::Node(Action action){
+Node::Node(const Action* action){
        _action = action;
-       _preconditions = _action.preconditions();
+       _preconditions = _action->preconditions();
+       _executed = false;
+}
+Node::Node(){
+       _executed = false;
+}
+
+Node::Node(const Node& node){
+       _action = node._action;
+       _preconditions = node._preconditions;
+       _executed = node._executed;
 }
 
-Action Node::action(){
+const Action* Node::action() const{
        return _action;
 }
 
@@ -16,16 +27,51 @@ void Node::addChild(Node* node){
        _children.push_back(node);
 }
 
+bool Node::executed() const{
+       return _executed;
+}
+
+const Literals& Node::effects() const{
+       return _effects;
+}
+void Node::satisfyCondition(std::string effect){
+       _preconditions.erase(_preconditions.find(effect));
+}
 
-void Node::execute(literals effects){
-       for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
-               _preconditions.erase(find(_preconditions.begin(),_preconditions.end(), *effect));
+void Node::execute(const Literals& effects){
+       cerr << "Executing: " << _action->name() << endl;
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               cerr << "Satisfied effect: " << *effect << endl;
+               _preconditions.erase(_preconditions.find(*effect));
        }
-       if (_preconditions.size() != 0)
+       cerr << "Number of preconditions left: " << _preconditions.size() << endl;
+       if(_executed)
+               cerr << "Already executed" << endl;
+       if ((_preconditions.size() != 0) || _executed)
                return;
-       _action.execute();
-       
+
+       _executed = true;
+       int value = _action->execute();
+       _effects = _action->effects(value);
+
+       cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl;
+
        for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
-               (*child)->execute(_action.effects());
+               (*child)->execute(_effects);
        }
 }
+
+StartNode::StartNode(const Literals& init){
+       EffectsMap initial;
+       initial[0] = init;
+       _action = new Action("start",Preconditions(),"", initial);
+}
+
+EndNode::EndNode(const Literals& goal){
+       Preconditions goalState;
+       for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
+               goalState[*g] = true;
+       _action = new Action("finish",goalState,"",EffectsMap());
+       _preconditions = _action->preconditions();
+}
+