Difference between revisions of "Transformation patterns"

From Event-B
Jump to navigationJump to search
imported>Ilya.lopatkin
m
imported>Ilya.lopatkin
m
Line 1: Line 1:
The ''Transformation patterns plugin'' allows users to write transformation scripts in [http://www.eclipse.org/gmt/epsilon/doc/eol/ EOL] (simple object-based imperative language) and easily run them over models within the same project.
+
The ''Transformation patterns plugin'' allows users to write transformation scripts in [http://www.eclipse.org/gmt/epsilon/doc/eol/ EOL] (simple object-based imperative language) and easily run them over models within the same project. Editing is done in a simple text editor with syntax coloring. No compilation is needed to run transformations.
  
Editing is done in a simple text editor with syntax coloring. No compilation is needed to run transformations.
+
The plugin is essentially a wrapper around the [http://www.eclipse.org/gmt/epsilon/ Epsilon framework] of interoperatable programming languages which are used to operate over the given EMF models (Event-B in our case). The plugin performs the necessary setup for running the transformations, and provides a simple library handling user input of Event-B elements such as machines, events, variables etc.
  
== What it is ==
+
== Syntax example ==
 +
 
 +
The following is a simple example showing a syntax of patterns when using the provided library. The pattern implements the atomicity refinement of an abstract event:
 +
 
 +
import "system.eol";
 +
import "util.eol";
 +
 +
var machine: Machine = inputMachine("target");
 +
var abstractMachine: Machine = machine.getOrInputRefines();
 +
var absEvent: Event = abstractMachine.inputEvent("av", "Abstract event");
 +
var counter: Variable = machine.inputOrNewVariable("cnt", "Counter variable");
 +
var num: Integer = enterInt("Number of events");
 +
 +
for (i:Integer in Sequence{1..num})
 +
{
 +
    machine.newEvent(absEvent.name + "_ref_" + i)
 +
              .addGuard("grd_cnt", counter.name + " = " + i)
 +
              .addAction("act_cnt", counter.name + " := " + counter.name + "+1");
 +
}
 +
 +
machine.newEvent(absEvent.name + "_ref_" + num).addRefines(absEvent);
 +
 
 +
The plugin is currently using EOL. The description of syntax and facilities provided by Epsilon can be found in the first few sections of [http://dev.eclipse.org/svnroot/modeling/org.eclipse.gmt.epsilon/trunk/doc/org.eclipse.epsilon.book/EpsilonBook.pdf The Epsilon Book].
  
The plugin is essentially a wrapper around the [http://www.eclipse.org/gmt/epsilon/ Epsilon framework] of interoperatable programming languages which are used to operate over the given EMF models (Event-B in our case). The plugin performs the necessary setup for running the transformations, provides simple user interface for choosing Event-B elements such as machines, events, variables etc, and caches the user input (see later).
 
  
 
== User input cache ==
 
== User input cache ==
  
The plugin provides a cache of objects (Event-B EMF elements and strings) entered by a user. If during the execution a pattern requires to enter an object more than once, then at the first time the user will be asked for input, and the result will be cached. All subsequent requests will return the cached object unless the record about the object is erased.
+
The plugin contains a cache of objects (Event-B EMF elements and strings) entered by a user. If during the execution a pattern requires to enter an object more than once, then at the first time the user will be asked for input, and the result will be cached. All subsequent requests will return the cached object unless the record about the object is erased.
  
 
The cache can be accessed directly from within the scripts, and used as a means to pass the objects between different transformations without them knowing the actual source of input. Thus, the input can be extended to other domain-specific EMF models.
 
The cache can be accessed directly from within the scripts, and used as a means to pass the objects between different transformations without them knowing the actual source of input. Thus, the input can be extended to other domain-specific EMF models.
Line 15: Line 36:
 
A cache is a map from ''keys'' to ''values'' where a key consists of a type of object and its identifier, and the value is the actual cached object. The types of objects are handled by the utility library, while the identifiers must be supplied by script developer.
 
A cache is a map from ''keys'' to ''values'' where a key consists of a type of object and its identifier, and the value is the actual cached object. The types of objects are handled by the utility library, while the identifiers must be supplied by script developer.
  
The operations which request input from the user without caching it all start with '''enter''', such as in enterString(..), enterEvent(..) etc. The operations starting with '''input''' do the following:
+
The operations which request input from the user without caching it all start with '''enter''', such as enterString(..), enterEvent(..) etc. The operations starting with '''input''' do the following:
# check the cache for existing objects, and return if the object is in the cache
+
# they check the cache for existing objects, and return if the object is in the cache
 
# if there is no object in the cache with the given id, then ask user to enter the object
 
# if there is no object in the cache with the given id, then ask user to enter the object
 
# put the entered object into the cache and return to caller
 
# put the entered object into the cache and return to caller
The parameter '''id''' is optional. If '''id'''=null then an '''input''' operation degenerates into '''enter'''.
+
The parameter '''id''' is optional. If '''id'''=null then an '''input*''' operation degenerates into '''enter*'''.
 
 
== Full list of provided operations ==
 
 
 
{|border="1" cellpadding="10" cellspacing="0" align="center" width = "70%"
 
|-
 
| enterString(userMessage:String):String
 
 
 
enterString(userMessage, _default:String):String
 
 
 
enterInt(userMessage:String):Integer
 
 
 
enterInt(userMessage:String, _default:Integer):String
 
 
 
inputString(id:String):String
 
 
 
inputString(id, userMessage:String):String
 
 
 
inputString(id,userMessage,_default:String):String
 
 
 
inputInt(id:String):Integer
 
 
 
inputInt(id,userMessage:String):Integer
 
 
 
inputInt(id,userMessage:String, _default:Integer):Integer
 
 
 
| Operations that provide input of basic strings and integers from user. Present a simple dialog using '''userMessage''' as a description of what the user is supposed to enter. If specified, '''_default''' gives a default value for the dialog input field.
 
'''input*''' operations additionally check the input cache. If the cache contains an object with the key '''id''' then the object is returned and no dialog is shown. Otherwise the user enters the requested object, and the operation puts the latter into the cache under the key '''id'''.
 
|-
 
|inputMachine(id:String):Machine
 
 
 
inputMachine(id,userMessage:String):Machine
 
 
 
inputMachine(id,userMessage,fileName:String):Machine
 
 
 
inputContext(id:String):Context
 
 
 
inputContext(id,userMessage:String):Context
 
 
 
inputContext(id,userMessage,fileName:String):Context
 
 
 
Machine enterEvent(userMessage:String):Event
 
 
 
Machine enterVariable(userMessage:String):Variable
 
 
 
Machine inputEvent(id:String):Event
 
 
 
Machine inputEvent(id,userMessage:String):Event
 
 
 
Machine inputVariable(id:String):Variable
 
 
 
Machine inputVariable(id,userMessage:String):Variable
 
 
 
|Ask user to choose an Event-B machine/context/event/variable.
 
The dialogs present a list of appropriate elements: machines and contexts are from the current project, events and variables are from the specified machine. The '''input*''' operations check the input cache against a given '''id''' key, and put the chosen elements into the cache.
 
If '''fileName''' is specified, then the machine/context with the given name is loaded and the dialog is not shown.
 
|-
 
|Machine findEvent(name: String): Event
 
 
 
Event findWitness(name: String): Witness
 
 
 
Event findParameter(name: String): Parameter
 
 
 
Event findGuard(name: String): Guard
 
 
 
Event findAction(name:String):Action
 
 
 
Machine findVariable(name: String): Variable
 
 
 
Machine findInvariant(name: String): Invariant
 
 
 
Context findConstant(name: String): Constant
 
 
 
Context findSet(name: String): CarrierSet
 
 
 
Context findAxiom(name: String): Axiom
 
|Return the child element with the given '''name''' if it exists. Return null otherwise.
 
Example:
 
m.findEvent("send")
 
will return the event named "send" of the machine m if it exists, or null.
 
|-
 
|Machine newEvent(name: String) : Event
 
 
 
Machine newEvent(name,id: String) : Event
 
 
 
Event newWitness(name, predicate: String): Witness
 
 
 
Event newParameter(name: String): Parameter
 
 
 
Event newGuard(name, predicate: String):Guard
 
 
 
Event newAction(name, predicate: String): Action
 
 
 
Machine newVariable(name: String) : Variable
 
 
 
Machine newVariable(name,id:String) : Variable
 
 
 
Machine newInvariant(name, predicate: String): Invariant
 
 
 
Context newConstant(name: String) : Constant
 
 
 
Context newSet(name: String): CarrierSet
 
 
 
Context newAxiom(name, predicate: String): Axiom
 
|Create and return new child element within a given element. If the element with '''name''' already exists, then return the existing one. For witnesses, guards, actions, invariants and axioms, a '''predicate''' must be specified. If '''id''' is specified, then add to the cache.
 
|-
 
|style=white-space:nowrap|Machine inputOrNewEvent(id,userMessage:String):Event
 
 
 
Machine inputOrNewVariable(id,userMessage:String):Variable
 
|Ask user to enter event/variable, or creates a new one based on user input. If there are no events/variables in the machine or a user clicks Cancel during the first dialog, operations ask user to enter a name for the new element and create it.
 
Uses the input cache as in other operations.
 
|-
 
|Machine addEvent(name:String):Machine
 
 
 
Machine addEvent(name,id:String):Machine
 
 
 
Event addWitness(name, predicate: String): Event
 
 
 
Event addParameter(name: String): Event
 
 
 
Event addGuard(name, predicate: String) : Event
 
 
 
Event addAction(name, predicate: String) : Event
 
 
 
Machine addVariable(name: String):Machine
 
 
 
Machine addVariable(name,id: String):Machine
 
 
 
Machine addInvariant(name, predicate: String): Machine
 
 
 
Context addConstant(name: String): Context
 
 
 
Context addSet(name: String): Context
 
 
 
Context addAxiom(name, predicate: String): Context
 
|Add a child element with the given '''name''' (and '''predicate''' where appropriate) and return the parent element. If '''id''' is specified, then also add to the cache. Equivalent to '''new*''' operations, the difference is that '''add*''' operations return the parent object as opposed to the newly created one. This allows structuring multiple creations in a better way:
 
var m:Machine = ...;
 
m.addVariable("stop")
 
  .addInvariant("stop_inv", "fail=TRUE => stop=TRUE")
 
  .newEvent("stop")
 
    .addGuard("stop_grd", "error=TRUE")
 
    .addAction("fail_act", "fail:=TRUE")
 
    .addAction("stop_act", "stop:=TRUE");
 
Note how different elements can be added to the model without introducing unnecessary variables.
 
|-
 
|Machine addRefines(name:String):Machine
 
 
 
Machine addRefines(machine:Machine):Machine
 
 
 
Machine addSees(name:String):Machine
 
  
Machine addSees(context:Context):Machine
+
Example. If two operations request objects with the same '''id''' then only the first operation would initiate a dialog with user:
|Add '''machine'''/'''context''' to the appropriate list of refined machines / seen contexts. Can be also given by their '''name'''s. Return the container machine.
 
  
|-
+
//the first operation will cache the event with id="myid"
|Machine getRefines(): Machine
+
machine.first();
 +
//the second operation will read the event from cache if called after the first
 +
machine.second();
 +
 +
operation Machine first()
 +
{
 +
    self.inputEvent("myid", "Choose the first event").addGuard("grd1", "stop=false");
 +
}
 +
 +
operation Machine second()
 +
{
 +
    self.inputEvent("myid", "Choose the second event").addGuard("grd2", "q>3");
 +
}
  
Machine getSees(): Context
+
If the second operation represents a separate pattern then it is safe to call it from elsewhere as the input will be redirected from cache to user.
|Return the first machine in the list of refined machines, and context in the list of seen contexts. If the according list is empty, then ask user for a machine/context and add the entered object in the appropriate list. Does not check the cache.
 
|}
 

Revision as of 16:38, 29 June 2011

The Transformation patterns plugin allows users to write transformation scripts in EOL (simple object-based imperative language) and easily run them over models within the same project. Editing is done in a simple text editor with syntax coloring. No compilation is needed to run transformations.

The plugin is essentially a wrapper around the Epsilon framework of interoperatable programming languages which are used to operate over the given EMF models (Event-B in our case). The plugin performs the necessary setup for running the transformations, and provides a simple library handling user input of Event-B elements such as machines, events, variables etc.

Syntax example

The following is a simple example showing a syntax of patterns when using the provided library. The pattern implements the atomicity refinement of an abstract event:

import "system.eol";
import "util.eol";

var machine: Machine = inputMachine("target");
var abstractMachine: Machine = machine.getOrInputRefines();
var absEvent: Event = abstractMachine.inputEvent("av", "Abstract event");
var counter: Variable = machine.inputOrNewVariable("cnt", "Counter variable");
var num: Integer = enterInt("Number of events");

for (i:Integer in Sequence{1..num})
{
    machine.newEvent(absEvent.name + "_ref_" + i)
             .addGuard("grd_cnt", counter.name + " = " + i)
             .addAction("act_cnt", counter.name + " := " + counter.name + "+1");
}

machine.newEvent(absEvent.name + "_ref_" + num).addRefines(absEvent);

The plugin is currently using EOL. The description of syntax and facilities provided by Epsilon can be found in the first few sections of The Epsilon Book.


User input cache

The plugin contains a cache of objects (Event-B EMF elements and strings) entered by a user. If during the execution a pattern requires to enter an object more than once, then at the first time the user will be asked for input, and the result will be cached. All subsequent requests will return the cached object unless the record about the object is erased.

The cache can be accessed directly from within the scripts, and used as a means to pass the objects between different transformations without them knowing the actual source of input. Thus, the input can be extended to other domain-specific EMF models.

A cache is a map from keys to values where a key consists of a type of object and its identifier, and the value is the actual cached object. The types of objects are handled by the utility library, while the identifiers must be supplied by script developer.

The operations which request input from the user without caching it all start with enter, such as enterString(..), enterEvent(..) etc. The operations starting with input do the following:

  1. they check the cache for existing objects, and return if the object is in the cache
  2. if there is no object in the cache with the given id, then ask user to enter the object
  3. put the entered object into the cache and return to caller

The parameter id is optional. If id=null then an input* operation degenerates into enter*.

Example. If two operations request objects with the same id then only the first operation would initiate a dialog with user:

//the first operation will cache the event with id="myid"
machine.first();
//the second operation will read the event from cache if called after the first
machine.second();

operation Machine first()
{
   self.inputEvent("myid", "Choose the first event").addGuard("grd1", "stop=false");
}

operation Machine second()
{
   self.inputEvent("myid", "Choose the second event").addGuard("grd2", "q>3");
}

If the second operation represents a separate pattern then it is safe to call it from elsewhere as the input will be redirected from cache to user.