Difference between pages "Event-B XText Front-end User Guide" and "The Use of Theories in Code Generation"

From Event-B
(Difference between pages)
Jump to navigationJump to search
imported>Son
 
imported>Andy
 
Line 1: Line 1:
== Introduction ==
+
= 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==
 +
<div id="fig:Translation Rules">
 +
<br/>
 +
[[Image:TheoryCGRules.png|center||caption text]]
 +
<center>'''Figure 1''': Translation Rules</center>
 +
<br/>
 +
</div>
  
The Event-B XText front-end provides text editors for XContexts and and XMachines which then compiled automatically to Event-B contexts and machines.<br>
+
Figure 1 shows the interface, and some translations rules of the mapping to Ada.
For more details about the principles of this editor,  see [[Event-B_XText_Front-end|the Event-B XText Front-end page]].<br>
 
  
== Installation ==
+
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 type. Meta-variables are used in the translator rules for pattern matching.
  
=== Setup ===
+
Translator rules are templates, which are used in pattern matching. 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.
  
* Before install the Event-B XText front-end, you need to add the XText update site (http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/) as an additional software site.
+
== Type Rules for Code Generation ==
* The Event-B XText front-end is available as a separate plug-in from the main Rodin update site (under 'Editors' category)
 
  
=== Release Notes ===
+
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.
See [[Event-B_XText_Front-end_Release_Notes | Event-B XText Front-end Release Notes]]
 
  
=== IMPORTANT ===
+
= Adding New (implementation-level) Types =
* Currently, Event-B XText front-end ONLY supports "standard" Event-B machines and contexts.
+
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.
* Since the XContexts and XMachines are compiled to the Rodin files, the corresponding Rodin contexts and machines will be '''OVER-WRITTEN'''. Any changes in the Rodin files will not be lost.
 
* '''DO NOT USE''' the Event-B XText Front-end if you use modelling plug-ins such as iUML-B state-machines and class-diagrams, as the additional modelling elements will be over-written.
 
  
=== KNOWN ISSUES ===
+
== An Array Type Definition ==
- Converting to XText: Currently, the "extended" attribute of events are not serialised.
+
<div id="fig:Extension with an Array Type">
 +
<br/>
 +
[[Image:ArrayDef.png|center||caption text]]
 +
<center>'''Figure 2''': Array Definition</center>
 +
<br/>
 +
</div>
  
== Configuration ==
+
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.
  
=== Event-B Explorer ===
+
== Translation Rules ==
By default, XContext files (extension ''bucx'') and XMachine files (extension ''bumx'') are not display in the ''Event-B Explorer''. To enable this, select ''Customize view'' for ''Event-B Explorer'' and uncheck the option ''All files and folders''.
 
  
 +
<div id="Translation Rules for the Array Type">
 +
<br/>
 +
[[Image:ArrayTrans.png|center||caption text]]
 +
<center>'''Figure 3''': Translation Rules for the Array Type</center>
 +
<br/>
 +
</div>
  
== Editing ==
+
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.
 
 
=== XContext ===
 
* Any file with extension ''*.bucx'' will be recognised as XContext file. As a result, XContext file can be created by the standard ''New File'' wizard of Eclipse.
 
* The XText syntax of XContext file (v0.0.3) is as follows.
 
  XContext returns econtext::Context:
 
    {econtext::Context}
 
    (comment=(ML_COMMENT | SL_COMMENT))?
 
    'context' name=ID
 
    ('extends' extends+=[econtext::Context]+)?
 
    ('sets' sets+=XCarrierSet+)?
 
    ('constants' constants+=XConstant+)?
 
    ('axioms' axioms+=XAxiom+)?
 
    'end'
 
  ;
 
 
 
  XCarrierSet returns econtext::CarrierSet:
 
    {econtext::CarrierSet}
 
    (comment=(ML_COMMENT | SL_COMMENT))?
 
    name=ID
 
  ;
 
 
 
  XConstant returns econtext::Constant:
 
    {econtext::Constant}
 
    (comment=(ML_COMMENT | SL_COMMENT))?
 
    name=ID
 
  ;
 
 
 
  XAxiom returns econtext::Axiom:
 
    {econtext::Axiom}
 
    (comment=(ML_COMMENT | SL_COMMENT))?
 
    name=XLABEL predicate=STRING (theorem?='theorem')?
 
  ;
 
 
 
  terminal XLABEL returns ecore::EString:
 
    '@' !(':')+ ':'
 
  ;
 
 
 
