Outline Model-Driven Language Engineering Jean-Marc Jézéquel Franck Fleurey and Brice Morin e-mail : jezequel@irisa.fr http://www.irisa.fr/prive/jezequel e-mail : Franck.Fleurey@sintef.no Brice.Morin@sintef.no with the support of – As e.g. in Smalltalk It's difficult -- in fact, next to impossible – for a large enterprise to standardize on a single middleware platform. (R. Soley) Soley) HTTP HTML Components Sun’s Java & EJB ReceiverI DataI Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up 2 A model represents reality for the given purpose; the model is an abstraction of reality in the sense that it cannot represent all aspects of reality. This allows us to deal with the world in a simplified manner, avoiding the complexity, danger and irreversibility of reality. DataI Jeff Rothenberg. Rothenberg. Required port Provided Port + until the next ultimate middleware platform (~2005) 3 © J.-M. Jézéquel et al., 2010 © J.-M. Jézéquel et al., 2010 A Model is a simplified representation of an aspect of the World for a specific purpose Person Specificity Specificityof ofEngineering: Engineering: Model Modelsomething something not notyet yet existing existing (in (inorder orderto tobuild build it) it) -t eaches Emplo yee -jobType : CString -room Nr : unsigned long -depart ment : CString -divi sion : CString -jobTitl e : CSt ri ng -manager : unsigned l ong -headsDept : CString -headsDivi sion : CString -mobileNr : CSt ri ng -birt hDate : unsigned long +Person() +PersonI nfo() -is taught by 0.. * Sun Tse: Tse: Do not take the map for the reality Magritte Software Models: from contemplative to productive -courseSessionI D : unsi gned long -courseDat e : unsigned long -courseID : unsigned long -courseLocati on : CSt ring RegForm +CourseSession() +CourseSessionInf o() 1 ApplicantList PersonList Applicant Application ApplicationList -i s in 1 findApplicant() +Em ployee() +GetCurrent Age() +Em ployeeI nfo() App li can t 0.. * -i s fill ed by findPerson( ) Cou rseRegi str ati on -is m ade by -makes 1 -registrati onDat e : unsigned long -compl et ionFlag : bool -confi rm edDat e : unsi gned long 0. .* +Appli cant() +Appli cantI nfo() +MakeApplicati on() Applicant( ) +CourseRegi st rat ion() +CourseRegi st rat ionInf o() Regi strati onFo rm MakeApplication( ) 1 +registrat ionform() -is m ade * by a-uses 1 -is t aken by * 0. .* Application() -takes Test 1.. * -made a +Applicati on() +Applicati onI nfo() 0.. * -al lows change in 0.. * -has a 1..* +Test() +TestInf o() Permi ttedStatusChange -f rom St atus : char -t oSt atus : char 0. .* -gives addPerson( ) +Permit tedStatusChange() +St atusChangeInf o() -appli es to a 1 1 addApplication( ) -t est Score : unsi gned long App licatio n -product Nr : unsigned l ong -certif icat ionLevel : unsigned l ong -applicati onDat e : unsigned long Is represented by Cou rseSession -personI D : unsi gned long -surnam e : CSt ring -givenName : CSt ring -m iddleInit ial : char -streetAddress : CSt ring -postCode : CSt ring -countrynam e : CSt ri ng -eM ailAddress : CString -companyName : CSt ring -experience : CString -ref erence1 : CSt ri ng -ref erence2 : CSt ri ng -ref erence3 : CSt ri ng 4 Model and Reality in Software Modeling in Science & Engineering (modeling space) Static Semantics with OCL Proprietary Middleware (eg. automotive) XML SOAP M1 Modeling, in the broadest sense, is the costcost-effective use of something in place of something else for some cognitive purpose. purpose. It allows us to use something that is simpler, simpler, safer or cheaper than reality instead of reality for some purpose. DecoderI Microsoft C# & .Net <<Component>> Decoder CORBA IIOP Aspects Designing MetaMeta-models: models: the LOGO example Collaborations COM+ DCOM Why modeling: master complexity To a multitude of concepts Middleware (middle war) Design patterns Introduction to Model Driven Engineering © J.-M. Jézéquel et al., 2010 Once upon a time… time… software development looked simple From the object as the only one concept -is f or a App Statu s -st at usCode : char -st at usNam e : CSt ring +AppStatus() +AppStatusInf o() Exam -examID : unsigned long -cert if icat ionLevel : unsigned long +Exam() +ExamInf o() -is achi eved ExamSessi on -uses 1 -is used in 0. .* -examSession : unsigned long -examl ocat ion : CSt ri ng -examDat e : unsigned long +ExamSession() +ExamSessionInf o() M0 (the world) © J.-M. Jézéquel et al., 2010 5 © J.-M. Jézéquel et al., 2010 6 Modeling and Weaving QoS Model Security Model Use Case Model Object Model Assigning Meaning to Models UI Model Challenges: Challenges: Business -Product -ProductFamilies Families Model -Reuse -Reuseof of Weaving WeavingProcess Process -Automatic -AutomaticWeaving Weaving Platform Model Test Model tester Design Model If a model is no longer just – fancy pictures to decorate your room – a graphical syntax for C++/Java/C#/Eiffel... Then tools must be able to manipulate models – Let’ Let’s make a model of what a model is! – => metameta-modeling » & metameta-metameta-modeling.. » Use MetaMeta-Object Facility (MOF) to avoid infinite MetaMeta-recursion Code Model Contains 0..1 ModelElement +c onstrainedE lement 1.. * 0..* Constrains +container 0..* {ordered} +containedElement Constraint Feature Namespace +subtype Generalizes 0..* +supertype GeneralizableElement BehavioralFeature St ruc turalFeature {ordered} 0..* Package Classifier Operation Reference 0.. * MofAttribute +referent RefersTo Dat aType Class Association 1 +referencedEnd AssociationEnd 7 © J.-M. Jézéquel et al., 2010 8 © J.-M. Jézéquel et al., 2010 Generalizations UML2 metameta-model (part., © OMG) 9 © J.-M. Jézéquel et al., 2010 © OMG * 10 © OMG © J.-M. Jézéquel et al., 2010 Comparing Abstract Syntax Systems The 4 layers in practice © J.-M. Jézéquel et al., 2010 NB: Tell you nothing about: •generalization being acyclic, •or semantics of dynamic binding Technology #1 Technology #2 (formal grammars (MOF + OCL) attribute grammars, etc.) 11 Technology #3 (XML Meta-Language) M3 EBNF MOF A XML DTD Or Schema M2 Pascal Language Grammar The UML meta-Model A XML document M1 A specific Pascal Program A Specific UML Model A specific execution of a Pascal program A Specific phenomenon corresponding to a UML Model © J.-M. Jézéquel et al., 2010 Technology #4 (Ontology engineering) Upper Level Ontologies A XML DTD or Schema A XML document + Xlink, Xpath, XSLT + RDF, OIL, DAML + etc. KIF Theories +Description Logics +Conceptual Graphs +etc. [XMI=MOF+XML+OCL] (From J. Bézivin) 12 MDA: the OMG vision "OMG is in the ideal position to provide the modelmodelbased standards that are necessary to extend integration beyond the middleware approach… approach… Now is the time to put this plan into effect. Now is the time for the Model Driven Architecture Architecture." Richard Soley & OMG staff, MDA Whitepaper Draft 3.2 November 27, 2000 Mappings to multiple and evolving platforms Platform neutral models based on UML & MOF Java EJB COM+ DCOM C# .Net CORBA 13 © J.-M. Jézéquel et al., 2010 Modeling to master complexity Models: from contemplative to productive Model Driven Engineering – MultiMulti-dimensional and aspect oriented by definition – PIM: PIM: Platform Independent Model » Business Model of a system abstracting away the deployment details of a system » Example: the UML model of the GPS system – MetaMeta-modeling tools, metameta-models used to define languages – PSM: PSM: Platform Specific Model – Weaving aspects into a design model » Operational model including platform specific aspects » Example: the UML model of the GPS system on .NET » E.g. Platform Specificities Possibly expressed with a UML profile (.NET profile for UML) – Not so clear about platform models » Reusable model at various levels of abstraction 14 Model Driven Engineering : Summary MDA models XML SOAP © J.-M. Jézéquel et al., 2010 The core idea of MDA: PIMs & PSMs HTTP HTML MOF & UML as the core Organization assets expressed as models Model transformations to map to technology specific platforms Model Driven Architecture (PIM / PSM): just a special case of Aspect Oriented Design Related: Generative Prog, Prog, Software Factories CCM, C#, EJB, EDOC, … 15 © J.-M. Jézéquel et al., 2010 Outline Introduction to Model Driven Engineering Designing MetaMeta-models: models: the LOGO example Static Semantics with OCL Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up © J.-M. Jézéquel et al., 2010 16 MetaMeta-Models as Shared Knowledge Definition of an Abstract Syntax in EE-MOF – – – – Repository of models with EMF Reflexive Editor in Eclipse JMI for accessing models from Java XML serialization for model exchanges Applied in more and more projects – SPEEDS, OpenEmbedd,DiVA… OpenEmbedd,DiVA… © J.-M. Jézéquel et al., 2010 17 © J.-M. Jézéquel et al., 2010 18 Example with StateMachines Breathing life into MetaMeta-Models owningFSM 1 FSM Model * State 0..1 x/y a /b run() initialState reset() 1 S1 S2 S3 b /a Meta-Model 1 FSM * State 0..1 run() initialState reset() 1 Transition ownedState currentState name: EString step() source 1 target outgoingTransition input: EString * incomingTransition 1 * output: EString incomingTransition 1 fire() 0..1 currentState := initialState 19 class Minimizer { operation minimize (source: FSM):FSM {…} © J.-M. Jézéquel et}al., 2010 20 Fractals in LOGO ; righthilbert ; lefthilbert to lefthilbert :level :size if :level != 0 [ left 90 righthilbert :level-1 :size forward :size right 90 lefthilbert :level-1 :size forward :size lefthilbert :level-1 :size right 90 forward :size righthilbert :level-1 :size left 90 ] end Consider LOGO programs of the form: repeat 3 [ pendown forward 3 penup forward 4 ] to square :width repeat 4 [ forward :width right 90] end pendown square 10 *10 *10 21 Featuring to righthilbert :level :size if :level != 0 [ right 90 lefthilbert :level-1 :size forward :size left 90 righthilbert level-1 :size forward :size righthilbert :level-1 :size left 90 forward :size lefthilbert :level-1 :size right 90 ] end 22 © J.-M. Jézéquel et al., 2010 Model Driven Language Engineering : the Process Case Study: Building a Programming Environment for Logo Specify abstract syntax Specify concrete syntax Build specific editors Specify static semantics Specify dynamic semantics Build simulator Compile to a specific platform – Edition in Eclipse – On screen simulation – Compilation for a Lego Mindstorms robot © J.-M. Jézéquel et al., 2010 1 target input: EString }} fire() 0..1 DIY with LOGO programs © J.-M. Jézéquel et al., 2010 step() outgoingTransition output: EString © J.-M. Jézéquel et al., 2010 name: EString source // MyKermetaProgram.kmt // An EE-MOF metamodel is an OO program that does nothing require "StateMachine.ecore "StateMachine.ecore"" // to import it in Kermeta // Kermeta lets you weave in aspects Context FSM // Contracts (OCL WFR) inv: ownedState->forAll(s1,s2| require “StaticSemantics.ocl” StaticSemantics.ocl” s1.name=s2.name implies s1=s2) // Method bodies (Dynamic semantics) require “DynamicSemantics.kmt” DynamicSemantics.kmt” aspect class FSM { // Transformations operation reset() : Void { y/x owningFSM Transition ownedState currentState 23 © J.-M. Jézéquel et al., 2010 24 MetaMeta-Modeling LOGO programs LOGO metamodel Let’ Let’s build a metameta-model for LOGO – Concentrate on the abstract syntax – Look for concepts: instructions, expressions… expressions… – Find relationships between these concepts » It’ It’s like UML modeling ! Defined as an ECore model – Using EMF tools and editors ASMLogo.ecore 25 © J.-M. Jézéquel et al., 2010 26 © J.-M. Jézéquel et al., 2010 LOGO metamodel Concrete syntax Any regular EMF based tools logo.sts using Sintaks Graphical using GMF or TopCased Textual ASMLogo.ecore 27 © J.-M. Jézéquel et al., 2010 Do It Yourself Outline Within Eclipse Introduction to Model Driven Engineering Designing MetaMeta-models: models: the LOGO example Static Semantics with OCL Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up – Load/ Load/Edit/Save Edit/Save Models » Conforming to the LOGO metameta-model » ie LOGO programs Install & Run the MDLE4LOGO Bundle – On your own PC – Or follow the beamed demo © J.-M. Jézéquel et al., 2010 28 © J.-M. Jézéquel et al., 2010 29 © J.-M. Jézéquel et al., 2010 30 Static Semantics with OCL Contracts in OO languages Complementing a metameta-model with WellWellFormedness Rules, aka Contracts e.g.; Inspired by the notion of Abstract Data Type Specification = Signature + – A procedure is called with the same number of arguments as specified in its declaration Expressed with the OCL (Object Constraint Language) – Preconditions – Postconditions – Class Invariants – 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 of) an object-oriented model or system. 31 © J.-M. Jézéquel et al., 2010 Behavioral contracts are inherited in subclasses OCL Simple constraints Can be used at both – M1 level (constraints on Models) Customer » aka DesignDesign-byby-Contract (Meyer) name: String title: String age: Integer isMale: Boolean – M2 level (constraints on MetaMeta-Models) » aka Static semantics 32 © J.-M. Jézéquel et al., 2010 Let’ Let’s overview it with M1 level exemples title = if isMale then ‘Mr.’ else ‘Ms.’ endif age >= 18 and age < 66 name.size < 100 33 © J.-M. Jézéquel et al., 2010 NonNon-local contracts: navigating associations 34 © J.-M. Jézéquel et al., 2010 Navigation of 0..* associations Through navigation, we no longer get a scalar but a collection of objects OCL defines 3 subsub-types of collection Each association is a navigation path – The context of an OCL expression is the starting point – Role names are used to select which association is to be traversed (or target class name if only one) – Set : when navigation of a 0..* association » Context Person inv: ownings return a Set[Car] Set[Car] » Each element is in the Set at most once – Bag : if more than one navigation step Person 1 owner ownership ownings * » An element can be present more than once in the Bag Car – Sequence : navigation of an association {ordered} » It is an ordered Bag Context ContextCar Carinv: inv: self.owner.age self.owner.age>= >=18 18 © J.-M. Jézéquel et al., 2010 35 Many predefined operations on type collection © J.-M. Jézéquel et al., 2010 Syntax:: Syntax:: Collection->operation Collection->operation 36 Collection hierarchy Basic operations on collections Collection isEmpty – true if collection has no element Set Bag minus symmetricDifference asSequence asBag asSequence asSet notEmpty size – true if collection has at least one element Sequence first last at(int) append prepend asBag asSet – Number of elements in the collection count (elem (elem)) – Number of occurrences of element elem in the collection 37 © J.-M. Jézéquel et al., 2010 forAll Operation possible syntax – – – collectioncollection->select(elem:T | expr) expr) collectioncollection->select(elem | expr) expr) collectioncollection->select(expr) select(expr) Selects the subset of collection for which property expr holds e.g. context contextPerson Personinv: inv: ownings->select(mileage<100000)->notEmpty ownings->select(mileage<100000)->notEmpty True iff expr holds for each element of the collection e.g. context contextPerson Personinv: inv: ownings->forall(v: ownings->forall(v:Car Car| |v.mileage<100000) v.mileage<100000) 39 © J.-M. Jézéquel et al., 2010 isEmpty notEmpty iterate(expression) sum(collection) exists(expression) forAll(expression) © J.-M. Jézéquel et al., 2010 shortcut: context contextPerson Personinv: inv: ownings->forall(mileage<100000) ownings->forall(mileage<100000) © J.-M. Jézéquel et al., 2010 Operations on Collections Operation size count(object) includes(object) includesAll(collection) collectioncollection->forall(elem:T | expr) expr) collectioncollection->forall(elem | expr) expr) collectioncollection->forall(expr) forall(expr) context contextPerson Personinv: inv: ownings->select(v: ownings->select(v:Car Car| |v.mileage<100000)->notEmpty v.mileage<100000)->notEmpty shortcut: possible syntax – – – 38 © J.-M. Jézéquel et al., 2010 select Operation Context ContextPerson Personinv: inv: age<18 age<18implies impliesownings->isEmpty ownings->isEmpty 40 Static Semantics for LOGO Description The number of elements in the collection The number of occurences of object in the collection. True if the object is an element of the collection. True if all elements of the parameter collection are present in the current collection. True if the collection contains no elements. True if the collection contains one or more elements. Expression is evaluated for every element in the collection. The addition of all elements in the collection. True if expression is true for at least one element in the collection. True if expression is true for all elements. 41 No two formal parameters of a procedure may have the same name: A procedure is called with the same number of arguments as specified in its declaration: © J.-M. Jézéquel et al., 2010 42 Static Semantics for LOGO Outline No two formal parameters of a procedure may have the same name: context ProcDeclaration inv unique_names_for_formal_arguments : args -> forAll ( a1 , a2 | a1. name = a2.name implies a1 = a2 ) A procedure is called with the same number of arguments as specified in its declaration: context ProcCall inv same_number_of_formals_and_actuals : actualArgs -> size = declaration .args -> size 43 © J.-M. Jézéquel et al., 2010 Introduction to Model Driven Engineering Designing MetaMeta-models: models: the LOGO example Static Semantics with OCL Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up Kermeta Rationale Operational Semantics of State Machines x/y a /b A model S1 S2 S3 b /a Its metamodel owningFSM FSM 1 * w source State 0..1 run() reset() step() 1 1 target name: EString initialState Transition ownedState currentState outgoingTransition * 1 output: EString fire() 0..1 Adding Operational Semantics to OO Metamodels 45 © J.-M. Jézéquel et al., 2010 Breathing life into MetaMeta-Models owningFSM FSM 1 * State 0..1 run() initialState reset() 1 name: EString step() Kermeta leverages this familiarity to make MetaMeta-modeling easy for the masses © J.-M. Jézéquel et al., 2010 46 Kermeta workbench snapshot Transition ownedState currentState On the other hand, engineers are familiars with – OO programming languages (Java,C#,C ++,..) (Java,C#,C++,..) – UML (at least class diagram) – May have heard of DesignDesign-byby-Contract input: EString incomingTransition Model, metameta-model, metameta-metamodel, metamodel, DSLs… DSLs… – MetaMeta-blabla-bla too complex for the normal engineer y/x 44 © J.-M. Jézéquel et al., 2010 source outgoingTransition 1 target incomingTransition input: EString * 1 output: EString fire() 0..1 // MyKermetaProgram.kmt // An EE-MOF metamodel is an OO program that does nothing require "StateMachine.ecore "StateMachine.ecore"" // to import it in Kermeta // Kermeta lets you weave in aspects Context FSM // Contracts (OCL WFR) inv: ownedState->forAll(s1,s2| require “StaticSemantics.ocl” StaticSemantics.ocl” s1.name=s2.name implies s1=s2) // Method bodies (Dynamic semantics) require “DynamicSemantics.kmt” DynamicSemantics.kmt” aspect class FSM { // Transformations operation reset() : Void { currentState := initialState }} class Minimizer { operation minimize (source: FSM):FSM {…} © J.-M. Jézéquel et}al., 2010 47 © J.-M. Jézéquel et al., 2010 48 Kermeta: Kermeta: a Kernel Kernel metamodeling metamodeling language Strict EMOF extension Statically Typed ObjectObject-Oriented EMOF Kermeta owningFSM 1 ModelModel-Oriented AspectAspect-Oriented – Simple syntax for static introduction – Arbitrary complex aspect weaving as a framework Still “kernel” kernel” language – Seamless import of Java classes in Kermeta for GUI/IO etc. 49 © J.-M. Jézéquel et al., 2010 name: EString step() 1 – Associations / Compositions – Model are first class citizens, notion of model type initialState reset() – Multiple inheritance / dynamic binding / reflection State run() outgoingTransition 1 target * incomingTransition 1 input: EString output: EString fire() 0..1 Example Association owningFSM 1 b B C 0..1 * d c A 1 run() initialState reset() b a1:A 1 c b1:B Ava nt Before d c1:C source State 0..1 * Transition ownedState currentState FSM D container() Ava nt Before source class State{ reference owningFSM : FSM[1..1]#ownedState class FSM attribute name : String { attribute outgoingTransition : Transition[0..*]#source attribute ownedState : State[0..*]#owningFSM reference incomingTransition : Transition#target reference initialState : State[1..1] operation step(c : String) : kermeta::standard::~Void is do reference currentState : State end operation run() : kermeta::standard::~Void is do } end operation reset() : kermeta::standard::~Void is doclass Transition{ reference source : State[1..1]#outgoingTransition end reference target : State[1..1]#incomingTransition } attribute input : String attribute output : String operation fire() : String is do end } 50 © J.-M. Jézéquel et al., 2010 Assignment semantics Composition Transition ownedState 0..1 – Generics, Function types (for OCLOCL-like iterators) iterators) * currentState FSM 1 target name: EString step() outgoingTransition * incomingTransition 1 input: EString output: EString fire() 0..1 d1:D operation fire() : String d2:D a2:A d2.c := c1 a2.b := b1 b a1:A d c1:C a2:A Aprè s After source.owningFSM.currentState := target result := output c b1:B d Aprè s After container() c d1:D d2:D 51 © J.-M. Jézéquel et al., 2010 owningFSM FSM owningFSM 1 * State initialState reset() 1 Transition ownedState currentState 0..1 run() name: EString step() source 1 target outgoingTransition * incomingTransition 1 FSM input: EString 1 * output: EString run() fire() State initialState reset() 1 0..1 Transition ownedState currentState 0..1 name: EString step() source 1 target outgoingTransition input: EString * incomingTransition 1 output: EString fire() 0..1 operation run() : Void operation step(c : String) : String 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 *") // 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 © J.-M. Jézéquel et al., 2010 52 © J.-M. Jézéquel et al., 2010 53 © J.-M. Jézéquel et al., 2010 54 Operational Semantics for LOGO x/y a /b S1 S2 Expressed as a mapping from a metameta-model to a virtual machine (VM) LOGO VM ? S3 y/x b /a – – – /** * Load a sample FSM from a xmi2 file */ operation 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 Concept of Turtle, Lines, points… points… Let’ ’ s Model it ! Let (Defined as an Ecore metameta-model) // Load the fsm (we get the main instance) result ?= resource.instances.one end 55 © J.-M. Jézéquel et al., 2010 56 © J.-M. Jézéquel et al., 2010 Virtual Machine - Model Virtual Machine - Semantics require "VMLogo.ecore" require "TurtleGUI.kmt" VMLogo.ecore LogoVMSemantics.kmt aspect class Point { method toString() : String is do result := "[" + x.toString + "," + y.toString + "]" end } aspect class Turtle { operation setPenUp(b : Boolean) is do penUp := b end operation rotate(angle : Integer) is do heading := (heading + angle).mod(360) end } Defined as an Ecore metameta-model 57 © J.-M. Jézéquel et al., 2010 Map Instructions to VM Actions Weave an interpretation aspect into the metameta-model – add an eval() eval() method into each class of the LOGO MM © J.-M. Jézéquel et al., 2010 58 © J.-M. Jézéquel et al., 2010 MetaMeta-level Anchoring Simple approach using the Kermeta VM to « ground » the semantics of basic operations Or reify it into the LOGO VM aspect class PenUp { eval (ctx: ctx: Context) Context) { ctx.getTurtle(). setPenUp((true) ctx.getTurtle().setPenUp true) } … aspect class Clear { eval (ctx: ctx: Context) Context) { ctx.getTurtle().reset() ctx.getTurtle().reset() } – Using eg a stackstack-based machine – Ultimately grounding it in kermeta though 59 © J.-M. Jézéquel et al., 2010 … aspect class Add { eval (ctx: ctx: Context) Context) : Integer { result = lhs.eval( lhs.eval(ctx) ctx) + rhs.eval(ctx rhs.eval(ctx)) } … aspect class Add { eval (ctx: ctx: Context) Context) { lhs.eval( lhs.eval(ctx) ctx) // put result // on top of ctx stack rhs.eval( rhs.eval(ctx) ctx) // idem ctx.getMachine(). add() () ctx.getMachine().add } 60 Handling control structures Operational semantics Block Conditional Repeat While require "ASMLogo.ecore" require "LogoVMSemantics.kmt" LogoDynSemantics.kmt aspect class If { operation eval(context : Context) : Integer is do if condition.eval(context) != 0 then result := thenPart.eval(context) else result := elsePart.eval(context) end end } aspect class Right { operation eval(context : Context) : Integer is do context.turtle.rotate(angle.eval(context)) end } 61 © J.-M. Jézéquel et al., 2010 Handling function calls 62 © J.-M. Jézéquel et al., 2010 Getting an Interpreter Use a stack frame – Owned in the Context Glue that is needed to load models – ie LOGO programs Vizualize the result – Print traces as text – Put an observer on the LOGO VM to graphically display the resulting figure 63 © J.-M. Jézéquel et al., 2010 Simulator Execute © J.-M. Jézéquel et al., 2010 64 © J.-M. Jézéquel et al., 2010 Outline the operational semantics 65 Introduction to Model Driven Engineering Designing MetaMeta-models: models: the LOGO example Static Semantics with OCL Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up © J.-M. Jézéquel et al., 2010 66 Implementing a modelmodel-driven compiler Map a LOGO program to Lego Mindstroms – – – Specific platform Lego The LOGO program is like a PIM The target program is a PSM => model transformation Mindstorms Turtle Robot – Two motors for wheels – One motor to control the pen Kermeta to weave a « compilation » aspect into the logo metameta-model aspect class PenUp { compile (ctx (ctx:: Context) Context) { } … aspect class Clear { } © J.-M. Jézéquel et al., 2010 67 ModelModel-toto-Text vs. ModelModel-toto-Model ModelModel-toto-Text Transformations ModelModel-toto-Text Approaches For generating: code, xml, html, doc. – VisitorVisitor-Based Approaches: – For generating: code, xml, html, doc. – Should be limited to syntactic level transcoding 68 © J.-M. Jézéquel et al., 2010 » Some visitor mechanisms to traverse the internal representation of a model and write code to a text stream » Iterators, Iterators, Write () ModelModel-toto-Model Transformations – To handle more complex, semantic driven transformations – TemplateTemplate-Based Approaches » A template consists of the target text containing slices of metametacode 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 © J.-M. Jézéquel et al., 2010 69 Classification of ModelModel-toto-Model Transformation Techniques 1. 2. ASMLogo.ecore ASMLogo.ecore Graph transformations, XSLT… XSLT… K.xmi Step Objecteering, Objecteering, Rose… Rose… Logo4NXC.kmt K1.xmi 2 – Code generation with template Dedicated model transformation tools – 5. 1 – ModelModel-toto-Model transformation CASE tools scripting languages – 4. Step Java/C#... Generic transformation tools – 3. Logo to NXC Compiler General purpose programming languages – 70 © J.-M. Jézéquel et al., 2010 OMG QVT style ASMLogo.ecore MetaMeta-modeling tools – Metacase, Metacase, Xactium, Xactium, Kermeta… Kermeta… © J.-M. Jézéquel et al., 2010 K1.xmi 71 © J.-M. Jézéquel et al., 2010 NXC.ket K.nxc 72 Step 1: ModelModel-toto-Model Step 1: ModelModel-toto-Model example Goal: prepare a LOGO model so that code generation is a simple traversal => ModelModel-toto-Model transformation Example: local2global In the LOGO metameta-model, functions can be declared anywhere, including (deeply) nested, without any impact on the operational semantics for NXC code generation, all functions must be declared in a “flat” flat” way at the beginning of the outermost block. => implement this model transformation as a locallocal-totoglobal aspect woven into the LOGO MM 73 © J.-M. Jézéquel et al., 2010 // aspect locallocal-toto-global aspect class Statement { method local2global(rootBlock local2global(rootBlock:: Block) is do end } aspect class ProcDeclaration method local2global(rootBlock local2global(rootBlock:: Block) is do … end } aspect class Block method local2global(rootBlock local2global(rootBlock:: Block) is do … end } … 74 © J.-M. Jézéquel et al., 2010 Execution Step 2: Kermeta Emitter Template NXC Code generation using a template 75 © J.-M. Jézéquel et al., 2010 Outline Logo Summary (1) Introduction to Model Driven Engineering Designing MetaMeta-models: models: the LOGO example Static Semantics with OCL Operational Semantics with Kermeta Building a Compiler: Model transformations Conclusion and WrapWrap-up 76 © J.-M. Jézéquel et al., 2010 ASMLogo.ecore TurtleGUI.java VMLogo.ecore logo.sts TurtleGUI.kmt LogoStaticSem.ocl Editor.java © J.-M. Jézéquel et al., 2010 TurtleControler.kmt LogoVMSemantics.kmt NXC.ket Logo4NXC.kmt LogoDynSemantics.kmt LogoNXCCompiler.kmt 77 © J.-M. Jézéquel et al., 2010 LogoSimulator.kmt 78 Logo Summary (2) Integrate From LOGO to Mindstorms all aspects coherently Logo Semantic in Kermeta – syntax / semantics / tools Use – – – – – Input scenario MOF for abstract syntax OCL for static semantics Kermeta for dynamic semantics Java for simulation GUI ... Simulator in Kermeta – For maintainability and evolutions Embedded source code inside the robot Interaction between the current simulation (Kermeta) and the GUI (Java) Result of a real execution Result of a simulation interpreted with Kermeta separation between concerns © J.-M. Jézéquel et al., 2010 79 © J.-M. Jézéquel et al., 2010 Kermeta in real projects 80 Conclusion and WrapWrap-up Artist2, the European network of excellence on realreal-time embedded systems UsineLogicielle, UsineLogicielle, a System@tic project where Kermeta based operational semantic is associated to functional requirement for test synthesis purposes. Speeds, a European FP6 project for aspectaspect-oriented metamodeling of avionics and automotive systems, including operational semantics aspects OpenEmbedd, OpenEmbedd, A French project building a MDE platform for realtime system. Mopcom, Mopcom, a French project applying MDE to hardware for generating SOC and introduce dynamic reconfigurability to them. Topcased, Topcased, a consortium that aims to build modelers for avionics and system engineering DiVA, DiVA, a European FP7 STREP project on handling Dynamic variability in complex, complex, adaptive systems 81 © J.-M. Jézéquel et al., 2010 Thank you ! Questions ? © J.-M. Jézéquel et al., 2010 Transformation written in Kermeta appropriate languages Keep Static constraints in OCL 83 Kermeta is an openopen-source initiative – Started January 2005 – More than 10 active developers Feel free to use – Start with a metameta-model in EMF » Get XML an reflective editor for free – Weave in static semantics in OCL – Weave in an interpreter, interpreter, » connect to a simulation platform – Weave in one or more compilers – Finaly care for concrete syntax issues Feel free to contribute! contribute! – www.kermeta.org © J.-M. Jézéquel et al., 2010 82