atom.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 class Atom(object):
00004 
00005     TERM = "term"
00006     ACTION = "action"
00007     FLUENT = "fluent"
00008     VARIABLE = "variable"
00009     LIST = "list"
00010 
00011     ACTION_NAMES = ["askploc", "greet", "gothrough", "opendoor", "approach"]
00012     FLUENT_NAMES = ["at", "open", "visiting", "beside", "open", 
00013                     "beside", "inside", "goal", "facing"]
00014 
00015     def __init__(self, name, value=None, time=None, negated=False):
00016 
00017         #print "parsing: " + str(name)
00018 
00019         if value != None:
00020             if name.find("(") != -1:
00021                 raise ValueError("Malformed atom - name: %s, value: %s"%
00022                                  str(name),str(value))
00023             self.name = name
00024             if isinstance(value, Atom):
00025                 self.value = value
00026             else:
00027                 self.value = Atom(value)
00028             self.type = None
00029         else:
00030             # Need to parse atom from string 
00031             start = [-1]
00032             end = []
00033             parenthesis_count = 0
00034             self.type = None
00035             for i in range(len(name)):
00036                 if name[i] == "," and parenthesis_count == 0:
00037                     self.type = Atom.LIST
00038                     start.append(i)
00039                     end.append(i)
00040                 if name[i] == "(":
00041                     parenthesis_count+=1
00042                 if name[i] == ")":
00043                     parenthesis_count-=1
00044                 if parenthesis_count < 0:
00045                     raise ValueError(
00046                         "Malformed atom - Need '(' before ')' - %s"%
00047                         str(name))
00048             end.append(len(name))
00049             if parenthesis_count != 0:
00050                 raise ValueError("Malformed atom - Expected ')' at end - %s"%
00051                                  str(name))
00052 
00053             if self.type == Atom.LIST:
00054                 self.name = None
00055                 self.value = [Atom(name[start[i]+1:end[i]]) 
00056                               for i in range(len(start))]
00057             elif name.find("(") == -1:
00058                 self.name = None
00059                 self.type = Atom.VARIABLE
00060                 self.value = name
00061 
00062             if self.type != None:
00063                 if time != None:
00064                     raise ValueError(
00065                         "Malformed atom - List/Var cannot be stamped - %s"%
00066                         str(name))
00067                 if negated:
00068                     raise ValueError(
00069                         "Malformed atom - List/Var cannot be negated - %s"%
00070                         str(name))
00071                 self.time = None
00072                 self.negated = False
00073                 return
00074 
00075             self.name = name[:name.find("(")]
00076             self.value = Atom(name[name.find("(")+1:name.rfind(")")])
00077 
00078         # If we are here, then the Atom is a Term, Fluent or Action
00079         self.negated = False
00080         if self.name[0] == '-':
00081             self.negated = True
00082             self.name = self.name[1:]
00083         if negated:
00084             self.negated = not self.negated
00085 
00086         # Find if stamped
00087         if time == None:
00088             if self.value.type == Atom.LIST:
00089                 try:
00090                     self.time = int(str(self.value.value[-1]))
00091                     self.value.value = self.value.value[:-1]
00092                     if len(self.value.value) == 1:
00093                         self.value = self.value.value[0]
00094                 except ValueError:
00095                     # This is a term
00096                     self.time = None
00097                     self.type = Atom.TERM
00098             else:
00099                 self.time = None
00100                 self.type = Atom.TERM
00101         else:
00102             self.time = time
00103 
00104     def conflicts_with(self, other):
00105         """
00106           Default contradiction test. Tests for hard negation only.
00107         """
00108 
00109         # Check for hard negation
00110         if self.name == other.name and \
00111            self.value == other.value and \
00112            self.negated != other.negated:
00113             return True
00114 
00115     def __str__(self):
00116         prefix = '-' if self.negated else ''
00117         if self.type == Atom.VARIABLE:
00118             return self.value
00119         if self.type == Atom.LIST:
00120             return ",".join([str(atom) for atom in self.value])
00121         if self.type == Atom.TERM:
00122             return prefix + self.name + "(" + str(self.value) + ")"
00123         return prefix + self.name + \
00124                 "(" + str(self.value) + "," + str(self.time) + ")"
00125 
00126     def __repr__(self):
00127         return self.__str__()
00128 
00129     def __eq__(self, other):
00130         return self.name == other.name and \
00131                 self.type == other.type and \
00132                 self.value == other.value and \
00133                 self.negated == other.negated
00134 
00135     def __ne__(self, other):
00136         return not self.__eq__(other)


bwi_planning
Author(s): Piyush Khandelwal , Fangkai Yang
autogenerated on Wed Aug 26 2015 10:54:52