|
|
@@ -0,0 +1,56 @@
|
|
|
+from drawio2py import abstract_syntax as dio_as
|
|
|
+from drawio2oml.cbd import abstract_syntax as cbd_as
|
|
|
+from drawio2oml.cbd import trace_abstract_syntax as trace_as
|
|
|
+
|
|
|
+from typing import List, Optional, Dict
|
|
|
+
|
|
|
+# Incomplete - only the bare minimum we need to parse our spring-mass-damper example
|
|
|
+BLOCK_TYPES = {
|
|
|
+ "ConstantBlock": {
|
|
|
+ "inputs": [],
|
|
|
+ "outputs": ["OUT1"],
|
|
|
+ },
|
|
|
+ "AdderBlock": {
|
|
|
+ "inputs": ["IN1", "IN2", "IN3", "IN4"], # actual number of ports is variable
|
|
|
+ "outputs": ["OUT1"],
|
|
|
+ },
|
|
|
+ "IntegratorBlock": {
|
|
|
+ "inputs": ["IC", "IN1"],
|
|
|
+ "outputs": ["OUT1"],
|
|
|
+ },
|
|
|
+ "ProductBlock": {
|
|
|
+ "inputs": ["IN1", "IN2", "IN3", "IN4"], # actual number of ports is variable
|
|
|
+ "outputs": ["OUT1"],
|
|
|
+ },
|
|
|
+}
|
|
|
+
|
|
|
+def parse(parent: dio_as.Cell) -> (cbd_as.Model, List[trace_as.TraceabilityLink]):
|
|
|
+ name_to_block = {}
|
|
|
+ trace_links = []
|
|
|
+
|
|
|
+ for cell in parent.children:
|
|
|
+ if "class_name" in cell.properties:
|
|
|
+ class_name = cell.properties["class_name"]
|
|
|
+ if class_name in BLOCK_TYPES:
|
|
|
+ block_type = BLOCK_TYPES[class_name]
|
|
|
+ name = cell.properties["block_name"]
|
|
|
+ if name == "":
|
|
|
+ raise Exception("Encountered CBD block with empty name. Every block must have a (unique) name.")
|
|
|
+ if name in name_to_block:
|
|
|
+ raise Exception("Non-unique block name: " + name)
|
|
|
+ if "numberOfInputs" in cell.properties:
|
|
|
+ n_inputs = int(cell.properties["numberOfInputs"])
|
|
|
+ in_ports = block_type["inputs"][0:n_inputs]
|
|
|
+ else:
|
|
|
+ in_ports = block_type["inputs"]
|
|
|
+ out_ports = block_type["outputs"]
|
|
|
+ block = cbd_as.Block(
|
|
|
+ name=name,
|
|
|
+ kind=class_name,
|
|
|
+ in_ports={n : cbd_as.InputPort(name=n, incoming=[]) for n in in_ports},
|
|
|
+ out_ports={n : cbd_as.OutputPort(name=n, outgoing=[]) for n in out_ports},
|
|
|
+ )
|
|
|
+ name_to_block[name] = block
|
|
|
+ trace_links.append(trace_as.TraceabilityLink(dio=cell, cbd=block))
|
|
|
+
|
|
|
+ return (cbd_as.Model(blocks=[b for b in name_to_block.values()]), trace_links)
|