__init__.py 1.83 KB
Newer Older
1
2
3
4
5
6
from abc import ABCMeta, abstractmethod

"""A basic abstract automaton model"""
class Automaton(metaclass=ABCMeta):
   def __init__(self, states, state_to_trans):
      super().__init__()
7
8
      self._states = states
      self._state_to_trans = state_to_trans
9

10
   def start_state(self):
11
      return self._states[0]
12
13

   def states(self):
14
      return list(self._states)
15
16
17

   def transitions(self, state, label=None):
      if label is None:
18
         return list(self._state_to_trans[state])
19
      else:
20
         return list([trans for trans in self._state_to_trans[state] if trans.start_label == label])
21
22
23
24
25
26
27
28

   @abstractmethod
   def state(self, trace):
       pass

"""An automaton model that generates output"""
class Transducer(Automaton, metaclass=ABCMeta):
    def __init__(self, states, state_to_trans):
29
        super().__init__(states, state_to_trans)
30
31
32
33
34
35
36
37

    @abstractmethod
    def outputs(self, trace):
        pass

"""An automaton model whose states are accepting/rejecting"""
class Acceptor(Automaton, metaclass=ABCMeta):
    def __init__(self, states, state_to_trans, state_to_acc):
38
        super().__init__(states, state_to_trans)
39
        self._state_to_acc = state_to_acc
40
41

    def is_accepting(self, state):
42
        return self._state_to_acc[state]
43
44

    def accepts(self, trace):
45
46
47
        state = self.state(trace)
        is_acc = self.is_accepting(state)
        return is_acc
48

49
"""The most basic transition class available"""
50
51
52
53
class Transition():
    def __init__(self, start_state, start_label, end_state):
        self.start_state = start_state
        self.start_label = start_label
54
55
56
        self.end_state = self.end_state


57
"""Exception raised when no transition can be fired"""
58
59
60
class NoTransitionFired(Exception):
   pass

61
62
"""Exception raised when several transitions can be fired in a deterministic machine"""
class MultipleTransitionsFired(Exception):
63
    pass