X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=node.cpp;h=b6793f6c73b804b78101eca68389e6d49db3e4e4;hb=3f82360c5e0c61eccacdbaf3bd077852b9326f34;hp=a9cb4539431d6d1f959339f9e042d867ba68fb74;hpb=066d3c3659f52a73a0344c3e08989cc73a15164b;p=popboot.git diff --git a/node.cpp b/node.cpp index a9cb453..b6793f6 100644 --- a/node.cpp +++ b/node.cpp @@ -1,6 +1,83 @@ #include "node.h" +#include +#include +using namespace std; -Node::Node(Action action, std::vector children){ +Node::Node(const Action* action){ _action = action; - _children = children; + _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; +} + +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){ + cerr << "Satisfied effect: " << *effect << endl; + _preconditions.erase(_preconditions.find(*effect)); + } + cerr << "Number of preconditions left: " << _preconditions.size() << endl; + return _preconditions.size() == 0 && !_executed; +} + +void Node::execute(){ + cerr << "Executing: " << _action->name() << endl; + if(_executed) + cerr << "Already executed" << endl; + if ((_preconditions.size() != 0) || _executed) + return; + + _executed = true; + int value = _action->execute(); + _effects = _action->effects(value); + + cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl; + +} + +const std::vector& Node::children() const{ + return _children; +} +StartNode::StartNode(const Literals& init){ + EffectsMap initial; + initial[0] = init; + _action = new Action("start",Preconditions(),"", initial); +} + +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(); +} +