Difference between pages "Code Generation Tutorial" and "The Use of Theories in Code Generation"

From Event-B
(Difference between pages)
Jump to navigationJump to search
imported>Andy
 
imported>Andy
 
Line 1: Line 1:
For more information contact Andy Edmunds - University of Southampton - mailto:ae2@ecs.soton.ac.uk
+
= Defining Translations Using The Theory Plug-in =
=== Tutorial Overview ===
+
The theory plug-in is used to add mathematical extensions to Rodin. The theories are created, and deployed, and can then be used in any models in the workspace. When dealing with implementation level models, such as in Tasking Event-B, we need to consider how to translate newly added types and operators into code. We have augmented the theory interface with a Translation Rules section. This enables a user to define translation rules that map Event-B formulas to code.
 +
== Translation Rules==
 +
Code generation rules are specified in a theory file, which is created using the Theory plug-in.
  
The aim of the tutorial is to allow users to explore the approach with a relatively simple example. The example uses a shared buffer with reader and writer processes. The tutorial is presented in three stages, making use of the example projects from the download site. There are two translations performed, one is to a common language model (IL1). The second is to an Event-B project which includes a model of the implementation. There is a PrettyPrinter for Ada source code, which uses the common language model. An overview of Tasking Event-B can be found at http://wiki.event-b.org/index.php/Tasking_Event-B_Overview.
+
<div id="fig:Translation Rules">
 +
<br/>
 +
[[Image:TheoryCGRules.png|center||caption text]]
 +
<center>'''Figure 1''': Translation Rules</center>
 +
<br/>
 +
</div>
  
A typical Event-B development may be refined to the point where it is ready for implementation, but the Event-B language is not expressive enough to fully describe the implementation. Tasking Event-B facilitates this final step to implementation, by extending Event-B with the necessary constructs. Event-B machines that are to be implemented (and their seen Contexts) are selected and added to a ''Tasking Development''; the Tasking Development files have the file extension ''.tasking''. The machines in the Tasking Development are then extended with implementation details.
+
Figure 1 shows a pretty print of some of the translations rules that have been specified to generate Ada code. In the figure we can see that the theory is given a name, and may import some other theories. Type parameters can be added, and we use them here to type the meta-variables. The meta-variable ''a'' is restricted to be an integer type, but meta-variable ''c'' can be any unspecified type, ''Q''. Meta-variables are used in the translator rules for pattern matching.
  
The example/tutorial projects are,
+
Translator rules are templates, used in a pattern matching algorithm in the code generator. Event-B formulas are defined on the left hand side of the rule, and the code to be output (as text) appears on the right hand side of the matching rule. During translation an abstract syntax tree (AST) representation of the formula is used. The theory plug-in attempts to match the formulas in the rules with each syntactic element of the AST. As it does so it builds the textual output as a string, until the whole AST has been successfully matched. When a complete tree is matched, the target code is returned. If the AST is not matched, a warning is issued, and a string representation of the original formula is returned.
  
{| border="1"
+
== Type Rules for Code Generation ==
|SharedBuffer20100819Demo
 
|An example project with a completed Tasking Development and IL1 model (post IL1 translation, but before Event-B translation).
 
|-
 
|Sharedbuffer20100819Tasking
 
|Same as the example project above, but with Event-B model translations. The difference being that this development includes a model of the implementation. These are refinements that include a program counter to describe flow of execution in each task.
 
|-
 
|SharedBuffer20100819Tutorial
 
|A bare project for step 1 of the tutorial.
 
|-
 
|Sharedbuffer20100819Tutorial2
 
|A partially completed tasking development for steps 2 and 3 of the tutorial.
 
|}
 
  
== Preliminaries ==
+
The type rules section, shown in Figure 1, is where the relationship is defined, between Event-B types and the type system of the implementation.
Before further discussion of the modelling aspects, we take a look at the PrettyPrint viewers. The PrettyPrinters make the viewing of IL1 and tasking models easier; it also provides a route to generate source code. The source code can easily be pasted from the IL1 Pretty Printer window into an the Ada source file .
 
==== The PrettyPrint View of a Tasking Development ====
 
To open the Tasking PrettyPrint viewer,
 
* from the top-menu select ''Window/Show View/Other/Tasking Pretty Printer''.
 
  
Note that the Tasking PrettyPrinter may have to be closed when editing the Tasking Development, since it can give rise to exceptions. The PrettyPrinter would need further work to make it robust, however it is intended only as a short-term solution.
+
= Adding New (implementation-level) Types =
 +
