|
| 1 | +"""Class Actor Represents an actor in a dataflow graph""" |
| 2 | + |
| 3 | +from cmtrace.dataflow.maxplus import mpplus, mpmax, mpmax2, trace, MPMINUSINF, output_sequence |
| 4 | + |
| 5 | +class Actor: |
| 6 | + """Represents an actor in a dataflow graph""" |
| 7 | + def __init__(self, name, actdelay, scenario=None): |
| 8 | + self.name = name |
| 9 | + self.delay = actdelay |
| 10 | + self.inputs = dict() |
| 11 | + self.primary_inputs = [] |
| 12 | + self.state_inputs = dict() |
| 13 | + self.firings = [] |
| 14 | + self.scenario = scenario |
| 15 | + |
| 16 | + def add_channel_input(self, actor, initial_tokens=0, arcdelay=0, initial_time=MPMINUSINF): |
| 17 | + """add a channel input dependency to another actor, including |
| 18 | + a time-offset arcdelay""" |
| 19 | + self.inputs[actor.name] = (actor, initial_tokens, arcdelay, initial_time) |
| 20 | + |
| 21 | + def add_state_input(self, state, tokendelay=0, arcdelay=0): |
| 22 | + """Add a dependency on a state token for an SADF graph""" |
| 23 | + self.state_inputs[state.name] = (state, tokendelay, arcdelay) |
| 24 | + return |
| 25 | + |
| 26 | + def add_primary_input(self, priminput): |
| 27 | + """Add dependency to a primary input to the graph.""" |
| 28 | + self.primary_inputs.append(priminput) |
| 29 | + |
| 30 | + def completions(self): |
| 31 | + """returns the current completion times of the actor""" |
| 32 | + # add the actor delay to the firing times |
| 33 | + return mpplus(self.firings, self.delay) |
| 34 | + |
| 35 | + def firing_intervals(self): |
| 36 | + """ Return a list of (start,end) pairs for all firings """ |
| 37 | + return [(f, f+self.delay) for f in self.firings] |
| 38 | + |
| 39 | + def update_firings(self): |
| 40 | + """Recomput the firings of the actor based on its input dependencies. |
| 41 | + Returns a boolean indicating if the computed firings remained the same.""" |
| 42 | + oldfirings = len(self.firings) |
| 43 | + traces = [] |
| 44 | + # collect all the incoming channels |
| 45 | + for i, (act, tok, arcdel, tokinit) in self.inputs.items(): |
| 46 | + traces.append(output_sequence(act.completions(), tok, arcdel, tokinit)) |
| 47 | + # collect traces for all primary inputs |
| 48 | + for i in self.primary_inputs: |
| 49 | + traces.append(i) |
| 50 | + if len(traces) == 0: |
| 51 | + raise Exception("Actor must have inputs") |
| 52 | + |
| 53 | + # determine the firings |
| 54 | + self.firings = mpmax(*traces) |
| 55 | + return oldfirings == len(self.firings) |
| 56 | + |
| 57 | + def set_scenario(self, scenario): |
| 58 | + """set the scenario in which the actor is active""" |
| 59 | + self.scenario = scenario |
| 60 | + |
| 61 | + def update_firings_sadf(self, scen_seq): |
| 62 | + """Recomput the firings of the SADF actor based on its input dependencies. |
| 63 | + Returns a boolean indicating if the computed firings remained the same.""" |
| 64 | + # update the actor firings |
| 65 | + oldfirings = len(self.firings) |
| 66 | + |
| 67 | + # primary inputs |
| 68 | + # assume for the moment that primary inputs are active in only one scenario! |
| 69 | + # hack to deal with absence of primary inputs. Should be an infinitely |
| 70 | + # long sequence of minus inf. |
| 71 | + if len(self.primary_inputs) > 0: |
| 72 | + self.firings = mpmax(*self.primary_inputs) |
| 73 | + else: |
| 74 | + self.firings = [MPMINUSINF] * len(scen_seq) |
| 75 | + |
| 76 | + # state_inputs |
| 77 | + for _, (state, tokdel, arcdel) in self.state_inputs.items(): |
| 78 | + if self.scenario is None: |
| 79 | + raise Exception("SADF actor has no scenario.") |
| 80 | + splicedfirings = state.spliced_firings(scen_seq, self.scenario, tokdel) |
| 81 | + self.firings = mpmax2(self.firings, mpplus(splicedfirings, arcdel)) |
| 82 | + |
| 83 | + # channel inputs |
| 84 | + for _, (act, tok, arcdel, tokinit) in self.inputs.items(): |
| 85 | + self.firings = mpmax2(self.firings, output_sequence(act.completions(), tok, arcdel, tokinit)) |
| 86 | + |
| 87 | + return oldfirings == len(self.firings) |
| 88 | + |
| 89 | + |
| 90 | + def get_trace(self, tracelength): |
| 91 | + """Get a string representation of the actor's execution trace.""" |
| 92 | + return trace(self.firings, self.delay, tracelength) |
0 commit comments