Пример #1
0
    def test_str_representation(self):
        sort = Sort('sort')
        sort.operation('const', ())
        p = Place('p', sort, [sort.const()])
        t = Transition('t')
        a = Arc(p, t, [sort.const()])
        self.assertEqual(
            str(a), 'arc from place p to transition t, with label\
 [sort.const(), ]')
Пример #2
0
    def test_marking(self):
        sort = Sort('sort')
        sort.operation('const', ())
        apn = AlgebraicPetriNet('apn', [], [])
        p = apn.add_place('p', sort, [sort.const()])

        markings = apn.marking()
        self.assertEqual(len(markings), 1)
        self.assertEqual(markings[p], [sort.const()])
Пример #3
0
 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)
Пример #4
0
    def test_instanciation(self):
        sort = Sort('sort')
        sort.operation('const', ())
        with self.assertRaises(AssertionError):
            Place(2, sort)  # Name of place must be a string.
            Place('place', 2)  # Sort of place must be a sort.
            Place('place', sort, [2])  # Marking must be a list of terms.

        place = Place('place', sort, [sort.const()])
        self.assertEqual(place.name, 'place')
        self.assertEqual(place.sort, sort)
        self.assertEqual(place.marking, [sort.const()])
Пример #5
0
 def test_reduce(self):
     sort = Sort('sort')
     sort.operation('const', ())
     sort.operation('reduce', (sort, ))
     sort.operation('merge', (sort, sort))
     sort.variable('x')
     sort.rewrite_rule(sort.reduce(sort.x()), sort.x())
     sort.rewrite_rule(sort.merge(sort.x(), sort.x()),
                       sort.reduce(sort.x()))
     t = sort.reduce(sort.merge(sort.reduce(sort.const()), sort.const()))
     t2 = t.reduce(sort.rewrite_rules)
     self.assertEqual(t2, sort.const())
Пример #6
0
    def test_outbound_arc(self):
        sort = Sort('sort')
        sort.operation('const', ())
        p = Place('p', sort, [sort.const()])
        t = Transition('t')

        with self.assertRaises(AssertionError):
            t.outbound_arc(2, [sort.const()])  # Target of arc must be a place.
            t.outbound_arc(p, [2])  # Label must be a list of terms.

        t.outbound_arc(p, [sort.const()])
        self.assertEqual(len(t.outbound_arcs), 1)
        self.assertEqual(type(t.outbound_arcs[0]), Arc)
Пример #7
0
    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, [])
Пример #8
0
    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)
Пример #9
0
 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()))
Пример #10
0
    def test_produce(self):
        sort = Sort('sort')
        sort.operation('const', ())
        place = Place('place', sort)

        with self.assertRaises(AssertionError):
            sort2 = Sort('sort2')
            sort2.operation('const', ())
            place.produce([2])  # Tokens produced in a place must be terms.
            # Tokens produced in a place must have the same sort as the place.
            place.produce([sort2.const()])

        self.assertEqual(place.marking, [])
        place.produce([sort.const()])
        self.assertEqual(place.marking, [sort.const()])
Пример #11
0
    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)
Пример #12
0
    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, [])
Пример #13
0
    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, [])
Пример #14
0
    def test_instanciation(self):
        sort = Sort('sort')
        sort.operation('const', ())
        p = Place('p', sort, [sort.const()])
        t = Transition('t')

        with self.assertRaises(AssertionError):
            # Source of an arc must be a Place or Transition.
            Arc(2, t, [sort.const()])
            # Target of an arc must be a Place or Transition.
            Arc(p, 2, [sort.const()])
            # Label of arc must be a list of terms.
            Arc(p, t, [2])

        a1 = Arc(p, t, [sort.const()])
        self.assertEqual(a1.source, p)
        self.assertEqual(a1.target, t)
        self.assertEqual(a1.label, [sort.const()])
Пример #15
0
    def test_add_arc(self):
        sort = Sort('sort')
        sort.operation('const', ())
        apn = AlgebraicPetriNet('apn', [], [])
        p = apn.add_place('p', sort, [sort.const()])
        t = apn.add_transition('t')

        with self.assertRaises(AssertionError):
            # Source of an arc must be a Place or Transition.
            apn.add_arc(2, t, [])
            # Target of an arc must be a Place or Transition.
            apn.add_arc(p, 2, [])
            # Label of an arc must be a list of terms.
            apn.add_arc(p, t, [2])

        apn.add_arc(p, t, [sort.const()])
        apn.add_arc(t, p, [sort.const()])
        self.assertEqual(len(t.inbound_arcs), 1)
        self.assertEqual(len(t.outbound_arcs), 1)
Пример #16
0
    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()])
Пример #17
0
    def test_add_place(self):
        sort = Sort('sort')
        sort.operation('const', ())
        apn = AlgebraicPetriNet('apn')

        with self.assertRaises(AssertionError):
            apn.add_place(2, sort)  # Name of place must be a string.
            apn.add_place('p', 2)  # Sort of place must be a sort.
            apn.add_place('p', sort, [2])  # Marking must be a list of terms.

        p = apn.add_place('p', sort, [sort.const()])
        self.assertEqual(len(apn.places), 1)
        self.assertEqual(type(apn.places[0]), Place)
        self.assertEqual(apn.places[0], p)
Пример #18
0
 def test_generic_sort(self):
     generic = GenericSort()
     generic.variable('x')
     sort = Sort('sort')
     sort.operation('op_gen', (generic, generic))
     sort.operation('const', ())
     sort2 = Sort('sort2')
     sort2.operation('const', ())
     r = RewriteRule(sort.op_gen(generic.x(), generic.x()), generic.x())
     t1 = r.apply(sort.op_gen(sort2.const(), sort2.const()))
     self.assertEqual(t1, sort2.const())
     t2 = r.apply(sort.op_gen(sort2.const(), sort.const()))
     self.assertEqual(t2, sort.op_gen(sort2.const(), sort.const()))
Пример #19
0
    def test_instanciation(self):
        sort = Sort('sort')
        sort.operation('const', ())
        p = Place('p', sort, [sort.const()])
        t = Transition('t')

        with self.assertRaises(AssertionError):
            AlgebraicPetriNet(2)  # Name of APN must be a string.
            # Places of APN must be instances of Place.
            AlgebraicPetriNet('apn', [2], [t])
            # Transitions of APN must be instances of Transition.
            AlgebraicPetriNet('apn', [p], [2])

        apn = AlgebraicPetriNet('apn', [p], [t])
        self.assertEqual(apn.name, 'apn')
        self.assertEqual(apn.places, [p])
        self.assertEqual(apn.transitions, [t])