When we are working at abstraction levels close to the implementation level, we may make an implementation decision which requires the introduction of a new type to the development. We give an example of our approach, where we add a new array type, shown in Figure 2, and then define its translation to code.
  
* Open the ''SharedBuffer20100819Demo'' Project and switch to the Resource Perspective.
+
== An Array Type Definition ==
* Open the ''.tasking'' model and inspect it. Clicking on the Main, Machine or Event nodes updates the pretty print window.
+
<div id="fig:Extension with an Array Type">
 +
<br/>
 +
[[Image:ArrayDef.png|center||caption text]]
 +
<center>'''Figure 2''': Array Definition</center>
 +
<br/>
 +
</div>
  
==== Viewing Source Code ====
+
The array operator notation is defined in the expression array(s: P(T)); and the semantics is defined in the direct definition. arrayN constrains the arrays to be of fixed length. Array lookup, update, and constructor operators are subsequently defined. In the next step we need to define any translations required to implement the array in code.
aka. The PrettyPrint View of an IL1 Model.
 
  
To view Ada source code,
+
== Translation Rules ==
* from the top-menu select ''Window/Show View/Other/IL1 Pretty Printer''.
 
* Open the ''SharedBuffer20100819Demo'' Project and switch to the Resource Perspective.
 
* Open the ''.il1'' model and inspect it. Clicking on the Protected, Main Entry, or Task nodes updates the pretty print window.
 
  
==== Cleaning the Tasking Development ====
+
<div id="Translation Rules for the Array Type">
If the ''.tasking'' file has errors, then it may need cleaning. To do this right-click on the ''Main'' node, select ''Epsilon Translation/CleanUp''. If a model has errors it can still be viewed by clicking on the ''Selection'' tab at the bottom of the tasking editor window.
+
<br/>
 +
[[Image:ArrayTrans.png|center||caption text]]
 +
<center>'''Figure 3''': Translation Rules for the Array Type</center>
 +
<br/>
 +
</div>
  
== The Tutorial ==
+
Figure 3 shows the Ada translation; beginning with the meta-variable definitions that are used for pattern matching in the translation rules. Each of the operators; ''newArray'', and ''update'', and an expression using the ''lookup'' operator, are mapped to their implementations on the right hand side of the rule. The ''Type Rules'' section describes the implementation's description of the ''arrayN'' type.
The steps needed to generate code from an Event-B model, in this tutorial, are as follows,
 
