Model-Driven Language Engineering
Example of the ThingML language
Franck Fleurey
SINTEF Research Scientist
(franck.fleurey@sintef.no) http://www.fleurey.com/franck
INF5120 - April 2012
Engineering Example of the ThingML language Franck Fleurey SINTEF - - PowerPoint PPT Presentation
Model-Driven Language Engineering Example of the ThingML language Franck Fleurey SINTEF Research Scientist (franck.fleurey@sintef.no) http://www.fleurey.com/franck INF5120 - April 2012 Language Engineering Programming languages Domain
SINTEF Research Scientist
(franck.fleurey@sintef.no) http://www.fleurey.com/franck
INF5120 - April 2012
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
2
Programming languages Domain specific languages Modelling languages Who is the user? What is the purpose?
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
3
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
4
Traditionally: Grammar / Compilers
Modelling
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
5
What is ThingML
Who are the target users? What is the purpose? What are the alternatives?
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
6
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
7
Health domain and ambient assisted living Energy domain and smart grids Environmental monitoring and oil and gas Safety in hazardous environments Automotive industry ...
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
8
Distributed Asynchronous Parallel Event based Imperative programing is
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
9
Distance Sensor Left Wheel control Right Wheel control Movement control Robot control Collision Sensor
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
10
Wheel Controller
Port Reusable unit (Black box) <= forward(speed:int) <= backward(speed:int) <= stop() => wheel_position(position: int) Messages
Left Wheel control Right Wheel control
instances component type (Thing)
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
11
Control Port Wheel Controller
Stopped Forward backward
?stop ?stop ?forward ?backward !wheel_position !wheel_position
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
12
Forward
?forward
reset_wheel_position() motor_start() end ... action do motor_set_speed(speed) motor_set_direction(FW) end
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
13
What are the concepts of the language?
What are the relations between these
Tools: EMF, MOF, etc.
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
14
Pascal Language Grammar A specific Pascal Program A specific execution
program EBNF MOF The UML meta-Model A Specific phenomenon corresponding to a UML Model A Specific UML Model
Technology #2 (MOF + OCL)
Technology #1 (formal grammars attribute grammars, etc.)
A XML document A XML DTD Or Schema A XML document A XML DTD
Technology #3 (XML Meta-Language)
KIF Theories Upper Level Ontologies
Technology #4 (Ontology engineering)
[XMI=MOF+XML+OCL]
+Description Logics +Conceptual Graphs +etc. + Xlink, Xpath, XSLT + RDF, OIL, DAML + etc.
(From J. Bézivin)
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
15
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
16
S1 S3 S2 a /b x/y b/a y/x
Model
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
17
S1 S3 S2 a /b x/y b/a y/x
Model
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
Meta-Model
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
18
S1 S3 S2 a /b x/y b/a y/x
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
Model Meta-Model
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
19
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
20
Textual vs. Graphical Intermediate Wizards Important: use standard and/or existing
Tools: EMFText, GMF, etc.
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
21
Made a attempt at graphical Made an attempt with UML Endded-up with a text-based syntax Built with EMFText
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
22
thing Blink includes LedMsgs, TimerMsgs { required port HW { sends led_toggle, timer_start receives timer_timeout } statechart BlinkImpl init Blinking { state Blinking {
transition -> Blinking event HW?timer_timeout action HW!led_toggle () } } }
Blink Stopped
?timer_timeout !led_toggle()
!timer_start(1000)
HW <= timer_timeout() => led_toggle() => timer_start(delay:int)
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
23
Timer:Timer do: DigitalOutput pin: IOPin Arduino Board app: BlinkApplication led: Led led group Led HW HW timer io group
configuration BlinkArduino { group led : LedArduino set led.io.digital_output.pin = DigitalPin:PIN_13 // The timer instance timer : TimerArduino // The blink application instance app : Blink connector app.HW => led.led.Led connector app.HW => timer.timer }
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
24
Parser to the abstract syntax Serialization (pretty-printer) Editor Good editor features
EMFText generates an eclipse editor
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
25
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
26
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
27
Complementing a meta-model with Well-
Expressed with the OCL (Object Constraint
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
28
Inspired by the notion of Abstract Data
Specification = Signature +
Behavioral contracts are inherited in
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
29
Can be used at both
» aka Design-by-Contract (Meyer)
» aka Static semantics Tools: EMF Validation Framework
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
30
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
31
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
32
Depends on the purpose of the language What is the meaning of the model?
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
33
A model Its metamodel Adding Operational Semantics to OO Metamodels
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
w
S1 S3 S2 a /b x/y b/a y/x
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
34
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
source.owningFSM.currentState := target result := output
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
35
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
// Get the valid transitions var validTransitions : Collection<Transition> validTransitions := outgoingTransition.select { t | t.input.equals(c) } // Check if there is one and only one valid transition if validTransitions.empty then raise NoTransition.new end if validTransitions.size > 1 then raise NonDeterminism.new end // fire the transition result := validTransitions.one.fire
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
36
run() reset()
FSM
name: EString step()
State
input: EString
fire()
Transition
initialState 1
1
* currentState 0..1 source 1
* target 1 incomingTransition 0..1
from var str : String until str == "exit" loop stdio.writeln("current state is " + currentState.name) str := stdio.read("Enter an input string or 'exit' to exit simulation : ") stdio.writeln(str) if str != "exit" then do stdio.writeln("Output string : " + currentState.step(str)) rescue (ex : FSMException) stdio.writeln("ERROR : " + ex.toString) end end end stdio.writeln("* END OF SIMULATION *")
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
37
S1 S3 S2 a /b x/y b/a y/x
/** * Load a sample FSM from a xmi2 file */
var repository : EMFRepository init EMFRepository.new var resource : EMFResource resource ?= repository.createResource("../models/fsm_sample1.xmi", "../metamodels/fsm.ecore") resource.load // Load the fsm (we get the main instance) result ?= resource.instances.one end
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
38
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
39
Model-to-Text Transformations
Model-to-Model Transformations
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
40
For generating: code, xml, html, doc.
» Some visitor mechanisms to traverse the internal representation
» Iterators, Write ()
» A template consists of the target text containing slices of meta- code to access information from the source and to perform text selection and iterative expansion » The structure of a template resembles closely the text to be generated » Textual templates are independent of the target language and simplify the generation of any textual artefacts
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
41
1.
2.
3.
4.
5.
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
42
Another way of mapping to a known
Micro-controllers (Arduino, Atmel AVRs, TI MSP430) Linux C (POSIX) Java (and Android) Scala
Written in Scala. Not much generic tools but
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
43
Abstract syntax Concrete syntax Editor(s) Static semantics Dynamic semantics Interpreter Compiler Debugger …
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
44
Support multiple platforms
Other micro-controllers (Other Atmel AVRs, TI MSP430) Linux C (POSIX) Java (and Android) Scala
Support distribution
Generation of communication protocols Transparent asynchronous messaging Bluetooth, Blootooth Smart, Xbee, RF, …
Open-source language and tools (http://www.thingml.org) Still under development In use in several research projects
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
45
Lots of steps to create a DSL Syntax has to be good but it is not the main
The purpose of the language should be clear and
Modelling does not imply graphics and graphics do
Modelling tools help make language engineering
Lots of tools and standards can be used
INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)
46
Any questions ?
franck.fleurey@sintef.no