Code Generation (Petri nets + Python) 

  Assignment Goal

The goal of this assignment is to generate code for two di fferent platforms. The first is CPNTools, to do analysis on the Petrinet models resulting from the denotational mapping of the previous exercise. The second is Python, by generating code that will actually create your modelled railway network and all its elements. For this, a Python framework is provided.

Write a report that includes a clear explanation of your complete solution, as well as an explanation of your testing process. Please also mention possible difficulties you encountered during the assignment, and how you solved them. You will have to complete this assignment in groups of 2. Submit your assignment (report in pdf, abstract and concrete syntax de nition, example models, all rule-based model transformations, and code generators) on Blackboard before November 27, 13:00h.

Contact Simon Van Mierlo ( if you have a question.


is section lists the requirements for the code generation assignment. They are split into two sections: one for CPNTools, and one for Python. Make sure to provide test models for each part, demonstrating the functionality you implemented!


For this part, three steps are involved:
  • Create realistic examples of railway networks on which you want to perform analysis. Translate your models to Petrinets (using the model transformation of the previous assignment), and export the generated Petrinet models to metaDepth (using the provided exporter).
  • Write an exporter using the Epsilon Generation Language (EGL) that exports the Petrinet model from metaDepth to CPNTools.
  • Perform analysis on the CPNTools model. Use the analysis to prove whether, for a model with two trains, there is a way to schedule these trains fully independently. This means that they will, on their way from the start to the end station, never enter a segment that was entered by the other train.


This task also consists of three steps:
  • Export the railway metamodel and your example model(s) to metaDepth (using the provided exporter).
  • Write an exporter using the Epsilon Generation Language (EGL) that exports the railway model from metaDepth to Python.
  • Run the program! A visual interface will allow you to play the role of the "control room".


Installation: Download Unzip this file in your root AToMPM directory. This will create a folder called "exported_to_md" containing a railway folder, files needed for code generation, and metaDepth.jar. Careful: this is another version of metaDepth.


  1. Generate your Petrinet model using your rule-based model transformation for denotational semantics.
  2. Remove all traceability links and railway elements by closing the respective toolbars.
  3. Load the MetaDepth toolbar (inside of the MetaDepth folder, it is called Export.buttons.model). This toolbar has two buttons: one for exporting models, and one for exporting metamodels. Normally, you will only need the first one, as the exported PN metamodel is already present in the "exported_to_md" folder.
  4. Click on the button for exporting models, and leave the name alone. Click OK. This will generate a file with name "exported.mdepth" in the "exported_to_md" folder.
  5. Write your EGL code in the file "generate_cpntools.egl" (initially empty). This file should generate a valid CPNTools model. For more information on CPNTools, see the "Useful Links" section.
  6. Tips for generating your CPNTools file:
      We do not use color in our Petrinets, so the type of tokens that can appear in your places is UNIT, and a token is represented by the value ().
      To get an idea of the format used by CPNTools, you can create a simple Petrinet, save it somewhere, and open the fi le in your favourite text editor.
      Identi ers should be unique, start with ID, followed by one or more numbers.
  7. To actually generate the .cpn file, execute the command "java -jar metaDepth.jar < commandlist_cpntools" inside of the "exported_to_md" folder. This will generate a "exported.cpn" file.
  8. Load the "exported.cpn" file in CPNTools, and perform your analysis.


  1. Using the MetaDepth toolbar, export your RPG game metamodel. You do this by loading your metamodel (as a model), loading the toolbar and clicking on the "Export MetaModel" button. In the input dialog, write the name of your compiled metamodel (for example, "Railway").
  2. Export the model you want to generate code for using the "Export Model" button. Leave the name alone (the resulting file will be called "exported.mdepth").
  3. Write your EGL code in the file "generate_python.egl" (initially empty). This file should generate a valid Python file, which creates your railway. For more information on PNML, see the "Useful Links" section. For an example of a valid file, look at fi le in the Railway folder. Feel free to make modifications to the railway framework to suit your needs!
  4. To actually generate the Python file, execute the command "java -jar metaDepth.jar < commandlist_python" inside of the "exported_to_md" folder. This will generate a "" file. To run your game, run "python" in your console. You will need the PIL library installed to run the program.

  State Space Analysis in CPNTools

CPNTools allows you to generate the reachability graph for a given Petrinet. First of all, open the State space tool, which will show the following palette:

Click the top-left button, and then click on your Petrinet. This will calculate the reachability graph of the Petrinet, and allow you to perform analysis.

CPNTools allows to query the reachability graph, and you will have to create an appropriate query to decide whether the two trains in your model can be scheduled independently. You can write the query as a string (text) in CPNTools, and evaluate it as an ML expression. Documentation on the functions you can use can be found in the following [PDF]. The easiest way to go about this is to try and fi nd a node in the reachability graph for which the condition is not satis ed: if such a node can be found, your query will return it, and the trains cannot be scheduled independently. If no such node is found, the trains can be scheduled independently. Use the SearchNodes function, and use the Mark facility to check the markings of speci c places in the nodes of the reachability graph in the condition passed to your call to SearchNodes. Please note that Mark has its limitations: it is not possible to check the markings of all places without knowing their name. You have to hard-code the name of the place as in Mark.exported'myPlace 1 n ('myPlace' cannot be a variable). This will probably force you to manually adapt your query whenever another exported model is being checked. Please explain the workflow for checking the condition on any railway model in your report.

  Useful Links