]> ruin.nu Git - popboot.git/blobdiff - node.cpp
small change
[popboot.git] / node.cpp
index 1fb2b2e3ee9448eeb50a7f0657e23f1082f72895..1efdf6a03199e01f882e27e4d4d7de499f01a27b 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -2,9 +2,19 @@
 #include <algorithm>
 using namespace std;
 
-Node::Node(Action action){
+Node::Node(const Action& action){
        _action = action;
        _preconditions = _action.preconditions();
+       _executed = false;
+}
+Node::Node(){
+       _executed = false;
+}
+
+Node::Node(const Node& node){
+       _action = node._action;
+       _preconditions = node._preconditions;
+       _executed = node._executed;
 }
 
 const Action& Node::action() const{
@@ -16,16 +26,48 @@ void Node::addChild(Node* node){
        _children.push_back(node);
 }
 
+bool Node::executed() const{
+       return _executed;
+}
+
+const Literals& Node::effects() const{
+       return _effects;
+}
 
-void Node::execute(const literals& effects){
-       for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
-               _preconditions.erase(find(_preconditions.begin(),_preconditions.end(), *effect));
+void Node::execute(const Literals& effects){
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               _preconditions.erase(_preconditions.find(*effect));
        }
-       if (_preconditions.size() != 0)
+       if ((_preconditions.size() != 0) || _executed)
                return;
-       _action.execute();
-       
+
+       if (_preconditions.size() != 0){
+               for (Preconditions::iterator precond = _preconditions.begin(); precond != _preconditions.end(); ++precond){
+                       if (precond->second)
+                               return;
+               }
+       }
+
+       _executed = true;
+       int value = _action.execute();
+       _effects = _action.effects(value);
+
        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 = 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 = Action("finish",goalState,"",EffectsMap());
+       _preconditions = _action.preconditions();
+}
+