|
1 |
| -""" |
2 |
| -Interface to an FSM module (graph) used by ProductModelProgram |
3 |
| -""" |
4 |
| - |
5 |
| -from model import Model |
6 |
| - |
7 |
| -class FSM(Model): |
8 |
| - |
9 |
| - def __init__(self, module, exclude, include): |
10 |
| - Model.__init__(self, module, exclude, include) |
11 |
| - |
12 |
| - def post_init(self): |
13 |
| - """ |
14 |
| - Now that all modules have been imported and executed their __init__ |
15 |
| - do a postprocessing pass |
16 |
| - to process metadata that might be affected by configuration modules |
17 |
| - """ |
18 |
| - # Do all of this work here rather than in __init__ |
19 |
| - # so it can include the effects of any pymodel config modules |
20 |
| - |
21 |
| - # Make copies of collections that may be altered later |
22 |
| - # self.actions is not used in this module outside this __init__ |
23 |
| - # BUT it is used in several places in Model and ProductModelProgram |
24 |
| - # EnabledTransitions below works directly on self.module.graph, |
25 |
| - # not self.actions |
26 |
| - if not hasattr(self.module, 'actions'): |
27 |
| - self.actions = list(self.actions_in_graph()) # default, make copy |
28 |
| - else: |
29 |
| - self.actions = list(self.module.actions) # copy |
30 |
| - Model.post_init(self) # uses self.actions |
31 |
| - # Construct self.graph like self.module.graph |
32 |
| - # BUT also accounts for include, exclude via self.actions |
33 |
| - self.graph = [ (current, (action,args,result), next) |
34 |
| - for (current, (action,args,result), next) in |
35 |
| - self.module.graph if action in self.actions ] |
36 |
| - # prepare for first run |
37 |
| - self.current = self.module.initial # raise exception if module is not FSM |
38 |
| - |
39 |
| - |
40 |
| - def actions_in_graph(self): |
41 |
| - return tuple(set([ action for (current, (action,args,result), next) in |
42 |
| - self.module.graph])) # not self.graph, here ONLY |
43 |
| - |
44 |
| - def make_properties(self, state): |
45 |
| - return { 'accepting': state in self.module.accepting, 'statefilter': True, |
46 |
| - 'stateinvariant': True } |
47 |
| - |
48 |
| - def Properties(self): |
49 |
| - return self.make_properties(self.current) |
50 |
| - |
51 |
| - def Reset(self): # needed by stepper |
52 |
| - self.current = self.module.initial |
53 |
| - |
54 |
| - def CleanupGraph(self, cleanup=False): |
55 |
| - """ |
56 |
| - if cleanup, return the graph with just the cleanup transitions |
57 |
| - """ |
58 |
| - if cleanup: |
59 |
| - graph = [(current,(action,args,result),next) |
60 |
| - for (current,(action,args,result),next) in self.graph |
61 |
| - if action in self.module.cleanup] |
62 |
| - else: |
63 |
| - graph = self.graph |
64 |
| - return graph |
65 |
| - |
66 |
| - def ActionEnabled(self, a, args): |
67 |
| - """ |
68 |
| - action a with args is enabled in the current state |
69 |
| - """ |
70 |
| - # no cleanup check here |
71 |
| - # any args matches empty arguments in FSM |
72 |
| - return any([(a == action and (not arguments or args == arguments)) |
73 |
| - for (current,(action, arguments, result),next) |
74 |
| - in self.graph |
75 |
| - if current == self.current ]) |
76 |
| - |
77 |
| - def EnabledTransitions(self, cleanup=False): |
78 |
| - """ |
79 |
| - Return list tuples for all enabled actions: |
80 |
| - (action, args, next state, properties) |
81 |
| - """ |
82 |
| - graph = self.CleanupGraph(cleanup) |
83 |
| - return [(action, args, result, next, self.make_properties(next)) |
84 |
| - for (current,(action,args,result),next) in graph |
85 |
| - if current == self.current ] |
86 |
| - |
87 |
| - def DoAction(self, a, arguments): |
88 |
| - ts = [(current,(action,args,result),next) |
89 |
| - for (current,(action,args,result),next) in self.graph |
90 |
| - if current == self.current and action == a |
91 |
| - and args == arguments[0:len(args)]] #ignore extra trailing args |
92 |
| - # print 'List ts %s' % ts # DEBUG |
93 |
| - # Might be nondet. FSM: for now, just pick first transition that matches |
94 |
| - current, (action,args,result), self.current = ts[0] |
95 |
| - return result |
96 |
| - |
97 |
| - def Current(self): |
98 |
| - return self.current |
99 |
| - |
100 |
| - def Restore(self, state): |
101 |
| - self.current = state |
102 |
| - |
103 |
| - # GetNext not needed |
| 1 | +""" |
| 2 | +Interface to an FSM module (graph) used by ProductModelProgram |
| 3 | +""" |
| 4 | + |
| 5 | +from .model import Model |
| 6 | + |
| 7 | +class FSM(Model): |
| 8 | + |
| 9 | + def __init__(self, module, exclude, include): |
| 10 | + Model.__init__(self, module, exclude, include) |
| 11 | + |
| 12 | + def post_init(self): |
| 13 | + """ |
| 14 | + Now that all modules have been imported and executed their __init__ |
| 15 | + do a postprocessing pass |
| 16 | + to process metadata that might be affected by configuration modules |
| 17 | + """ |
| 18 | + # Do all of this work here rather than in __init__ |
| 19 | + # so it can include the effects of any pymodel config modules |
| 20 | + |
| 21 | + # Make copies of collections that may be altered later |
| 22 | + # self.actions is not used in this module outside this __init__ |
| 23 | + # BUT it is used in several places in Model and ProductModelProgram |
| 24 | + # EnabledTransitions below works directly on self.module.graph, |
| 25 | + # not self.actions |
| 26 | + if not hasattr(self.module, 'actions'): |
| 27 | + self.actions = list(self.actions_in_graph()) # default, make copy |
| 28 | + else: |
| 29 | + self.actions = list(self.module.actions) # copy |
| 30 | + Model.post_init(self) # uses self.actions |
| 31 | + # Construct self.graph like self.module.graph |
| 32 | + # BUT also accounts for include, exclude via self.actions |
| 33 | + self.graph = [ (current, (action,args,result), next) |
| 34 | + for (current, (action,args,result), next) in |
| 35 | + self.module.graph if action in self.actions ] |
| 36 | + # prepare for first run |
| 37 | + self.current = self.module.initial # raise exception if module is not FSM |
| 38 | + |
| 39 | + |
| 40 | + def actions_in_graph(self): |
| 41 | + return tuple(set([ action for (current, (action,args,result), next) in |
| 42 | + self.module.graph])) # not self.graph, here ONLY |
| 43 | + |
| 44 | + def make_properties(self, state): |
| 45 | + return { 'accepting': state in self.module.accepting, 'statefilter': True, |
| 46 | + 'stateinvariant': True } |
| 47 | + |
| 48 | + def Properties(self): |
| 49 | + return self.make_properties(self.current) |
| 50 | + |
| 51 | + def Reset(self): # needed by stepper |
| 52 | + self.current = self.module.initial |
| 53 | + |
| 54 | + def CleanupGraph(self, cleanup=False): |
| 55 | + """ |
| 56 | + if cleanup, return the graph with just the cleanup transitions |
| 57 | + """ |
| 58 | + if cleanup: |
| 59 | + graph = [(current,(action,args,result),next) |
| 60 | + for (current,(action,args,result),next) in self.graph |
| 61 | + if action in self.module.cleanup] |
| 62 | + else: |
| 63 | + graph = self.graph |
| 64 | + return graph |
| 65 | + |
| 66 | + def ActionEnabled(self, a, args): |
| 67 | + """ |
| 68 | + action a with args is enabled in the current state |
| 69 | + """ |
| 70 | + # no cleanup check here |
| 71 | + # any args matches empty arguments in FSM |
| 72 | + return any([(a == action and (not arguments or args == arguments)) |
| 73 | + for (current,(action, arguments, result),next) |
| 74 | + in self.graph |
| 75 | + if current == self.current ]) |
| 76 | + |
| 77 | + def EnabledTransitions(self, cleanup=False): |
| 78 | + """ |
| 79 | + Return list tuples for all enabled actions: |
| 80 | + (action, args, next state, properties) |
| 81 | + """ |
| 82 | + graph = self.CleanupGraph(cleanup) |
| 83 | + return [(action, args, result, next, self.make_properties(next)) |
| 84 | + for (current,(action,args,result),next) in graph |
| 85 | + if current == self.current ] |
| 86 | + |
| 87 | + def DoAction(self, a, arguments): |
| 88 | + ts = [(current,(action,args,result),next) |
| 89 | + for (current,(action,args,result),next) in self.graph |
| 90 | + if current == self.current and action == a |
| 91 | + and args == arguments[0:len(args)]] #ignore extra trailing args |
| 92 | + # print 'List ts %s' % ts # DEBUG |
| 93 | + # Might be nondet. FSM: for now, just pick first transition that matches |
| 94 | + current, (action,args,result), self.current = ts[0] |
| 95 | + return result |
| 96 | + |
| 97 | + def Current(self): |
| 98 | + return self.current |
| 99 | + |
| 100 | + def Restore(self, state): |
| 101 | + self.current = state |
| 102 | + |
| 103 | + # GetNext not needed |
0 commit comments