]> ruin.nu Git - popboot.git/commitdiff
some done, need parser
authorMichael Andreen <harv@ruin.nu>
Tue, 17 May 2005 12:35:24 +0000 (12:35 +0000)
committerMichael Andreen <harv@ruin.nu>
Tue, 17 May 2005 12:35:24 +0000 (12:35 +0000)
action.cpp
action.h
node.cpp
node.h
planner.cpp
planner.h

index 7b7a58807f5536e436e615a82ac19b2a2373831d..87947ca054bb6684bfe9e23e006a16ff94df7c6c 100644 (file)
@@ -3,13 +3,13 @@
 using namespace std;
 using namespace __gnu_cxx;
 
-const literals Action::_empty;
+const Literals Action::_empty;
 
-Action::Action(std::string name, preconditionsVector preconditions, effectsMap effects){
+Action::Action(std::string name,const Preconditions& preconditions, std::string executable,const EffectsMap& effects){
        _name = name;
+       _executable = executable;
        _preconditions = preconditions;
        _effects = effects;
-       _currentPrecondition = _preconditions.begin();
 }
 
 Action::Action(const Action& action){
@@ -18,32 +18,22 @@ Action::Action(const Action& action){
        _effects = action._effects;
 }
 
-const literals& Action::effects(int value) const{
-       effectsMap::const_iterator effects = _effects.find(value);
+const Literals& Action::effects(int value) const{
+       EffectsMap::const_iterator effects = _effects.find(value);
        if (effects != _effects.end())
                return effects->second;
        return _empty;
 }
 
-const literals& Action::preconditions() const{
-       return _currentPrecondition->second;
+const Preconditions& Action::preconditions() const{
+       return _preconditions;
 }
 
 int Action::execute() const{
-       cout << "Executing: " << _currentPrecondition->first << endl;
+       cout << "Executing: " << _executable << endl;
        return 0;
 }
 
 const string& Action::name() const{
        return _name;
 }
-
-bool Action::nextExecutable(){
-       if (++_currentPrecondition != _preconditions.end())
-               return true;
-       return false;
-}
-
-void Action::reset(){
-       _currentPrecondition = _preconditions.begin();
-}
index dd14e28798d0ab83b9370901085e983b1d570195..606857d8524ade62545f3455d8984e0dfbe4325a 100644 (file)
--- a/action.h
+++ b/action.h
@@ -5,28 +5,53 @@
 #include <string>
 #include <ext/hash_map>
 
-typedef std::vector<std::string> literals;
-typedef std::vector<std::pair<std::string,literals> > preconditionsVector;
-typedef __gnu_cxx::hash_map<int,literals> effectsMap;
+typedef std::vector<std::string> Literals;
+typedef __gnu_cxx::hash_map<std::string,bool> Preconditions;
+typedef __gnu_cxx::hash_map<int,Literals> EffectsMap;
 
+namespace __gnu_cxx {
+
+       template< typename CharT, typename Traits, typename Alloc >
+               struct hash< std::basic_string<CharT, Traits, Alloc> > {
+                       size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+                               const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
+
+                               return c.hash(s.c_str(), s.c_str() + s.size());
+
+                       }
+
+               };
+
+       template< typename CharT, typename Traits, typename Alloc >
+               struct hash< const std::basic_string<CharT, Traits, Alloc> > { //yes you need this version aswell!
+
+                       size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+                               const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
+
+                               return c.hash(s.c_str(), s.c_str() + s.size());
+                       }
+
+               };
+};
 class Action {
        public:
-               Action(std::string name, preconditionsVector preconditions, effectsMap effects);
+               Action(std::string name, const Preconditions& preconditions, std::string executable, const EffectsMap& effects);
                Action(const Action& action);
                Action(){};
-               const literals& effects(int value) const;
-               const literals& preconditions() const;
+               const Literals& effects(int value) const;
+               const Preconditions& preconditions() const;
                const std::string& name() const;
-               bool nextExecutable();
                int execute() const;
-               void reset();
 
        protected:
-               preconditionsVector::const_iterator _currentPrecondition;
                std::string _name;
-               preconditionsVector _preconditions;
-               effectsMap _effects;
-               static const literals _empty;
+               std::string _executable;
+               Preconditions _preconditions;
+               EffectsMap _effects;
+               static const Literals _empty;
 };
 
+
 #endif
index acc5bd7090144cf31ffd4b536e2a5a68f1a72f18..9bedf3834d34c4786dddbd46354f4879af759f07 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -30,38 +30,44 @@ bool Node::executed() const{
        return _executed;
 }
 
-const literals& Node::effects() const{
+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));
+void Node::execute(bool strict, const Literals& effects){
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               _preconditions.erase(_preconditions.find(*effect));
        }
-       if (_preconditions.size() != 0 || _executed)
+       if ((_preconditions.size() != 0 && strict) || _executed)
                return;
 
+       if (_preconditions.size() != 0){
+               for (Preconditions::iterator precond = _preconditions.begin(); precond != _preconditions.end(); ++precond){
+                       if (precond->second)
+                               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);
+               (*child)->execute(strict,effects);
        }
 }
 
