modules.py
Go to the documentation of this file.
00001 import pddl_types
00002 import functions
00003 import sys
00004 import f_expression
00005 import conditions
00006 
00007 class Module(object):
00008     def __init__(self, name, parameters, type, modulecall, effects = [], parent = None):
00009       self.name = name
00010       self.parameters = tuple(parameters)
00011       self.type = type
00012       self.modulecall = modulecall
00013       self.effects = tuple(effects)
00014       self.parent = parent
00015       self.hash = hash((self.__class__, self.name, self.parameters, 
00016                         self.type, self.modulecall, self.effects))
00017       assert self.type in ("conditionchecker", "effect", "cost"), self.type
00018       assert (self.type == "effect") or (len(self.effects) == 0)
00019     def __hash__(self):
00020       return self.hash
00021     def __eq__(self, other):
00022       # Compare hash first for speed reasons.
00023       return (self.hash == other.hash and
00024               self.__class__ is other.__class__ and
00025               self.name == other.name and
00026               self.parameters == other.parameters and
00027               self.type == other.type and
00028               self.modulecall == other.modulecall and
00029               self.effects == other.effects)
00030     def __ne__(self, other):
00031       return not self == other
00032     def __str__(self):
00033       if self.type == "effect":
00034         return "%s %s(%s) %s %s -> %s" % (self.__class__.__name__, self.name,
00035                             ", ".join(map(str, self.parameters)), self.type, self.modulecall,
00036                             ", ".join(map(str, self.effects)))
00037       else:
00038         return "%s %s(%s) %s %s" % (self.__class__.__name__, self.name,
00039                             ", ".join(map(str, self.parameters)), self.type, self.modulecall)
00040     def toModuleCall(self):
00041       mod_args = []
00042       for param in self.parameters:
00043         if param.name.startswith("?"):
00044           mod_args.append(conditions.Variable(param.name))
00045         else:
00046           mod_args.append(conditions.ObjectTerm(param.name))
00047       return conditions.ModuleCall(self.name, mod_args)
00048     def rename_variables(self, renamings):
00049         new_params = []
00050         for param in self.parameters:
00051           new_var = renamings.get(conditions.Variable(param.name), conditions.Variable(param.name))
00052           new_param = pddl_types.TypedObject(new_var.name, param.type)
00053           new_params.append(new_param)
00054         new_effects = []
00055         for effect in self.effects:
00056           new_effect = effect.rename_variables(renamings)
00057           new_effects.append(new_effect)
00058         return self.__class__(self.name, new_params, self.type, self.modulecall, new_effects)
00059     def instantiate(self, var_mapping, new_modules):
00060         new_params = []
00061         for param in self.parameters:
00062           new_var = var_mapping.get(conditions.Variable(param.name), conditions.Variable(param.name))
00063           new_param = pddl_types.TypedObject(new_var.name, param.type)
00064           new_params.append(new_param)
00065 
00066         new_effects = []
00067         for effect in self.effects:
00068           new_effect = effect.rename_variables(var_mapping)
00069           new_effects.append(new_effect)
00070         # ahh need to inst the effects too!
00071         # -- need vars as args
00072         # also for effects
00073         mc = Module(self.name, new_params, self.type, self.modulecall, new_effects, self)
00074         if mc not in new_modules:
00075           new_modules.add(mc)
00076     def parse(alist):
00077       assert len(alist) >= 3
00078       name = alist[0]
00079       # effects might contain (set) functions here like (x ?a)
00080       parameterList = [someParam for someParam in alist[1:-2] if not isinstance(someParam,list)]
00081       functionParamList = [someParam for someParam in alist[1:-2] if isinstance(someParam, list)]
00082 
00083       parameters = pddl_types.parse_typed_list(parameterList)
00084       effects = [f_expression.parse_expression(entry) for entry in functionParamList]
00085       #effects = [functions.Function.parse_typed(entry, "number") for entry in functionParamList]
00086 
00087       modulecall = alist[-1]
00088       type = alist[-2]
00089       return Module(name, parameters, type, modulecall, effects)
00090     parse = staticmethod(parse)
00091     def dump(self, indent="  "):
00092       if self.type == "effect":
00093         print "%s%s(%s) Effects: (%s) %s [%s]" % (indent, self.name, ", ".join(map(str, self.parameters)), ", ".join(map(str, self.effects)), self.type, self.modulecall)
00094       else:
00095           print "%s%s(%s) %s [%s]" % (indent, self.name, ", ".join(map(str, self.parameters)), self.type, self.modulecall)
00096 
00097 class SubplanGenerator(object):
00098   def __init__(self, genFn, outputFn, execFn):
00099     self.genFn = genFn
00100     self.outputFn = outputFn
00101     self.execFn = execFn
00102   def parse(alist):
00103     assert(alist[0] == "subplan_generator")
00104     assert(len(alist) == 4)
00105     return SubplanGenerator(alist[1], alist[2], alist[3])
00106   def dump(self, indent="  "):
00107     print "%sGenerator: %s Output: %s Execute: %s" % (indent, self.genFn, self.outputFn, self.execFn)
00108   parse = staticmethod(parse)
00109 
00110 class ModuleInit(object):
00111   def __init__(self, init_function, params):
00112     self.init_function = init_function
00113     self.parameters = params
00114   def parse(alist):
00115     init_function = alist[0]
00116     params = alist[1:]
00117     return ModuleInit(init_function, params)
00118   def dump(self, indent="  "):
00119     print "%sFunction: %s Parameters: %s" % (indent, self.init_function, " ".join(self.parameters))
00120   parse = staticmethod(parse)
00121 
00122 class OplInit(object):
00123   def __init__(self, init_function):
00124     self.init_function = init_function
00125   def parse(alist):
00126     init_function = alist
00127     return OplInit(init_function)
00128   def dump(self, indent="  "):
00129     print "%sFunction: %s" % (indent, self.init_function)
00130   parse = staticmethod(parse)


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