예제 #1
0
def garage_counter(ploting=True):
    """Example 3.4, p.49 [LS11], for M=2

    no state variables in this Finite-State Machine
    """
    m = trs.Mealy()

    m.add_inputs([
        ['up', {'present', 'absent'}],
        ['down', {'present', 'absent'}]
    ])

    m.add_outputs([
        ('count', range(3) )
    ])

    m.states.add_from(range(3) )
    m.states.initial.add(0)

    m.transitions.add(0, 1, up='present', down='absent', count=1)
    m.transitions.add(1, 0, up='absent', down='present', count=0)

    m.transitions.add(1, 2, up='present', down='absent', count=2)
    m.transitions.add(2, 1, up='absent', down='present', count=1)

    if ploting:
        m.plot()

    return m
예제 #2
0
def pedestrians():
    """Example 2.14, p.63 [LS11]
    """
    m = trs.Mealy()

    m.add_inputs([
        ('sigR', mc.pure),
        ('sigG', mc.pure),
        ('sigY', mc.pure)
    ])

    m.add_outputs([
        ('pedestrian', mc.pure)
    ])

    m.states.add_from(['none', 'waiting', 'crossing'] )
    m.states.initial.add('crossing')

    for sigR in mc.pure:
        for sigG in mc.pure:
            for sigY in mc.pure:
                m.transitions.add(
                    'none', 'none',
                    sigR=sigR, sigG=sigG, sigY=sigY,
                    pedestrian='absent'
                )

                m.transitions.add(
                    'none', 'waiting',
                    sigR=sigR, sigG=sigG, sigY=sigY,
                    pedestrian='present'
                )

    m.transitions.add(
        'waiting', 'crossing',
        sigR='present', sigG='absent', sigY='absent',
        pedestrian='absent'
    )
    m.transitions.add(
        'crossing', 'none',
        sigR='absent', sigG='present', sigY='absent',
        pedestrian='absent'
    )
    m.plot()
    return m
예제 #3
0
def thermostat_with_hysteresis():
    """Example 3.5, p.50 [LS11]
    """
    class temperature_type():
        def is_valid_value(x):
            if not isinstance(x, [float, int]):
                raise TypeError('Input temperature must be float.')

        def __contains__(self, guard):
            # when properly implemented, do appropriate syntactic check
            if isinstance(guard, float):
                return True

            return False

    m = trs.Mealy()

    m.add_inputs([('temperature', ) ])
예제 #4
0
def traffic_light():
    m = trs.Mealy()
    pure_signal = {'present', 'absent'}

    m.add_inputs([('tick', pure_signal) ])
    m.add_outputs([('go', pure_signal), ('stop', pure_signal) ])

    m.states.add_from(['red', 'green', 'yellow'])
    m.states.initial.add('red')

    p = 'present'
    a = 'absent'

    m.transitions.add('red', 'green', tick=p, go=p, stop=a)
    m.transitions.add('green', 'yellow', tick=p, go=a, stop=p)
    m.transitions.add('yellow', 'red', tick=p, go=a, stop=p)

    m.plot()
    return m