def test_modules(self): t = ''' (:domain test_domain (:module mod_a) (:module mod_b *)) ''' actual_domain = domain.build(s_expression.parse(t)) self.assertEqual(set([('mod_a', None), ('mod_b', '*')]), set(actual_domain.modules))
def test_and_chain_is_flattened(self): t = ''' ((not (and (a) (and (b) (or (c) (d)) (and (e) (f)))))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.flatten(actual) # (a && (b && (c && d))) -> a && b && c && d expected = node_type.LogicalOp( op = node_type.OP_NOT, children = [ node_type.LogicalOp( op = node_type.OP_AND, children = [ node_type.Atom(name='a'), node_type.Atom(name='b'), node_type.LogicalOp( op = node_type.OP_OR, children = [ node_type.Atom(name='c'), node_type.Atom(name='d'), ]), node_type.Atom(name='e'), node_type.Atom(name='f'), ]), ]) self.assertEqual(expected, actual)
def test_vars_are_bound_or_free_or_constants(self): t = ''' ((or (a ?method_var str_const1) (b ?method_var str_const2)) (c ?method_var ?out_var) (d ?out_var 123.0)) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) bound_variables = ['?method_var'] logical_expression.classify_variables(bound_variables=bound_variables, root_node=actual) # bound_variables argument isn't mutable self.assertEqual(['?method_var'], bound_variables) a_node = list(self.world_state_facts(actual, 'a'))[0] b_node = list(self.world_state_facts(actual, 'b'))[0] c_node = list(self.world_state_facts(actual, 'c'))[0] d_node = list(self.world_state_facts(actual, 'd'))[0] self.assertEqual(set(['?method_var']), set(a_node.bound_variables)) self.assertEqual(set(['str_const1']), set(a_node.constants)) self.assertEqual(set(['?method_var']), set(b_node.bound_variables)) self.assertEqual(set(['str_const2']), set(b_node.constants)) self.assertEqual(set(['?method_var']), set(c_node.bound_variables)) self.assertEqual(set(), set(c_node.constants)) self.assertEqual(set(['?out_var']), set(d_node.bound_variables)) self.assertEqual(set([123.0]), set(d_node.constants))
def test_de_morgans_law(self): t = ''' ((not (and (a) (or (b) (not (c)))))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_nnf(actual) # !(a && (b || !c)) -> !a || !(b || c) -> !a || (!b && c) expected = node_type.LogicalOp( op = node_type.OP_OR, children = [ node_type.LogicalOp( op = node_type.OP_NOT, children = [ node_type.Atom(name='a') ]), node_type.LogicalOp( op = node_type.OP_AND, children = [ node_type.LogicalOp( op = node_type.OP_NOT, children = [node_type.Atom(name='b')]), node_type.Atom(name='c'), ]) ]) self.assertEqual(expected, actual)
def main(): if len(sys.argv) == 3: src = sys.argv[1] dst = sys.argv[2] self_dir = os.path.dirname(os.path.realpath(__file__)) domain_std_path = os.path.join(self_dir, 'domain_std.py') if not os.path.isfile(domain_std_path): print 'Error: Cant find domain_std module.' sys.exit(1) try: s = s_expression.parse(open(src, 'rt').read()) d = domain.build(s) c = domain_code.generate( d, inline_modules=[open(domain_std_path, 'rt')]) except (s_expression.MalformedExpressionError, s_expression.SyntaxException) as e: print 'Error: line=%s column=%s' % (e.pos[0], e.pos[1]) sys.exit(1) with open(dst, 'wt') as f: f.write('# Auto-generated with translate_domain.py\n\n') f.write(c) print 'Done!' else: print 'translate_domain.py <source> <destination>'
def main(): if len(sys.argv) == 3: src = sys.argv[1] dst = sys.argv[2] self_dir = os.path.dirname(os.path.realpath(__file__)) domain_std_path = os.path.join(self_dir, "domain_std.py") if not os.path.isfile(domain_std_path): print "Error: Cant find domain_std module." sys.exit(1) try: s = s_expression.parse(open(src, "rt").read()) d = domain.build(s) c = domain_code.generate(d, inline_modules=[open(domain_std_path, "rt")]) except (s_expression.MalformedExpressionError, s_expression.SyntaxException) as e: print "Error: line=%s column=%s" % (e.pos[0], e.pos[1]) sys.exit(1) with open(dst, "wt") as f: f.write("# Auto-generated with translate_domain.py\n\n") f.write(c) print "Done!" else: print "translate_domain.py <source> <destination>"
def test_empty_methods(self): t = ''' (:domain test_domain (:method (test_method ?v1 ?v2))) ''' actual_domain = domain.build(s_expression.parse(t)) actual_method = actual_domain.children[0] self.assertEqual('test_method', actual_method.name) self.assertEqual(['?v1', '?v2'], actual_method.variables)
def test_conversion_with_call_term(self): t = ''' ((or (a) (b)) (call func ?x ?y)) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) expected = '(or (and a func) (and b func))' self.assertEqual(expected, self.tree_to_sexp(actual))
def test_literal_is_dnf(self): actual = logical_expression.build(s_expression.parse('((a))')) actual = logical_expression.convert_to_dnf(actual) expected = node_type.LogicalOp( op=node_type.OP_OR, children=[node_type.Atom(name='a')]) self.assertEqual(expected, actual) actual = logical_expression.build(s_expression.parse('((not (a)))')) actual = logical_expression.convert_to_dnf(actual) expected = node_type.LogicalOp( op=node_type.OP_OR, children=[node_type.LogicalOp( op=node_type.OP_NOT, children=[node_type.Atom(name='a')])]) self.assertEqual(expected, actual)
def test_conversion_1(self): t = ''' ((and (q1) (or (r1) (r2)) (q2) (or (r3) (r4)) (q3))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) expected = '(or (and q1 r1 q2 r3 q3) (and q1 r1 q2 r4 q3) (and q1 r2 q2 r3 q3) (and q1 r2 q2 r4 q3))' self.assertEqual(expected, self.tree_to_sexp(actual))
def test_neg_neg_is_pos(self): t = ''' ((not (not (a)))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_nnf(actual) expected = node_type.Atom(name='a') self.assertEqual(expected, actual)
def test_conversion_5(self): t = ''' ((t1) (or (not (t2)) (and (t2) (t3) (t4)))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) expected = '(or (and t1 (not t2)) (and t1 t2 t3 t4))' self.assertEqual(expected, self.tree_to_sexp(actual))
def test_conversion_4(self): t = ''' ((or (not (call f1)))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) expected = '(or (not f1))' self.assertEqual(expected, self.tree_to_sexp(actual))
def test_conversion_2(self): t = ''' ((a) (not (or (b) (c)))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_dnf(actual) expected = '(or (and a (not b) (not c)))' self.assertEqual(expected, self.tree_to_sexp(actual))
def test_and_is_default_root(self): t = ''' ((a) (b)) ''' actual = logical_expression.build(s_expression.parse(t)) expected = node_type.LogicalOp( op = node_type.OP_AND, children = [ node_type.Atom(name='a'), node_type.Atom(name='b'), ]) self.assertEqual(expected, actual)
def test_simple(self): t = ''' ((or (a) (b) (c))) ''' actual = logical_expression.build(s_expression.parse(t)) expected = node_type.LogicalOp( op = node_type.OP_OR, children = [ node_type.Atom(name='a'), node_type.Atom(name='b'), node_type.Atom(name='c'), ]) self.assertEqual(expected, actual)
def test_no_conversion_is_required(self): t = ''' ((or (a) (b))) ''' actual = logical_expression.build(s_expression.parse(t)) actual = logical_expression.convert_to_nnf(actual) expected = node_type.LogicalOp( op = node_type.OP_OR, children = [ node_type.Atom(name='a'), node_type.Atom(name='b'), ]) self.assertEqual(expected, actual)
def test_full_domain(self): t = ''' (:domain test-domain (:method (test-method ?var_a ?var_b) ((fact1 ?var_a str_const) (fact2 ?var_a 239.0) (fact3 ?var_b)) ((empty-method ?var_a ?var_b str_const 123.0)) branch_name ((fact4)) ((empty-method2)) ) (:method (empty-method ?var_x ?var_y ?var_z ?var_w) branch-default () () ) ) ''' actual_domain = domain.build(s_expression.parse(t)) self.assertEqual(2, len(actual_domain.children)) test_method_node = actual_domain.children[0] empty_method_node = actual_domain.children[1] test_method_branch_node = test_method_node.children[0] test_method_tasklist = test_method_branch_node.tasklist self.assertEqual('test_method', test_method_node.name) self.assertEqual('empty_method', empty_method_node.name) self.assertEqual(['?var_x', '?var_y', '?var_z', '?var_w'], empty_method_node.variables) self.assertEqual('branch_0', test_method_branch_node.name) self.assertEqual(1, len(test_method_tasklist)) self.assertEqual('empty_method', test_method_tasklist[0].name) self.assertEqual(['?var_a', '?var_b', 'str_const', 123.0], test_method_tasklist[0].child_names()) self.assertEqual('branch_name', test_method_node.children[1].name)
def test_empty_domain(self): t = ''' (:domain expected-name) ''' actual = domain.build(s_expression.parse(t)) self.assertEqual('expected_name', actual.name)
def test_empty_expression_is_dnf(self): actual = logical_expression.build(s_expression.parse('()')) actual = logical_expression.convert_to_dnf(actual) expected = node_type.LogicalOp(op=node_type.OP_OR) self.assertEqual(expected, actual)