memory.py
Go to the documentation of this file.
00001 # Copyright (c) 2013, Felix Kolbe
00002 # All rights reserved. BSD License
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 # * Redistributions of source code must retain the above copyright
00009 #   notice, this list of conditions and the following disclaimer.
00010 #
00011 # * Redistributions in binary form must reproduce the above copyright
00012 #   notice, this list of conditions and the following disclaimer in the
00013 #   documentation and/or other materials provided with the distribution.
00014 #
00015 # * Neither the name of the {organization} nor the names of its
00016 #   contributors may be used to endorse or promote products derived
00017 #   from this software without specific prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 #    def matches(self, memory):
00057 #        for (k, v) in self._memory.iteritems():
00058 #            if k in memory._memory and memory._memory[k] != v:
00059 #                return False
00060 #        return True
00061 
00062 
00063 #class MemoryAction(Action):
00064 #
00065 #    def __init(self, memory, preconditions, effects):
00066 #        Action.__init__(self, preconditions, effects)
00067 #        self._memory = memory
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()  # this action has one variable effect
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 


rgoap
Author(s): Felix Kolbe
autogenerated on Sun Oct 5 2014 23:53:02