def start_wait_for(self, query=None, **kwargs): if not query: query = Query(**kwargs) self.find_buffer.acquire() self.think('wait for {}'.format(query)) visual = self._try_find(query.eq('seen', False)) if visual is not None: self._finish_wait_for(visual) else: self.wait_for_query = query
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)
def search_for(self, query, target): if query is None: query = Query() query = query.eq('seen', False) visual = self.find(query) obj = self.encode(visual) if visual else None while visual and target and obj != target: visual = self.find(query) obj = self.encode(visual) if visual else None if obj and obj == target: return visual else: return None
def search_for(self, query, target): if query is None: query = Query() query = query.eq('seen', False) visual = self.find(query) obj = self.encode(visual) while (visual is not None and obj is not None and not obj.equals(target)): visual = self.find(query) obj = self.encode(visual) if obj is not None and obj.equals(target): return visual else: return None
def executor(self, action, context): """Provides executors that take semantic actions + context and executes them""" if action.type == 'read': query = Query(x=action.x, y=action.y) context.set(action.object, self.vision.find_and_encode(query)) elif action.type == 'type': self.typing.type(context.get(action.object))
def start_recall(self, query=None, **kwargs): if not query or not isinstance(query, Query): query = Query(**kwargs) self.buffer.acquire() self.think('recall {}'.format(query)) self.log('recalling {}'.format(query)) chunk = self._get_chunk(query) self._start_recall(chunk)
def test_memory(self): agent = Agent(output=False) memory = Memory(agent) memory.add(Chunk(isa='cat', name='Whiskers', owner='Jane')) chunk = memory.recall(Query(isa='cat')) self.assertEqual('Jane', chunk.get('owner')) self.assertEqual('Whiskers', chunk.id) memory.add(isa='dog', name='Spot', owner='John') self.assertEqual('Spot', memory.recall(isa='dog').name) self.assertEqual('Whiskers', memory.recall( Query().ne('isa', 'dog')).name) chunk = Chunk(isa='cat', name='Whiskers', owner='Jen') self.assertEqual('Whiskers', chunk.id) memory.add(chunk) self.assertEqual('Whiskers~2', chunk.id) self.assertEqual(chunk, memory.get(chunk.id))
def run(self, time): while self.time() < time: self.vision.wait_for(isa='letter', seen=False) visual = self.vision.search_for( Query(isa='letter', color='red', region='vs'), 'X') if visual: # self.log('target present') self.motor.type('w') self.wait(1.0) else: # self.log('target absent') self.motor.type('r') self.wait(1.0)
def start_find(self, query=None, **kwargs): if not query: query = Query(**kwargs) self.find_buffer.acquire() self.think("find {}".format(query)) match = self._try_find(query) duration = self.find_time if match is not None: def fn(): for fn in self.attend_fns: fn(match) self.find_buffer.set(match, duration, "found {}".format(match), fn) else: self.find_buffer.clear(duration, "find failed")
def test_vision(self, output=False): agent = Agent(output=output) eyes = Eyes(agent) vision = Vision(agent, eyes) eyes.move_to(100, 100) vision.add(Visual(50, 50, 20, 20, 'text'), "Hello") vision.add(Visual(150, 150, 20, 20, 'text'), "Goodbye") self.assertEqual("Hello", vision.find_and_encode( Query(isa='text').lt('x', 100))) self.assertEqual("Goodbye", vision.find_and_encode(seen=False)) vision.start_wait_for(isa='cross') agent.wait(2.0) vision.add(Visual(200, 200, 20, 20, 'cross'), "cross") self.assertEqual("cross", vision.encode(vision.get_found())) self.assertAlmostEqual(2.7, agent.time(), 1) agent.wait_for_all()
def test_vision(self, output=False): agent = Agent(output=output) display = Environment().display eyes = Eyes(agent) vision = Vision(agent, display, eyes) eyes.move_to(100, 100) display.add_text(50, 50, 'Hello') display.add_text(150, 150, 'Goodbye') self.assertEqual( "Hello", vision.find_and_encode(Query(isa='text').lt('x', 100))) self.assertEqual("Goodbye", vision.find_and_encode(seen=False)) vision.start_wait_for(isa='cross') agent.wait(2.0) display.add(200, 200, 20, 20, 'cross', "cross") self.assertEqual("cross", vision.encode(vision.get_found())) self.assertAlmostEqual(2.7, agent.time(), 1) agent.wait_for_all()
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))
def executor(action, context): query = Query(x=action.x, y=action.y) context.set(action.object, vision.find_and_encode(query))
def _construct_query(self, query, heard, kwargs): if query is None: query = Query(**kwargs) if heard is not None and not query.has('heard'): query = query.eq('heard', heard) return query
def executor(action, context): if action.type == 'read': query = Query(x=action.x, y=action.y) context.set(action.object, vision.find_and_encode(query)) elif action.type == 'type': motor.type(context.get(action.object))
def execute(self, action, context): if action.type == 'read': query = Query(x=action.x, y=action.y) context.set(action.object, self.vision.find_and_encode(query)) elif action.type == 'type': self.typing.type(context.get(action.object))
def find(self, query=None, **kwargs): if not query: query = Query(**kwargs) self.start_find(query) return self.get_found()
def recall(self, query=None, **kwargs): if not query or not isinstance(query, Query): query = Query(**kwargs) self.start_recall(query) return self.get_recalled()
def find_and_encode(self, query=None, **kwargs): if not query: query = Query(**kwargs) return self.encode(self.find(query))