00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 from common import Condition, Precondition, Effect, VariableEffect, Action
00033
00034
00035 class Memory(object):
00036 """Store condition data not representing (or mocking) real world."""
00037
00038 def __init__(self):
00039 self._memory = {}
00040
00041 def __repr__(self):
00042 return '<Memory %s>' % self._memory
00043
00044 def declare_state(self, state_name, value=None):
00045 """Note that the value will not be stored if the state_name already
00046 exists, even if the current value is None."""
00047 if state_name not in self._memory:
00048 self._memory[state_name] = value
00049
00050 def get_value(self, state_name):
00051 return self._memory[state_name]
00052
00053 def set_value(self, state_name, value):
00054 self._memory[state_name] = value
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 class MemorySetVarAction(Action):
00071
00072 def __init__(self, memory, state_name, new_value, preconditions, effects):
00073 Action.__init__(self, preconditions, effects)
00074 self._memory = memory
00075 self._state_name = state_name
00076 self._new_value = new_value
00077
00078 self._memory.declare_state(self._state_name)
00079
00080 def __repr__(self):
00081 return '<%s state_name=%s new=%s>' % (self.__class__.__name__, self._state_name, self._new_value)
00082
00083 def run(self, next_worldstate):
00084 self._memory.set_value(self._state_name, self._new_value)
00085
00086
00087 class MemoryChangeVarAction(MemorySetVarAction):
00088
00089 def __init__(self, memory, state_name, old_value, new_value):
00090 MemorySetVarAction.__init__(self, memory, state_name, new_value,
00091 [Precondition(Condition.get(state_name), old_value)],
00092 [Effect(Condition.get(state_name), new_value)]
00093 )
00094 self._old_value = old_value
00095
00096 def __str__(self):
00097 return '%s:%s=%s->%s' % (self.__class__.__name__,
00098 self._state_name, self._old_value, self._new_value)
00099
00100 def __repr__(self):
00101 return '<%s state_name=%s old=%s new=%s>' % (self.__class__.__name__,
00102 self._state_name, self._old_value, self._new_value)
00103
00104
00105 class MemoryIncrementerAction(Action):
00106
00107 def __init__(self, memory, state_name, increment=1):
00108 self._condition = Condition.get(state_name)
00109 Action.__init__(self, [], [VariableEffect(self._condition)])
00110 self._memory = memory
00111 self._state_name = state_name
00112 self._increment = increment
00113 self._memory.declare_state(self._state_name)
00114
00115 def __str__(self):
00116 return '%s:%s%s=%s' % (self.__class__.__name__,
00117 self._state_name,
00118 ('-' if self._increment < 0 else '+'),
00119 abs(self._increment))
00120
00121 def __repr__(self):
00122 return '<%s state_name=%s incr=%s>' % (self.__class__.__name__,
00123 self._state_name, self._increment)
00124
00125 def cost(self):
00126 return self.validate_cost(abs(2 - float(1) / abs(self._increment)))
00127
00128 def run(self, next_worldstate):
00129 self._memory.set_value(self._state_name, self._memory.get_value(self._state_name) + self._increment)
00130
00131 def _generate_variable_preconditions(self, var_effects, worldstate, start_worldstate):
00132 effect = var_effects.pop()
00133 assert effect._condition is self._condition
00134 precond_value = worldstate.get_condition_value(effect._condition) - self._increment
00135 return [Precondition(effect._condition, precond_value, None)]
00136
00137
00138
00139
00140
00141 class MemoryCondition(Condition):
00142
00143 def __init__(self, memory, state_name, value=None):
00144 Condition.__init__(self, state_name)
00145 self._memory = memory
00146 self._state_name = state_name
00147 memory.declare_state(self._state_name)
00148 if value is not None:
00149 memory.set_value(self._state_name, value)
00150
00151 def get_value(self):
00152 return self._memory.get_value(self._state_name)
00153