=== XMachine ===
 
* Any file with extension ''*.bumx'' will be recognised as XMachine file. As a result, XMachine file can be created by the standard ''New File'' wizard of Eclipse.
 
* The XText syntax of XMachine file is as follows.
 
  XMachine returns emachine::Machine:
 
    {emachine::Machine}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    'machine' name=ID
 
    ('refines' refines+=[emachine::Machine])?
 
    ('sees' sees+=[econtext::Context]+)?
 
    ('variables' variables+=XVariable+)?
 
    ('invariants' invariants+=XInvariant+)?
 
    ('variant' variant=XVariant)?
 
    ('events' events+=XEvent (';' events+=XEvent)*)?
 
    ('end')
 
  ;
 
 
 
  XVariable returns emachine::Variable:
 
    {emachine::Variable}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=ID
 
  ;
 
 
 
  XInvariant returns emachine::Invariant:
 
    {emachine::Invariant}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=XLABEL predicate=STRING (theorem?='theorem')?
 
  ;
 
 
 
  terminal XLABEL returns ecore::EString:
 
    '@' !(':')+ ':'
 
  ;
 
 
 
  XVariant returns emachine::Variant:
 
    {emachine::Variant}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    expression=STRING
 
  ;
 
 
 
  XEvent returns emachine::Event:
 
    {emachine::Event}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=ID
 
    (
 
      (extended?='extended')? &
 
      (convergence=XConvergence)?
 
    )
 
    ('refines' refines+=[emachine::Event]+)?
 
    (
 
      ('with' witnesses+=XWitness+)?
 
      'begin'
 
        actions+=XAction+
 
      'end'
 
    |
 
      'when'
 
        guards+=XGuard+
 
      ('with' witnesses+=XWitness+)?
 
      ('then'
 
        actions+=XAction+)?
 
      'end'
 
    |
 
      'any'
 
        parameters+=XParameter+
 
      'where'
 
        guards+=XGuard+
 
      ('with' witnesses+=XWitness+)?
 
      ('then'
 
        actions+=XAction+)?
 
      'end'
 
    )?
 
  ;
 
 
 
  enum XConvergence returns emachine::Convergence:
 
    ordinary = 'ordinary' | convergent = 'convergent' | anticipated = 'anticipated';
 
 
 
  XParameter returns emachine::Parameter:
 
    {emachine::Parameter}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=ID
 
  ;
 
 
 
  XGuard returns emachine::Guard:
 
    {emachine::Guard}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=XLABEL predicate=STRING (theorem?='theorem')?
 
  ;
 
 
 
  XWitness returns emachine::Witness:
 
    {emachine::Witness}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=XLABEL predicate=STRING
 
  ;
 
 
 
  XAction returns emachine::Action:
 
    {emachine::Action}
 
    (comment=(ML_COMMENT|SL_COMMENT))?
 
    name=XLABEL action=STRING
 
  ;
 
 
 
=== Type Setting Event-B Symbols ===
 
The Event-B Symbols are supported via Content assist, e.g., when type setting invariant or action. Sometime, a preceding blank space is required to enable the correct content assist.
 
 
 
== Converting Rodin files to Event-B XText ==
 
Rodin contexts and machines can be converted to XContext and XMachine files using context menu. From the ''Event-B Explorer'', right click on a Rodin project, a Rodin context, or a Rodin machine will offer option ''Convert to XText''. When a Rodin project is selected, all Rodin contexts and machines within that project will be converted.
 

Revision as of 15:51, 15 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


caption text
Figure 1: Translation Rules


Figure 1 shows the interface, and some translations rules of the mapping to Ada.

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 type. Meta-variables are used in the translator rules for pattern matching.

Translator rules are templates, which are used in pattern matching. 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.