44
Domain-Specific Languages with Sven Efftinge copyright 2008 by itemis AG

Xtext @ Profict Summer Camp

Embed Size (px)

Citation preview

Page 1: Xtext @ Profict Summer Camp

Domain-Specific Languages with

Sven Efftinge

copyright 2008 by itemis AG

Page 2: Xtext @ Profict Summer Camp

Kiel, Germany

Page 3: Xtext @ Profict Summer Camp
Page 4: Xtext @ Profict Summer Camp

Peter

Dennis

Jan

Page 5: Xtext @ Profict Summer Camp
Page 6: Xtext @ Profict Summer Camp
Page 7: Xtext @ Profict Summer Camp
Page 8: Xtext @ Profict Summer Camp

“... won’t talk about

model-driven stuff”

Page 9: Xtext @ Profict Summer Camp

You’re lucky!

Page 10: Xtext @ Profict Summer Camp

No UML!

Page 11: Xtext @ Profict Summer Camp

No MDA!

No meta meta meta meta ...

Page 12: Xtext @ Profict Summer Camp
Page 13: Xtext @ Profict Summer Camp

taken from Martin Fowler’s upcoming Book on DSLs

Hands-on a

concrete example

Page 14: Xtext @ Profict Summer Camp

You’re working for a company specialized on systems for

secret compartments

Page 15: Xtext @ Profict Summer Camp

Your customer:

Mrs. H she likes secrets

Page 16: Xtext @ Profict Summer Camp

To open the secret compartment, she has to

Page 17: Xtext @ Profict Summer Camp

To open the secret compartment, she has to

close the door,

Page 18: Xtext @ Profict Summer Camp

To open the secret compartment, she has to

close the door,

open the second draw in her chest,

Page 19: Xtext @ Profict Summer Camp

turn her bedside light on.

To open the secret compartment, she has to

close the door,

open the second draw in her chest,

Page 20: Xtext @ Profict Summer Camp

events doorClosed drawOpened lightOn doorOpened panelClosedend

resetEvents doorOpenedend

commands unlockPanel lockPanel lockDoor unlockDoor end

state idle actions {unlockDoor lockPanel} doorClosed => activeend

state active drawOpened => waitingForLight lightOn => waitingForDrawend

state waitingForLight lightOn => unlockedPanelend

state waitingForDraw drawOpened => unlockedPanelend

state unlockedPanel actions {unlockPanel lockDoor} panelClosed => idleend

Page 21: Xtext @ Profict Summer Camp

events doorClosed drawOpened lightOn doorOpened panelClosedend

resetEvents doorOpenedend

commands unlockPanel lockPanel lockDoor unlockDoor end

state idle actions {unlockDoor lockPanel} doorClosed => activeend

state active drawOpened => waitingForLight lightOn => waitingForDrawend

state waitingForLight lightOn => unlockedPanelend

state waitingForDraw drawOpened => unlockedPanelend

state unlockedPanel actions {unlockPanel lockDoor} panelClosed => idleend

event :doorClosedevent :drawOpenedevent :lightOnevent :doorOpenedevent :panelClosed

command :unlockPanelcommand :lockPanelcommand :lockDoorcommand :unlockDoor

resetEvents :doorOpened

state :idle do actions :unlockDoor, :lockPanel transitions :doorClosed => :activeend

state :active do transitions :drawOpened => :waitingForLight, :lightOn => :waitingForDrawend

state :waitingForLight do transitions :lightOn => :unlockedPanelend

state :waitingForDraw do transitions :drawOpened => :unlockedPanelend

state :unlockedPanel do actions :unlockPanel, :lockDoor transitions :panelClosed => :idleend

External DSL Internal DSL

Page 22: Xtext @ Profict Summer Camp

events doorClosed drawOpened lightOn doorOpened panelClosedend

resetEvents doorOpenedend

commands unlockPanel lockPanel lockDoor unlockDoor end

state idle actions {unlockDoor lockPanel} doorClosed => activeend

state active drawOpened => waitingForLight lightOn => waitingForDrawend

state waitingForLight lightOn => unlockedPanelend

state waitingForDraw drawOpened => unlockedPanelend

state unlockedPanel actions {unlockPanel lockDoor} panelClosed => idleend

event :doorClosedevent :drawOpenedevent :lightOnevent :doorOpenedevent :panelClosed

command :unlockPanelcommand :lockPanelcommand :lockDoorcommand :unlockDoor

resetEvents :doorOpened

state :idle do actions :unlockDoor, :lockPanel transitions :doorClosed => :activeend

state :active do transitions :drawOpened => :waitingForLight, :lightOn => :waitingForDrawend

state :waitingForLight do transitions :lightOn => :unlockedPanelend

state :waitingForDraw do transitions :drawOpened => :unlockedPanelend

state :unlockedPanel do actions :unlockPanel, :lockDoor transitions :panelClosed => :idleend

External DSL Internal DSL

Page 23: Xtext @ Profict Summer Camp

On top of external DSLs

no compromises

Page 24: Xtext @ Profict Summer Camp

On top of external DSLs

domain-specific static analysis

no compromises

Page 25: Xtext @ Profict Summer Camp

On top of external DSLs

domain-specific static analysisgraphical views

no compromises

Page 26: Xtext @ Profict Summer Camp

On top of external DSLs

domain-specific static analysisgraphical views

no compromises

closed scope

Page 27: Xtext @ Profict Summer Camp

Implementing anexternal DSLis complicated

Page 28: Xtext @ Profict Summer Camp
Page 29: Xtext @ Profict Summer Camp

... to develop DSLs?

Why not using a DSL ...

Page 30: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

Page 31: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

starts with keyword ‘events’

Page 32: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

followed by at least one definition of Event

which is defined here and consists of just one

identifier (ID)

Page 33: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

this is a cross reference, referencing Events

declared in the previous section.

Page 34: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

commands are very similar to events

Page 35: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

States have a name (ID)

Page 36: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

States have a name (ID)

optional action block

Page 37: Xtext @ Profict Summer Camp

Statemachine : 'events' (events+=Event)+ 'end' 'resetEvents' (resetEvents+=[Event])+ 'end' 'commands' (commands+=Command)+ 'end' (states+=State)+;Event : name=ID;Command : name=ID;State : 'state' name=ID ('actions' '{' (actions+=[Command])+ '}')? (transitions+=Transition)* 'end';Transition : event=[Event] '=>' state=[State];

States have a name (ID)

optional action block

any number of Transitions

Page 38: Xtext @ Profict Summer Camp

This was the example DSL implemented in

So what do we get from such a description?

Page 39: Xtext @ Profict Summer Camp
Page 40: Xtext @ Profict Summer Camp

Antlr based Parser

Page 41: Xtext @ Profict Summer Camp

Eclipse based Editor

Antlr based Parser

Page 42: Xtext @ Profict Summer Camp

Statemachine

name:StringCommand

name:StringState

name:StringEvent

Transition

** *

*

*

stateseventscommands

actions

stateevent

resetStates

EMF based Semantic Model

Eclipse based Editor

Antlr based Parser

Page 43: Xtext @ Profict Summer Camp

Demo

Page 44: Xtext @ Profict Summer Camp

oaw.itemis.comThank you very much!

the eclipse distro can be downloaded from

http://oaw.itemis.com