-StartNode::StartNode(const literals& init){
-       effectsMap initial;
+StartNode::StartNode(const Literals& init){
+       EffectsMap initial;
        initial[0] = init;
-       Node(Action("start",preconditionsVector(), initial));
+       _action = Action("start",Preconditions(),"", 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()));
+EndNode::EndNode(const Literals& goal){
+       Preconditions goalState;
+       for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
+               goalState[*g] = true;
+       _action = Action("finish",goalState,"",EffectsMap());
+       _preconditions = _action.preconditions();
 }
 
diff --git a/node.h b/node.h
index cfa02223854a9d66dfad039fc2fcd0deb57e2a76..28a828728e9109a73181baf6fde79d98198ed908 100644 (file)
--- a/node.h
+++ b/node.h
@@ -14,26 +14,26 @@ class Node {
                virtual ~Node(){}
                void addChild(Node* node);
                const Action& action() const;
-               void execute(const literals& effects);
+               void execute(bool strict, const Literals& effects);
                bool executed() const;
-               const literals& effects() const;
+               const Literals& effects() const;
 
        protected:
                Action _action;
                std::vector<Node*> _children;
-               literals _preconditions;
+               Preconditions _preconditions;
                bool _executed;
-               literals _effects;
+               Literals _effects;
 };
 
-class StartNode : Node {
+class StartNode :public Node {
        public:
-               StartNode(const literals& init);
+               StartNode(const Literals& init);
 };
 
-class EndNode : Node {
+class EndNode :public Node {
        public:
-               EndNode(const literals& goal);
+               EndNode(const Literals& goal);
 };
 
 #endif
index 324c2dd73a3c0754d3a4899edbbf739473465d80..15fc248372361b188b63f3e67dce49f628dc3fa5 100644 (file)
@@ -5,21 +5,20 @@
 using namespace std;
 using namespace __gnu_cxx;
 
-Planner::Planner(std::vector<Action> actions, literals init, literals goal){
+Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
        _init = init;
        _goal = goal;
        _start = new StartNode(_init);
-       Node* finish = new EndNode(_goal);
+       _finish = new EndNode(_goal);
 
        for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
-               Action* act = new Action(*action);
-               const literals& effects = act->effects(0);
-               for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               const Literals& effects = action->effects(0);
+               for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                        cerr << "Adding effect: " << *effect << endl;
-                       _actions[*effect] = act;
+                       _actions[*effect] = *action;
                }
        }
-       makePlan(finish);
+       makePlan(_finish);
 }
 
 Planner::~Planner(){
@@ -34,7 +33,7 @@ void Planner::makePlan(Node* node){
        addNode(node);
 
        cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
-       const literals& preconds = node->action().preconditions();
+       const Preconditions& preconds = node->action().preconditions();
        cerr << "done" << endl;
 
 
@@ -42,21 +41,26 @@ void Planner::makePlan(Node* node){
                cerr << "Found no preconds" << endl;
                _start->addChild(node);
        }else{
-               for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
-                       cerr << "Looking for: " << *precond << endl;
-                       hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
+               for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+                       cerr << "Looking for: " << precond->first << endl;
+                       hash_map<string,Node*>::iterator addedNode = _addedNodes.find(precond->first);
                        if(addedNode != _addedNodes.end()){
                                cerr << "Using already added node" << endl;
                                addedNode->second->addChild(node);
                        }else {
-                               hash_map<string, Action*>::iterator action = _actions.find(*precond);
+                               hash_map<string, Action>::iterator action = _actions.find(precond->first);
                                if (action != _actions.end()){
                                        cerr << "Adding new node" << endl;
-                                       Node* newnode = new Node(*action->second);
+                                       Node* newnode = new Node(action->second);
                                        newnode->addChild(node);
                                        makePlan(newnode);
+                               }else if (precond->second){
+                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
+                                       return;
                                }else{
-                                       cerr << "Action with effect: " << *precond << " not found!" << endl;
+                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       cerr << "This is a soft precondition, so we will continue" << endl;
                                }
                        }
                }
@@ -64,9 +68,9 @@ void Planner::makePlan(Node* node){
 }
 
 void Planner::addNode(Node* node){
-       const literals& effects = node->action().effects(0);
+       const Literals& effects = node->action().effects(0);
 
-       for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                cout << "Adding node for effect: " << *effect << endl;
                _addedNodes[*effect] = node;
        }
@@ -74,6 +78,9 @@ void Planner::addNode(Node* node){
 
 
 void Planner::execute(){
-       _start->execute(literals());
+       _start->execute(true,Literals());
        for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+               if (!node->second->executed())
+                       node->second->execute(false,Literals());
+       }
 }
index bb6836a269d23bf0ff8a85c9451dca6d40143abe..3faa6fb6918559f0182e45b51fc167c340920db1 100644 (file)
--- a/planner.h
+++ b/planner.h
@@ -7,38 +7,9 @@
 
 class Node;
 
-
-
-namespace __gnu_cxx {
-
-    template< typename CharT, typename Traits, typename Alloc >
-    struct hash< std::basic_string<CharT, Traits, Alloc> > {
-        size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
-            
-           const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
-           
-           return c.hash(s.c_str(), s.c_str() + s.size());
-
-}
-
-    };
-
-    template< typename CharT, typename Traits, typename Alloc >
-    struct hash< const std::basic_string<CharT, Traits, Alloc> > { //yes you need this version aswell!
-
-size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
-       
-          const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
-      
-          return c.hash(s.c_str(), s.c_str() + s.size());
-        }
-
-    };
-};
-
 class Planner {
        public:
-               Planner(std::vector<Action> actions, literals init, literals goal);
+               Planner(std::vector<Action> actions, Literals init, Literals goal);
                ~Planner();
 
                void execute();
@@ -49,9 +20,10 @@ class Planner {
                void addNode(Node* node);
                
                Node* _start;
+               Node* _finish;
                __gnu_cxx::hash_map<std::string,Node*> _addedNodes;
-               __gnu_cxx::hash_map<std::string,Action*> _actions;
-               literals _init;
-               literals _goal;
+               __gnu_cxx::hash_map<std::string,Action> _actions;
+               Literals _init;
+               Literals _goal;
 };
 #endif