Engineering Example of the ThingML language Franck Fleurey SINTEF - - PowerPoint PPT Presentation

engineering
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

2

Language Engineering

 Programming languages  Domain specific languages  Modelling languages  Who is the user?  What is the purpose?

slide-3
SLIDE 3

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

3

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-4
SLIDE 4

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

4

Model-driven language engineering

 Traditionally: Grammar / Compilers

approaches

 Modelling

– Focus on the abstract syntax – More flexible concrete syntaxes – Better modularity – More maintainable

slide-5
SLIDE 5

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

5

Example of the ThingML language

 What is ThingML

– Name comes from Internet of Things (IoT)

 Who are the target users?  What is the purpose?  What are the alternatives?

slide-6
SLIDE 6

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

6

What is the Internet of Things (IoT)?

slide-7
SLIDE 7

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

7

Example applications

 Health domain and ambient assisted living  Energy domain and smart grids  Environmental monitoring and oil and gas  Safety in hazardous environments  Automotive industry  ...

slide-8
SLIDE 8

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

8

Programming IoT systems

 Distributed  Asynchronous  Parallel  Event based  Imperative programing is

not well suited

slide-9
SLIDE 9

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

9

ThingML: Architecture Model

Distance Sensor Left Wheel control Right Wheel control Movement control Robot control Collision Sensor

slide-10
SLIDE 10

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

10

ThingML: Component

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)

slide-11
SLIDE 11

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

11

ThingML: State Machines

Control Port Wheel Controller

Stopped Forward backward

?stop ?stop ?forward ?backward !wheel_position !wheel_position

slide-12
SLIDE 12

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

12

ThingML: Action Language

Forward

?forward

  • n entry do

reset_wheel_position() motor_start() end ... action do motor_set_speed(speed) motor_set_direction(FW) end

slide-13
SLIDE 13

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

13

ThingML – Abstract syntax

 What are the concepts of the language?

– Components – Instances – States – Transitions – …

 What are the relations between these

concepts

 Tools: EMF, MOF, etc.

slide-14
SLIDE 14

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

14

Comparing Abstract Syntax Systems

Pascal Language Grammar A specific Pascal Program A specific execution

  • f a Pascal

program EBNF MOF The UML meta-Model A Specific phenomenon corresponding to a UML Model A Specific UML Model

Technology #2 (MOF + OCL)

M3 M2 M1

Technology #1 (formal grammars attribute grammars, etc.)

A XML document A XML DTD Or Schema A XML document A XML DTD

  • r Schema

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)

slide-15
SLIDE 15

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

15

ThingML Abstract Syntax

slide-16
SLIDE 16

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

16

Abstract syntax – State Machines

S1 S3 S2 a /b x/y b/a y/x

Model

slide-17
SLIDE 17

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

17

Abstract syntax – State Machines

S1 S3 S2 a /b x/y b/a y/x

Model

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

Meta-Model

slide-18
SLIDE 18

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

18

What do we get?

S1 S3 S2 a /b x/y b/a y/x

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

Model Meta-Model

slide-19
SLIDE 19

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

19

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-20
SLIDE 20

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

20

Concrete syntax

 Textual vs. Graphical  Intermediate  Wizards  Important: use standard and/or existing

notations for the target users

 Tools: EMFText, GMF, etc.

slide-21
SLIDE 21

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

21

ThingML Concrete syntax

 Made a attempt at graphical  Made an attempt with UML  Endded-up with a text-based syntax  Built with EMFText

slide-22
SLIDE 22

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

22

Blink example state machine

thing Blink includes LedMsgs, TimerMsgs { required port HW { sends led_toggle, timer_start receives timer_timeout } statechart BlinkImpl init Blinking { state Blinking {

  • n entry HW!timer_start (1000)

transition -> Blinking event HW?timer_timeout action HW!led_toggle () } } }

Blink Stopped

?timer_timeout !led_toggle()

  • n entry

!timer_start(1000)

HW <= timer_timeout() => led_toggle() => timer_start(delay:int)

slide-23
SLIDE 23

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

23

Blink example and instance groups

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 }

slide-24
SLIDE 24

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

24

Tooling the concrete syntax

 Parser to the abstract syntax  Serialization (pretty-printer)  Editor  Good editor features

– Highlighting – Outline – Completion – Navigation

 EMFText generates an eclipse editor

slide-25
SLIDE 25

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

25

ThingML Editor

