00001 import conditions
00002 import predicates
00003 import f_expression
00004
00005 class Axiom(object):
00006 def __init__(self, name, parameters, condition):
00007 self.name = name
00008 self.parameters = parameters
00009 self.condition = condition
00010 self.uniquify_variables()
00011 def parse(alist):
00012 assert len(alist) == 3
00013 assert alist[0] == ":derived"
00014 predicate = predicates.Predicate.parse(alist[1])
00015 condition = conditions.parse_condition(alist[2])
00016 return Axiom(predicate.name, predicate.arguments, condition)
00017 parse = staticmethod(parse)
00018 def dump(self):
00019 print "Axiom %s(%s)" % (self.name, ", ".join(map(str, self.parameters)))
00020 self.condition.dump()
00021 def uniquify_variables(self):
00022 self.type_map = dict([(par.name, par.type) for par in self.parameters])
00023 self.condition = self.condition.uniquify_variables(self.type_map)
00024 def instantiate(self, var_mapping, init_facts, fluent_facts,
00025 fluent_functions, init_function_vals, task, new_constant_axioms, new_modules):
00026
00027 arg_list = [var_mapping[conditions.Variable(par.name)].name for par in self.parameters]
00028 name = "(%s %s)" % (self.name, " ".join(arg_list))
00029
00030 condition = []
00031 try:
00032 self.condition.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals,
00033 fluent_functions, task, new_constant_axioms, new_modules, condition)
00034 except conditions.Impossible:
00035 return None
00036
00037 effect_args = [var_mapping.get(conditions.Variable(arg.name),
00038 conditions.Variable(arg.name)) for arg in self.parameters]
00039 effect = conditions.Atom(self.name, effect_args)
00040 return PropositionalAxiom(name, condition, effect)
00041
00042 class PropositionalAxiom:
00043 def __init__(self, name, condition, effect):
00044 self.name = name
00045 self.condition = condition
00046 self.effect = effect
00047 def clone(self):
00048 return PropositionalAxiom(self.name, list(self.condition), self.effect)
00049 def dump(self):
00050 if self.effect.negated:
00051 print "not",
00052 print self.name
00053 for fact in self.condition:
00054 print "PRE: %s" % fact
00055 print "EFF: %s" % self.effect
00056
00057 class NumericAxiom(object):
00058 def __init__(self, name, parameters, op, parts):
00059 self.name = name
00060 self.parameters = parameters
00061 self.op = op
00062 self.parts = parts
00063 def __str__(self):
00064 return "%s: %s(%s)" %(self.__class__.__name__, self.name, ", ".join(map(str, self.parameters)))
00065 def get_head(self):
00066 return f_expression.PrimitiveNumericExpression(self.name,self.parameters)
00067 def dump(self,indent):
00068 head = "(%s %s)" % (self.name, ", ".join(map(str, self.parameters)))
00069 op = ""
00070 if self.op:
00071 op = self.op + " "
00072 body = "%s" % " ".join(map(str, self.parts))
00073 print "%s%s -: %s%s" % (indent,head,op,body)
00074 def instantiate(self, var_mapping, fluent_functions, init_function_vals, task, new_constant_axioms):
00075 arg_list = [var_mapping[conditions.Variable(par.name)] for par in self.parameters]
00076 name = "(%s %s)" % (self.name, " ".join([arg.name for arg in arg_list]))
00077 parts = []
00078 for part in self.parts:
00079 if isinstance(part,f_expression.NumericConstant):
00080 parts.append(part)
00081 else:
00082 parts.append(part.instantiate(var_mapping, fluent_functions,
00083 init_function_vals, task, new_constant_axioms))
00084 effect = f_expression.PrimitiveNumericExpression(self.name, arg_list)
00085 return PropositionalNumericAxiom(name, self.op, parts, effect)
00086
00087 class PropositionalNumericAxiom(object):
00088 def __init__(self, name, op, parts, effect):
00089 self.name = name
00090 self.op = op
00091 self.parts = parts
00092
00093 self.effect = effect
00094 def __str__(self):
00095 return self.name
00096 def __cmp__(self, other):
00097 return cmp((self.__class__, self.name), (other.__class__, other.name))
00098 def __hash__(self):
00099 return hash((self.__class__,self.name))
00100 def dump(self):
00101 print self.name
00102 print "OP: %s" % self.op
00103 for part in self.parts:
00104 print "PART: %s" % part
00105 print "EFF: %s" % self.effect
00106