X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=blobdiff_plain;f=node.cpp;h=24da8b1dd60c36d9575151630ca3e990905b9ef2;hp=1fb2b2e3ee9448eeb50a7f0657e23f1082f72895;hb=HEAD;hpb=e26fd7b3195576b25ffe51253a391b53f412a650 diff --git a/node.cpp b/node.cpp index 1fb2b2e..24da8b1 100644 --- a/node.cpp +++ b/node.cpp @@ -1,13 +1,24 @@ #include "node.h" #include +#include 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; } -const Action& Node::action() const{ +const Action* Node::action() const{ return _action; } @@ -16,16 +27,58 @@ 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)); +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) + return _preconditions.size() == 0 && !_executed; +} + +void Node::execute(){ + if(_executed) + cerr << "Already executed " << _action->name() << endl; + if ((_preconditions.size() != 0) || _executed) return; - _action.execute(); - - for(vector::iterator child = _children.begin(); child != _children.end(); ++child){ - (*child)->execute(_action.effects()); - } + + _executed = true; + int value = _action->execute(); + _effects = _action->effects(value); + +} + +const std::vector& Node::children() const{ + return _children; +} +StartNode::StartNode(const Literals& init){ + EffectsMap initial; + initial[0] = init; + _action = new Action("start",Preconditions(),"", initial); + _executed = true; + _effects = init; } + +const Preconditions& Node::preconditions() const{ + return _preconditions; +} + +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(); +} +