AbstractStateFactory.cpp
Go to the documentation of this file.
00001 /*
00002  * AbstractStateFactory.cpp
00003  *
00004  *  Created on: Jul 20, 2011
00005  *      Author: Andreas Hertle
00006  */
00007 
00008 #include <assert.h>
00009 #include "tfd_modules/opl/stringutil.h"
00010 #include "tfd_modules/opl/AbstractStateFactory.h"
00011 
00012 namespace opl
00013 {
00014 
00015 namespace interface
00016 {
00017 
00018 opl::interface::AbstractState* AbstractStateFactory::createState(
00019         const ObjectTypeMap& objects,
00020         const PredicateMapping& predicateMapping,
00021         const FunctionMapping& functionMapping,
00022         const modules::PredicateList& predicateConstants,
00023         const modules::NumericalFluentList& numericConstants)
00024 {
00025     AbstractState* state = instantiateState(objects);
00026 
00027     // create fluents mappings
00028     for (PredicateMapping::const_iterator fluentIterator = predicateMapping.begin(); fluentIterator != predicateMapping.end(); fluentIterator++)
00029     {
00030         createFluentMapping(state, *fluentIterator);
00031     }
00032     for (FunctionMapping::const_iterator fluentIterator = functionMapping.begin(); fluentIterator != functionMapping.end(); fluentIterator++)
00033     {
00034         createFluentMapping(state, *fluentIterator);
00035     }
00036 
00037     // create constants mappings
00038     for (modules::PredicateList::const_iterator fluentIterator = predicateConstants.begin(); fluentIterator != predicateConstants.end(); fluentIterator++)
00039     {
00040         createFluentMapping(state, *fluentIterator);
00041     }
00042     for (modules::NumericalFluentList::const_iterator fluentIterator = numericConstants.begin(); fluentIterator != numericConstants.end(); fluentIterator++)
00043     {
00044         createFluentMapping(state, *fluentIterator);
00045     }
00046 
00047     state->updateFluentMappings();
00048 
00049     return state;
00050 }
00051 
00052 void AbstractStateFactory::createFluentMapping(AbstractState* state, const std::pair<const std::string, ::VarVal>& mapping)
00053 {
00054     vector<string> arguments = StringUtil::split(mapping.first, " ");
00055     std::string fluentName = arguments.front();
00056     arguments.erase(arguments.begin());
00057 
00058     std::string::size_type index = fluentName.find_first_of("!");
00059     if (index != std::string::npos)
00060     {
00061         // Object fluent: create return object mapping
00062         string realFluentName = fluentName.substr(0, index);
00063         fluentName = realFluentName;
00064         std::string returnObjectName = arguments.back();
00065         arguments.pop_back();
00066         opl::interface::Object* returnObject = state->objects.find(returnObjectName)->second;
00067         std::string returnObjectKey = ObjectLookupTable::instance->createObjectKey(mapping.second.first, mapping.second.second);
00068         state->returnObjectMappings.insert(make_pair(returnObjectKey, returnObject));
00069     }
00070 
00071     // create key and FluentMapping
00072     std::string key = opl::interface::ObjectLookupTable::instance->createKey(fluentName, arguments);
00073     opl::interface::FluentMapping* value = new opl::interface::FluentMapping(mapping.second.first, mapping.second.second);
00074 
00075     // initialize fluent
00076     state->fluentMappings.insert(make_pair(key, value));
00077 }
00078 
00079 void AbstractStateFactory::createFluentMapping(AbstractState* state, const std::pair<const std::string, int>& mapping)
00080 {
00081     vector<string> arguments = StringUtil::split(mapping.first, " ");
00082     std::string fluentName = arguments.front();
00083     arguments.erase(arguments.begin());
00084 
00085     // create key and FluentMapping
00086     std::string key = opl::interface::ObjectLookupTable::instance->createKey(fluentName, arguments);
00087     opl::interface::FluentMapping* value = new opl::interface::FluentMapping(mapping.second);
00088 
00089     // initialize fluent
00090     state->fluentMappings.insert(make_pair(key, value));
00091 }
00092 
00093 void AbstractStateFactory::createFluentMapping(AbstractState* state, modules::Predicate fluent)
00094 {
00095     modules::ParameterList parameters = fluent.parameters;
00096     vector<string> arguments;
00097     for (modules::ParameterList::const_iterator parameterIterator = parameters.begin(); parameterIterator != parameters.end(); parameterIterator++)
00098     {
00099         arguments.push_back(parameterIterator->value);
00100     }
00101     std::string fluentName = fluent.name;
00102     std::string::size_type index = fluentName.find_first_of("!");
00103     if (index != std::string::npos)
00104     {
00105         // Object fluent: create return object mapping
00106         string realFluentName = fluentName.substr(0, index);
00107         fluentName = realFluentName;
00108         std::string returnObjectName = arguments.back();
00109         arguments.pop_back();
00110         opl::interface::Object* returnObject = state->objects.find(returnObjectName)->second;
00111         std::string returnObjectKey = ObjectLookupTable::instance->createObjectKey((int)state->fluentMappings.size(), 0);
00112         state->returnObjectMappings.insert(make_pair(returnObjectKey, returnObject));
00113     }
00114 
00115     // create key and FluentMapping
00116     std::string key = opl::interface::ObjectLookupTable::instance->createKey(fluentName, arguments);
00117     opl::interface::FluentMapping* value = new opl::interface::FluentMapping((int)state->fluentMappings.size(), 0, true);
00118 
00119     // initialize fluent
00120     state->fluentMappings.insert(make_pair(key, value));
00121 }
00122 
00123 void AbstractStateFactory::createFluentMapping(AbstractState* state, const modules::NumericalFluent& fluent)
00124 {
00125     modules::ParameterList parameters = fluent.parameters;
00126     vector<string> arguments;
00127     for (modules::ParameterList::const_iterator parameterIterator = parameters.begin(); parameterIterator != parameters.end(); parameterIterator++)
00128     {
00129         arguments.push_back(parameterIterator->value);
00130     }
00131     std::string fluentName = fluent.name;
00132 
00133     // create key and FluentMapping
00134     std::string key = opl::interface::ObjectLookupTable::instance->createKey(fluentName, arguments);
00135     opl::interface::FluentMapping* value = new opl::interface::FluentMapping((int)state->fluentMappings.size(), fluent.value, true);
00136 
00137     // initialize fluent
00138     state->fluentMappings.insert(make_pair(key, value));
00139 }
00140 
00141 }
00142 
00143 }
00144 
00145 


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06