pddl_types.py
Go to the documentation of this file.
00001 # Renamed from types.py to avoid clash with stdlib module.
00002 # In the future, use explicitly relative imports or absolute
00003 # imports as a better solution.
00004 
00005 import conditions
00006 
00007 import graph
00008 
00009 import itertools
00010 
00011 class Type(object):
00012   def __init__(self, name, basetype_name=None):
00013     self.name = name
00014     self.basetype_name = basetype_name
00015   def __str__(self):
00016     return self.name
00017   def __repr__(self):
00018     return "Type(%s, %s)" % (self.name, self.basetype_name)
00019 
00020 def set_supertypes(type_list):
00021   typename_to_type = {}
00022   child_types = []
00023   for type in type_list:
00024     type.supertype_names = []
00025     typename_to_type[type.name] = type
00026     if type.basetype_name:
00027       child_types.append((type.name, type.basetype_name))
00028   for (desc_name, anc_name) in graph.transitive_closure(child_types):
00029     typename_to_type[desc_name].supertype_names.append(anc_name)
00030 
00031 
00032 class TypedObject(object):
00033   def __init__(self, name, type):
00034     self.name = name
00035     if isinstance(type,list):
00036         self.type = tuple(type)
00037     else:
00038         self.type = type
00039   def __hash__(self):
00040     return hash((self.name, self.type))
00041   def __eq__(self, other):
00042     return self.name == other.name and self.type == other.type
00043   def __ne__(self, other):
00044     return not self == other
00045   def __str__(self):
00046     return "%s: %s" % (self.name, self.type)
00047   def opl_print(self):
00048     return "%s %s" % (self.type, self.name)
00049   def uniquify_name(self, type_map, renamings):
00050     if self.name not in type_map:
00051       type_map[self.name] = self.type
00052       return self
00053     for counter in itertools.count(1):
00054       new_name = self.name + str(counter)
00055       if new_name not in type_map:
00056         renamings[self.name] = new_name
00057         type_map[new_name] = self.type
00058         return TypedObject(new_name, self.type)
00059   def to_untyped_strips(self):
00060     return conditions.Atom(self.type, [self.name])
00061 
00062 # types is used to add (either ...) statements as supertypes
00063 def parse_typed_list(alist, only_variables=False, constructor=TypedObject, functions=False, types=[]):
00064   result = []
00065   while alist:
00066     try:
00067       separator_position = alist.index("-")
00068     except ValueError:
00069       items = alist
00070       if functions:
00071         type = "number"
00072       else:
00073         type = "object"
00074       alist = []
00075     else:
00076       items = alist[:separator_position]
00077       type = alist[separator_position + 1]
00078       if isinstance(type,list):
00079         assert type[0]=="either"
00080         type = tuple(type)
00081         for subtype in type[1:]:
00082             types.append(Type(subtype,type))
00083       alist = alist[separator_position + 2:]
00084     for item in items:
00085       assert not only_variables or item.startswith("?")
00086       entry = constructor(item, type)
00087       result.append(entry)
00088   return result
00089 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03