]> ruin.nu Git - popboot.git/blobdiff - node.cpp
Spelling fix in inittab
[popboot.git] / node.cpp
index acc5bd7090144cf31ffd4b536e2a5a68f1a72f18..24da8b1dd60c36d9575151630ca3e990905b9ef2 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -1,10 +1,11 @@
 #include "node.h"
 #include <algorithm>
+#include <iostream>
 using namespace std;
 
-Node::Node(const Action& action){
+Node::Node(const Action* action){
        _action = action;
-       _preconditions = _action.preconditions();
+       _preconditions = _action->preconditions();
        _executed = false;
 }
 Node::Node(){
@@ -17,7 +18,7 @@ Node::Node(const Node& node){
        _executed = node._executed;
 }
 
-const Action& Node::action() const{
+const Action* Node::action() const{
        return _action;
 }
 
@@ -30,38 +31,54 @@ bool Node::executed() const{
        return _executed;
 }
 
-const literals& Node::effects() const{
+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));
+bool Node::satisfyCondition(std::string effect){
+       _preconditions.erase(_preconditions.find(effect));
+       return _preconditions.size() == 0 && !_executed;
+}
+
+bool Node::satisfyConditions(const Literals& effects){
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               _preconditions.erase(_preconditions.find(*effect));
        }
-       if (_preconditions.size() != 0 || _executed)
+       return _preconditions.size() == 0 && !_executed;
+}
+
+void Node::execute(){
+       if(_executed)
+               cerr << "Already executed " << _action->name() << endl;
+       if ((_preconditions.size() != 0) || _executed)
                return;
 
        _executed = true;
-       int value = _action.execute();
-       _effects = _action.effects(value);
+       int value = _action->execute();
+       _effects = _action->effects(value);
 
-       for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
-               (*child)->execute(effects);
-       }
 }
 
-StartNode::StartNode(const literals& init){
-       effectsMap initial;
+const std::vector<Node*>& Node::children() const{
+       return _children;
+}
+StartNode::StartNode(const Literals& init){
+       EffectsMap initial;
        initial[0] = init;
-       Node(Action("start",preconditionsVector(), initial));
+       _action = new Action("start",Preconditions(),"", initial);
+       _executed = true;
+       _effects = init;
+}
+
+const Preconditions& Node::preconditions() const{
+       return _preconditions;
 }
 
-EndNode::EndNode(const literals& goal){
-       preconditionsVector goalState;
-       pair<string,literals> goalPair;
-       goalPair.first = "";
-       goalPair.second = goal;
-       goalState.push_back(goalPair);
-       Node(Action("finish",goalState,effectsMap()));
+EndNode::EndNode(const Literals& goal){
+       Preconditions goalState;
+       for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
+               goalState[*g] = false;
+       _action = new Action("finish",goalState,"",EffectsMap());
+       _preconditions = _action->preconditions();
 }