module.cpp
Go to the documentation of this file.
00001 #include "module.h"
00002 #include <cassert>
00003 #include <iostream>
00004 #include <sstream>
00005 #include "state.h"
00006 #include "globals.h"
00007 #include "operator.h"
00008 
00009 static const bool s_OutputPredMappings = false;
00010 
00011 Module::Module(istream &in)
00012 {
00013     string function_name, lib;
00014     in >> function_name >> lib;
00015     libCall = function_name;
00016     libCall.append("@").append(lib);
00017 
00018     int count;
00019     in >> count;
00020     for (int i = 0; i < count; ++i) {
00021         string paramname, type, value;
00022         in >> paramname >> type >> value;
00023         params.push_back(Parameter(paramname, type, value));
00024     }
00025 }
00026 
00027 void Module::dump()
00028 {
00029     std::cout << "LibCall: " << libCall.c_str() << std::endl; 
00030     std::cout << "Params: " << params << std::endl;
00031 }
00032 
00033 ConditionModule::ConditionModule(istream &in) :
00034     Module(in)
00035 {
00036     in >> var;
00037     g_variable_types[var] = module;
00038     checkCondition = g_module_loader->getConditionChecker(libCall);
00039     if (checkCondition == NULL) {
00040         printf("Failed to load ConditionModule at \"%s\".\n", libCall.c_str());
00041         assert(checkCondition != NULL);
00042     }
00043 }
00044 
00045 void ConditionModule::dump()
00046 {
00047     std::cout << "ConditionModule" << std::endl;
00048     Module::dump();
00049     std::cout << "Variable: " << var << std::endl;
00050 
00051     std::cout << "checkCondition call is: " << (long)(checkCondition) << std::endl;
00052 }
00053 
00054 EffectModule::EffectModule(istream &in) :
00055     Module(in)
00056 {
00057     in >> internal_name;
00058     int count;
00059     in >> count;
00060     writtenVars.reserve(count);
00061     int var_no;
00062     for (int i = 0; i < count; ++i) {
00063         in >> var_no;
00064         writtenVars.push_back(var_no);
00065     }
00066     applyEffect = g_module_loader->getApplyEffect(libCall);
00067     if (applyEffect == NULL) {
00068         printf("Failed to load EffectModule at \"%s\".\n", libCall.c_str());
00069         assert(applyEffect != NULL);
00070     }
00071 }
00072 
00073 void EffectModule::dump()
00074 {
00075     std::cout << "EffectModule" << std::endl;
00076     Module::dump();
00077     std::cout << "Internal name: " << internal_name << std::endl;
00078     std::cout << "Written vars: ";
00079     for(vector<int>::iterator it = writtenVars.begin(); it != writtenVars.end(); it++)
00080         std::cout << *it << " ";
00081     std::cout << std::endl;
00082 
00083     std::cout << "ApplyEffect call is: " << (long)(applyEffect) << std::endl;
00084 }
00085 
00086 CostModule::CostModule(istream &in) :
00087     Module(in)
00088 {
00089     in >> var;
00090     assert(g_variable_types[var] == costmodule);
00091     checkCost = g_module_loader->getCostChecker(libCall);
00092     if (checkCost == NULL) {
00093         printf("Failed to load CostModule at \"%s\".\n", libCall.c_str());
00094         assert(checkCost != NULL);
00095     }
00096 }
00097 
00098 void CostModule::dump()
00099 {
00100     std::cout << "CostModule" << std::endl;
00101     Module::dump();
00102     std::cout << "Variable: " << var << std::endl;
00103 
00104     std::cout << "checkCost call is: " << (long)(checkCost) << std::endl;
00105 }
00106 
00107 InitModule::InitModule(istream &in)
00108 {
00109     string function_name, lib;
00110     in >> function_name >> lib;
00111     libCall = function_name;
00112     libCall.append("@").append(lib);
00113 
00114     int count;
00115     in >> count;
00116     string param;
00117     for (int i = 0; i < count; ++i) {
00118         in >> param;
00119         parameters.push_back(param);
00120     }
00121 
00122     initModule = g_module_loader->getModuleInit(libCall);
00123     if (initModule == NULL) {
00124         printf("Failed to load InitModule at \"%s\".\n", libCall.c_str());
00125         assert(initModule != NULL);
00126     }
00127 }
00128 
00129 void InitModule::execInit()
00130 {
00131     int argc = parameters.size() + 1;
00132     char** argv = new char*[argc];
00133 
00134     argv[0] = strdup(libCall.c_str());
00135     for (int i = 0; i < parameters.size(); i++) {
00136         argv[i + 1] = strdup(parameters.at(i).c_str());
00137     }
00138     initModule(argc, argv);
00139     for (int i = 0; i < argc; i++) {
00140         free(argv[i]);
00141     }
00142     delete[] argv;
00143 }
00144 
00145 OplInit::OplInit(istream &in)
00146 {
00147     string function_name, lib;
00148     in >> function_name >> lib;
00149     libCall = function_name;
00150     libCall.append("@").append(lib);
00151 
00152     oplInit = g_module_loader->oplCalbackInit(libCall);
00153     if (oplInit == NULL) {
00154         printf("ERROR: %s\tcould not load %s  ", __PRETTY_FUNCTION__, libCall.c_str());
00155         assert(false);
00156     }
00157 }
00158 
00159 opl::interface::OplCallbackInterface* OplInit::execInit(const ObjectTypeMap& objects,
00160         const PredicateMapping& predicateMapping,
00161         const FunctionMapping& functionMapping,
00162         const modules::PredicateList& predicateConstants,
00163         const modules::NumericalFluentList& numericConstants)
00164 {
00165     return oplInit(objects, predicateMapping, functionMapping, predicateConstants, numericConstants);
00166 }
00167 
00168 Module::~Module()
00169 {
00170 }
00171 
00172 // global definitions
00173 
00174 const TimeStampedState* g_modulecallback_state = NULL;
00175 opl::interface::OplCallbackInterface* g_OplModuleCallback = NULL;
00176 
00177 map<int, ConditionModule *> g_condition_modules;
00178 vector<EffectModule *> g_effect_modules;
00179 map<int, CostModule*> g_cost_modules;
00180 vector<InitModule *> g_init_modules;
00181 vector<SubplanModuleSet> g_subplan_modules;
00182 OplInit* g_oplinit = NULL;
00183 
00184 PredicateMapping g_pred_mapping;
00185 FunctionMapping g_func_mapping;
00186 
00187 PredicateList g_pred_constants;
00188 NumericalFluentList g_func_constants;
00189 
00190 PDDLModuleLoader *g_module_loader;
00191 
00192 // global interface functions
00193 
00194 void dump_modules()
00195 {
00196     for(map<int, ConditionModule*>::iterator it =  g_condition_modules.begin();
00197             it != g_condition_modules.end(); it++)
00198         it->second->dump();
00199     for(vector<EffectModule *>::iterator it =  g_effect_modules.begin(); it != g_effect_modules.end(); it++)
00200         (*it)->dump();
00201     for(map<int, CostModule*>::iterator it =  g_cost_modules.begin(); it != g_cost_modules.end(); it++)
00202         it->second->dump();
00203 }
00204 
00205 void g_setModuleCallbackState(const TimeStampedState* currentState)
00206 {
00207     g_modulecallback_state = currentState;
00208     if (g_OplModuleCallback != NULL)
00209     {
00210         g_OplModuleCallback->setCurrentState(currentState);
00211     }
00212 }
00213 
00214 bool getPreds(PredicateList* & predicateList)
00215 {
00216     assert(g_modulecallback_state);
00217     if (g_modulecallback_state == NULL)
00218         return false;
00219 
00220     //cout << "Predicate callback!" << endl;
00221 
00222     if (predicateList == NULL) {
00223         //printf("creating PL\n");
00224         predicateList = new PredicateList();
00225         // fill predicate list with subs of all existing predicates
00226         for (PredicateMapping::iterator it = g_pred_mapping.begin(); it
00227                 != g_pred_mapping.end(); it++) {
00228             string pred = it->first;
00229             int var = it->second.first;
00230             int val = it->second.second;
00231 
00232             std::string token;
00233             std::istringstream iss(pred);
00234             string pName;
00235             ParameterList params;
00236             while (getline(iss, token, ' ')) {
00237                 if (pName.empty()) {
00238                     pName = token;
00239                     continue;
00240                 }
00241                 params.push_back(Parameter("", g_objectTypes[token], token));
00242                 //   std::cout << token << std::endl;
00243             }
00244             Predicate p(pName, params);
00245             p.value = ((*g_modulecallback_state)[var] == val);
00246             predicateList->push_back(p);
00247 
00248             //cout << "PREDMAP: \"" << pred << "\" var " << var << " val " << val << endl;
00249         }
00250         // add all constants to the predicate list. NOTE: only those predicates that are true in the initial state are added, because
00251         // constants are NOT grounded during preprocess!!
00252         predicateList->insert(predicateList->end(), g_pred_constants.begin(),
00253                 g_pred_constants.end());
00254 
00255         return true;
00256     }
00257 
00258     string key;
00259 
00260     PredicateList::iterator it;
00261     for (it = predicateList->begin(); it != predicateList->end(); ++it) {
00262         key = it->name;
00263         for (ParameterList::const_iterator jt = it->parameters.begin(); jt
00264                 != it->parameters.end(); ++jt) {
00265             key.append(" ").append(jt->value);
00266         }
00267         PredicateMapping::iterator entry = g_pred_mapping.find(key);
00268         VarVal res;
00269         if (entry == g_pred_mapping.end()) {
00270             // check whether the predicate is a constant. NOTE: it's possible the predicate is NOT found because constants
00271             // are not grounded during preprocess and only those constants exist that are true in the initial state. If the predicate is
00272             // not found, it is also possible that the predicates name is missspelled, but this function returns the predicate as false
00273             // HACK!!!
00274             bool predFound = false;
00275             for (PredicateList::iterator cit = g_pred_constants.begin(); cit
00276                     != g_pred_constants.end(); cit++) {
00277                 if (cit->name == it->name) {
00278                     assert(cit->parameters.size() == it->parameters.size());
00279                     bool paramsFit = true;
00280                     for (unsigned int i = 0; i < cit->parameters.size(); i++) {
00281                         if (cit->parameters[i].value != it->parameters[i].value) {
00282                             paramsFit = false;
00283                             break;
00284                         }
00285                     }
00286                     if (paramsFit) {
00287                         it->value = true;
00288                         predFound = true;
00289                         break;
00290                     }
00291                 }
00292             }
00293             if (!predFound)
00294                 it->value = false;
00295             //cout << "Error! Could not find entry in predicate mapping with key: " << key;
00296             // exit(1);
00297         } else {
00298             res = entry->second;
00299             it->value = (res.second == (*g_modulecallback_state)[res.first]);
00300         }
00301         //it->value = (res.second == (*g_modulecallback_state)[res.first]);
00302     }
00303 
00304     return true;
00305 }
00306 
00307 bool getFuncs(NumericalFluentList* & fluentList)
00308 {
00309     assert(g_modulecallback_state);
00310     if (g_modulecallback_state == NULL)
00311         return false;
00312     //cout << "Function callback!" << endl;
00313 
00314     if (fluentList == NULL) {
00315         fluentList = new NumericalFluentList();
00316         for (FunctionMapping::iterator it = g_func_mapping.begin(); it
00317                 != g_func_mapping.end(); it++) {
00318             string pred = it->first;
00319             int var = it->second;
00320 
00321             std::string token;
00322             std::istringstream iss(pred);
00323             string pName;
00324             ParameterList params;
00325             while (getline(iss, token, ' ')) {
00326                 if (pName.empty()) {
00327                     pName = token;
00328                     continue;
00329                 }
00330                 params.push_back(Parameter("",  g_objectTypes[token], token));
00331                 //   std::cout << token << std::endl;
00332             }
00333             NumericalFluent f(pName, params);
00334             f.value = (*(g_modulecallback_state))[var];
00335             fluentList->push_back(f);
00336         }
00337         fluentList->insert(fluentList->end(), g_func_constants.begin(),
00338                 g_func_constants.end());
00339         return true;
00340     }
00341 
00342     string key;
00343 
00344     NumericalFluentList::iterator it;
00345     for (it = fluentList->begin(); it != fluentList->end(); ++it) {
00346         key = it->name;
00347         for (ParameterList::const_iterator jt = it->parameters.begin(); jt
00348                 != it->parameters.end(); ++jt)
00349             key.append(" ").append(jt->value);
00350 
00351         FunctionMapping::iterator entry = g_func_mapping.find(key);
00352         int res;
00353         if (entry == g_func_mapping.end()) {
00354 
00355             bool funcFound = false;
00356             for (NumericalFluentList::iterator cit = g_func_constants.begin(); cit
00357                     != g_func_constants.end(); cit++) {
00358                 if (cit->name == it->name) {
00359                     assert(cit->parameters.size() == it->parameters.size());
00360                     bool paramsFit = true;
00361                     for (unsigned int i = 0; i < cit->parameters.size(); i++) {
00362                         if (cit->parameters[i].value != it->parameters[i].value) {
00363                             paramsFit = false;
00364                             break;
00365                         }
00366                     }
00367                     if (paramsFit) {
00368                         it->value = cit->value;
00369                         funcFound = true;
00370                         break;
00371                     }
00372                 }
00373             }
00374             if (!funcFound) {
00375                 cout
00376                     << "Error! Could not find entry in func mapping with key: "
00377                     << key << endl;
00378                 return false;
00379             }
00380         } else {
00381             res = entry->second;
00382             it->value = (*(g_modulecallback_state))[res];
00383         }
00384     }
00385     return true;
00386 }
00387 
00388 void handleSubplans(const vector<PlanStep> & plan)
00389 {
00390     for (vector<SubplanModuleSet>::iterator it = g_subplan_modules.begin(); it
00391             != g_subplan_modules.end(); it++) {
00392         SubplanModuleSet sm = *it;
00393         Module* generateSp = std::tr1::get<0>(sm);
00394         Module* outputSp = std::tr1::get<1>(sm);
00395         Module* execSubplan = std::tr1::get<2>(sm);
00396 
00397         subplanGeneratorType genFn = g_module_loader->getSubplanGenerator(
00398                 generateSp->libCall);
00399         outputSubplanType outputFn = g_module_loader->getOutputSubplan(
00400                 outputSp->libCall);
00401         executeModulePlanType execFn = g_module_loader->getExecuteModulePlan(
00402                 execSubplan->libCall);
00403         if (genFn == NULL || outputFn == NULL || execFn == NULL) {
00404             cerr << "Error in loading subplan generators." << endl;
00405             continue;
00406         }
00407 
00408         // make this temporal and another, read: better interface for outputting plans
00409         modulePlanType subplan;
00410         stringstream ss;
00411         for (int i = 0; i < plan.size(); i++) {
00412             const PlanStep& step = plan[i];
00413             ParameterList pl;
00414             subplanType spt = genFn(step.op->get_name(), pl, NULL, NULL, 0);
00415             ss << outputFn(spt) << endl << endl;
00416             subplan.push_back(spt);
00417         }
00418         cout << "SubplanPlan:" << endl;
00419         cout << ss.str() << endl;
00420         execFn(subplan);
00421     }
00422 }
00423 
00424 string read_name_and_params(istream &in)
00425 {
00426     string ret, param;
00427     int paramcount;
00428     in >> ret >> paramcount;
00429     for (int i = 0; i < paramcount; ++i) {
00430         in >> param;
00431         ret.append(" " + param);
00432     }
00433     return ret;
00434 }
00435 
00436 void read_pddl_translation(istream &in)
00437 {
00438     check_magic(in, "begin_pddl_translation");
00439     int count;
00440     string name;
00441     int var, value;
00442     in >> count;
00443     for (int i = 0; i < count; ++i) {
00444         name = read_name_and_params(in);
00445         in >> var >> value;
00446         g_pred_mapping.insert(make_pair(name, make_pair(var, value)));
00447     }
00448     in >> count;
00449     for (int i = 0; i < count; ++i) {
00450         name = read_name_and_params(in);
00451         in >> var;
00452         g_func_mapping.insert(make_pair(name, var));
00453     }
00454     check_magic(in, "end_pddl_translation");
00455     if(s_OutputPredMappings) {
00456         cout << "DEBUG: g_pred_mapping contains:" << endl;
00457         PredicateMapping::iterator it;
00458         for (it = g_pred_mapping.begin(); it != g_pred_mapping.end(); ++it) {
00459             cout << "key: " << it->first << ", var: " << it->second.first
00460                 << ", val: " << it->second.second << endl;
00461         }
00462         cout << "DEBUG: g_func_mapping contains:" << endl;
00463         FunctionMapping::iterator it2;
00464         for (it2 = g_func_mapping.begin(); it2 != g_func_mapping.end(); ++it2) {
00465             cout << "key: " << it2->first << ", var: " << it2->second << endl;
00466         }
00467     }
00468 }
00469 
00470 void read_modules(istream &in)
00471 {
00472     check_magic(in, "begin_modules");
00473     int count;
00474     in >> count;
00475     for (int i = 0; i < count; ++i) {
00476         InitModule *init_module = new InitModule(in);
00477         g_init_modules.push_back(init_module);
00478     }
00479     in >> count;
00480     for (int i = 0; i < count; ++i) {
00481         string gen, output, exec;
00482         string lib1, lib2, lib3;
00483         in >> gen >> lib1 >> output >> lib2 >> exec >> lib3;
00484         g_subplan_modules.push_back(tr1::make_tuple(new Module(
00485                         gen.append("@").append(lib1)), new Module(
00486                         output.append("@").append(lib2)), new Module(
00487                         exec.append("@").append(lib3))));
00488     }
00489 
00490     in >> count;
00491     for (int i = 0; i < count; ++i) {
00492         ConditionModule *cond_module = new ConditionModule(in);
00493         g_condition_modules[cond_module->var] = cond_module;
00494     }
00495     in >> count;
00496     for (int i = 0; i < count; ++i) {
00497         EffectModule *eff_module = new EffectModule(in);
00498         g_effect_modules.push_back(eff_module);
00499     }
00500     in >> count;
00501     for (int i = 0; i < count; i++) {
00502         CostModule *cost_module = new CostModule(in);
00503         g_cost_modules[cost_module->var] = cost_module;
00504     }
00505 
00506     check_magic(in, "end_modules");
00507 }
00508 
00509 void read_constant_facts(istream& in)
00510 {
00511     check_magic(in, "begin_constant_facts");
00512 
00513     int count;
00514     in >> count;
00515     for (int i = 0; i < count; i++) {
00516         string name;
00517         in >> name;
00518 
00519         int paramCount;
00520         in >> paramCount;
00521 
00522         ParameterList params;
00523 
00524         for (int j = 0; j < paramCount; j++) {
00525             string param;
00526             in >> param;
00527             params.push_back(Parameter("", "", param));
00528         }
00529         g_pred_constants.push_back(Predicate(name, params, true));
00530     }
00531 
00532     in >> count;
00533     for (int i = 0; i < count; i++) {
00534         string name;
00535         in >> name;
00536 
00537         int paramCount;
00538         in >> paramCount;
00539 
00540         ParameterList params;
00541 
00542         for (int j = 0; j < paramCount; j++) {
00543             string param;
00544             in >> param;
00545             params.push_back(Parameter("", "", param));
00546         }
00547         double value;
00548         in >> value;
00549 
00550         g_func_constants.push_back(NumericalFluent(name, params, value));
00551     }
00552 
00553     check_magic(in, "end_constant_facts");
00554 }
00555 
00556 void read_oplinits(istream &in)
00557 {
00558     check_magic(in, "begin_oplinits");
00559     int count;
00560     in >> count;
00561     if (count > 0)
00562     {
00563         g_oplinit = new OplInit(in);
00564     }
00565     else
00566     {
00567         g_oplinit = NULL;
00568     }
00569     check_magic(in, "end_oplinits");
00570 }
00571 
00572 void read_objects(istream &in)
00573 {
00574     check_magic(in, "begin_objects");
00575     int count;
00576     in >> count;
00577     for (int i = 0; i < count; i++) {
00578         string type;
00579         string name;
00580         in >> type;
00581         in >> name;
00582         g_objectTypes[name] = type;
00583     }
00584     check_magic(in, "end_objects");
00585 }


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