Exemplo n.º 1
0
 def test_generic_sort(self):
     sort = Sort('sort')
     sort.operation('op_generic', (GenericSort(), ))
     sort2 = Sort('sort2')
     sort2.variable('x')
     t = sort.op_generic(sort2.x())
     self.assertEqual(type(t), Term)
     self.assertEqual(t.args[0].sort, sort2)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
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()])
Exemplo n.º 4
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(), ]')
Exemplo n.º 5
0
 def test_operation_definition(self):
     sort = Sort('sort')
     with self.assertRaises(AssertionError):
         sort.operation(4, ())  # Name must be a string.
         sort.operation('op', sort)  # Signature must be a tuple.
         sort.operation('op', (3, 2))  # Elts of sign. must be sorts.
         # Sort of the operation must be a sort.
         sort.operation('op', (sort, ), 3)
     sort.operation('op', (sort, ), sort)
     self.assertEqual(type(sort.op), Operation)
Exemplo n.º 6
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)
Exemplo n.º 7
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()])
Exemplo n.º 8
0
 def test_instanciation(self):
     sort = Sort('sort')
     sort.operation('op', (sort, ))
     sort.variable('x')
     with self.assertRaises(AssertionError):
         Term(3, ())  # Head must be an operation or a variable.
         Term(sort.op, sort.x)  # Args must be a tuple.
         Term(sort.op, ())  # Args must match signature of operation.
         Term(sort.op, (3, ))  # Args must be a tuple of terms.
     term = Term(sort.op, (sort.x(), ))
     self.assertEqual(type(term), Term)
     self.assertEqual(term.head, sort.op)
     self.assertEqual(term.args, (sort.x(), ))
Exemplo n.º 9
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)
Exemplo n.º 10
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, [])
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
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()])
Exemplo n.º 15
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, [])
Exemplo n.º 16
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])
Exemplo n.º 17
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, [])
Exemplo n.º 18
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)
Exemplo n.º 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):
            # 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()])
Exemplo n.º 20
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)
Exemplo n.º 21
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()])
Exemplo n.º 22
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()))
Exemplo n.º 23
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())
Exemplo n.º 24
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()))
Exemplo n.º 25
0
    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)')
Exemplo n.º 26
0
 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()))])
Exemplo n.º 27
0
"""
Adt for the Boolean Sort.
"""
# Aurelien Coet, 2018.

from alpyne.adt import Sort, GenericSort

# Sort Definition.
boolean = Sort('bool')

# ---------- Operations on booleans ---------- #
# Generators.
boolean.operation('true', ())
boolean.operation('false', ())

# Modifiers.
boolean.operation('not_', (boolean, ))
boolean.operation('and_', (boolean, boolean))
boolean.operation('or_', (boolean, boolean))

# Observers.
boolean.operation('equal', (boolean, boolean))

# ---------- Variables ---------- #
boolean.variable('b')
boolean.variable('c')

# ---------- Rewrite rules ---------- #
# not(true) -> false.
boolean.rewrite_rule(boolean.not_(boolean.true()), boolean.false())
Exemplo n.º 28
0
 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)')
Exemplo n.º 29
0
"""
Adt for the Map Sort.
"""
# Aurelien Coet, 2018.

from alpyne.adt import Sort, GenericSort
from alpyne.adts.boolean import boolean

# Sort Definition.
kv_map = Sort('map')
generic = GenericSort()

# ---------- Operations on maps ---------- #
# Generators.
kv_map.operation('empty', ())
kv_map.operation('add', (kv_map, generic, generic))

# Observers.
kv_map.operation('get', (kv_map, generic))
kv_map.operation('isempty', (kv_map, ), boolean)

# Modifiers.
kv_map.operation('delete', (kv_map, generic))

# Operations for the generic sort.
generic.operation('equal', (generic, generic))

# ---------- Variables ---------- #
kv_map.variable('m')
kv_map.variable('n')
Exemplo n.º 30
0
chars = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '_'
]

# ---------------------------------------- #
# Definition of the character sort.
# ---------------------------------------- #

char = Sort('char')

# ---------- Operations on characters ---------- #
# Generators.
for i, c in enumerate(chars):
    char.operation(c, ())
    if i < 26:
        char.operation(c.upper(), ())

# Observer.
char.operation('equal', (char, char), boolean)

# ---------- Character variables ---------- #
char.variable('c1')
char.variable('c2')

# ---------- Rewrite rules on characters ---------- #
for i, c in enumerate(chars):
    if i < 26:
        c_upper = c.upper()
    else: