def test_turn_lamps_on_via_interpreter_matching(self): kb = self.atomese.parse(''' (= (lamp) dev:kitchen-lamp) (= (lamp) dev:bedroom-lamp) ''') target = self.atomese.parse(''' (call:turn_on (lamp)) ''') interpret_until_result(target, kb) self.assertTrue(self._get_device("kitchen-lamp").is_on) self.assertTrue(self._get_device("bedroom-lamp").is_on)
def test_turn_lamps_on_via_grounded_match_atomese(self): Logger.setLevel(Logger.DEBUG) kb = self.atomese.parse(''' (isa dev:kitchen-lamp lamp) (isa dev:bedroom-lamp lamp) ''') self.atomese.add_atom("kb", ValueAtom(kb)) target = self.atomese.parse(''' (match kb (isa $x lamp) (q call:turn_on $x)) ''') interpret_until_result(target, kb) self.assertTrue(self._get_device("kitchen-lamp").is_on) self.assertTrue(self._get_device("bedroom-lamp").is_on)
def test_interpret_grounded_symbol(self): target = GroundingSpace() target.add_atom(E(AddAtom(), ValueAtom(1), ValueAtom(2))) result = interpret_until_result(target, GroundingSpace()) self.assertEqual(result, ValueAtom(3))
def test_match_single_symbol_in_interpret(self): kb = GroundingSpace() kb.add_atom(E(S("="), E(S("f")), S("g"))) target = GroundingSpace() target.add_atom(E(S("f"))) actual = interpret_until_result(target, kb) self.assertEqual(actual, S('g'))
def test_turn_lamps_on_via_grounded_match(self): kb = GroundingSpace() kb.add_atom( E(S("isa"), ValueAtom(self._get_device("bedroom-lamp")), S("lamp"))) kb.add_atom( E(S("isa"), ValueAtom(self._get_device("kitchen-lamp")), S("lamp"))) target = GroundingSpace() target.add_atom( E(MatchAtom(), ValueAtom(kb), E(S("isa"), V("x"), S("lamp")), E(S("q"), CallAtom("turn_on"), V("x")))) interpret_until_result(target, GroundingSpace()) self.assertTrue(self._get_device("kitchen-lamp").is_on) self.assertTrue(self._get_device("bedroom-lamp").is_on)
def test_match_variable_in_target(self): kb = self.atomese.parse(''' (= (isa Fred frog) True) ''') target = self.atomese.parse(''' (isa Fred $x) ''') actual = interpret_until_result(target, kb) self.assertEqual(actual, ValueAtom(True))
def test_interprete_grounded_symbol_atomese(self): text_kb = TextSpace() text_kb.register_token("\\d+(\\.\\d+)?", lambda s: ValueAtom(float(s))) text_kb.register_token("\\+", lambda s: AddAtom()) text_kb.add_string("(+ 2.0 1.0)") target = GroundingSpace() target.add_from_space(text_kb) result = interpret_until_result(target, GroundingSpace()) self.assertEqual(result, ValueAtom(3))
def interpret_and_print_results(target, kb, add_results_to_kb=False): output = "" while True: next = interpret_until_result(target, kb) if next == S('eos'): break print(next) output = output + str(next) + "\n" if add_results_to_kb: kb.add_atom(next) return output
def test_factorial_via_unification(self): atomese = Atomese() kb = atomese.parse(''' (= (if True $then $else) $then) (= (if False $then $else) $else) (= (fact $n) (if (== $n 0) 1 (* (fact (- $n 1)) $n))) ''') target = atomese.parse(''' (fact 5) ''') result = interpret_until_result(target, kb) self.assertEqual(result, ValueAtom(120))
def test_nested_matching(self): Logger.setLevel(Logger.TRACE) kb = self.atomese.parse(''' (isa Fred frog) (isa frog green) ''') self.atomese.add_atom("kb", ValueAtom(kb)) target = self.atomese.parse(''' (match kb (isa $x $y) (q match kb (isa $y $z) (isa $x $z))) ''') actual = interpret_until_result(target, kb) self.assertEqual(actual, E(S('isa'), S('Fred'), S('green')))
def test_turn_lamps_on_via_condition_and_matching(self): kb = self.atomese.parse(''' (= (if True $then $else) $then) (= (if False $then $else) $else) (= (lamp dev:kitchen-lamp) True) (= (lamp dev:bedroom-lamp) True) (= (lamp dev:kettle) False) (= (turn_lamp_on) (if (lamp $x) (call:turn_on $x) nop)) ''') target = self.atomese.parse(''' (turn_lamp_on) ''') interpret_until_result(target, kb) interpret_until_result(target, kb) interpret_until_result(target, kb) self.assertTrue(self._get_device("kitchen-lamp").is_on) self.assertTrue(self._get_device("bedroom-lamp").is_on) self.assertFalse(self._get_device("toilet").is_on)