X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=node.cpp;h=acc5bd7090144cf31ffd4b536e2a5a68f1a72f18;hb=3dadaa088d9fff7ca05cbb297f3d7e88179faccb;hp=11fed08507b52bfe5953a1558aed3c3e2be6818a;hpb=dc49c0c521090f0eb4b9692b25a129537c07e19e;p=popboot.git diff --git a/node.cpp b/node.cpp index 11fed08..acc5bd7 100644 --- a/node.cpp +++ b/node.cpp @@ -1,5 +1,67 @@ #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; +} + +const Action& Node::action() const{ + return _action; +} + + +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())); +} +