def test_equality(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.variable('x') t1 = sort.op(sort.x()) t2 = sort.op(sort.op(sort.x())) self.assertTrue(t1 == sort.op(sort.x())) self.assertFalse(t1 == t2) self.assertTrue(t1 != t2)
def test_apply_binding(self): sort = Sort('sort') sort.operation('op', (sort, sort)) sort.operation('const', ()) sort.variable('x') sort.variable('y') t1 = sort.op(sort.x(), sort.y()) t2 = sort.op(sort.op(sort.const()), sort.const()) _, binding = t1.match(t2) t3 = t1.apply_binding(binding) self.assertEqual(t3, t2)
def test_match(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('op2', (sort, sort)) sort.variable('x') sort.variable('y') t1 = sort.op(sort.x()) t2 = sort.op(sort.op(sort.y())) t3 = sort.op2(sort.x(), sort.y()) match1, binding1 = t1.match(t2) match2, _ = t1.match(t3) self.assertTrue(match1) self.assertEqual(binding1[sort.x], sort.op(sort.y())) self.assertFalse(match2)
def test_instanciation(self): sort = Sort('sort') sort.operation('op', (sort, sort)) sort.operation('op2', (sort, )) sort.operation('eq', (sort, sort)) sort.variable('x') sort.variable('y') rule = RewriteRule( sort.op(sort.x(), sort.y()), sort.op2(sort.x()), [(sort.eq(sort.x(), sort.y()), sort.eq(sort.x(), sort.y()))]) self.assertEqual(type(rule), RewriteRule) self.assertEqual(rule.lhs, sort.op(sort.x(), sort.y())) self.assertEqual(rule.rhs, sort.op2(sort.x())) self.assertEqual( rule.conditions, [(sort.eq(sort.x(), sort.y()), sort.eq(sort.x(), sort.y()))])
def test_str_representation(self): sort = Sort('sort') sort.operation('op', (sort, sort)) sort.operation('op2', (sort, )) sort.operation('eq', (sort, sort)) sort.variable('x') sort.variable('y') rule = RewriteRule( sort.op(sort.x(), sort.y()), sort.op2(sort.x()), [(sort.eq(sort.x(), sort.y()), sort.eq(sort.x(), sort.y()))]) self.assertEqual( str(rule), '(sort.eq(sort.x, sort.y) ==\ sort.eq(sort.x, sort.y)), => sort.op(sort.x, sort.y) -> sort.op2(sort.x)')
def test_apply(self): sort = Sort('sort') sort.operation('op', (sort, sort)) sort.operation('op2', (sort, )) sort.operation('eq', (sort, sort)) sort.operation('const', ()) sort.variable('x') sort.variable('y') rule = RewriteRule( sort.op(sort.x(), sort.y()), sort.op2(sort.x()), [(sort.eq(sort.x(), sort.y()), sort.eq(sort.x(), sort.y()))]) term = Term(sort.op, (sort.const(), sort.const())) term2 = rule.apply(term) self.assertEqual(term2, sort.op2(sort.const()))
def test_fire_random(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) apn = AlgebraicPetriNet('apn', [], []) p = apn.add_place('p', sort, [sort.const()]) t1 = apn.add_transition('t1') t2 = apn.add_transition('t2') apn.add_arc(p, t1, [sort.const()]) apn.add_arc(p, t2, [sort.op(sort.const())]) self.assertEqual(p.marking, [sort.const()]) apn.fire_random() self.assertEqual(p.marking, [])
def test_fireables(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) apn = AlgebraicPetriNet('apn', [], []) p = apn.add_place('p', sort, [sort.const()]) t1 = apn.add_transition('t1') t2 = apn.add_transition('t2') apn.add_arc(p, t1, [sort.const()]) apn.add_arc(p, t2, [sort.op(sort.const())]) fireables = apn.fireables() self.assertEqual(len(fireables), 1) self.assertEqual(fireables[0], t1)
def test_fireable(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) sort.variable('x') p = Place('p', sort, [sort.const()]) t1 = Transition('t1') t2 = Transition('t2') t1.inbound_arc(p, [sort.x()]) t2.inbound_arc(p, [sort.op(sort.const())]) fireable, binding = t1.fireable() self.assertEqual(fireable, True) self.assertEqual(binding[sort.x], sort.const()) fireable, _ = t2.fireable() self.assertEqual(fireable, False)
def test_fire(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) apn = AlgebraicPetriNet('apn', [], []) p = apn.add_place('p', sort, [sort.const()]) t1 = apn.add_transition('t1') t2 = apn.add_transition('t2') apn.add_arc(p, t1, [sort.const()]) apn.add_arc(p, t2, [sort.op(sort.const())]) with self.assertRaises(FiringException): apn.fire(t2) self.assertEqual(p.marking, [sort.const()]) apn.fire(t1) self.assertEqual(p.marking, [])
def test_consume(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) place = Place('place', sort, [sort.const()]) with self.assertRaises(AssertionError): place.consume([2]) # Tokens to consume must be a list of terms. with self.assertRaises(ConsumeException): # An error is raised when an attempt is made to consume tokens # absent from the place. place.consume([sort.op(sort.const())]) self.assertEqual(place.marking, [sort.const()]) place.consume([sort.const()]) self.assertEqual(place.marking, [])
def test_fire(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.operation('const', ()) sort.variable('x') p1 = Place('p1', sort, [sort.const()]) p2 = Place('p2', sort, []) t1 = Transition('t1') t2 = Transition('t2') t1.inbound_arc(p1, [sort.x()]) t1.outbound_arc(p2, [sort.x()]) t2.inbound_arc(p1, [sort.op(sort.const())]) with self.assertRaises(FiringException): t2.fire() self.assertEqual(p1.marking, [sort.const()]) self.assertEqual(p2.marking, []) t1.fire() self.assertEqual(p1.marking, []) self.assertEqual(p2.marking, [sort.const()])
def test_str_representation(self): sort = Sort('sort') sort.operation('op', (sort, )) sort.variable('x') term = sort.op(sort.x()) self.assertEqual(str(term), 'sort.op(sort.x)')