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
00012
00013
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
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
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(std::logic_error) {
00072 if(fluents.empty())
00073 throw logic_error("maxTimeStep() invoked on an empty answer set, which therefore has not time step at all");
00074
00075 return fluents.rbegin()->getTimeStep();
00076 }
00077
00078
00079 }