[GRASS-SVN] r54801 - in grass-addons/grass7/raster/r.agent: libagent tests

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jan 27 16:40:21 PST 2013


Author: mic
Date: 2013-01-27 16:40:20 -0800 (Sun, 27 Jan 2013)
New Revision: 54801

Modified:
   grass-addons/grass7/raster/r.agent/libagent/playground.py
   grass-addons/grass7/raster/r.agent/libagent/world.py
   grass-addons/grass7/raster/r.agent/tests/test_aco.py
   grass-addons/grass7/raster/r.agent/tests/test_world.py
Log:
get some agents into the world

Modified: grass-addons/grass7/raster/r.agent/libagent/playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-01-27 23:33:09 UTC (rev 54800)
+++ grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-01-28 00:40:20 UTC (rev 54801)
@@ -106,3 +106,15 @@
         ns = random.randrange(0,self.region["rows"])
         nw = random.randrange(0,self.region["cols"])
         return [ns,nw]
+
+    def isvalidposition(self, position):
+        """
+        Test if a position realy is on the playground
+        @return boolean True if on, False if off the playground
+        """
+        if self.region["s"] <= position[0] < self.region["n"] and \
+            self.region["w"] <= position[1] < self.region["e"]:
+            return True
+        else:
+            return False
+

Modified: grass-addons/grass7/raster/r.agent/libagent/world.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/world.py	2013-01-27 23:33:09 UTC (rev 54800)
+++ grass-addons/grass7/raster/r.agent/libagent/world.py	2013-01-28 00:40:20 UTC (rev 54801)
@@ -56,6 +56,22 @@
         """
         return self.playground.getlayer(layername)
 
+    def findposition(self, position=None):
+        """
+        Find a given position on the playground, i.e. test if the
+        given position is valid or invent a new one by creating a
+        random one
+        @param list optional coordinates on the playground or None for random
+        @return list position or False if the given one was invalid
+        """
+        if position:
+            if self.playground.isvalidposition(position):
+                return position
+            else:
+                return False
+        else:              
+            return self.playground.getrandomposition()
+
     def bear(self, timetolive, position=None, agenttype=None):
         """
         Set a new agent into the world
@@ -63,19 +79,23 @@
         @param list coordinates to put the agent or none for a random position
         @return agent the newly created agent
         """
+        position = self.findposition(position)
         if not position:
-            position = self.playground.getrandomposition()
+            raise error.DataError("world.bear", "invalid position")
         agent = self.agenttype(timetolive, self, position)
         self.agents.append(agent)
         return agent
 
-    def move(self, agent, position):
+    def move(self, agent, position=None):
         """
         Set agent to a new position
         @param agent to be moved
-        @param list coordinates of the new position
+        @param list coordinates of the new position or none for a random one
         """
-        pass
+        position = self.findposition(position)
+        if not position:
+            raise error.DataError("world.bear", "invalid position")
+        agent.setposition(position)
 
     def getposition(self, agent):
         """

Modified: grass-addons/grass7/raster/r.agent/tests/test_aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_aco.py	2013-01-27 23:33:09 UTC (rev 54800)
+++ grass-addons/grass7/raster/r.agent/tests/test_aco.py	2013-01-28 00:40:20 UTC (rev 54801)
@@ -17,6 +17,9 @@
     def test_getlayer(self):
         pass
 
+    def findposition(self):
+        pass
+
     def test_bear(self):
         pass
 

Modified: grass-addons/grass7/raster/r.agent/tests/test_world.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_world.py	2013-01-27 23:33:09 UTC (rev 54800)
+++ grass-addons/grass7/raster/r.agent/tests/test_world.py	2013-01-28 00:40:20 UTC (rev 54801)
@@ -2,7 +2,7 @@
 import unittest2 as unittest
 #import unittest
 
-from libagent import world
+from libagent import world, error
 
 class TestWorld(unittest.TestCase):
     def setUp(self):
@@ -23,13 +23,25 @@
         self.assertIs(self.world.getlayer("foo"),
                         self.world.playground.layers["foo"])
 
+    def findposition(self):
+        self.assertFalse(self.world.findposition([-1,-1]))
+        self.assertNotNone(self.world.findposition([0,0]))
+        self.assertEqual(0,*self.world.findposition())
+
     def test_bear(self):
-        agent = self.world.bear(1, [1,1])
+        self.assertRaises(error.DataError, self.world.bear, *(1, [-1,-1]))
+        agent = self.world.bear(1, [0,0])
         self.assertIsInstance(agent, self.world.agenttype)
         self.assertIs(agent, self.world.agents.pop())
+        agent = self.world.bear(1)
+        self.assertIs(agent, self.world.agents.pop())
 
     def test_move(self):
-        pass
+        agent = self.world.bear(1, [0,0])
+        self.assertRaises(error.DataError, self.world.move, *(agent, [-1,-1]))
+        position = [0,0]
+        self.world.move(agent, position)
+        self.assertEqual(agent.position, position)
 
     def test_getposition(self):
         pass



More information about the grass-commit mailing list