AnswerSet.cpp
Go to the documentation of this file.
00001 #include <actasp/AnswerSet.h>
00002 
00003 #include <actasp/Action.h>
00004 
00005 #include <algorithm>
00006 
00007 using namespace std;
00008 
00009 namespace actasp {
00010 
00011 // AnswerSet::AnswerSet(bool satisfied,const std::set<actasp::AspFluent>& fluents) throw () :
00012 //      satisfied(satisfied),
00013 //      fluents(fluents.begin(),fluents.end())  {}
00014 
00015 bool AnswerSet::isSatisfied() const throw() {
00016         return satisfied;
00017 }
00018 
00019 bool AnswerSet::contains(const actasp::AspFluent& fluent) const throw() {
00020   
00021   pair<FluentSet::const_iterator, FluentSet::const_iterator> bounds = 
00022           equal_range(fluents.begin(), fluents.end(), fluent, TimeStepComparator());
00023   
00024   FluentSet::const_iterator element = find(bounds.first, bounds.second, fluent); 
00025         return element != bounds.second;
00026 }
00027 
00028 static void clearPlan(std::list<actasp::Action*>& plan) {
00029         std::list<actasp::Action*>::iterator planIt = plan.begin();
00030         for(; planIt != plan.end(); ++planIt)
00031                 delete (*planIt);
00032 }
00033 
00034 std::list<Action *> AnswerSet::instantiateActions(const std::map<std::string, actasp::Action*> &actionMap) const
00035                                                                         throw (std::logic_error) {
00036 
00037         list<Action *> plan;
00038         unsigned int maxTimeStep = 0;
00039 
00040         FluentSet::const_iterator fluentIt = fluents.begin();
00041 
00042         for (; fluentIt != fluents.end(); ++fluentIt) {
00043                 
00044                 map<string, Action * >::const_iterator actIt = actionMap.find(fluentIt->getName());
00045                 
00046                 if (actIt != actionMap.end()) {
00047                         plan.push_back(actIt->second->cloneAndInit(*fluentIt));
00048                         maxTimeStep = std::max(maxTimeStep,fluentIt->getTimeStep());
00049                 } 
00050                 //if a fluent is not a known action, just ignore it.
00051         }
00052         
00053         if (maxTimeStep > 0 && maxTimeStep >= plan.size()) {
00054                                 clearPlan(plan);
00055                                 throw logic_error("AnswerSet: the plan is missing an action for some time step. Check the list of actions shown in the plan query.");
00056         }
00057 
00058         return plan;
00059 }
00060 
00061 std::set<actasp::AspFluent> AnswerSet::getFluentsAtTime(unsigned int timeStep) const throw() {
00062         
00063         //create fake fluent with the required time step
00064         AspFluent fake("-",vector<string>(),timeStep);
00065   
00066   pair<FluentSet::const_iterator, FluentSet::const_iterator> bounds = equal_range(fluents.begin(), fluents.end(),fake, TimeStepComparator());
00067         
00068         return set<AspFluent>(bounds.first,bounds.second);
00069 }
00070 
00071 unsigned int AnswerSet::maxTimeStep() const throw() {
00072   return fluents.rbegin()->getTimeStep();
00073 }
00074 
00075 
00076 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:46