diff --git a/examples/petrinet/helpers.py b/examples/petrinet/helpers.py new file mode 100644 index 0000000..1860a69 --- /dev/null +++ b/examples/petrinet/helpers.py @@ -0,0 +1,6 @@ +from uuid import UUID + +def get_num_tokens(odapi, place: UUID): + pn_of = odapi.get_incoming(place, "pn_of")[0] + place_state = odapi.get_source(pn_of) + return odapi.get_slot_value(place_state, "numTokens") diff --git a/examples/petrinet/runner_export_tapaal.py b/examples/petrinet/runner_export_tapaal.py new file mode 100644 index 0000000..7ef1ab8 --- /dev/null +++ b/examples/petrinet/runner_export_tapaal.py @@ -0,0 +1,35 @@ +from state.devstate import DevState +from bootstrap.scd import bootstrap_scd +from util import loader + +from examples.petrinet.translational_semantics.tapaal.exporter import export_tapaal + +if __name__ == "__main__": + import os + THIS_DIR = os.path.dirname(__file__) + + # get file contents as string + def read_file(filename): + with open(THIS_DIR+'/'+filename) as file: + return file.read() + + state = DevState() + scd_mmm = bootstrap_scd(state) + # Read models from their files + mm_cs = read_file('metamodels/mm_design.od') + mm_rt_cs = mm_cs + read_file('metamodels/mm_runtime.od') + # m_cs = read_file('models/m_example_simple.od') + # m_rt_initial_cs = m_cs + read_file('models/m_example_simple_rt_initial.od') + m_cs = read_file('models/m_example_mutex.od') + m_rt_initial_cs = m_cs + read_file('models/m_example_mutex_rt_initial.od') + # m_cs = read_file('models/m_example_inharc.od') + # m_rt_initial_cs = m_cs + read_file('models/m_example_inharc_rt_initial.od') + + # Parse them + mm = loader.parse_and_check(state, mm_cs, scd_mmm, "Petri-Net Design meta-model") + mm_rt = loader.parse_and_check(state, mm_rt_cs, scd_mmm, "Petri-Net Runtime meta-model") + m = loader.parse_and_check(state, m_cs, mm, "Example model") + m_rt_initial = loader.parse_and_check(state, m_rt_initial_cs, mm_rt, "Example model initial state") + + with open('exported.tapn', 'w') as f: + f.write(export_tapaal(state, m=m_rt_initial, mm=mm_rt)) diff --git a/examples/petrinet/translational_semantics/tapaal/exporter.py b/examples/petrinet/translational_semantics/tapaal/exporter.py new file mode 100644 index 0000000..27e14f2 --- /dev/null +++ b/examples/petrinet/translational_semantics/tapaal/exporter.py @@ -0,0 +1,17 @@ +import jinja2 +import os +THIS_DIR = os.path.dirname(__file__) + +from api.od import ODAPI +from examples.petrinet import helpers +from util.module_to_dict import module_to_dict + +def export_tapaal(state, m, mm): + loader = jinja2.FileSystemLoader(searchpath=THIS_DIR) + environment = jinja2.Environment(loader=loader) + template = environment.get_template("tapaal.jinja2") + return template.render({ + 'odapi': ODAPI(state, m, mm), + **globals()['__builtins__'], + **module_to_dict(helpers), + }) diff --git a/examples/petrinet/translational_semantics/tapaal/tapaal.jinja2 b/examples/petrinet/translational_semantics/tapaal/tapaal.jinja2 new file mode 100644 index 0000000..445dc99 --- /dev/null +++ b/examples/petrinet/translational_semantics/tapaal/tapaal.jinja2 @@ -0,0 +1,52 @@ + + + + + {% for i, (place_name, place) in enumerate(odapi.get_all_instances("PNPlace")) %} + + {% endfor %} + + {% for i, (transition_name, transition) in enumerate(odapi.get_all_instances("PNTransition")) %} + + {% endfor %} + + {% for arc_name, arc in odapi.get_all_instances("arc") %} + + + + + {% endfor %} + + {% for inh_arc_name, inh_arc in odapi.get_all_instances("inh_arc") %} + + + + + {% endfor %} + + + + \ No newline at end of file