#include "node.h"
+#include <algorithm>
+using namespace std;
-Node::Node(Action action, std::vector<Node&> 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;
+}
+
+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<Node*>::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<string,literals> goalPair;
+ goalPair.first = "";
+ goalPair.second = goal;
+ goalState.push_back(goalPair);
+ Node(Action("finish",goalState,effectsMap()));
+}
+