X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=node.cpp;h=acc5bd7090144cf31ffd4b536e2a5a68f1a72f18;hb=3dadaa088d9fff7ca05cbb297f3d7e88179faccb;hp=4f81069e111eaf55e603c2bfd26b9a312eef216d;hpb=b177aeec472c3941b39b874a83883ff87a41919c;p=popboot.git diff --git a/node.cpp b/node.cpp index 4f81069..acc5bd7 100644 --- a/node.cpp +++ b/node.cpp @@ -1,10 +1,23 @@ #include "node.h" +#include +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; } -Action Node::action(){ +const Action& Node::action() const{ return _action; } @@ -13,3 +26,42 @@ 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)); + } + if (_preconditions.size() != 0 || _executed) + return; + + _executed = true; + int value = _action.execute(); + _effects = _action.effects(value); + + for(vector::iterator child = _children.begin(); child != _children.end(); ++child){ + (*child)->execute(effects); + } +} + +StartNode::StartNode(const literals& init){ + effectsMap initial; + initial[0] = init; + Node(Action("start",preconditionsVector(), initial)); +} + +EndNode::EndNode(const literals& goal){ + preconditionsVector goalState; + pair goalPair; + goalPair.first = ""; + goalPair.second = goal; + goalState.push_back(goalPair); + Node(Action("finish",goalState,effectsMap())); +} +