Skip to content

Commit

Permalink
Create working test cases for AST transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
namcsi committed Jun 16, 2023
1 parent f304215 commit ec1104c
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 109 deletions.
1 change: 0 additions & 1 deletion src/renopro/asp/tests/transform/not_bad/output.lp
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#program base.
good(X) :- person(X); not bad(X).
good(dog(X,"spotty")) :- cute(dog(X,"spotty")); not bad(dog(X,"spotty")).
23 changes: 7 additions & 16 deletions src/renopro/asp/tests/transform/not_bad/transform.lp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@ max_index(I,N) :- N = #max{ P : literal_tuple(I,P,_); -1 }, literal_tuple(I,_,_)


% the transformation itself
add_not_bad(LitTuple,N+1,Fargs)
:- rule(_,literal(Lit),literal_tuple(LitTuple)),
literal(Lit,"pos",function(Func)),
function(Func,good,Fargs),
max_index(LitTuple,N).


% effect of add_not_bad
add(literal_tuple(LitTuple,Pos,literal(new_id(LitTuple,0))))
:- add_not_bad(LitTuple,Pos,Fargs).

add(literal(new_id(LitTuple,0),"not",function(new_id(LitTuple,1))))
:- add_not_bad(LitTuple,Pos,Fargs).

add(function(new_id(LitTuple,1),bad,Fargs))
:- add_not_bad(LitTuple,Pos,Fargs).
add((literal_tuple(LT,N+1,literal(new_id(LT,0))));
literal(new_id(LT,0),"not",function(new_id(LT,1)));
function(new_id(LT,1),bad,Fargs))
:- rule(_,literal(L),literal_tuple(LT)),
literal(L,"pos",function(F)),
function(F,good,Fargs),
max_index(LT,N).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a :- prev(b), prev(prev(prev(a))).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(t) :- b((t-1)); a((t-3)).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sad(robot(X),t) :- sad(robot(X),(t-1)); not cheer_up(robot(X),t).


61 changes: 61 additions & 0 deletions src/renopro/asp/tests/transform/prev_to_timepoints/transform.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
% auxiliary atoms
max_index(T,Index) :- Index = #max{ Pos : term_tuple(T,Pos,_); -1 }, term_tuple(T,_,_).
arity(F,N+1) :- function(F,_,term_tuple(T)), max_index(T,N).


% when a literal is not prev

% when atom of a literal is a predicate
add((term_tuple(T,N+1,constant(new_id(T)));
constant(new_id(T),t)))
:- literal(L,_,function(F)), function(F,Name,term_tuple(T)),
Name!=prev, max_index(T,N).

% when atom of a literal is a propositional constant
delete(literal(L,S,constant(F))) :- literal(L,S,constant(F)).
add((literal(L,S,function(F));
function(F,Name,term_tuple(new_id(F)));
term_tuple(new_id(F),0,constant(new_id(F)));
constant(new_id(F),t)))
:- literal(L,S,constant(F)), constant(F,Name).


prev_chain(L,function(F),O)
:- literal(L,_,function(F)), function(F,prev,term_tuple(T)), arity(F,1),
term_tuple(T,0,O).

prev_chain(L,function(F),O)
:- prev_chain(L,_,function(F)), function(F,prev,term_tuple(T)), arity(F,1),
term_tuple(T,0,O).

final_operand(L,O)
:- prev_chain(L,F,O), not prev_chain(L,O,_), O=(constant(_);function(_)).

first_prev(L,F)
:- prev_chain(L,F,O), not prev_chain(L,_,F).

num_prevs(L,N)
:- N = #count{ F : prev_chain(L,F,_) }, prev_chain(L,_,_).

% when final operand is a function
replace(function(F,N,A),function(F,Name,term_tuple(T)))
:- first_prev(L,function(F)), function(F,N,A), final_operand(L,function(O)),
function(O,Name,term_tuple(T)).

add(term_tuple(T,I+1,binary_operation(new_id(O)));
binary_operation(new_id(O),"-",constant(new_id(O)),number(new_id(O)));
constant(new_id(O),t);
number(new_id(O),N))
:- final_operand(L,function(O)), function(O,_,term_tuple(T)), max_index(T,I),
num_prevs(L,N).

% when final operand is a constant
replace(function(F,N,A),function(F,Name,term_tuple(new_id(O))))
:- first_prev(L,function(F)), function(F,N,A), final_operand(L,constant(O)),
constant(O,Name).

add(term_tuple(new_id(O),0,binary_operation(new_id(O)));
binary_operation(new_id(O),"-",constant(new_id(O)),number(new_id(O)));
constant(new_id(O),t);
number(new_id(O),N))
:- final_operand(L,constant(O)), num_prevs(L,N).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
very_sad(robot(X)) :- prev(sad(robot(X))), prev(prev(sad(robot(X)))).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
very_sad(robot(X),t) :- sad(robot(X),(t-1)); sad(robot(X),(t-2)).
3 changes: 0 additions & 3 deletions src/renopro/asp/tests/transform/robot_output.lp

This file was deleted.

30 changes: 0 additions & 30 deletions src/renopro/asp/tests/transform/robot_reified.lp

This file was deleted.

27 changes: 0 additions & 27 deletions src/renopro/asp/tests/transform/robot_reified_flat.lp

This file was deleted.

21 changes: 0 additions & 21 deletions src/renopro/asp/tests/transform/robot_transform_flat.lp

This file was deleted.

28 changes: 17 additions & 11 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class TestTransform(TestCase):
"""Tests for ast transformations defined via a meta-encodings."""

base_str = "#program base.\n"

def test_transform_add_literal(self):
"""Test adding an additional literal to the body of rules.
Expand All @@ -22,18 +24,22 @@ def test_transform_add_literal(self):
rast.reify_files([files_dir / "input.lp"])
rast.transform(meta_files=[files_dir / "transform.lp"])
rast.reflect()
transformed_str = rast.program_string
with (files_dir / "output.lp").open("r") as good_output:
good_output_str = good_output.read().strip()
self.assertEqual(transformed_str.strip(), good_output_str)
transformed_str = rast.program_string.strip()
with (files_dir / "output.lp").open("r") as output:
expected_str = self.base_str + output.read().strip()
self.assertEqual(transformed_str, expected_str)

def test_transform_add_time(self):
"""Test transforming a temporal logic program with the
previous operator into a program with explicit time points."""
rast = ReifiedAST()
rast.reify_files([test_transform_files / "robot_input.lp"])
rast.transform(meta_files=[test_transform_files / "robot_transform.lp"])
transformed_str = rast.reflect()
with (test_transform_files / "robot_output.lp").open("r") as robot_output:
robot_output_str = robot_output.read().strip()
self.assertEqual(transformed_str.strip(), robot_output_str)
for testname in ["sad", "very_sad", "constant"]:
with self.subTest(testname=testname):
rast = ReifiedAST()
files_dir = test_transform_dir / "prev_to_timepoints"
rast.reify_files([files_dir / (testname + "_input.lp")])
rast.transform(meta_files=[files_dir / "transform.lp"])
rast.reflect()
transformed_str = rast.program_string.strip()
with (files_dir / (testname + "_output.lp")).open("r") as output:
expected_str = self.base_str + output.read().strip()
self.assertEqual(transformed_str, expected_str)

0 comments on commit ec1104c

Please sign in to comment.