Skip to content

Commit

Permalink
Add support for interval terms. Rewrite test docstrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
namcsi committed Jul 17, 2023
1 parent 6537d17 commit f67c813
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 57 deletions.
23 changes: 7 additions & 16 deletions src/renopro/asp/tests/reify/good_ast/function.lp
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
% reified fact representation of program:
% #program base.
% rel(2,1) :- rel(1,2).
% next(move(a)).

program("base",constant_tuple(0),statement_tuple(1)).
statement_tuple(1,0,rule(2)).
rule(2,literal(3),literal_tuple(9)).
rule(2,literal(3),literal_tuple(11)).
% facts representing head literal rel(2,1).
literal(3,"pos",atom(4)).
atom(4,function(5)).
function(5,rel,term_tuple(6)).
term_tuple(6,0,number(7)).
number(7,2).
term_tuple(6,1,number(8)).
number(8,1).
% facts representing body literal rel(1,2).
literal_tuple(9,0,literal(10)).
literal(10,"pos",atom(11)).
atom(11,function(12)).
function(12,rel,term_tuple(13)).
term_tuple(13,0,number(14)).
number(14,1).
term_tuple(13,1,number(15)).
number(15,2).
function(5,next,term_tuple(6)).
term_tuple(6,0,function(7)).
function(7,move,term_tuple(8)).
term_tuple(8,0,function(9)).
function(9,a,term_tuple(10)).
15 changes: 0 additions & 15 deletions src/renopro/asp/tests/reify/good_ast/nested_function.lp

This file was deleted.

31 changes: 31 additions & 0 deletions src/renopro/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,31 @@ class Meta:
Term_Field.fields.append(Binary_Operation1.Field)


class Interval(Predicate):
"""Predicate representing an interval term.
id: Identifier of the interval.
left: Left bound of the interval.
right: Right bound of the interval.
"""

id = Identifier_Field(default=lambda: next(id_count))
left = Term_Field
right = Term_Field


class Interval1(ComplexTerm):
"Term identifying a child interval predicate."
id = Identifier_Field(default=lambda: next(id_count))

class Meta:
# pylint: disable=too-few-public-methods, missing-class-docstring
name = "interval"


Term_Field.fields.append(Interval1.Field)


class Atom(Predicate):
"""Predicate representing a symbolic atom.
Expand Down Expand Up @@ -465,6 +490,8 @@ class Program(Predicate):
Function1,
Binary_Operation,
Binary_Operation1,
Interval,
Interval1,
Atom,
Atom1,
Literal,
Expand Down Expand Up @@ -495,6 +522,8 @@ class Program(Predicate):
Function1,
Binary_Operation,
Binary_Operation1,
Interval,
Interval1,
Atom,
Atom1,
Literal,
Expand All @@ -519,6 +548,7 @@ class Program(Predicate):
Function,
Term_Tuple,
Binary_Operation,
Interval,
Atom,
Literal,
Literal_Tuple,
Expand All @@ -536,6 +566,7 @@ class Program(Predicate):
Function,
Term_Tuple,
Binary_Operation,
Interval,
Atom,
Literal,
Literal_Tuple,
Expand Down
17 changes: 17 additions & 0 deletions src/renopro/reify.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ def _reify_symbol_string(self, symb):
self._reified.add(preds.String(id=string1.id, value=symb.string))
return string1

@reify_node.register(ASTType.Interval)
def _reify_interval(self, node):
interval1 = preds.Interval1()
left = self.reify_node(node.left)
right = self.reify_node(node.right)
interval = preds.Interval(id=interval1.id, left=left, right=right)
self._reified.add(interval)
return interval1

def _reflect_child_pred(self, parent_fact, child_id_fact):
"""Utility function that takes a unary ast predicate
containing only an identifier pointing to a child predicate,
Expand Down Expand Up @@ -570,6 +579,14 @@ def _reflect_binary_operation(self, operation: preds.Binary_Operation) -> AST:
right=self._reflect_child_pred(operation, operation.right),
)

@reflect_predicate.register
def _reflect_interval(self, interval: preds.Interval) -> AST:
return ast.Interval(
location=DUMMY_LOC,
left=self._reflect_child_pred(interval, interval.left),
right=self._reflect_child_pred(interval, interval.right),
)

def reflect(self):
"""Convert stored reified ast facts into a (sequence of) AST
node(s), and it's string representation.
Expand Down
42 changes: 16 additions & 26 deletions tests/test_reify_reflect.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def setUp(self):
preds.id_count = count()

def test_reify_prop_fact(self):
"""Test reification of a propositional fact."""
"Test reification and reflection of a propositional fact."
self.assertReifyReflectEqual("a.", ["prop_fact.lp"])
rast = ReifiedAST()
rast.reify_string("a.")
Expand All @@ -136,55 +136,45 @@ def test_reify_prop_fact(self):
self.assertEqual(rast.program_ast, statements)

def test_reify_prop_normal_rule(self):
"""
Test reification of a normal rule containing only propositional atoms.
"""
"Test reification and reflection of a normal rule containing only propositional atoms."
self.assertReifyReflectEqual("a :- b; not c.", ["prop_normal_rule.lp"])

def test_reify_function(self):
"""
Test reification of a variable-free normal rule with a function.
"""
self.assertReifyReflectEqual("rel(2,1) :- rel(1,2).", ["function.lp"])
"Test reification and reflection of a variable-free normal rule with a symbolic atom."
self.assertReifyReflectEqual("rel(2,1) :- rel(1,2).", ["atom.lp"])

def test_reify_nested_function(self):
"""
Test reification of a rule with nested functions.
"""
self.assertReifyReflectEqual("next(move(a)).", ["nested_function.lp"])
"Test reification and reflection of a rule with a function term."
self.assertReifyReflectEqual("next(move(a)).", ["function.lp"])

def test_reify_variable(self):
"""
Test reification of normal rule with variables.
"""
"Test reification and reflection of normal rule with variable terms."
self.assertReifyReflectEqual("rel(Y,X) :- rel(X,Y).", ["variable.lp"])

def test_reify_string(self):
"""
Test reification of normal rule with string.
"""
"Test reification and reflection of normal rule with a string term."
self.assertReifyReflectEqual('yummy("carrot").', ["string.lp"])

def test_reify_constant_term(self):
"""
Test reification of normal rule with constant term.
"""
"Test reification and reflection of a normal rule with constant term."
self.assertReifyReflectEqual("good(human).", ["constant_term.lp"])

def test_reify_interval(self):
"Test reification and reflection of a normal rule with an interval term."
self.assertReifyReflectEqual("a((1..3)).", ["interval.lp"])

def test_reify_binary_operation(self):
"""
Test reification of a normal rule with a binary operation.
"""
"Test reification and reflection of a normal rule with a binary operation."
self.assertReifyReflectEqual("equal((1+1),2).", ["binary_operation.lp"])

def test_reify_external_false(self):
"""Test reification of an external statement with default value false."""
"Test reification of an external statement with default value false."
self.assertReifyReflectEqual(
"#external a(X) : c(X); d(e(X)). [false]", ["external.lp"]
)

def test_reify_program_params(self):
"""Test reification of a program statement with parameters."""
"Test reification and reflection of a program statement with parameters."
self.assertReifyReflectEqual("#program acid(k).", ["program_acid.lp"])

def test_reify_node_failure(self):
Expand Down

0 comments on commit f67c813

Please sign in to comment.