Ejemplo n.º 1
0
    def test_query(self):
        item = Item(slot='val', slot2='val2')
        self.assertTrue(Query(slot='val').matches(item))
        self.assertTrue(Query().eq('slot', 'val').matches(item))
        self.assertFalse(Query().ne('slot', 'val').matches(item))

        item = Item(slot=3)
        self.assertTrue(Query(slot=3).matches(item))
        self.assertTrue(Query().eq('slot', 3).matches(item))
        self.assertFalse(Query().eq('slot', 2).matches(item))
        self.assertFalse(Query().eq('slot', 4).matches(item))

        self.assertFalse(Query().ne('slot', 3).matches(item))
        self.assertTrue(Query().ne('slot', 2).matches(item))
        self.assertTrue(Query().ne('slot', 4).matches(item))

        self.assertFalse(Query().gt('slot', 3).matches(item))
        self.assertTrue(Query().gt('slot', 2).matches(item))
        self.assertFalse(Query().gt('slot', 4).matches(item))

        self.assertFalse(Query().lt('slot', 3).matches(item))
        self.assertFalse(Query().lt('slot', 2).matches(item))
        self.assertTrue(Query().lt('slot', 4).matches(item))

        self.assertTrue(Query().ge('slot', 3).matches(item))
        self.assertTrue(Query().ge('slot', 2).matches(item))
        self.assertFalse(Query().ge('slot', 4).matches(item))

        self.assertTrue(Query().le('slot', 3).matches(item))
        self.assertFalse(Query().le('slot', 2).matches(item))
        self.assertTrue(Query().le('slot', 4).matches(item))
Ejemplo n.º 2
0
    def run(self, time=300):
        context = Item()

        chunk = None
        done = Query(predicate='isa', object='done')
        while not (chunk and done.matches(chunk)):
            text = self.audition.listen_for_and_encode()
            chunk = self.language.interpret(text)

        while self.time() < time:
            chunk = self.memory.recall(isa='rule')
            self.execute(chunk, context)
Ejemplo n.º 3
0
 def execute(self, name, context=None):
     if name not in self.goals:
         raise Exception
     goal = self.goals[name]
     self.log('executing goal {}'.format(goal.name))
     context = context or Item()
     previous = 'start'
     for action in goal.actions:
         self.memory.recall(goal=goal.name, previous=previous)
         self.log('executing action {}'.format(action))
         for executor in self.executors:
             executed = executor(action, context)
             if executed:
                 break
         previous = action
     return context
Ejemplo n.º 4
0
 def interpreter(words):
     if words[0] == 'read':
         sem = Item(isa='action', type='read', object=words[1])
         pointer = vision.find(isa='pointer')
         if pointer is not None:
             vision.encode(pointer)
             sem.set('x', pointer.x).set('y', pointer.y)
         return sem
     elif words[0] == 'done':
         return Item(isa='done')
     else:
         return Item(isa='action', type=words[0], object=words[1])
Ejemplo n.º 5
0
 def interpret(self, words):
     self._ace_to_owl(' '.join(words))
     if words[0] == 'read':
         sem = Item(isa='action', type='read', object=words[1])
         pointer = self.vision.find(isa='pointer')
         if pointer is not None:
             self.vision.encode(pointer)
             sem.set('x', pointer.x).set('y', pointer.y)
         return sem
     elif words[0] == 'done':
         return Item(isa='done')
     else:
         return Item(isa='action', type=words[0], object=words[1])
Ejemplo n.º 6
0
    def test_item(self):
        item = Item(slot='val', slot2='val2')
        self.assertEqual('val', item.get('slot'))
        self.assertEqual('val', item.slot)
        self.assertEqual('val2', item.get('slot2'))
        self.assertEqual('val2', item.slot2)

        item = Item().set('slot', 'val').set('slot2', 'val2')
        self.assertEqual('val', item.get('slot'))
        self.assertEqual('val2', item.get('slot2'))

        item.unset('slot2')
        self.assertEqual(None, item.get('slot2'))

        item = Item(slot='val', slot2='val2')
        item2 = Item(slot='val', slot2='val2')
        self.assertTrue(item.equals(item))
        self.assertTrue(item2.equals(item2))
        self.assertTrue(item.equals(item2))
        self.assertTrue(item.matches(item))
        self.assertTrue(item2.matches(item2))
        self.assertTrue(item.matches(item2))
        item.unset('slot2')
        self.assertFalse(item.equals(item2))
        self.assertFalse(item.matches(item2))
        self.assertTrue(item2.matches(item))
Ejemplo n.º 7
0
 def interpreter(words):
     if len(words) == 2:
         return Item(isa='action', verb=words[0], object=words[1])
     else:
         return Item(isa=words[0])