forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_metta.py
100 lines (81 loc) · 2.98 KB
/
test_metta.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
87
88
89
90
91
92
93
94
95
96
97
98
99
import unittest
from hyperon import *
class MettaTest(unittest.TestCase):
def test_adding_tokens_while_parsing(self):
metta = MeTTa(env_builder=Environment.test_env())
atom = metta.parse_single('(A B)')
self.assertEqual(atom, E(S('A'), S('B')))
metta.register_atom('A', S('C'))
atom = metta.parse_single('(A B)')
self.assertEqual(atom, E(S('C'), S('B')))
metta.register_atom('A', S('F'))
atom = metta.parse_single('(A B)')
self.assertEqual(atom, E(S('F'), S('B')))
def test_metta_runner(self):
program = '''
(= (And T T) T)
(= (frog $x)
(And (croaks $x)
(eat_flies $x)))
(= (croaks Fritz) T)
(= (eat_flies Fritz) T)
(= (green $x) (frog $x))
!(green Fritz)
'''
runner = MeTTa(env_builder=Environment.test_env())
result = runner.run(program)
self.assertEqual([[S('T')]], result)
def test_metta_evaluate_atom(self):
program = '''
(= (And T T) T)
(= (frog $x)
(And (croaks $x)
(eat_flies $x)))
(= (croaks Fritz) T)
(= (eat_flies Fritz) T)
(= (green $x) (frog $x))
'''
runner = MeTTa(env_builder=Environment.test_env())
runner.run(program)
result = runner.evaluate_atom(E(S('green'), S('Fritz')))
self.assertEqual([S('T')], result)
def test_incremental_runner(self):
program = '''
!(+ 1 (+ 2 (+ 3 4)))
'''
runner = MeTTa(env_builder=Environment.test_env())
runner_state = RunnerState(runner, program)
step_count = 0
while not runner_state.is_complete():
runner_state.run_step()
step_count += 1
results = runner_state.current_results()
self.assertEqual(repr(results), "[[10]]")
def test_gnd_type_error(self):
program = '''
!(+ 2 "String")
'''
runner = MeTTa(env_builder=Environment.test_env())
result = runner.run(program)
self.assertEqual([[E(S('Error'), ValueAtom('String'), S('BadType'))]], result)
def test_runner_error(self):
program = '''
!(+ 2 3
'''
runner = MeTTa(env_builder=Environment.test_env())
try:
runner.run(program)
self.assertTrue(False, "Parse error expected")
except RuntimeError as e:
self.assertEqual(e.args[0], 'Unexpected end of expression')
def test_match_with_rust_grounded_atom(self):
program = '''
; True is used as a Python grounded object
(grounded True)
; import! is used as a Rust grounded object which can be
; received from Python
!(match &self (grounded import!) Ok)
'''
runner = MeTTa(env_builder=Environment.test_env())
result = runner.run(program)
self.assertEqual([[]], result)