def test_complex_unification(self): t1 = Sum([ Const(5, [1, 2, 3, 4, 5]), Product(Const(1, [6])), Branch([ Sum([Const(1, [7]), Product(Const(1, [8]))]), Sum([Const(1, [9]), Accept(), End()]) ]) ]).normalize() t2 = Sum([ Const(5, [101, 102, 103, 104, 105]), Product(Const(1, [106])), Const(1, [107]), Accept(), End() ]).normalize() print t1 print t2 res = alg.leq_unify(t1, t2, EmptyOptions) self.assertEqual(res, []) res = alg.leq_unify(t2, t1, EmptyOptions) # Need at least one of these to have no structural additions no_branches = False for u in res: if len(u.additions_from_node) == 0: no_branches = True self.assertTrue(no_branches)
def test_unifier_sum(self): t1 = Sum([Product(Const(1, [1])), Const(1, [1])]) t2 = Sum([Const(1, [2])]) # Can't do these due to homogeneity --- that is, # the product will have the same inputs as the enxt # element so will fail unification. res = alg.leq_unify(t1, t2, EmptyOptions) self.assertEqual(res, []) res = alg.leq_unify(t2, t1, EmptyOptions) self.assertEqual(res, [])
def test_unifier_sum_sum(self): t1 = Sum([Const(1, [1]), Const(1, [2])]) t2 = Sum([Const(1, [1]), Product(Const(1, [2])), Const(1, [3])]) res = alg.leq_unify(t2, t1, EmptyOptions) res = [x for x in res if x is not None] self.assertNotEqual(res, [])
def test_loop_skipping(self): t1 = parse_terms("1 + {1, 1 + (1)* + 1} + 1 + a + e") t2 = parse_terms("3 + a + e") EmptyOptions.correct_mapping = False res = alg.leq_unify(t2, t1, EmptyOptions) EmptyOptions.correct_mapping = True assert len(res) > 0
def test_unifier_branches(self): t1 = Branch([Const(2, [(1, 2), (2, 3)]), Const(1, [(4, 5)])]).normalize() t2 = Const(2, [(-1, -2), (-2, -3)]).normalize() res = alg.leq_unify(t2, t1, EmptyOptions)[0] self.assertEqual(res.to_edges, [(1, 2), (2, 3)]) self.assertEqual(res.from_edges, [(-1, -2), (-2, -3)]) self.assertEqual(res.disabled_edges, [(4, 5)])
def test_unifier_deep_sum_selection(self): t1 = Sum([Const(3, [(1, 2), (2, 3), (3, 4)]), Accept()]).normalize() t2 = Sum([ Const(1, [(0, 1)]), Branch([End(), Accept(), Const(1, [(1, 2)])]), Const(1, [(2, 3)]), Accept() ]).normalize() res = alg.leq_unify(t1, t2, EmptyOptions) self.assertNotEqual(res, [])
def test_unifier_both_branches(self): t1 = Branch([ Sum([Const(1, [(0, 1)]), Accept()]), Sum([Const(2, [(1, 2), (2, 3)]), End()]) ]).normalize() t2 = Branch([ Sum([Const(1, [(0, 5)]), End()]), Sum([Const(1, [(0, 1)]), Accept()]), Sum([Const(2, [(2, 3), (3, 4)]), End()]) ]).normalize() res = alg.leq_unify(t1, t2, EmptyOptions) self.assertNotEqual(res, [])
def test_complex_unification_4(self): t1 = parse_terms( "7 + (1)* + 5 + (1)* + 1 + {9 + a + {2 + a + e, 1 + a + e}, 5 + {{2, 1} + 1, 1} + 1 + {{2, 1} + 1, 1} + 1 + a + {2 + a + e, 1 + a + e}, 7 + {{2, 1} + 1 , 1} + 1 + a + {2 + a + e, 1 + a + e}, 9 + {{2, 1} + 1, 1} + 1 + a + {2 + a + e, 1 + a + e}, 13 + a + {2 + a + e, 1 + a + e}, 1 + {3, 3, 3} + 1 + {{2, 1} + 1, 1} + 1 + {{2, 1} + 1, 1} + 1 + a + {2 + a + e, 1 + a + e}}" ) EmptyOptions.use_structural_change = False res = alg.leq_unify(t1, t1, EmptyOptions) for r in res: self.assertTrue(not r.has_structural_additions()) EmptyOptions.use_structural_change = True for unifier in res: unifier.unify_single_state
def test_unifier_sum_product_const(self): t1 = Sum([Const(1, [1]), Accept(), End()]) t2 = Sum([ Const(1, [1]), Product(Const(1, [2])), Const(1, [1]), Accept(), End() ]) EmptyOptions.correct_mapping = False res = alg.leq_unify(t1, t2, EmptyOptions) EmptyOptions.correct_mapping = True self.assertNotEqual(res, [])
def test_loop_insert_2(self): t1 = parse_terms( "1 + (1)* + 12 + (1)* + 3 + {1 + (1)* + 1, 1} + (1)* + 1 + (1)* + 1 + (1)* + 1 + (1)* + 1 + a + e" ) t2 = parse_terms( "1 + (1)* + 11 + (1)* + {1, 1 + (1)* + 1, 1 + (1)* + 1} + 2 + {1 + (1)* + 1, 1} + (1)* + 1 + (1)* + 1 + (1)* + 1 + (1)* + 1 + a + e" ) EmptyOptions.correct_mapping = False EmptyOptions.use_structural_change = False res = alg.leq_unify(t2, t1, EmptyOptions) EmptyOptions.correct_mapping = True EmptyOptions.use_structural_change = True for r in res: pass
def test_complex_unification_2(self): t1 = Sum([ Const(2, [1, 2]), Product(Const(1, [7])), Branch([ Sum([Const(1, [8]), Product(Const(1, [9])), Const(1, [10])]), Const(1, [11]) ]), Branch([ Sum([ Const(1, [12]), Product(Const(1, [13])), Const(1, [14]), Accept(), End() ]), Sum([Const(1, [15]), Accept(), End()]) ]) ]).normalize() print t1 t2 = Sum([ Const(1, [1]), Product(Const(1, [5])), Const(1, [6]), Branch([ Sum([Const(1, [9]), Product(Const(1, [10])), Const(1, [11])]), Const(1, [12]) ]), Branch([ Sum([ Const(1, [12]), Product(Const(1, [13])), Branch([ Sum([Const(1, [14]), Accept(), End()]), Sum([Const(1, [15]), Accept(), End()]) ]) ]), Sum([Const(1, [15]), Accept(), End()]) ]) ]).normalize() print t2 res = alg.leq_unify(t1, t2, EmptyOptions) self.assertTrue(len(res) == 0)
def test_branch_addition(self): l1 = {(0, 1): [10], (1, 2): [11], (2, 3): [12]} l2 = { (0, 1): [14], (1, 2): [15], } u = alg.leq_unify( Sum([Const(2, [(0, 1), (1, 2)]), Accept(), End()]).normalize(), Sum([Const(3, [(0, 1), (1, 2), (2, 3)]), Accept(), End()]).normalize(), EmptyOptions) self.assertEqual(len(u), 1) generated = u[0].unify_single_state(l1, l2, EmptyOptions) print generated.modifications self.assertEqual( generated.modifications.all_modifications()[0].edges_after, [(1, 2)])
def test_complex_unification_3(self): t1 = Sum([ Const(1, [1]), Branch([ Sum([Const(1, [4]), Product(Const(1, [5])), Const(1, [6])]), Const(1, [7]) ]), Const(2, [8, 9]), Accept(), End() ]).normalize() t2 = Sum([ Branch([ Sum([ Product(Const(1, [4])), Branch([ Sum([ Const(1, [5]), Branch([ Sum([ Const(1, [6]), Product(Const(1, [7])), Const(1, [8]) ]), Const(1, [9]) ]), Const(2, [10, 11]), Accept(), End() ]), Sum([Const(1, [12]), Accept(), End()]) ]) ]) ]) ]).normalize() print t1 print t2 EmptyOptions.use_structural_change = False res = alg.leq_unify(t1, t2, EmptyOptions) EmptyOptions.use_structural_change = True print res
def test_loop_addition(self): l1 = {(0, 1): [65], (1, 2): [66], (2, 3): [67]} l2 = {(0, 1): [68], (1, 1): [69], (1, 2): [69], (2, 3): [70]} u = alg.leq_unify( Sum([ Const(1, [(0, 1)]), Product(Const(1, [(1, 1)])), Const(2, [(1, 2), (2, 3)]), Accept(), End() ]).normalize(), Sum([Const(3, [(0, 1), (1, 2), (2, 3)]), Accept(), End()]).normalize(), EmptyOptions) self.assertEqual(len(u), 1) generated = u[0].unify_single_state(l2, l1, EmptyOptions) self.assertEqual( generated.modifications.all_modifications()[0].edges_after, [(1, 2)]) print[str(x) for x in generated.modifications.all_modifications()] self.assertFalse((1, 2) in generated.lookup) self.assertTrue(generated.lookup[65] != 65) self.assertTrue(generated.lookup[69] == 65)
def test_unifier_sum_selection(self): t1 = Sum([Const(1, [(0, 1)]), Branch([Const(1, [(1, 2)]), End()])]) t2 = Sum([Const(2, [(0, 1), (1, 2)])]).normalize() res = alg.leq_unify(t2, t1, EmptyOptions) print res self.assertNotEqual(res, [])
def test_simple_unifier(self): res = alg.leq_unify(Const(1, [(1, 2)]), Const(1, [(2, 3)]), EmptyOptions)[0] self.assertEqual(res.from_edges, [(1, 2)]) self.assertEqual(res.to_edges, [(2, 3)])
def test_complex_unification_5(self): t1 = parse_terms( "3 + {{{{{{{{{{{{{{{{{{{2, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1 } + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + 1, 1} + {6 + a + (1 + a)*, 6 + a + (1 + a)*, 5 + a + (1 + a)*, 5 + a + (1 + a)*, 5 + a + (1 + a)*, 5 + a + e} " ) res = alg.leq_unify(t1, t1, EmptyOptions) print res
def test_unifier_const_to_branch_in_sum(self): t1 = Const(1, [1]) t2 = Sum([Branch([Const(1, [1]), Const(1, [1])])]).normalize() res = alg.leq_unify(t1, t2, EmptyOptions) self.assertNotEqual(res, [])
def test_loop_insert(self): t1 = parse_terms("{1 + (1)* + 1, 1} + 1 + a + e") t2 = parse_terms("{1 + (1)* + 1, 1} + 1 + a + e") res = alg.leq_unify(t2, t1, EmptyOptions) assert len(res) > 0
def test_unifier_single_to_branch(self): t1 = End() t2 = Branch([End(), End()]) res = alg.leq_unify(t2, t1, EmptyOptions) self.assertNotEqual(res, [])
def test_unifier_end(self): t1 = End() t2 = Sum([Const(1, [(0, 1)]), Const(1, [(1, 2)])]).normalize() res = alg.leq_unify(t1, t2, EmptyOptions) self.assertNotEqual(res, [])
def test_unifier_plus_plus(self): t1 = Sum([Const(30, [(0, 1)] * 30)]).normalize() t2 = Sum([Const(30, [(0, 1)] * 30)]).normalize() res = alg.leq_unify(t1, t2, EmptyOptions) self.assertNotEqual(res, [])