|
|
@@ -0,0 +1,119 @@
|
|
|
+import io
|
|
|
+import os
|
|
|
+import typing
|
|
|
+
|
|
|
+import jinja2
|
|
|
+
|
|
|
+from drawio2oml.pm import abstract_syntax as pm_as
|
|
|
+from drawio2oml.pm import trace_abstract_syntax as trace_as
|
|
|
+from drawio2oml import util
|
|
|
+
|
|
|
+# Mapping from Python class to OML concept
|
|
|
+TYPENAMES = {
|
|
|
+ pm_as.InitialNode: "pm:Initial",
|
|
|
+ pm_as.FinalNode: "pm:Final",
|
|
|
+ pm_as.Activity: "pm:Activity",
|
|
|
+ pm_as.Artifact: "pm:Artifact",
|
|
|
+ pm_as.CtrlInPort: "pm:CtrlInputPort",
|
|
|
+ pm_as.CtrlOutPort: "pm:CtrlOutputPort",
|
|
|
+ pm_as.DataInPort: "pm:DataInputPort",
|
|
|
+ pm_as.DataOutPort: "pm:DataOutputPort",
|
|
|
+ pm_as.ForkJoin: "pm:ForkJoin",
|
|
|
+ pm_as.CtrlFlowConnection: "pm:CtrlFlow",
|
|
|
+ pm_as.DataFlowConnection: "pm:DataFlow",
|
|
|
+}
|
|
|
+
|
|
|
+def assign_names(pm_model: pm_as.ProcessModel) -> typing.Dict[pm_as.Element, str]:
|
|
|
+ """Assign unique names to all the elements in the process model, in a deterministic fashion"""
|
|
|
+ names = {
|
|
|
+ pm_model.initial: "initial",
|
|
|
+ pm_model.final: "final",
|
|
|
+ }
|
|
|
+ names.update({
|
|
|
+ artifact: "artifact_"+str(idx) for (idx,artifact) in enumerate(pm_model.artifacts)
|
|
|
+ })
|
|
|
+ names.update({
|
|
|
+ activity: "activity_"+str(idx) for (idx,activity) in enumerate(pm_model.activities)
|
|
|
+ })
|
|
|
+ for a_idx, activity in enumerate(pm_model.activities):
|
|
|
+ for p_idx, port in enumerate(util.concat(activity.ctrl_in, activity.ctrl_out, activity.data_in, activity.data_out)):
|
|
|
+ names[port] = "activity_"+str(a_idx)+"_port_"+str(p_idx);
|
|
|
+ names.update({
|
|
|
+ forkjoin: "forkjoin_"+str(idx) for (idx,forkjoin) in enumerate(pm_model.forkjoins)
|
|
|
+ })
|
|
|
+ names.update({
|
|
|
+ ctrl_flow: "ctrl_flow_"+str(idx) for (idx,ctrl_flow) in enumerate(pm_model.ctrl_flows)
|
|
|
+ })
|
|
|
+ names.update({
|
|
|
+ data_flow: "data_flow_"+str(idx) for (idx,data_flow) in enumerate(pm_model.data_flows)
|
|
|
+ })
|
|
|
+ return names
|
|
|
+
|
|
|
+
|
|
|
+def write_pm_oml(
|
|
|
+ input_filename: str,
|
|
|
+ pm_model: pm_as.ProcessModel,
|
|
|
+ pm_names: typing.Dict[pm_as.Element, str],
|
|
|
+ ostream: io.TextIOBase,
|
|
|
+ output_namespace: str = "http://ua.be/sdo2l/description/artifacts/my_pm",
|
|
|
+ namespaces=util.DTDESIGN_NAMESPACES):
|
|
|
+ """
|
|
|
+ Generate an OML description of a process model.
|
|
|
+ Parameters:
|
|
|
+ input_filename: has no precise semantics - only written to the output in a comment
|
|
|
+ pm_model: the parsed process model to write as an OML description
|
|
|
+ pm_names: mapping from every element in the process model to a unique name
|
|
|
+ ostream: stream to write OML output to (e.g., stdout, a file, ...)
|
|
|
+ output_namespace: namespace for the to-be-generated OML description
|
|
|
+ namespaces: namespaces of vocabularies to use
|
|
|
+ """
|
|
|
+
|
|
|
+ environment = jinja2.Environment(
|
|
|
+ loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
|
|
|
+ environment.filters['to_oml_string_literal'] = util.to_oml_string_literal
|
|
|
+ template = environment.get_template("oml_template_pm.j2")
|
|
|
+
|
|
|
+
|
|
|
+ for piece in template.generate(
|
|
|
+ model=pm_model,
|
|
|
+ input_filename=input_filename,
|
|
|
+ enumerate=enumerate,
|
|
|
+ concat=util.concat,
|
|
|
+ pm_names=pm_names,
|
|
|
+ types=TYPENAMES,
|
|
|
+ output_namespace=output_namespace,
|
|
|
+ namespaces=namespaces):
|
|
|
+ ostream.write(piece)
|
|
|
+
|
|
|
+
|
|
|
+def write_traceability_oml(
|
|
|
+ input_filename: str,
|
|
|
+ traceability_links: typing.List[trace_as.TraceabilityLink],
|
|
|
+ pm_names: typing.Dict[pm_as.Element, str],
|
|
|
+ ostream: io.TextIOBase,
|
|
|
+ output_namespace: str = "http://ua.be/sdo2l/description/artifacts/my_trace",
|
|
|
+ namespaces=util.DTDESIGN_NAMESPACES):
|
|
|
+ """
|
|
|
+ Generate an OML description of the traceability links between a Drawio-description and a PM-description.
|
|
|
+ Parameters:
|
|
|
+ input_filename: has no precise semantics - only written to the output in a comment
|
|
|
+ traceability_links: list of traceability links to write as an OML description
|
|
|
+ ostream: stream to write OML output to (e.g., stdout, a file, ...)
|
|
|
+ output_namespace: namespace for the to-be-generated OML description
|
|
|
+ namespaces: namespaces of vocabularies to use
|
|
|
+ """
|
|
|
+ environment = jinja2.Environment(
|
|
|
+ loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
|
|
|
+ environment.filters['to_oml_string_literal'] = util.to_oml_string_literal
|
|
|
+ trace_template = environment.get_template("oml_template_trace.j2")
|
|
|
+
|
|
|
+ for piece in trace_template.generate(
|
|
|
+ traceability_links=traceability_links,
|
|
|
+ pm_names=pm_names,
|
|
|
+ input_filename=input_filename,
|
|
|
+ enumerate=enumerate,
|
|
|
+ concat=util.concat,
|
|
|
+ types=TYPENAMES,
|
|
|
+ output_namespace=output_namespace,
|
|
|
+ namespaces=namespaces):
|
|
|
+ ostream_trace.write(piece)
|