def __init__(self): self.running = True self.exited = False self.cancelled = False self.configuration = OrderedSet() self.internalQueue = Queue() self.externalQueue = Queue() self.statesToInvoke = OrderedSet() self.historyValue = {} self.dm = None self.invokeId = None self.parentId = None self.logger = None
def exitStates(self, enabledTransitions): statesToExit = OrderedSet() for t in enabledTransitions: if t.target: tstates = self.getTargetStates(t.target) if t.type == "internal" and isCompoundState(t.source) and all(map(lambda s: isDescendant(s,t.source), tstates)): ancestor = t.source else: ancestor = self.findLCA([t.source] + tstates) for s in self.configuration: if isDescendant(s,ancestor): statesToExit.add(s) for s in statesToExit: self.statesToInvoke.delete(s) statesToExit.sort(key=exitOrder) for s in statesToExit: for h in s.history: if h.type == "deep": f = lambda s0: isAtomicState(s0) and isDescendant(s0,s) else: f = lambda s0: s0.parent == s self.historyValue[h.id] = filter(f,self.configuration) #+ s.parent for s in statesToExit: for content in s.onexit: self.executeContent(content) for inv in s.invoke: self.cancelInvoke(inv) self.configuration.delete(s)
def filterPreempted(self, enabledTransitions): filteredTransitions = [] for t in enabledTransitions: # does any t2 in filteredTransitions preempt t? if not, add t to filteredTransitions if not any(map(lambda t2: self.preemptsTransition(t2, t), filteredTransitions)): filteredTransitions.append(t) return OrderedSet(filteredTransitions)
def and_rules(name: str, singleton: str, accept_singular: bool = False, first_singleton: Optional[str] = None, last_singleton: Optional[str] = None) -> Set[Rule]: """ Creates a mini-grammar of rules that are needed to parse 'A and B', 'A, B and C', 'A, B, C and D', etc. where A, B, C and D all are parseable using the rule name passed using the singleton argument. Grammar: <As> ::= A_helper `and' A_last <A_helper> ::= A_helper `,' A <A_helper> ::= A_first <As> ::= A """ if last_singleton is None: last_singleton = singleton if first_singleton is None: first_singleton = singleton helper = name + "_" rules = { # _ and C Rule(name, [RuleRef(helper), Literal('and'), RuleRef(last_singleton)], lambda state, data: data[0] + data[2] + Interpretation(local=data[0].local | OrderedSet([data[2].local]))), # A, B # (allows for 'A, B and C') Rule(helper, [RuleRef(helper), Literal(','), RuleRef(singleton)], lambda state, data: data[0] + data[2] + Interpretation(local=data[0].local | OrderedSet([data[2].local]))), # A (allows for 'A and B') Rule(helper, [RuleRef(first_singleton)], lambda state, data: data[0] + Interpretation(local=OrderedSet([data[0].local]))) } if accept_singular: rules |= { Rule(name, [RuleRef(singleton)], lambda state, data: data[0] + Interpretation(local=OrderedSet([data[0].local]))) } return rules
def enterStates(self, enabledTransitions): statesToEnter = OrderedSet() statesForDefaultEntry = OrderedSet() for t in enabledTransitions: if t.target: tstates = self.getTargetStates(t.target) if t.type == "internal" and isCompoundState(t.source) and all(map(lambda s: isDescendant(s,t.source), tstates)): ancestor = t.source else: ancestor = self.findLCA([t.source] + tstates) for s in tstates: self.addStatesToEnter(s,statesToEnter,statesForDefaultEntry) for s in tstates: for anc in getProperAncestors(s,ancestor): statesToEnter.add(anc) if isParallelState(anc): for child in getChildStates(anc): if not any(map(lambda s: isDescendant(s,child), statesToEnter)): self.addStatesToEnter(child, statesToEnter,statesForDefaultEntry) statesToEnter.sort(key=enterOrder) for s in statesToEnter: self.statesToInvoke.add(s) self.configuration.add(s) if self.doc.binding == "late" and s.isFirstEntry: s.initDatamodel() s.isFirstEntry = False for content in s.onentry: self.executeContent(content) if s in statesForDefaultEntry: self.executeContent(s.initial) if isFinalState(s): parent = s.parent grandparent = parent.parent self.internalQueue.put(Event(["done", "state", parent.id], s.donedata())) if isParallelState(grandparent): if all(map(self.isInFinalState, getChildStates(grandparent))): self.internalQueue.put(Event(["done", "state", grandparent.id])) for s in self.configuration: if isFinalState(s) and isScxmlState(s.parent): self.running = False;
def selectEventlessTransitions(self): enabledTransitions = OrderedSet() atomicStates = filter(isAtomicState, self.configuration) atomicStates = sorted(atomicStates, key=documentOrder) for state in atomicStates: done = False for s in [state] + getProperAncestors(state, None): if done: break for t in s.transition: if not t.event and self.conditionMatch(t): enabledTransitions.add(t) done = True break filteredTransitions = self.filterPreempted(enabledTransitions) return filteredTransitions