TMF Xtext – Textual Modeling Framework

60 downloads 52 Views 2MB Size Report
TMF Xtext – Textual Modeling Framework. ▫ Provides a DSL, runtime and tools for building textual editors. ▫ EMF is used as the underlying target language.
TDT4250 - Model-driven Development of Information Systems, Autumn 2009

TMF Xtext – Textual Modeling Framework 

Provides a DSL, runtime and tools for building textual editors



EMF is used as the underlying target language 



Ecore model is implicitly defined by the grammar or existing EMF models may be used

Grammar covers: 

lexical syntax (terminals)



rules for syntactic structures



relation between syntax and Ecore classes

1

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Xtext – grammar file 

Define the grammar name and specify existing grammar to inherit from 





Either generate or import Ecore model 

generate



import as

Define terminal 



grammar with

terminal ‘:’ ‘;’

Define syntax rule 

‘:’ ‘;’

2

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Xtext – syntax rules 

Each rule implicitly defines an Eclass 





‘:’ ‘;’ defines an EClass named

Elements in the syntax defintion define EAttributes and EReferences for the rule’s Eclass 

= defines an EAttribute



= defines a containment EReference



=‘[‘’]’ defines an EReference



+= is used instead of = to indicate multiple values



?= indicates a boolean EAttribute

Syntax is based on BNF 

literal keywords



grouping with ()



optionality with ? and multiplicity with * and +



alternatives with | 3

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Xtext – building an EMF-based parse tree 



Parsing process 

the parsing process relates the input to the grammar rules



when the input matches a rule, actions that build an abstract syntax tree are performed



Xtext’s AST is a tree of instances of the Ecore model

Editing and parsing 

during editing, the input is continuously (re)parsed



parse errors are (silently) indicated by error markers



valid input results in a complete tree of Ecore objects

4

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Language concepts 



State 

name



sub-states



transitions



variables



definitions for script-based events, conditions and actions

Transition 

external, between states 



internal, used to trigger actions within a state 



event, condition, action

event, condition

initial, default transition when entering parent state 

condition, action 5

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Language syntax 

State 



either just a name or content within braces { }

Transition 

internal, actions triggered by transitions or states 



initial, default transition when entering parent state 



name!, event script reference, source -> target, source-> or target target when event [ condition ] / action

Event 



-> target when [ condition ] / action

external, between states 



action when event [ condition ]

name! or action script reference

Condition 

condition script reference 6

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Create an Xtext project

7

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

The xtext grammar 

Generated projects and files 







grammar is initialised to an example grammar

grammar project 

grammar compiler



grammar

generator project

The grammar project 

defines a grammar



contains files generated from the grammar

The generator project 

used for impl. transformations on instance files (more later) 8

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

First tentative grammar 



grammar statement 

the grammar name



inherit from existing grammar, here the default lexical syntax

generate statement 





the name and URI of the generated Ecore model

StateMachine 

a sequence of States



contained in an Ereference named states

State 

an identifier followed by a semicolon



set as the value of an EAttribute name name

9

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Run the MWE workflow

10

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Examine generated Ecore model 

An Ecore model is generated (and lots of code)



For each grammar element there is a corresponding Ecore model element 

rule corresponds to EClass



rule reference corresponds to EReference



terminal rule reference corresponds to EAttribute

11

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Try the grammar... 

Start new Eclipse



Create text file and open with generated editor

12

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Try the grammar... 

Enter example text



Open as Ecore data

13

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Examine text’s correspondance with Ecore data 

The text file as a whole corresponds to the root class, here StateMachine



Each ID ‘;’ matches the State rule and results in a State instance



The states’s name is set to the ID terminal

14

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Development instance of Eclipse

The big picture

Xtext grammar / model

Generated Ecore model

Xtext instance

Ecore instance

Test instance of Eclipse

15

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Extend to grammar 



Transitions 

from a source state to a target state



uses the [State] notation to refer to states by name (ID)

A StateMachine can contain 

one or more states



zero or more transitions

16

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Try the grammar once more 

Generate (and examine) the Ecore model



Run the Eclipse instance and edit the example text

17

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Extend to grammar once more 

Allow alternating states and transitions





instead a sequence of states followed by transitions, we now allow a sequence of a state or a transition

Support hierarchical states



the same content as a StateMachine within braces { } 18

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Try the grammar once more 

Generate (and examine) the Ecore model



Run the Eclipse instance and edit the example text

19

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Extend to grammar once more 

Support another kind of transition: initial transition



I.e. a Transition is an ExternalTransition or an InitialTransition



Interpreted as (disjoint) specialisation, i.e. inheritance from a base class



Common EAttributes and EReferences are pushed up into the base class 20

Q

TDT4250 - Model-driven Development of Information Systems, Autumn 2009

Try the grammar once more 

Generate (and examine) the Ecore model



Run the Eclipse instance and edit the example text

21

Q