def FindSpace(self, item): """ haz sitio sobre el objeto X """ message = 'FindSpace %s' % (item) if self.pickable(item): return GoalTree(message) return GoalTree(message, [ self.ClearTop(item), self.FindSpace(item) ])
def Grasp(self, item): """ pilla el bloque X """ message = 'Grasp %s' % (item) gtree = GoalTree(message) if self._table.get(item) != None: gtree.add_child(self.ClearTop(item)) return gtree
def GetRidOf(self, item): """ saca el objeto X del medio (edited) """ message = 'GetRidOf %s' % (item) gtree = GoalTree(message) if self._table.get(item) != None: gtree.add_child(self.PutOnTable(item)) return gtree
def test_init(self): g = GoalTree('Test') self.assertEqual(g.parent, None) self.assertEqual(g.name, 'Test') g = GoalTree('parent', [ GoalTree('child')]) self.assertEqual(g._children[0].parent, 'parent') self.assertEqual(g._children[0].name, 'child') self.assertEqual(g.name, 'parent') self.assertEqual(g.parent, None)
def test_why(self): g = GoalTree('root', [ GoalTree('child1', [ GoalTree('subchild1'), GoalTree('subchild2') ]), GoalTree('child2') ]) self.assertEqual(g.Why('F**k the police'), 'I didn\'t.') self.assertEqual(g.Why('root'), 'Because you told me.') self.assertEqual(g.Why('subchild1'), 'Because I was trying to child1')
def test_how(self): g = GoalTree('root', [ GoalTree('child1', [ GoalTree('subchild1'), GoalTree('subchild2') ]), GoalTree('child2') ]) self.assertEqual(g.How('child1'), 'Following this steps: subchild1 & subchild2') self.assertEqual(g.How('Not exists'), 'I didn\'t.') self.assertEqual(g.How('child2'), 'By doing it.')
def PutOn(self, item1, item2): """ pon el objeto X encima del objeto Y """ message = 'PutOn %s %s' % (item1, item2) dest = self._table.get(item2) return GoalTree(message, [ self.FindSpace(item2), self.Grasp(item1), self.Move(item1, dest.col), self.Ungrasp(item1) ])
def ClearTop(self, item): """ despeja la parte superior del objeto X """ message = 'ClearTop %s' % (item) gtree = GoalTree(message) stack = [] blk = self._table.get(item) if self.pickable(blk.name): return gtree for row in range(blk.row +1, self._table.height): if self._table.grid[blk.col][row] != None: stack.append(self._table.grid[blk.col][row].name) while len(stack) > 0: new_blk = stack.pop() gtree.add_child(self.GetRidOf(new_blk)) return gtree
def PutOnTable(self, item): """ pon el objeto X en el suelo (espacio no definido) """ message = 'PutOnTable %s' % (item) dest = self.FindSpaceOnTable() if dest < 0: raise "No space to get rid of the block" return GoalTree(message, [ self.Grasp(item), self.Move(item, dest), self.Ungrasp(item) ])
def Move(self, item, col): """ mueve el objeto X a la columna Y (y apilalo) """ message = 'Move %s %i' % (item, col) picked = self._table.get(item) if picked == None: raise Exception("Cant move") self._table.grid[picked.col][picked.row] = None next_row = self._table.next_empty(col) self._table.set(col, next_row, picked) return GoalTree(message)
def Ungrasp(self, item): """ suelta el bloque X """ message = 'Ungrasp %s' % (item) return GoalTree(message)
def test_bfs(self): g = GoalTree('root', [ GoalTree('child1', [ (GoalTree('subchild1'))]), GoalTree('child2')]) subchild1 = g.bfs('subchild1') self.assertEqual(subchild1.parent, 'child1') self.assertEqual(subchild1.name, 'subchild1')
def test_add_child(self): g = GoalTree('Test') g.add_child(GoalTree('child')) self.assertEqual(g._children[0].parent, 'Test') self.assertEqual(g._children[0].name, 'child')