Skip to content

Commit

Permalink
Merge pull request #730 from vsbogd/add-eval-atom
Browse files Browse the repository at this point in the history
Add Metta.evaluate_atom() method to the Python API
  • Loading branch information
Necr0x0Der authored Jul 5, 2024
2 parents 4b05f05 + aef05f8 commit 359e924
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 0 additions & 2 deletions lib/src/metta/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,6 @@ impl Metta {
state.run_to_completion()
}

// TODO: this method is deprecated and should be removed after switching
// to the minimal MeTTa
pub fn evaluate_atom(&self, atom: Atom) -> Result<Vec<Atom>, String> {
#[cfg(feature = "minimal")]
let atom = if is_bare_minimal_interpreter(self) {
Expand Down
14 changes: 11 additions & 3 deletions python/hyperon/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,22 @@ def run(self, program, flat=False):
"""Runs the MeTTa code from the program string containing S-Expression MeTTa syntax"""
parser = SExprParser(program)
results = hp.metta_run(self.cmetta, parser.cparser)
err_str = hp.metta_err_str(self.cmetta)
if (err_str is not None):
raise RuntimeError(err_str)
self._run_check_for_error()
if flat:
return [Atom._from_catom(catom) for result in results for catom in result]
else:
return [[Atom._from_catom(catom) for catom in result] for result in results]

def evaluate_atom(self, atom):
result = hp.metta_evaluate_atom(self.cmetta, atom.catom)
self._run_check_for_error()
return [Atom._from_catom(catom) for catom in result]

def _run_check_for_error(self):
err_str = hp.metta_err_str(self.cmetta)
if (err_str is not None):
raise RuntimeError(err_str)

class Environment:
"""This class contains the API for configuring the host platform interface used by MeTTa"""

Expand Down
16 changes: 16 additions & 0 deletions python/tests/test_metta.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def test_metta_runner(self):

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)))
Expand Down

0 comments on commit 359e924

Please sign in to comment.