* Step 1 - [http://wiki.event-b.org/index.php/Code_Generation_Tutorial#Creating_The_Tasking_Development Create the tasking development].
 
* Step 2 - [http://wiki.event-b.org/index.php/Code_Generation_Tutorial#Providing_the_Annotations_for_Implementations Add annotations]
 
* Step 3 - [http://wiki.event-b.org/index.php/Code_Generation_Tutorial#Invoking_the_Translation Invoke translators].
 
 
 
==== Creating The Tasking Development ====
 
* Change to the Event-B Perspective.
 
* Open the ''SharedBuffer20100819Tutorial'' Project.
 
* Select the following Machines: Reader, Writer and Shared.
 
* Right-click and select ''Make Tasking Development/Generate Tasking Development''.
 
 
 
The new Tasking Development will not be visible in the Event-B perspective, change to the resource perspective, open and inspect the new ''.tasking'' file. The Tasking Development contains (the EMF representation of) the machines that we wish to provide implementations for. In order to introduce the new concepts we have prepared a partially complete development.
 
 
 
Change to the Project ''SharedBuffer20100819Tutorial2'' to begin the next step.
 
 
 
==== Providing the Annotations for Implementations ====
 
* Close any Tasking Pretty Print Viewers that remain open. The incomplete model will give rise to exceptions.
 
* Go to the to the Resource Perspective.
 
* Open and inspect the ''.tasking'' machine.
 
 
 
The ''WriterTsk'' and ''SharedObj'' machines are incomplete. We will take the steps to necessary to provide implementation details.
 
 
 
===== The WriterTsk Machine =====
 
In the partially complete tutorial project we already identified the ''WriterTsk'' as an ''Auto Task'' Tasking Machine, by adding the ''Auto Task'' extension. ''Auto Tasks'' are tasks that will be declared and defined in the ''Main'' procedure of the implementation. The effect of this is that the ''Auto Tasks'' are created when the program first loads, and then activated (made ready to run) before the ''Main'' procedure body runs. We have added the ''Periodic Task'' extension to the ''Auto Task'', and set a period of 250 milliseconds. We will now complete the sequence that has been partially defined in the task body.
 
 
 
*'''Add Synchronisation between TWrite and SWrite'''.
 
** Expand the ''Auto Task Machine'' node.
 
** Expand the ''Seq'' sub-tree.
 
** Right-click on the ''Seq'' node and select ''New Child/Left Branch EventWrapper''.
 
** Provide the event label ''w1'' using the properties view.
 
** Right-click on Event Wrapper and select ''New Child/ Synch Events''.
 
** Select ''Synch Events'' and go to the drop-down menu of the ''Local Event'' property.
 
** At this point the drop-down box displays a number of event names, select the ''TWrite'' event.
 
** Go to the drop-down menu of the ''Remote Event'' property.
 
** From the list of events select the ''SWrite'' event.
 
 
 
The Synch Events construct is used to implement [http://wiki.event-b.org/index.php/Tasking_Event-B_Overview#Control_Constructs Event Synchronisation]. The next step wraps an event in an Event Wrapper in order to update the local state; there is no synchronisation as such but we will re-use the constructs that already exist.
 
 
 
*'''Add the Wrapped Event TcalcWVal'''.
 
** Expand the sub-tree of the second ''Seq'' node.
 
** Right-click on the ''Seq'' node and select ''New Child/Left Branch EventWrapper''.
 
** Provide the event label ''w2'' using the properties view.
 
** Right-click on Event Wrapper and select ''New Child/ Synch Events''.
 
** Select ''Synch Events'' and go to the drop-down menu of the ''Local Event'' property.
 
** From the list of events select the ''TcalcWVal'' event.
 
 
 
We have now completed the task body, and it just remains to complete provide details for the ''TWrite'' event. The ''TWrite'' event in ''WriterTsk'' is to be synchronized with the ''SWrite'' event in the ''SharedObj''.
 
*'''Add Event Extensions'''.
 
** Right-click on the ''TWrite'' Event node.
 
** Select ''New Child/Extension''.
 
** Right-click on the ''Extension'' node and select ''New Child/Implementation'' from the menu.
 
** Go to the Implementation properties view and set the ''Implementation Type'' property to ''ProcedureSynch''.
 
 
 
*'''Identify Incoming and Outgoing parameters'''.
 
** Right-click on the ''outAP'' node and add an ''Extension''.
 
** Right-click on the ''Extension'' and select''New Child/Parameter Type''.
 
** Go to the ''Parameter Type'' properties view and set the ''Parameter Type'' property to ''actualOut''.
 
** Right-click on the ''inAP'' node and add an ''Extension''.
 
** Right-click on the ''Extension'' and select''New Child/Parameter Type''.
 
** Go to the ''Parameter Type'' properties view and set the ''Parameter Type'' property to ''actualIn''.
 
 
 
===== The Shared Machine =====
 
 
 
The next step is to identify the ''SharedObj'' machine as a ''Shared Machine''. The ''SharedObj'' Machine will be extended using the Event-B EMF extension mechanism.
 
* Right-click on the ''SharedObj'' Machine node in the ''.tasking'' file.
 
* Select ''New Child/Extension''.
 
* Right-click on the ''Extension'' node and select ''New Child/Shared Machine'' from the menu.
 
 
 
We now show how to extend the ''SWrite'' event of the Shared Machine with details about its implementation. The ''SWrite'' event in ''SharedObj'' is to be synchronized with the ''TWrite'' event in the ''WriterTsk''.
 
* '''Identify SWrite as a Syncronisation'''.
 
** Right-click on the ''SWrite'' Event node.
 
** Select ''New Child/Extension''.
 
** Right-click on the ''Extension'' node and select ''New Child/Implementation'' from the menu.
 
** Go to the Implementation properties view and set the ''Implementation Type'' property to ''ProcedureSynch''.
 
 
 
* '''Identify incoming and outgoing parameters'''.
 
** Right-click on the ''inFP'' node and add an ''Extension''.
 
** Right-click on the ''Extension'' and select''New Child/Parameter Type''.
 
** Go to the ''Parameter Type'' properties view and set the ''Parameter Type'' property to ''formalIn''.
 
** Right-click on the ''outFP'' node and add an ''Extension''.
 
** Right-click on the ''Extension'' and select''New Child/Parameter Type''.
 
** Go to the ''Parameter Type'' properties view and set the ''Parameter Type'' property to ''formalOut''.
 
 
 
===== A Summary of Steps =====
 
 
 
For a Tasking Machine definition:
 
# Add the Tasking Machine type (Auto etc).
 
# Add the task type (Periodic etc.).
 
# Define the task priority.
 
# Define the task body.
 
# For each event, add the Event Type.
 
# For each event parameter, add the Parameter Type.
 
 
 
 
 
For a Shared Machine definition:
 
# Add the ''SharedMachine'' Machine type.
 
# For each event, define the Event Type.
 
# For each event parameter, define the Parameter Type.
 
 
 
==== Invoking the Translation ====
 
 
 
* To create the IL1 model,
 
** Right-Click on the Main node, select ''Epsilon Translation/Translate Task Mch 2 IL1 EMF''.
 
** Open the Resource Perspective.
 
** Right-click on the ''sharedbuffer20100819Tutorial2'' project folder.
 
** Select refresh, the ''.il1'' file should appear in the project.
 
** Open and inspect the file, and view the source code by opening the IL1 Pretty Print view if desired.
 
 
 
* To create the Event-B model of the implementation,
 
** Return to the Rodin Modelling Perspective.
 
** Right-Click on the Main node, select ''Epsilon Translation/Translate Task Mch 2 Event-B EMF''.
 
** The ''sharedbuffer20100819bTasking'' project is generated, it can be opened and inspected.
 
 
 
There are errors in the generated machines (not investigated the cause yet); these can be fixed in the following way.
 
* Open a Machine in the Event-B Machine Editor.
 
* Select the Edit tab.
 
* Open the REFINES section, the error lies here.
 
* The correct machine is refined, but choose a different machine to refine (any one, it doesn't matter).
 
* Select the original refined machine again.
 
* Save and clean the project, and the error should disappear.
 
* Repeat for the same errors in the other machines; save and clean again.
 
* The machines can viewed as normal using the Rodin editors.
 
 
 
[[Category:User documentation]]
 

Revision as of 10:51, 17 May 2012

Defining Translations Using The Theory Plug-in

The theory plug-in is used to add mathematical extensions to Rodin. The theories are created, and deployed, and can then be used in any models in the workspace. When dealing with implementation level models, such as in Tasking Event-B, we need to consider how to translate newly added types and operators into code. We have augmented the theory interface with a Translation Rules section. This enables a user to define translation rules that map Event-B formulas to code.

Translation Rules

Code generation rules are specified in a theory file, which is created using the Theory plug-in.


caption text
Figure 1: Translation Rules


Figure 1 shows a pretty print of some of the translations rules that have been specified to generate Ada code. In the figure we can see that the theory is given a name, and may import some other theories. Type parameters can be added, and we use them here to type the meta-variables. The meta-variable a is restricted to be an integer type, but meta-variable c can be any unspecified type, Q. Meta-variables are used in the translator rules for pattern matching.

Translator rules are templates, used in a pattern matching algorithm in the code generator. Event-B formulas are defined on the left hand side of the rule, and the code to be output (as text) appears on the right hand side of the matching rule. During translation an abstract syntax tree (AST) representation of the formula is used. The theory plug-in attempts to match the formulas in the rules with each syntactic element of the AST. As it does so it builds the textual output as a string, until the whole AST has been successfully matched. When a complete tree is matched, the target code is returned. If the AST is not matched, a warning is issued, and a string representation of the original formula is returned.

Type Rules for Code Generation

The type rules section, shown in Figure 1, is where the relationship is defined, between Event-B types and the type system of the implementation.

Adding New (implementation-level) Types

When we are working at abstraction levels close to the implementation level, we may make an implementation decision which requires the introduction of a new type to the development. We give an example of our approach, where we add a new array type, shown in Figure 2, and then define its translation to code.

An Array Type Definition


caption text
Figure 2: Array Definition


The array operator notation is defined in the expression array(s: P(T)); and the semantics is defined in the direct definition. arrayN constrains the arrays to be of fixed length. Array lookup, update, and constructor operators are subsequently defined. In the next step we need to define any translations required to implement the array in code.

Translation Rules


caption text
Figure 3: Translation Rules for the Array Type


Figure 3 shows the Ada translation; beginning with the meta-variable definitions that are used for pattern matching in the translation rules. Each of the operators; newArray, and update, and an expression using the lookup operator, are mapped to their implementations on the right hand side of the rule. The Type Rules section describes the implementation's description of the arrayN type.