forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_grounding_space.py
86 lines (61 loc) · 2.62 KB
/
test_grounding_space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import unittest
from hyperon import *
from test_common import HyperonTestCase
class GroundingSpaceTest(HyperonTestCase):
def test_add(self):
kb = GroundingSpaceRef()
kb.add_atom(S("a"))
kb.add_atom(S("b"))
self.assertEqualNoOrder(kb.get_atoms(), [S("a"), S("b")])
def test_remove(self):
kb = GroundingSpaceRef()
kb.add_atom(S("a"))
kb.add_atom(S("b"))
kb.add_atom(S("c"))
self.assertTrue(kb.remove_atom(S("b")))
self.assertEqualNoOrder(kb.get_atoms(), [S("a"), S("c")])
def test_replace(self):
kb = GroundingSpaceRef()
kb.add_atom(S("a"))
kb.add_atom(S("b"))
kb.add_atom(S("c"))
self.assertTrue(kb.replace_atom(S("b"), S("d")))
self.assertEqualNoOrder(kb.get_atoms(), [S("a"), S("d"), S("c")])
def test_complex_query(self):
kb = GroundingSpaceRef()
kb.add_atom(E(S("A"), S("B")))
kb.add_atom(E(S("C"), S("B")))
result = kb.query(E(S(","), E(S("A"), V("x")), E(S("C"), V("x"))))
self.assertEqualNoOrder(result, [{"x": S("B")}])
def test_match_nested_grounding_space(self):
nested = GroundingSpaceRef()
nested.add_atom(E(S("A"), S("B")))
space_atom = G(nested)
runner = MeTTa(env_builder=Environment.test_env())
runner.space().add_atom(space_atom)
runner.tokenizer().register_token("nested", lambda token: space_atom)
result = runner.run("!(match nested (A $x) $x)")
self.assertEqual([[S("B")]], result)
def test_python_wrapped_grounding_space(self):
kb = SpaceRef(GroundingSpace())
kb.add_atom(S("a"))
kb.add_atom(S("b"))
kb.add_atom(S("c"))
self.assertTrue(kb.remove_atom(S("b")))
self.assertEqualNoOrder(kb.get_atoms(), [S("a"), S("c")])
kb.add_atom(E(S("A"), S("B")))
kb.add_atom(E(S("C"), S("B")))
result = kb.query(E(S(","), E(S("A"), V("x")), E(S("C"), V("x"))))
self.assertEqualNoOrder(result, [{"x": S("B")}])
class ExtendedGroundingSpace(GroundingSpace):
def query(self, query_atom):
modified_query = E(S("blue"), query_atom)
result = super().query(modified_query)
result.add_var_binding(V("color"), S("blue"))
return result
def test_python_extended_grounding_space(self):
kb = SpaceRef(self.ExtendedGroundingSpace())
kb.add_atom(E(S("blue"), E(S("A"), S("B"))))
kb.add_atom(E(S("red"), E(S("A"), S("C"))))
result = kb.query(E(S("A"), V("x")))
self.assertEqualNoOrder(result, [{"x": S("B"), "color": S("blue")}])