summaryrefslogtreecommitdiff
path: root/tools/dfagen/dfagen/automaton.py
blob: 63a701f6653349eba58bd9005155f8ef9bbb3e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class Event:
    """Event definition."""

    def __init__ (self, name, comments = '', origin = None):
        self.name = name
        self.comments = comments
        self.origin = origin
        pass

    def __str__ (self):
        s = ' ' + self.name + '\n'
        if self.comments:
            s += '  ' + self.comments.replace ('\n', '\n  ') + '\n'
        return s

class State:
    """State definition."""

    def __init__ (self, name, comments = '', initial = False, origin = None):
        self.name = name
        self.comments = comments
        self.initial = initial
        self.origin = origin
        self.transitions = { }
        self.transitions_list = [ ]
        self.attributes = None

    def __str__ (self):
        s = ' ' + self.name
        if self.attributes:
            s += ' [ %s ]' % self.attributes
        s += '\n'
        if self.comments:
            s += '  ' + self.comments.replace ('\n', '\n  ') + '\n'
        return s

    def add_branch (self, branch):
        if branch.event not in self.transitions:
            tr = Transition (branch.event)
            self.transitions[branch.event] = tr
            self.transitions_list.append (tr)
        self.transitions[branch.event].add_branch (branch)

    def iter_transitions (self):
        return iter (self.transitions_list)

class Transition:
    """Transition definition."""

    def __init__ (self, event):
        self.event = event
        self.branches = { }
        self.branches_list = [ ]

    def add_branch (self, branch):
        assert self.event is branch.event
        if branch.name == None and self.branches:
            raise KeyError (branch.name)
        if branch.name != None and None in self.branches:
            raise KeyError (branch.name)
        if branch.name in self.branches:
            raise KeyError (branch.name)
        self.branches[branch.name] = branch
        self.branches_list.append (branch)

    def iter_branches (self):
        return iter (self.branches_list)

    def get_attributes (self):
        return [ b.attributes for b in self.iter_branches ()
                if b.attributes is not None ]

    def __str__ (self):
        return ''.join (str (br) for br in self.iter_branches ())

class TransitionBranch:

    def __init__ (self, event, name = None, to = None, comments = '',
            origin = None):
        self.event = event
        self.name = name
        self.to = to
        self.comments = comments
        self.origin = origin
        self.attributes = None

    def __str__ (self):
        s = ' ' + self.event.name
        if self.name:
            s += ': ' + self.name
        s += ' -> ' + (self.to and self.to.name or '.')
        if self.attributes:
            s += ' [ %s ]' % self.attributes
        s += '\n'
        if self.comments:
            s += '  ' + self.comments.replace ('\n', '\n  ') + '\n'
        return s

class Automaton:

    def __init__ (self, name):
        self.name = name
        self.comments = ''
        self.initials = [ ]
        self.states = { }
        self.states_list = [ ]
        self.events = { }
        self.events_list = [ ]

    def add_state (self, state):
        if state.name in self.states:
            raise KeyError (state.name)
        if state.initial:
            self.initials.append (state)
        if not self.states:
            state.initial = True
            self.initials.append (state)
        self.states[state.name] = state
        self.states_list.append (state)

    def add_event (self, event):
        if event.name in self.events:
            raise KeyError (event.name)
        self.events[event.name] = event
        self.events_list.append (event)

    def iter_states (self):
        return iter (self.states_list)

    def iter_initials (self):
        return iter (self.initials)

    def iter_events (self):
        return iter (self.events_list)

    def __str__ (self):
        s = self.name + '\n'
        if self.comments:
            s += '  ' + self.comments.replace ('\n', '\n  ') + '\n'
        s += '\nStates:\n'
        s += ''.join (str (state) for state in self.iter_states ())
        s += '\nEvents:\n'
        s += ''.join (str (event) for event in self.iter_events ())
        s += '\n'
        for state in self.iter_states ():
            s += state.name + ':\n'
            s += ''.join (str (tr) for tr in state.iter_transitions ())
            s += '\n'
        return s