slide-26
SLIDE 26

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

26

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-27
SLIDE 27

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

27

Static Semantics with OCL

 Complementing a meta-model with Well-

Formedness Rules, aka Contracts e.g.;

– A procedure is called with the same number of arguments as specified in its declaration

 Expressed with the OCL (Object Constraint

Language)

– The OCL is a language of typed expressions. – A constraint is a valid OCL expression of type Boolean. – A constraint is a restriction on one or more values of (part

  • f) an object-oriented model or system.
slide-28
SLIDE 28

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

28

Contracts in OO languages

 Inspired by the notion of Abstract Data

Type

 Specification = Signature +

– Preconditions – Postconditions – Class Invariants

 Behavioral contracts are inherited in

subclasses

slide-29
SLIDE 29

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

29

OCL for static semantics

 Can be used at both

– M1 level (constraints on Models)

» aka Design-by-Contract (Meyer)

– M2 level (constraints on Meta-Models)

» aka Static semantics  Tools: EMF Validation Framework

– Static constraints can be written in OCL or Java – Well integrated with EMFText

slide-30
SLIDE 30

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

30

Static semantics example

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

Context FSM inv: ownedState->forAll(s1,s2| s1.name=s2.name implies s1=s2)

slide-31
SLIDE 31

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

31

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-32
SLIDE 32

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

32

Dynamic semantics

 Depends on the purpose of the language  What is the meaning of the model?

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

slide-33
SLIDE 33

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

33

Operational Semantics of State Machines

 A model  Its metamodel  Adding Operational Semantics to OO Metamodels

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

w

S1 S3 S2 a /b x/y b/a y/x

slide-34
SLIDE 34

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

34

Example

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* target 1 incomingTransition 0..1

  • peration fire() : String

source.owningFSM.currentState := target result := output

slide-35
SLIDE 35

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

35

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* 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

  • peration step(c : String) : String
slide-36
SLIDE 36

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

36

run() reset()

FSM

name: EString step()

State

input: EString

  • utput: EString

fire()

Transition

initialState 1

  • w ningFSM

1

  • w nedState

* currentState 0..1 source 1

  • utgoingTransition

* 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 *")

  • peration run() : Void
slide-37
SLIDE 37

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 */

  • peration loadFSM() : FSM is do

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

slide-38
SLIDE 38

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

38

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-39
SLIDE 39

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

39

Model-to-Text vs. Model-to-Model

 Model-to-Text Transformations

– For generating: code, xml, html, doc. – Should be limited to syntactic level transcoding

 Model-to-Model Transformations

– To handle more complex, semantic driven transformations

slide-40
SLIDE 40

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

40

Model-to-Text Approaches

 For generating: code, xml, html, doc.

– Visitor-Based Approaches:

» Some visitor mechanisms to traverse the internal representation

  • f a model and write code to a text stream

» Iterators, Write ()

– Template-Based Approaches

» 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

slide-41
SLIDE 41

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

41

Classification of Model-to-Model Transformation Techniques

1.

General purpose programming languages

– Java/C#...

2.

Generic transformation tools

– Graph transformations, XSLT…

3.

CASE tools scripting languages

– Objecteering, Rose…

4.

Dedicated model transformation tools

– OMG QVT style

5.

Meta-modeling tools

– Metacase, Xactium, Kermeta…

slide-42
SLIDE 42

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

42

Compiling ThingML

 Another way of mapping to a known

language ThingML Compilers

Micro-controllers (Arduino, Atmel AVRs, TI MSP430) Linux C (POSIX) Java (and Android) Scala

 Written in Scala. Not much generic tools but

some patterns

slide-43
SLIDE 43

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

43

Outline

 Abstract syntax  Concrete syntax  Editor(s)  Static semantics  Dynamic semantics  Interpreter  Compiler  Debugger  …

slide-44
SLIDE 44

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

44

ThingML Language

 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

slide-45
SLIDE 45

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

45

Summary

 Lots of steps to create a DSL  Syntax has to be good but it is not the main

development effort

 The purpose of the language should be clear and

narrow

 Modelling does not imply graphics and graphics do

not imply modelling

 Modelling tools help make language engineering

more accessible

 Lots of tools and standards can be used

slide-46
SLIDE 46

INF5120 – 2013– Franck Fleurey (franck.fleurey@sintef.no)

46

 Any questions ?

http://www.thingml.org

Thank you for your attention!

franck.fleurey@sintef.no