|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ----------------------------------------------------------------------- |
| 3 | +# QmParser.py |
| 4 | +# |
| 5 | +# |
| 6 | +# ----------------------------------------------------------------------- |
| 7 | +# mypy: ignore-errors |
| 8 | + |
| 9 | +from copy import deepcopy |
| 10 | +import xmiModelApi |
| 11 | +import flattenstatemachine as flatt |
| 12 | +import qmlib |
| 13 | +from anytree import Node |
| 14 | +from xmiModelApi import XmiModel |
| 15 | + |
| 16 | +from lxml.etree import _ElementTree |
| 17 | +ElementTreeType = _ElementTree |
| 18 | + |
| 19 | + |
| 20 | +class UniqueNumberGenerator: |
| 21 | + def __init__(self): |
| 22 | + self.generator = self.unique_number_generator() # Create a generator instance |
| 23 | + |
| 24 | + def unique_number_generator(self): |
| 25 | + counter = 0 |
| 26 | + while True: |
| 27 | + yield counter |
| 28 | + counter += 1 |
| 29 | + |
| 30 | + def get_unique_number(self): |
| 31 | + return next(self.generator) # Return the next unique number |
| 32 | + |
| 33 | +# ------------------------------------------------------------------------- |
| 34 | +# parseTrans |
| 35 | +# |
| 36 | +# Recursively parse a qm transition and populate the xmiModel |
| 37 | +# -------------------------------------------------------------------------- |
| 38 | +def parseTrans(qmTrans: ElementTreeType, |
| 39 | + xmiModel: XmiModel, |
| 40 | + xmiNode: Node, |
| 41 | + number_gen: UniqueNumberGenerator): |
| 42 | + |
| 43 | + source = xmiNode.id |
| 44 | + target = int(qmTrans.get('target')) if qmTrans.get('target') else None |
| 45 | + kind = qmTrans.get('kind') |
| 46 | + guard = qmlib.pick_guard(qmTrans) |
| 47 | + action = flatt.pick_action(qmTrans) |
| 48 | + event = qmTrans.get('trig') |
| 49 | + |
| 50 | + if target is None: |
| 51 | + choices = qmTrans.findall("choice") |
| 52 | + if len(choices) == 2: |
| 53 | + psId = number_gen.get_unique_number() |
| 54 | + psNode = xmiModel.addPsuedostate(psId) |
| 55 | + xmiModel.addTransition(source, psId, event, guard, action, kind) |
| 56 | + for choice in choices: |
| 57 | + parseTrans(choice, xmiModel, psNode, number_gen) |
| 58 | + else: |
| 59 | + xmiModel.addTransition(source, target, event, guard, action, kind) |
| 60 | + else: |
| 61 | + xmiModel.addTransition(source, target, event, guard, action, kind, xmiNode.parent) |
| 62 | + |
| 63 | +# ------------------------------------------------------------------------- |
| 64 | +# parseStateTree |
| 65 | +# |
| 66 | +# Recursively parse the qm model and populate the xmiModel |
| 67 | +# -------------------------------------------------------------------------- |
| 68 | +def parseStateTree(qmRoot: ElementTreeType, |
| 69 | + xmiModel: XmiModel, |
| 70 | + xmiNode: Node, |
| 71 | + number_gen: UniqueNumberGenerator): |
| 72 | + |
| 73 | + for init in qmRoot.findall("initial"): |
| 74 | + psNode = xmiModel.addPsuedostate(number_gen.get_unique_number(), xmiNode) |
| 75 | + parseTrans(init, xmiModel, psNode, number_gen) |
| 76 | + |
| 77 | + for tran in qmRoot.findall("tran"): |
| 78 | + parseTrans(tran, xmiModel, xmiNode, number_gen) |
| 79 | + |
| 80 | + for state in qmRoot.findall("state"): |
| 81 | + stateName = state.get('name') |
| 82 | + entry = flatt.pick_entry(state) |
| 83 | + exit = flatt.pick_exit(state) |
| 84 | + state_id = int(state.get('id')) |
| 85 | + thisNode = xmiModel.addState(stateName, xmiNode, entry, exit, state_id) |
| 86 | + parseStateTree(state, xmiModel, thisNode, number_gen) |
| 87 | + |
| 88 | +# ------------------------------------------------------------------------- |
| 89 | +# populateXmiModel |
| 90 | +# |
| 91 | +# Recursively parse the qm model and populate the xmiModel |
| 92 | +# -------------------------------------------------------------------------- |
| 93 | +def populateXmiModel(qmRoot: ElementTreeType, |
| 94 | + smname: str) -> XmiModel: |
| 95 | + |
| 96 | + qmFix = fixQMThing(qmRoot) |
| 97 | + number_gen = UniqueNumberGenerator() |
| 98 | + |
| 99 | + xmiModel = xmiModelApi.XmiModel(smname + "Package", smname) |
| 100 | + |
| 101 | + # Add a unique ID to every state |
| 102 | + states = qmFix.iter("state") |
| 103 | + for state in states: |
| 104 | + state.set("id", str(number_gen.get_unique_number())) |
| 105 | + |
| 106 | + # Replace the relative target attributes with ID's |
| 107 | + for node in qmFix.iter(): |
| 108 | + target = node.get("target") |
| 109 | + if target is not None: |
| 110 | + targetId = flatt.state_from_target(node).get("id") |
| 111 | + node.set("target", str(targetId)) |
| 112 | + |
| 113 | + # Search for internal transitions. Internal transitions do not have a |
| 114 | + # target and do not have an option child. Mark the transition as internal |
| 115 | + for tran in qmFix.iter("tran"): |
| 116 | + if tran.get('target') is None: |
| 117 | + # Look for choice nodes |
| 118 | + choices = list(tran.iter("choice")) |
| 119 | + if len(choices) == 0: |
| 120 | + tran.set("kind", "internal") |
| 121 | + |
| 122 | + parseStateTree(qmFix, xmiModel, xmiModel.tree, number_gen) |
| 123 | + |
| 124 | + return xmiModel |
| 125 | + |
| 126 | +# ----------------------------------------------------------------------- |
| 127 | +# fixQMThing |
| 128 | +# |
| 129 | +# The QM modeling tool does not support guards on transitions. |
| 130 | +# These are specified in the xml file as a transition to a single |
| 131 | +# choice. |
| 132 | +# |
| 133 | +# This routine makes a fix where it looks for transitions with a single |
| 134 | +# choice and then adds the children directly under the transition, |
| 135 | +# essentilly moving everything up. |
| 136 | +# ----------------------------------------------------------------------- |
| 137 | +def fixQMThing(qmRoot: ElementTreeType) -> ElementTreeType: |
| 138 | + |
| 139 | + qmFix = deepcopy(qmRoot) |
| 140 | + for tran in qmFix.iter("tran"): |
| 141 | + choices = tran.findall("choice") |
| 142 | + if len(choices) == 1: |
| 143 | + |
| 144 | + choice_children = list(choices[0]) |
| 145 | + |
| 146 | + for child in choice_children: |
| 147 | + tran.append(child) |
| 148 | + |
| 149 | + # Remove the <choice> node after moving its children |
| 150 | + # But first check if the choice had a target attribute |
| 151 | + target = choices[0].get('target') |
| 152 | + if target is not None: |
| 153 | + tran.set('target', target) |
| 154 | + tran.remove(choices[0]) |
| 155 | + |
| 156 | + # Look for all the targets and move them up as well. |
| 157 | + for child in list(tran.iter()): |
| 158 | + target = child.get('target') |
| 159 | + if target is not None: |
| 160 | + child.set('target', target[3:]) |
| 161 | + |
| 162 | + return qmFix |
| 163 | + |
| 164 | + |
| 165 | +# ----------------------------------------------------------------------- |
| 166 | +# getXmiModel |
| 167 | +# |
| 168 | +# Process the input qmRoot and return an xmiModel |
| 169 | +# ----------------------------------------------------------------------- |
| 170 | +def getXmiModel(qmRoot: ElementTreeType) -> XmiModel: |
| 171 | + |
| 172 | + smRoot, smname = qmlib.get_state_machine(qmRoot) |
| 173 | + |
| 174 | + xmiModel = populateXmiModel(smRoot, smname) |
| 175 | + |
| 176 | + return xmiModel |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
0 commit comments