def __init__(self, gdl): """Initialize by parsing a GDL string. Args: gdl (str) : GDL string to parse. """ with Frame(): self._term_record = TermRecord( prefix_gdl_statement_to_prolog(gdl))
def test_rule_variadic_translation(self): rule = Functor(':-', 2) and_ = Functor(',', 2) assert_equal( prefix_gdl_statement_to_prolog('(<= foo 1 2 3 4)'), rule(Term.from_atom_name('foo'), and_(Term.from_atom_name('1'), and_(Term.from_atom_name('2'), and_(Term.from_atom_name('3'), Term.from_atom_name('4'))))))
def check_parse_variables(*variables): variables = ['?' + var for var in variables] for variable in variables: assert_true(prefix_gdl_statement_to_prolog(variable).is_variable()) term_list = prefix_gdl_statements_to_prolog('\n'.join(variables)) terms = [] # Check that all variable terms are unique while not term_list.is_nil(): term = term_list.get_arg(0) assert_not_in(term, terms) terms.append(term) term_list = term_list.get_arg(1) assert_equal(len(terms), len(variables))
def check_parse_statement(target, gdl_statement): assert_equal(prefix_gdl_statement_to_prolog(gdl_statement), target)
def test_rule_symbol_translation(self): assert_equal(prefix_gdl_statement_to_prolog('(<= foo bar)'), Functor(':-', 2)(Term.from_atom_name('foo'), Term.from_atom_name('bar')))
def test_compound_term_no_args(self): assert_equal(prefix_gdl_statement_to_prolog('(foo)'), Functor('foo', 0)())
def test_compound_term_nexted(self): assert_equal( prefix_gdl_statement_to_prolog('(foo (bar 1 2) (baz 3))'), Functor('foo', 2)(Functor('bar', 2)(Term.from_atom_name('1'), Term.from_atom_name('2')), Functor('baz', 1)(Term.from_atom_name('3'))))
def test_compound_repeated_variable(self): term = prefix_gdl_statement_to_prolog('(foo ?X ?X)') assert_equal(term.get_functor(), Functor('foo', 2)) assert_true(term.get_arg(0).is_variable()) assert_true(term.get_arg(1).is_variable()) assert_equal(term.get_arg(0), term.get_arg(1))
def test_compound_term_mixed_atomic(self): term = prefix_gdl_statement_to_prolog('(foo arg1 ?arg2 arg3)') assert_equal(term.get_functor(), Functor('foo', 3)) assert_equal(term.get_arg(0), Term.from_atom_name('arg1')) assert_true(term.get_arg(1).is_variable()) assert_equal(term.get_arg(2), Term.from_atom_name('arg3'))
def test_compound_term_one_atom(self): assert_equal(prefix_gdl_statement_to_prolog('(foo arg)'), Functor('foo', 1)(Term.from_atom_name('arg')))