def test_recursion_detection_recognizes_recursion_with_choice_rules(): prg = """ {x(X)} :- y(X). y(Y) :- x(Y). """ rule_set = [] clingo.parse_program(prg, lambda stm: filter_prg(stm, rule_set)) rt = FindRecursiveRulesTransformer() for rule in rule_set: rt.visit(rule) g = rt.make_dependency_graph(rule_set) g = remove_loops(g) assert len(g) > 0, "dependency graph should be created." assert len( g.nodes) == 2, "There should be rule nodes in the dependency graph." assert len( g.edges) == 2, "There should be no dependency in the dependency graph." assert len(list(nx.simple_cycles( g))) == 1, "There should be a circle in the dependency graph."
def test_recursion_detection_doesnt_add_conditionals(): prg = """ 1 { q(X,Y) : number(Y) } 1 :- number(X). 1 { q(X,Y) : number(X) } 1 :- number(Y). """ rule_set = [] clingo.parse_program(prg, lambda stm: filter_prg(stm, rule_set)) rt = FindRecursiveRulesTransformer() for rule in rule_set: rt.visit(rule) g = rt.make_dependency_graph(rule_set) g = remove_loops(g) assert len(g) > 0, "dependency graph should be created." assert len( g.nodes ) == 2, "There should be two rule nodes in the dependency graph." assert len( g.edges) == 0, "There should be no dependency in the dependency graph." assert len(list(nx.simple_cycles( g))) == 0, "There should be no circles in the dependency graph."
def test_recursion_detection_doesnt_confuse_choice_rules(): prg = """ a. {b} :- a. 1{x(b)} :- b. """ rule_set = [] clingo.parse_program(prg, lambda stm: filter_prg(stm, rule_set)) rt = FindRecursiveRulesTransformer() for rule in rule_set: rt.visit(rule) g = rt.make_dependency_graph(rule_set) g = remove_loops(g) assert len(g) > 0, "dependency graph should be created." assert len( g.nodes) == 3, "There should be rule nodes in the dependency graph." assert len( g.edges ) == 2, "There should be two dependency in the dependency graph." assert len(list(nx.simple_cycles( g))) == 0, "There should be no circles in the dependency graph."
def str_to_ast(rule): ast = [] clingo.parse_program(rule, lambda stm: filter_prg(stm, ast)) return ast