0
Enrich Your Models with OCL Edward Willink,  Thales Eclipse (MDT) OCL Project Lead Axel Uhl,  SAP Eclipse (MDT) OCL Commit...
Tutorial Material <ul><li>EclipseCon Session Page </li></ul>http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2271...
link to a ZIP of ZIPs (only one file allowed) </li></ul><li>Individual ZIPs on USB stick and at </li><ul><li>MainWorkspace...
Preparation Overview <ul><li>Strongly recommended (Wifi) - see slides </li><ul><li>Unzip ZippedZip.zip from EclipseCon ses...
Install Eclipse 3.7M6 (Indigo)
Install New Software ... for Indigo, 6 projects
(Install New Software ... OCL M6b)
Load MainWorkspaceProjects.zip in Eclipse
Load RuntimeWorkspaceProjects.zip in RT Eclipse </li></ul><li>Slow last ditch resort (USB stick) </li><ul><li>Numerous Ins...
Preparation from USB stick alone <ul><li>Copy one platform from Eclipse3.7M6Platforms
Copy all projects from Eclipse3.7M6Projects
Copy all files from EclipseCon2011OCL
Follow instructions in README.noWiFi </li></ul>
Preparation - Indigo M6 Platform <ul><li>Install Eclipse 3.7M6 (or M5) </li><ul><li>e.g. Download & Unzip eclipse-SDK-3.7M...
Preparation - Indigo M6 Projects <ul><li>Help->Install New Software... from </li></ul>Indigo - http://download.eclipse.org...
Modeling->Graphical Modeling Framework (GMF) Runtime
Modeling->OCL Examples and Editors
Modeling->Operational QVT SDK
Modeling->UML2 Extender SDK </li><ul><li>part of Modelling EPP </li></ul><li>Modeling->Xtext SDK </li></ul><li>uncheck &qu...
Next ... Restart Now </li></ul>
Preparation - Indigo M6b Updates <ul><li>M6b for OCL Examples </li><ul><li>optional but useful fixes for OCL
no help for adverse SWT/Modeling interaction  </li></ul></ul><ul><li>Help->Install New Software... from </li></ul>http://d...
Preparation - Main Workspace <ul><li>Close Wecome
File->Import->General </li><ul><li>Existing Projects into Workspace
Next </li></ul><li>Select achive File:  MainWorkspaceProjects.zip </li><ul><li>Finish </li><ul><li>9 projects
0 errors
98 warnings </li></ul></ul></ul>
Preparation - Runtime Eclipse <ul><li>Run->Run Configurations </li><ul><li>Select Eclipse->Eclipse OCL Tutorial </li></ul>...
Preparation - Runtime Workspace <ul><li>Close Wecome
File->Import->General </li><ul><li>Existing Projects into Workspace
Next </li></ul><li>Select achive File:  RuntimeWorkspaceProjects.zip </li><ul><li>Finish </li><ul><li>3 projects
0 errors
3 warnings </li></ul></ul></ul>
Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible -...
Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - indepen...
OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul...
The OCL Landscape Java Modeler Informal Debug Specification Author EMF Modeler DSL Modeler M2T Modeler M2M Modeler
Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible -...
Models <ul><li>The world is full of data </li></ul><ul><li>Models are good for controlling data </li></ul><ul><li>EMF is g...
Xtext editors </li></ul></ul><ul><li>Java is empowering </li></ul><ul><li>What more could you want? </li></ul>
Rules <ul><li>The world is full of rules </li><ul><li>password must be at least 6 characters
password must contain only letters and numbers </li></ul><li>Models should be good for applying rules </li><ul><li>direct ...
semantic validation for complex rules </li></ul><li>EMF (meta-modeling) captures simple rules </li><ul><li>multiplicity, o...
Behaviors <ul><li>The world is full of behaviors </li><ul><li>things happen
changes occur </li></ul></ul><ul><li>EMF (and OCL) support static data models </li></ul><ul><li>M2M supports changing/rela...
M2M: ATL, Epsilon, VIATRA, QVTo (OMG QVT)
M2T: Acceleo (OMG MOFM2T) </li></ul></ul>
Object Constraint Language <ul><li>Requirement: </li><ul><li>an expression language for models </li></ul><li>Influences: <...
to collections of model elements </li></ul></ul>
Simple Meta-Modeling Graphics <ul><ul><li>Box </li><ul><li>Class, enumeration </li></ul><li>Compartment </li><ul><li>Prope...
Multiplicity </li></ul></ul>Example Family Tree Meta-Model Ecore Diagram (similar to UML Class Diagram)
Richer Meta-Modelling <ul><li>Implicit constraints </li><ul><li>Up to  2 parents
MALE/FEMALE gender </li></ul></ul><ul><li>Arbitrary constraints </li><ul><li>At least 5 characters in name
1 MALE, 1 FEMALE parent
Self is not  an ancestor  of self </li></ul></ul>
Example Family Tree Model Simple GMF Diagram Editor Runtime Workspace: ...playspace/model/default.people_diagram
Simple Query Evaluation Window->Show View->Other... Console Console: New: Interactive Xtext OCL Select  Zeus  as the Model...
Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible -...
OCL Principles <ul><li>Natural/Formal Language compromise </li><ul><li>natural language error prone </li><ul><li>&quot;pre...
side effect free, no model changes, atomic execution
strongly typed, using UML generalization </li></ul></ul></ul>
Primitive Types and Literals Java (implementation) OCL (specification) boolean,Boolean,true,false Boolean,true,false short...
Mathematical Operators Java (implementation) OCL (specification) +, -, *, / +, -, *, / !, &&, ||, ^ not, and, or, xor, imp...
More Complex Query Selecting  Hera  defines the implicit context variable self : Person =  Hera
Object Navigation Properties <ul><ul><li>self.name   or just  name (cf.  this.getName()   or  getName() ) </li></ul></ul>O...
OCL in context <ul><li>Pure OCL expressions </li><ul><li>academic interest </li></ul><li>OCL expressions for models </li><...
adds value to model usage </li><ul><li>Essential OCL editor embedded in Console </li></ul></ul><li>OCL expressions for met...
transforms the capabilties </li><ul><li>OCLinEcore editor </li></ul></ul></ul>
EMF Delegates (Helios) <ul><li>EOperations have no body </li><ul><li>genmodel: UnsupportedOperationException stub </li></u...
Setting Delegate for EStructuralFeature initial value </li></ul></ul>
Example Validation Delegate
The OCLinEcore Editor OCLinEcore editor maintains EAnnotations automatically OCLinEcore editor provides OCL syntax checkin...
OCLinEcore Editor <ul><ul><li>Open with -> OCLinEcore
Save As *.ecore </li><ul><li>Loses formatting and comments </li></ul><li>Save As *.oclinecore </li><ul><li>Text file prese...
Searchable/replaceable text </li></ul></ul></ul>
Example Validation Failure Open ESEExampleTree/model/People1.xmi in Main Eclipse, with Sample Reflective Ecore Editor Sele...
Evaluator exercises <ul><li>In Runtime Eclipse </li><ul><li>open ...playspace/model/default.people_diagram
open Console, then Interactive Xtext Console
identify parents of Zeus
determine the number of letters in Hephaestus </li><ul><li>hint: String::size() : Integer </li></ul></ul></ul>
OCLinEcore exercises <ul><li>In Main Eclipse </li><ul><li>open ESEExampleTree/model/people1.xmi </li><ul><li>Validate
inspect validation failures </li></ul><li>edit people1.ecore as OCLinEcore </li><ul><li>change name limit to at most five ...
save
revalidate </li></ul><li>open your own favourite Ecore file as OCLinEcore </li></ul></ul>
Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible -...
Multiplicities and Collections Meta-models specify multiplicities <ul><ul><ul><li>children : Person[*] {ordered,unique}
parents : Person[0..2] {unique} </li></ul><li>multiplicities are specification concepts; not objects </li></ul></ul>Implem...
getChildren().get(2)  getChildren().add(newChild) </li></ul></ul></ul>OCL needs more than just UML multiplicities
OCL 2.0 Collections Typed Collections partially reify multiplicities Collections are different to objects Navigation from ...
Example Collection Operations Collection::size()  self.children->size() 'get' Sequence::at(Integer)  self.children->at(1) ...
Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible -...
Collection::select iteration <ul><li>Children </li></ul>self.children <ul><li>Sons </li></ul>self.children->select(gender ...
Collection::collect iteration <ul><li>Children </li></ul>self.children <ul><li>Grandchildren </li></ul>self.children->coll...
Some Other Collection Iterators <ul><li>any(iterator : type | body) </li><ul><li>(an arbitrary) any element for which body...
Some OrderedCollection Iterators <ul><li>sortedBy(iterator : type | body) </li><ul><li>an ordered of the elements </li><ul...
The iterate Iterators All iterations are degenerate iterate calls. <ul><li>iterate(iterator : T1;   accumulator : T2 = ini...
body  may involve  accumulator  and/or  iterator </li></ul><li>result </li><ul><li>accumulator </li></ul></ul></ul>
OCL Navigation Operators anObject.  ...  object navigation aCollection->  ...  collection navigation Shorthands aCollectio...
Implicit Collect Query parents.parents = parents->collect(parents) 3 symbols, compared to 4 lines of Java 4 grandparents, ...
Cleaned up query parents.parents->asSet()->sortedBy(name) ->asSet()  converts to Set(Person), removes duplicates ->sortedB...
Implicit As Set Conversion object - > <ul><ul><li>Co nverts object (if a defined) to a Set of object </li></ul></ul>object...
Collection::closure iteration <ul><li>children, grandchildren, greatgrandchildren etc </li></ul>self->closure(children) <u...
OCL as Implementation any(x)  iteration selects an arbitrary element for which x is true.
Derived Properties For Hera invariant   MixedGenderParents:  father . gender  <>  mother . gender ; fails because father i...
Collection, Iteration exercises <ul><li>In Runtime Eclipse </li><ul><li>open ...playspace/model/default.people_diagram
open Console, then Interactive Xtext Console
repeat slide examples
determine all ancestors of Zeus
determine all ancestors of Hephaestus
determine all four-lettered ancestors of Ares
determine the number of siblings of Perseus </li></ul></ul>
Other OCL Capabilities No time to mention <ul><li>Other operations
Tuples
Association Classes/Qualifiers
@pre values
Messages
States </li></ul>
OCL in Code - Java API <ul><li>MDT/OCL started as a Java API </li><ul><li>parsing OCL
evaluating OCL
Upcoming SlideShare
Loading in...5
×

Enrich Your Models With OCL

10,464

Published on

EclipseCon 2011 Tutorial

Published in: Technology
3 Comments
6 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,464
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
290
Comments
3
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "Enrich Your Models With OCL"

  1. 1. Enrich Your Models with OCL Edward Willink, Thales Eclipse (MDT) OCL Project Lead Axel Uhl, SAP Eclipse (MDT) OCL Committer 21st March 2011
  2. 2. Tutorial Material <ul><li>EclipseCon Session Page </li></ul>http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2271 <ul><ul><li>link to these slides
  3. 3. link to a ZIP of ZIPs (only one file allowed) </li></ul><li>Individual ZIPs on USB stick and at </li><ul><li>MainWorkspaceProjects.zip </li></ul></ul>(http:// www.eclipse.org /modeling/mdt/ocl/docs/publications/EclipseCon2011Tutorial/MainWorkspaceProjects.zip) <ul><ul><li>RuntimeWorkspaceProjects.zip </li></ul></ul>(http://www.eclipse.org/modeling/mdt/ocl/docs/publications/EclipseCon2011Tutorial/RuntimeWorkspaceProjects.zip) <ul><li>Import ... Existing Projects from Archive </li><ul><ul><li>ESEExampleTree/model/People1.ecore </li></ul></ul><li>Run nested Eclipse, Import ESEExampleTree </li><ul><ul><li>ESEExampleTree/model/default.people_diagram </li></ul></ul></ul>
  4. 4. Preparation Overview <ul><li>Strongly recommended (Wifi) - see slides </li><ul><li>Unzip ZippedZip.zip from EclipseCon session page
  5. 5. Install Eclipse 3.7M6 (Indigo)
  6. 6. Install New Software ... for Indigo, 6 projects
  7. 7. (Install New Software ... OCL M6b)
  8. 8. Load MainWorkspaceProjects.zip in Eclipse
  9. 9. Load RuntimeWorkspaceProjects.zip in RT Eclipse </li></ul><li>Slow last ditch resort (USB stick) </li><ul><li>Numerous Installs (see USB stick README) </li></ul></ul>
  10. 10. Preparation from USB stick alone <ul><li>Copy one platform from Eclipse3.7M6Platforms
  11. 11. Copy all projects from Eclipse3.7M6Projects
  12. 12. Copy all files from EclipseCon2011OCL
  13. 13. Follow instructions in README.noWiFi </li></ul>
  14. 14. Preparation - Indigo M6 Platform <ul><li>Install Eclipse 3.7M6 (or M5) </li><ul><li>e.g. Download & Unzip eclipse-SDK-3.7M6-win32.zip or eclipse-modeling-indigo-M6-incubation-win32.zip </li></ul></ul><ul><ul><li>known lower bounds: OCL M6, Xtext M6, EMF M5 </li></ul></ul><ul><li>Start Eclipse, with plenty of memory, e.g. </li></ul>C:ToolsEclipse3.7M6EclipseConeclipse.exe -data C:/EclipseCon/Workspace -vmargs -Xmx1g -XX:PermSize=64M -XX:MaxPermSize=128M
  15. 15. Preparation - Indigo M6 Projects <ul><li>Help->Install New Software... from </li></ul>Indigo - http://download.eclipse.org/releases/indigo <ul><ul><li>Modeling->Acceleo SDK
  16. 16. Modeling->Graphical Modeling Framework (GMF) Runtime
  17. 17. Modeling->OCL Examples and Editors
  18. 18. Modeling->Operational QVT SDK
  19. 19. Modeling->UML2 Extender SDK </li><ul><li>part of Modelling EPP </li></ul><li>Modeling->Xtext SDK </li></ul><li>uncheck &quot;Contact all sites during install to find required software&quot;
  20. 20. Next ... Restart Now </li></ul>
  21. 21. Preparation - Indigo M6b Updates <ul><li>M6b for OCL Examples </li><ul><li>optional but useful fixes for OCL
  22. 22. no help for adverse SWT/Modeling interaction </li></ul></ul><ul><li>Help->Install New Software... from </li></ul>http://download.eclipse.org/modeling/mdt/ocl/updates/milestones or USB stick: mdt-ocl-Update-3.1.0M6b.zip <ul><ul><li>MDT/OCL (3.1.0-v20110320...) </li></ul></ul>
  23. 23. Preparation - Main Workspace <ul><li>Close Wecome
  24. 24. File->Import->General </li><ul><li>Existing Projects into Workspace
  25. 25. Next </li></ul><li>Select achive File: MainWorkspaceProjects.zip </li><ul><li>Finish </li><ul><li>9 projects
  26. 26. 0 errors
  27. 27. 98 warnings </li></ul></ul></ul>
  28. 28. Preparation - Runtime Eclipse <ul><li>Run->Run Configurations </li><ul><li>Select Eclipse->Eclipse OCL Tutorial </li></ul><li>Sun JVM </li><ul><li>Run </li><ul><li>sets VM arguments for plenty of memory, e.g. </li></ul></ul></ul>-Xms40m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128M <ul><li>Other JVM </li><ul><li>Select the &quot;Arguments&quot; tab </li><ul><li>Set VM arguments for plenty of memory </li></ul><li>Run </li></ul></ul>
  29. 29. Preparation - Runtime Workspace <ul><li>Close Wecome
  30. 30. File->Import->General </li><ul><li>Existing Projects into Workspace
  31. 31. Next </li></ul><li>Select achive File: RuntimeWorkspaceProjects.zip </li><ul><li>Finish </li><ul><li>3 projects
  32. 32. 0 errors
  33. 33. 3 warnings </li></ul></ul></ul>
  34. 34. Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible - different to Java <ul><ul><li>OCL Collections </li></ul></ul>Powerful - more relevant than Java <ul><ul><li>OCL Iterators exercises </li></ul></ul>Tool support - available and improving
  35. 35. Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - independent validation language exercises
  36. 36. OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul><li>OCL in Acceleo - navigation/evaluation aid exercises </li></ul><li>QVTo State Flattening transformation </li><ul><li>OCL in QVT Operational - navigation/evaluation aid exercises </li></ul></ul>
  37. 37. The OCL Landscape Java Modeler Informal Debug Specification Author EMF Modeler DSL Modeler M2T Modeler M2M Modeler
  38. 38. Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible - different to Java <ul><ul><li>OCL Collections </li></ul></ul>Powerful - more relevant than Java <ul><ul><li>OCL Iterators exercises </li></ul></ul>Tool support - available and improving
  39. 39. Models <ul><li>The world is full of data </li></ul><ul><li>Models are good for controlling data </li></ul><ul><li>EMF is good for (meta-)modeling </li></ul><ul><li>Extended EMF even better </li><ul><li>EMF editors
  40. 40. Xtext editors </li></ul></ul><ul><li>Java is empowering </li></ul><ul><li>What more could you want? </li></ul>
  41. 41. Rules <ul><li>The world is full of rules </li><ul><li>password must be at least 6 characters
  42. 42. password must contain only letters and numbers </li></ul><li>Models should be good for applying rules </li><ul><li>direct implementation for simple rules
  43. 43. semantic validation for complex rules </li></ul><li>EMF (meta-modeling) captures simple rules </li><ul><li>multiplicity, ordering, uniqueness, containment </li></ul><li>OCL supports complex rules </li><ul><li>used extensively in OMG specifications </li></ul></ul>
  44. 44. Behaviors <ul><li>The world is full of behaviors </li><ul><li>things happen
  45. 45. changes occur </li></ul></ul><ul><li>EMF (and OCL) support static data models </li></ul><ul><li>M2M supports changing/related models </li></ul><ul><li>OCL underpins modern modeling specifications </li><ul><li>UML: state machine guards/transitions
  46. 46. M2M: ATL, Epsilon, VIATRA, QVTo (OMG QVT)
  47. 47. M2T: Acceleo (OMG MOFM2T) </li></ul></ul>
  48. 48. Object Constraint Language <ul><li>Requirement: </li><ul><li>an expression language for models </li></ul><li>Influences: </li><ul><li>Fortran </li><ul><li>provided fundamental expression/function notation </li></ul><li>Evolution </li><ul><li>'.' operator for records/structures/objects/operations </li></ul></ul><li>OCL extension </li><ul><li>from objects
  49. 49. to collections of model elements </li></ul></ul>
  50. 50. Simple Meta-Modeling Graphics <ul><ul><li>Box </li><ul><li>Class, enumeration </li></ul><li>Compartment </li><ul><li>Property, operation </li></ul><li>Line </li><ul><li>Association </li></ul><li>Decoration </li><ul><li>Composition, navigability </li></ul></ul></ul>Text <ul><ul><li>Name, type, stereotype
  51. 51. Multiplicity </li></ul></ul>Example Family Tree Meta-Model Ecore Diagram (similar to UML Class Diagram)
  52. 52. Richer Meta-Modelling <ul><li>Implicit constraints </li><ul><li>Up to 2 parents
  53. 53. MALE/FEMALE gender </li></ul></ul><ul><li>Arbitrary constraints </li><ul><li>At least 5 characters in name
  54. 54. 1 MALE, 1 FEMALE parent
  55. 55. Self is not an ancestor of self </li></ul></ul>
  56. 56. Example Family Tree Model Simple GMF Diagram Editor Runtime Workspace: ...playspace/model/default.people_diagram
  57. 57. Simple Query Evaluation Window->Show View->Other... Console Console: New: Interactive Xtext OCL Select Zeus as the Model Context Type children then carriage return
  58. 58. Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible - different to Java <ul><ul><li>OCL Collections </li></ul></ul>Powerful - more relevant than Java <ul><ul><li>OCL Iterators exercises </li></ul></ul>Tool support - available and improving
  59. 59. OCL Principles <ul><li>Natural/Formal Language compromise </li><ul><li>natural language error prone </li><ul><li>&quot;press any key to continue&quot;, &quot;press enter or stop&quot; </li></ul><li>formal language unapproachable to many </li></ul></ul>∀ x ∈ y ∃ z(x) <ul><li>Specification (not Programming) Language </li><ul><ul><li>declarative, modeling language
  60. 60. side effect free, no model changes, atomic execution
  61. 61. strongly typed, using UML generalization </li></ul></ul></ul>
  62. 62. Primitive Types and Literals Java (implementation) OCL (specification) boolean,Boolean,true,false Boolean,true,false short,int,long,Integer,BigInteger Integer , UnlimitedNatural float, double, BigDecimal Real Character, String, 'c', &quot; linen &quot; String, 'c', ' linen ' Object,this,null OclAny, self ,null Exception invalid Integer value value : Integer Java: practical implementation language OCL: simpler, idealistic, specification language
  63. 63. Mathematical Operators Java (implementation) OCL (specification) +, -, *, / +, -, *, / !, &&, ||, ^ not, and, or, xor, implies <, >, <=, >= <, >, <=, >= ==, != = , <> Math.max(4,5) 4.max(5) if (a) b; else c; if a then b else c endif Integer value = 5; doSomething(value); let value : Integer = 5 in doSomething(value)
  64. 64. More Complex Query Selecting Hera defines the implicit context variable self : Person = Hera
  65. 65. Object Navigation Properties <ul><ul><li>self.name or just name (cf. this.getName() or getName() ) </li></ul></ul>Operations <ul><ul><li>self.child('John') or just child('John') (cf. this.child(&quot;John&quot;) or child(&quot;John&quot;) ) </li></ul></ul>
  66. 66. OCL in context <ul><li>Pure OCL expressions </li><ul><li>academic interest </li></ul><li>OCL expressions for models </li><ul><li>useful for practice/experiment
  67. 67. adds value to model usage </li><ul><li>Essential OCL editor embedded in Console </li></ul></ul><li>OCL expressions for meta-models </li><ul><li>adds value to meta-models
  68. 68. transforms the capabilties </li><ul><li>OCLinEcore editor </li></ul></ul></ul>
  69. 69. EMF Delegates (Helios) <ul><li>EOperations have no body </li><ul><li>genmodel: UnsupportedOperationException stub </li></ul><li>EOperation may have a special EAnnotation </li><ul><li>configures an Invocation Delegate </li></ul><li>Eclipse OCL provides OCL Invocation Delegate </li></ul><ul><li>Similarly </li><ul><li>Validation Delegate for EClassifier invariants
  70. 70. Setting Delegate for EStructuralFeature initial value </li></ul></ul>
  71. 71. Example Validation Delegate
  72. 72. The OCLinEcore Editor OCLinEcore editor maintains EAnnotations automatically OCLinEcore editor provides OCL syntax checking OCLinEcore editor will provide OCL semantic checking
  73. 73. OCLinEcore Editor <ul><ul><li>Open with -> OCLinEcore
  74. 74. Save As *.ecore </li><ul><li>Loses formatting and comments </li></ul><li>Save As *.oclinecore </li><ul><li>Text file preserves comments </li></ul><li>Useful for plain Ecore too: </li><ul><li>Printable/reviewable text
  75. 75. Searchable/replaceable text </li></ul></ul></ul>
  76. 76. Example Validation Failure Open ESEExampleTree/model/People1.xmi in Main Eclipse, with Sample Reflective Ecore Editor Select Universe, Right button menu, Validate
  77. 77. Evaluator exercises <ul><li>In Runtime Eclipse </li><ul><li>open ...playspace/model/default.people_diagram
  78. 78. open Console, then Interactive Xtext Console
  79. 79. identify parents of Zeus
  80. 80. determine the number of letters in Hephaestus </li><ul><li>hint: String::size() : Integer </li></ul></ul></ul>
  81. 81. OCLinEcore exercises <ul><li>In Main Eclipse </li><ul><li>open ESEExampleTree/model/people1.xmi </li><ul><li>Validate
  82. 82. inspect validation failures </li></ul><li>edit people1.ecore as OCLinEcore </li><ul><li>change name limit to at most five characters
  83. 83. save
  84. 84. revalidate </li></ul><li>open your own favourite Ecore file as OCLinEcore </li></ul></ul>
  85. 85. Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible - different to Java <ul><ul><li>OCL Collections </li></ul></ul>Powerful - more relevant than Java <ul><ul><li>OCL Iterators exercises </li></ul></ul>Tool support - available and improving
  86. 86. Multiplicities and Collections Meta-models specify multiplicities <ul><ul><ul><li>children : Person[*] {ordered,unique}
  87. 87. parents : Person[0..2] {unique} </li></ul><li>multiplicities are specification concepts; not objects </li></ul></ul>Implementations (e.g. Ecore) reify multiplicities <ul><ul><ul><li>getName() setName(newName) </li></ul><li>'many' properties have extra implementation objects </li><ul><li>getChildren() returns a UniqueEList<Person>
  88. 88. getChildren().get(2) getChildren().add(newChild) </li></ul></ul></ul>OCL needs more than just UML multiplicities
  89. 89. OCL 2.0 Collections Typed Collections partially reify multiplicities Collections are different to objects Navigation from a Collection uses -> <ul><ul><ul><li>[Navigation from an Object (OclAny) uses . ] </li></ul></ul></ul>Collections have type parameters Collections have useful operations Collections have very useful iterations Collections are immutable Collection(T) Unordered Ordered Non-Unique Bag(T) Sequence(T) Unique Set(T) OrderedSet(T)
  90. 90. Example Collection Operations Collection::size() self.children->size() 'get' Sequence::at(Integer) self.children->at(1) <ul><ul><ul><li>nb 1 is the first index, size() is the last </li></ul></ul></ul>'add' Collection(T)::including(T) : Collection(T) <ul><ul><ul><li>returns a new collection with added content </li></ul></ul></ul>'contains' Collection(T)::includes(T) : Boolean <ul><ul><ul><li>tests existing content </li></ul></ul></ul>
  91. 91. Part 1: OCL in isolation Why OCL? Easy - similar Java <ul><ul><li>OCL Basic Facilities exercises </li></ul></ul>Sensible - different to Java <ul><ul><li>OCL Collections </li></ul></ul>Powerful - more relevant than Java <ul><ul><li>OCL Iterators exercises </li></ul></ul>Tool support - available and improving
  92. 92. Collection::select iteration <ul><li>Children </li></ul>self.children <ul><li>Sons </li></ul>self.children->select(gender = Gender::MALE) self.children->select(child | child.gender = Gender::MALE) self.children->select(child : Person | child.gender = Gender::MALE) <ul><li>select(iterator : type | body) </li><ul><ul><li>filters to select elements for which the body is true </li></ul></ul><li>reject(iterator : type | body) </li><ul><ul><li>filters to reject elements for which the body is true </li></ul></ul><li>cf multi-line Java loop </li></ul>
  93. 93. Collection::collect iteration <ul><li>Children </li></ul>self.children <ul><li>Grandchildren </li></ul>self.children->collect(children) self.children->collect(child | child.children) self.children->collect(child : Person | child.children) <ul><li>collect(iterator : type | body) </li><ul><ul><li>creates a new collection comprising all the bodies </li></ul></ul></ul>
  94. 94. Some Other Collection Iterators <ul><li>any(iterator : type | body) </li><ul><li>(an arbitrary) any element for which body is true </li></ul><li>exists(iterator : type | body) exists(iterator1 : type, iterator2 : type | body) </li><ul><li>true if body is true for some element </li></ul><li>forAll(iterator : type | body) forAll(iterator1 : type, iterator2 : type | body) </li><ul><li>true if body is true for all elements </li></ul><li>isUnique(iterator : type | body) </li><ul><li>true if body is distinct for all elements </li></ul><li>one(iterator : type | body) </li><ul><li>true if body is true for exactly one element </li></ul></ul>
  95. 95. Some OrderedCollection Iterators <ul><li>sortedBy(iterator : type | body) </li><ul><li>an ordered of the elements </li><ul><li>ordered by the < operation of the body's type </li></ul></ul></ul>
  96. 96. The iterate Iterators All iterations are degenerate iterate calls. <ul><li>iterate(iterator : T1; accumulator : T2 = initializer | body) : T2 </li><ul><li>initialization </li><ul><li>accumulator = initializer </li></ul><li>iteration using iterator </li><ul><li>accumulator = body
  97. 97. body may involve accumulator and/or iterator </li></ul><li>result </li><ul><li>accumulator </li></ul></ul></ul>
  98. 98. OCL Navigation Operators anObject. ... object navigation aCollection-> ... collection navigation Shorthands aCollection. ... implicit collect anObject-> ... implicit as set Object Collection . Navigation ? -> ? Navigation
  99. 99. Implicit Collect Query parents.parents = parents->collect(parents) 3 symbols, compared to 4 lines of Java 4 grandparents, but not all different!
  100. 100. Cleaned up query parents.parents->asSet()->sortedBy(name) ->asSet() converts to Set(Person), removes duplicates ->sortedBy(name) alphabeticizes
  101. 101. Implicit As Set Conversion object - > <ul><ul><li>Co nverts object (if a defined) to a Set of object </li></ul></ul>object->notEmpty() <ul><ul><li>Standard OCL idiom </li></ul></ul>Object Collection . Navigation Implicit collect() -> Implicit as set Navigation object as set result notEmpty() result defined object Set{object} true null, undefined object Set{} false invalid, error invalid invalid
  102. 102. Collection::closure iteration <ul><li>children, grandchildren, greatgrandchildren etc </li></ul>self->closure(children) <ul><li>Implicit collection of self, then closure of all children </li></ul>[ closure in MDT/OCL 1.2, in OMG OCL 2.3 ]
  103. 103. OCL as Implementation any(x) iteration selects an arbitrary element for which x is true.
  104. 104. Derived Properties For Hera invariant MixedGenderParents: father . gender <> mother . gender ; fails because father is null and mother is null
  105. 105. Collection, Iteration exercises <ul><li>In Runtime Eclipse </li><ul><li>open ...playspace/model/default.people_diagram
  106. 106. open Console, then Interactive Xtext Console
  107. 107. repeat slide examples
  108. 108. determine all ancestors of Zeus
  109. 109. determine all ancestors of Hephaestus
  110. 110. determine all four-lettered ancestors of Ares
  111. 111. determine the number of siblings of Perseus </li></ul></ul>
  112. 112. Other OCL Capabilities No time to mention <ul><li>Other operations
  113. 113. Tuples
  114. 114. Association Classes/Qualifiers
  115. 115. @pre values
  116. 116. Messages
  117. 117. States </li></ul>
  118. 118. OCL in Code - Java API <ul><li>MDT/OCL started as a Java API </li><ul><li>parsing OCL
  119. 119. evaluating OCL
  120. 120. Facade to hide internals
  121. 121. 'Internal' API for derived language parse/evaluate </li></ul><li>OCL can be much faster than Java! </li><ul><li>OCL amenable to analysis </li></ul></ul>
  122. 122. The Re-Evaluation Problem <ul><li>System defined by a set of OCL expressions
  123. 123. System comprising a set of model elements
  124. 124. A model element changes ....
  125. 125. Which of the OCL expressions may have changed its value on which context elements?
  126. 126. Naïve approach </li><ul><li>re-evaluate all expressions for all their contexts
  127. 127. takes O(|expressions| * |modelElements|) </li></ul></ul>
  128. 128. The Impact Analyzer <ul><li>Smart approach </li></ul><ul><ul><li>pre-evaluate all OCL expression dependencies
  129. 129. re-evaluate </li><ul><li>only changed model elements
  130. 130. only expressions for which model element is changed </li></ul></ul></ul>Go to Efficient, Scalable Notification Handling for EMF (2 hours ago).
  131. 131. Benchmark Context Reduction (Average Case) Naive Event-Filtered (*6 Speed-Up) Event-Filtered and Context-Reduced (*1300 Speed-Up vs. Naive, *220 vs. Event-Filtered-Only) Total Re-Evaluation Time in Seconds Number of Model Elements
  132. 132. Benchmark Context Reduction (Worst Case) Naive Event-Filtered (*2.4 Speed-Up) Event-Filtered and Context-Reduced (*27 Speed-Up vs. Naive, *11 vs. Event-Filtered-Only) Total Re-Evaluation Time in Seconds Number of Model Elements (apply changes to very central elements, referenced by all other model packages)
  133. 133. Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - independent validation language exercises
  134. 134. OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul><li>OCL in Acceleo - navigation/evaluation aid exercises </li></ul><li>QVTo State Flattening transformation </li><ul><li>OCL in QVT Operational - navigation/evaluation aid exercises </li></ul></ul>
  135. 135. Simple State Machine DSL module &quot;simple.states&quot; statemachine Machine { events START STOP; state Start { STOP => Stop } initial state Stop { START => Start } }
  136. 136. The Xtext States Grammar http://www.eclipse.org/modeling/mdt/ocl/docs/publications/EclipseCon2011Tutorial /org.eclipse.ocl.tutorial.eclipsecon2011.states/src/org/eclipse/ocl/tutorial/eclipsecon2011/States.xtext grammar org.eclipse.ocl.tutorial.eclipsecon2011.States with org.eclipse.xtext.common.Terminals generate states &quot;http://ocl.eclipse.org/tutorial/eclipsecon2011/States&quot; Module: 'module' name=STRING (machines+=Statemachine)*; Statemachine: (initial?= 'initial' )? 'statemachine' name=ID ( 'value' value=INT)? '{' 'events' (events+=Event)* ';' (states+=State)* '}' ; Event: name=ID; State: SimpleState | CompoundState; SimpleState: (initial?= 'initial' )? 'state' name=ID ( 'value' value=INT)? '{' (transitions+=Transition)* '}' ; CompoundState: 'compound' (initial?= 'initial' )? 'state' name=ID 'machine' machine=[ Statemachine ] '{' (transitions+=Transition)* '}' ; Transition: event=[ Event ] '=>' state=[ State ];
  137. 137. Validation Diagnose inappropriate source text <ul><li>Xtext grammar </li><ul><li>validates syntax and some semantics </li><ul><li>typos:
  138. 138. inappropriate names: </li></ul></ul><li>Additional Java/Check Validator </li><ul><li>specialized validation </li></ul></ul><ul><li>External Complete OCL Validation
  139. 139. Embedded OCLinEcore Validation </li></ul>
  140. 140. External Complete OCL Validation <ul><li>Xtext States project </li><ul><li>src-gen/.../States.ecore </li><ul><li>auto-generated; don't want to edit it </li></ul><li>META-INF/MANIFEST.MF </li><ul><li>add dependency on </li></ul></ul></ul>org.eclipse.ocl.examples.xtext.completeocl <ul><ul><li>src/.../validation/StatesJavaValidator.java </li><ul><li>generated once; can edit it </li></ul></ul><li>Helpwanted: validation.CompleteOCLValidatorFragment </li></ul>
  141. 141. StatesJavaValidator.java <ul><li>Use Java Validator to invoke Complete OCL </li></ul>import org.eclipse.emf.common.util.URI; import org.eclipse.ocl.examples.xtext.completeocl.validation.CompleteOCLEObjectValidator; import org.eclipse.ocl.tutorial.eclipsecon2011.states.StatesPackage; import org.eclipse.xtext.validation.EValidatorRegistrar; public class StatesJavaValidator extends AbstractStatesJavaValidator { @Override public void register(EValidatorRegistrar registrar) { super .register(registrar); StatesPackage ePackage = StatesPackage. eINSTANCE ; URI oclURI = URI. createPlatformResourceURI ( &quot;/org.eclipse.ocl.tutorial.eclipsecon2011.states.ocl/model/States.ocl&quot; , true ); registrar.register(ePackage, new CompleteOCLEObjectValidator(ePackage, oclURI)); } }
  142. 142. Complete OCL Validation 1 /org.eclipse.ocl.tutorial.eclipsecon2011.states.ocl/model/States.ocl import 'http://ocl.eclipse.org/tutorial/eclipsecon2011/States' package states context Statemachine inv HasInitialState( 'No initial state' ): states -> exists ( s : State | s . initial ) endpackage Evaluates: states -> exists ( s : State | s . initial ) true => silent, invariant is satisfied false => invariant is not satisfied evaluates 'No initial state' to determine warning diagnostic null => invariant is not satisfied evaluates 'No initial state' to determine error diagnostic invalid => exception, evaluation failure
  143. 143. Xtext with independent OCL Main Eclipse <ul><ul><li>meta-modeling </li></ul></ul><ul><li>Edit states.xtext
  144. 144. Tailor Editor
  145. 145. Generate States Editor </li><ul><li>Generate states.ecore </li></ul></ul><ul><li>Edit states.ocl </li></ul>Run-time Eclipse <ul><ul><li>modeling </li></ul></ul><ul><li>Exit Run-time
  146. 146. Start Run-time
  147. 147. Edit simple.states </li></ul><ul><li>Exit Run-time
  148. 148. Start Run-time
  149. 149. Edit simple.states </li><ul><li>with OCL validation </li></ul></ul>
  150. 150. Example Invariant Violation
  151. 151. Complete OCL Validation 2 context Statemachine def : asError(verdict : Boolean ) : Boolean = if verdict then true else null endif inv HasInitialState( 'No initial state' ): asError ( states -> exists ( s : State | s . initial )) context SimpleState inv NameLength( ''' + name + '' has ' + name . size (). toString () + ' characters when at least 4 wanted' ): name . size () >= 4 inv NameIsLeadingUpperCase( ''' + name + '' must be Leading Uppercase' ): let firstLetter : String = name . substring ( 1 , 1 ) in firstLetter . toUpperCase () = firstLetter
  152. 152. Complete OCL Validation 3 context SimpleState def : statemachine : Statemachine = oclContainer ()-> oclAsType ( Statemachine ) inv UniqueInitialState( 'There is more than one initial state' ): initial implies statemachine . states -> select ( initial )-> size () = 1 inv EveryEventIsHandled( let allEvents : Set ( Event ) = statemachine . events -> asSet (), myEvents : Set ( Event ) = self . transitions . event -> asSet () in ( allEvents - myEvents )-> iterate ( e : Event ; s : String = 'The following events are not handled:' | s + ' ' + e . name )): let allEvents : Set ( Event ) = statemachine . events -> asSet (), myEvents : Set ( Event ) = self . transitions . event -> asSet () in ( allEvents - myEvents )-> isEmpty ()
  153. 153. Composite Validation Errors
  154. 154. Complete OCL Exercises 1 <ul><li>Open Simple.states in Run-time Eclipse </li><ul><li>edit and see similar errors to previous slide
  155. 155. add additional state </li></ul><li>Exit Run-time Eclipse </li><ul><li>edit States.ocl </li><ul><li>change NameIsLeadingUpperCase to NameIsUppercase </li></ul><li>restart Run-time Eclipse
  156. 156. check that NameIsUppercase is working </li></ul></ul>
  157. 157. Complete OCL Exercises 2 <ul><li>Add a StateIsReachable invariant </li><ul><li>tip 1: a sequence of transitions from an initial state reaches the state in question
  158. 158. tip 2: the closure of all transitions from all initial states includes the state in question </li></ul></ul>
  159. 159. Complete OCL activation <ul><li>Xtext </li><ul><li>CompleteOCLEObjectValidator </li></ul><li>Interactive Xtext OCL Console </li><ul><li>Manual Load Resource </li></ul></ul>eClass, eType load referredType ???? States.ecore simple.states Modeling Tool States.ocl
  160. 160. Model URI Resolution <ul><li>Main Eclipse, no Global Package Registry </li></ul>platform:/resource/org.eclipse.ocl.tutorial.eclipsecon2011.states/ src-gen/org/eclipse/ocl/tutorial/eclipsecon2011/States.ecore <ul><li>Run-time Eclipse, no Project Resources </li></ul>http://ocl.eclipse.org/tutorial/eclipsecon2011/States <ul><li>Complete OCL </li><ul><li>editor runs in Main Eclipse
  161. 161. executed in Run-time Eclipse </li></ul><li>OCL Model Registry, Project Package Registry </li></ul>
  162. 162. OCL Model Registry URI mapping <ul><li>Defined for: org.eclipse.ocl.tutorial.eclipsecon2011.states.ocl
  163. 163. AccessorName: http://ocl.eclipse.org/tutorial/eclipsecon2011/States
  164. 164. Model URI: </li></ul>platform:/resource/org.eclipse.ocl.tutorial.eclipsecon2011.states/ src-gen/org/eclipse/ocl/tutorial/eclipsecon2011/States.ecore
  165. 165. Complete OCL Validation <ul><li>Independent add-on to an Ecore file </li><ul><li>no need to edit the auto-generated Ecore file
  166. 166. need to register the Complete OCL for validation </li></ul></ul><ul><li>Not part of the editor framework </li><ul><li>re-useable in other tool contexts
  167. 167. need to inform tool of Complete OCL existence </li></ul></ul><ul><li>UML specified with additional Complete OCL </li></ul>
  168. 168. Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - independent validation language exercises
  169. 169. OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul><li>OCL in Acceleo - navigation/evaluation aid exercises </li></ul><li>QVTo State Flattening transformation </li><ul><li>OCL in QVT Operational - navigation/evaluation aid exercises </li></ul></ul>
  170. 170. Xtext with embedded OCL Main Eclipse <ul><ul><li>meta-modeling </li></ul></ul><ul><li>Edit states.xtext or states.ecore with OCL
  171. 171. Tailor Editor
  172. 172. Generate OCLStates Editor
  173. 173. GenModel States </li></ul>Run-time Eclipse <ul><ul><li>modeling </li></ul></ul><ul><li>Exit Run-time </li></ul><ul><li>Start Run-time
  174. 174. Edit simple.states </li><ul><li>with OCL validation </li></ul></ul>
  175. 175. Embedded OCL Validation <ul><li>OCLinEcore editor embeds OCL in Ecore </li><ul><li>no need to inform tools that OCL exists
  176. 176. EMF automatically invokes OCL via delegates </li></ul><li>Ecore can no longer be autogenerated </li><ul><li>often appropriate for stable models anyway </li></ul></ul>eClass, eType load OCLStates.ecore simple.oclstates Modeling Tool embedded OCL
  177. 177. Move Ecore <ul><li>Move States.ecore, States.genmodel </li><ul><li>from org.eclipse.ocl.tutorial.eclipsecon2011.states/ src-gen/org/eclipse/ocl/tutorial/eclipsecon2011/States.ecore
  178. 178. to org.eclipse.ocl.tutorial.eclipsecon2011.oclstates/ model/OCLStates.ecore
  179. 179. change all ... States.ecore refs to OCLStates.ecore </li></ul></ul>
  180. 180. Import rather than Generate Ecore grammar org.eclipse.ocl.tutorial.eclipsecon2011.States with org.eclipse.xtext.common.Terminals generate states &quot;http://ocl.eclipse.org/tutorial/eclipsecon2011/States&quot; import &quot;platform:/resource/org.eclipse.ocl.tutorial.eclipsecon2011.oclstates/model/OCLStates.ecore&quot; <ul><li>(Change MWE2 script to *.oclstates) </li></ul><ul><li>The Generate OCLStates Editor launch may be used </li></ul>
  181. 181. Optional Workaround Bug 322639 <ul><li>Use of EObjectValidator gives </li><ul><ul><li>'An object may not circularly contain itself' </li></ul></ul><li>NoEObjectValidator setUseEObjectValidator( false ) </li></ul>import org.eclipse.ocl.examples.xtext.base.utilities.NoEObjectCompositeEValidator; import org.eclipse.xtext.validation.CompositeEValidator; /** * Use this class to register components to be used at runtime / without the Equinox extension registry . */ public class StatesRuntimeModule extends org.eclipse.ocl.tutorial.eclipsecon2011.AbstractStatesRuntimeModule { public Class<? extends CompositeEValidator> bindCompositeEValidator() { return NoEObjectCompositeEValidator. class ; } }
  182. 182. Embedded Constraints 1 /org.eclipse.ocl.tutorial.eclipsecon2011.oclstates/model/OCLStates.ecore class Statemachine { invariant HasInitialState( 'No initial state' ): ( states )-> exists ( s : State | s . initial ); attribute _'initial' : Boolean [?] { ordered }; attribute name : String [?] { ordered }; attribute value : ecore :: EInt [?] { ordered }; property events : Event [*] { ordered composes }; property states : State [*] { ordered composes }; }
  183. 183. Embedded Constraints 2 invariant UniqueInitialState( 'There is more than one initial state' ): initial implies ((( statemachine ). states )-> select ( initial ))-> size () = 1 ; invariant NameIsLeadingUpperCase( ''' + name + '' must be Leading Uppercase' ): let firstLetter : String = ( name ). substring ( 1 , 1 ) in firstLetter . toUpperCase () = firstLetter ; invariant NameLength( ''' + name + '' has ' + (( name ). size ()). toString () + ' characters when at least 4 wanted' ): ( name ). size () >= 4 ; invariant EveryEventIsHandled( let allEvents : Set ( Event ) = (( statemachine ). events )-> asSet () in let myEvents : Set ( Event ) = (( self . transitions )-> collect ( event ))-> asSet () in ( allEvents - myEvents )-> iterate ( e : Event ; s : String = 'The following events are not handled:' | s + ' ' + e . name )): let allEvents : Set ( Event ) = (( statemachine ). events )-> asSet () in let myEvents : Set ( Event ) = (( self . transitions )-> collect ( event ))-> asSet () in ( allEvents - myEvents )-> isEmpty (); property statemachine : Statemachine [?] { derived } { derivation : ( oclContainer ()). oclAsType ( Statemachine ); }
  184. 184. Enable Custom Validation Messages <ul><li>(Vote for EMF Bug 337792 to obviate this)
  185. 185. Use an extended Validator API for
  186. 186. /emf-gen/org/eclipse/ocl/tutorial/eclipsecon2011/ oclstates/util/OCLStatesValidator.java </li></ul>* @generated not */ public class OCLStatesValidator extends OCLinEcoreEObjectValidator { <ul>/META-INF/MANIFEST.MF <li>add dependency on </li></ul>org.eclipse.ocl.examples.xtext.oclinecore
  187. 187. Embedded OCL Exercises 1 In Main Eclipse <ul><li>Open model/OCLStates.ecore with OCLinEcore (in org.eclipse.ocl.tutorial.eclipsecon2011.oclstates) </li><ul><li>edit and verify semantic checking
  188. 188. add additional state </li></ul><li>Open model/OCLStates.ecore with Sample Ecore </li><ul><li>browse to see xxx and xxx$message EAnnotations </li></ul></ul>
  189. 189. Embedded OCL Exercises 2 In Run-time Eclipse <ul><li>open simple.oclstates (in org.eclipse.ocl.tutorial.eclipsecon2011.states2classes) </li><ul><li>verify invariant checking </li></ul></ul><ul><li>Exit Run-time Eclipse
  190. 190. Add a new constraint to OCLStates.ecore
  191. 191. Regenerate Java with OCLStates.genmodel
  192. 192. In Run-time Eclipse open simple.oclstates </li><ul><li>verify new invariant </li></ul></ul>
  193. 193. Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - independent validation language exercises
  194. 194. OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul><li>OCL in Acceleo - navigation/evaluation aid exercises </li></ul><li>QVTo State Flattening transformation </li><ul><li>OCL in QVT Operational - navigation/evaluation aid exercises </li></ul></ul>
  195. 195. Acceleo with integral OCL Main Eclipse <ul><ul><li>meta-modeling </li></ul></ul><ul><li>States Editor </li></ul><ul><li>states.ecore Model </li></ul>Run-time Eclipse <ul><ul><li>modeling </li></ul></ul><ul><li>Edit generate.mtl
  196. 196. or simple.states
  197. 197. Close simple.states
  198. 198. Run Generate Simple States
  199. 199. Inspect *.java </li></ul>generate.mtl
  200. 200. Use of OCL for M2T <ul><li>Acceleo is the Eclipse realisation of MOFM2T </li><ul><li>OMG specification for Model to Text transformation
  201. 201. exploits/extends OCL for core expressions </li></ul></ul><ul><li>Tutorial Example </li><ul><li>generate Java classes from State Model
  202. 202. one class for the State Machine
  203. 203. one class for each State
  204. 204. one method for each Event </li></ul></ul>
  205. 205. States to Java Classes module &quot;simple.states&quot; statemachine Machine { events START STOP; state Start { START => Start } initial state Stop { STOP => Stop } } package simple.states; public class Stop implements Machine.State { private final Machine sm ; public Stop(Machine sm) { this . sm = sm; } public void doSTOP() { sm .setState(STATES. STATE_Stop ); } } package simple.states; public class Machine { public interface State { public void doSTART(); public void doSTOP(); enum STATES { STATE_Start , STATE_Stop } }
  206. 206. Xtext files as Eclipse Models <ul><li>Xtext maintains Concrete Syntax text files </li><ul><li>behave as EMF models
  207. 207. Open With->Sample Ecore Model Editor </li><ul><li>shows CST as an Ecore model </li></ul></ul></ul><ul><li>XtextResourceFactory not XMIResourceFactory </li><ul><li>defined by plugin.xml </li><ul><li>works in nested Eclipse
  208. 208. doesn't work standalone </li></ul></ul></ul>
  209. 209. Xtext files as Standalone Models <ul><li>XtextResourceFactory not registered </li><ul><li>invoke XXXStandaloneSetup.doSetup() </li><ul><li>injected references to </li><ul><li>org.eclipse.xtext
  210. 210. org.apache.log4j </li></ul></ul></ul></ul><ul><ul><li>either export from 'editor' plugin (Bug 339083) </li><ul><li>edit MANIFEST.mf to re-export </li></ul><li>or import to consumer plugin </li><ul><li>edit MANIFEST.mf to add dependencies </li></ul></ul></ul>
  211. 211. Acceleo configuration to use Xtext /org.eclipse.ocl.tutorial.eclipsecon2011.states2classes/ src/org/eclipse/ocl/tutorial/eclipsecon2011/states2classes/files/Generate.java <ul><li>Acceleo generator runs Standalone
  212. 212. Edit 'Generate.java'.registerPackages </li><ul><li>invoke 'editor' Standalone setup </li></ul></ul>* @generated NOT */ @Override public void registerPackages(ResourceSet resourceSet) { super .registerPackages(resourceSet); StatesStandaloneSetup . doSetup (); // resourceSet.getPackageRegistry().put( // org.eclipse.xtext.example.fowlerdsl.FowlerdslPackage.eINSTANCE.getNsURI(), // org.eclipse.xtext.example.fowlerdsl.FowlerdslPackage.eINSTANCE); }
  213. 213. Acceleo Control Logic [template public generate(m : Module)] [comment @main /] [for ( e : Statemachine | m.machines) ] [file (e.name + '.java' , false, 'UTF-8' ) ] [ generateStatemachineClass(m, e) /] [/file] [for ( s : State | e.states) ] [file (s.name + '.java' , false, 'UTF-8' ) ] [ generateStateClass(m, e, s) /] [/file] [/for] [/for] [/template] <ul><li>Whitespace copied to output; limited indentation
  214. 214. OCL: e.name + '.java' e.states </li></ul>
  215. 215. Acceleo Text Template 1 package simple.states; public class Stop implements Machine.State { private final Machine sm ; public Stop(Machine sm) { this . sm = sm; } public void doSTOP() { sm .setState(STATES. STATE_Stop ); } } [template public generateStateClass(m : Module, e : Statemachine, s : SimpleState)] package [ m.getPackageName() /] ; public class [ s.getStateClassName() /] implements [ e.name + '.State' /] { private final [ e.getStateMachineClassName() /] sm; public [ s.getStateClassName() /] ( [ e.getStateMachineClassName() /] sm) { this.sm = sm; } [for ( t : Transition | s.transitions) ] public void [ t.event.getEventMethodName() /] () { sm.setState(STATES. [ t.state.getStateEnumName() /] ); } [/for] } [/template] <ul><li>OCL: </li></ul>t.state.getStateEnumName()
  216. 216. Acceleo Queries <ul><li>Queries support modularisation </li><ul><li>repeated sub-expression can be factored out </li></ul></ul>[query getStateEnumName(s : State) : String = 'STATE_' + s.name /] [query getInitialState(e : Statemachine) : State = e.states->any(initial) /]
  217. 217. package simple.states; public class Machine { public interface State { public void doSTART(); public void doSTOP(); enum STATES { STATE_Start , STATE_Stop } } Acceleo Text Template 2 [template public generateStatemachineClass(m : Module, e : Statemachine)] package [ m.getPackageName() /] ; public class [ e.getStateMachineClassName() /] { public interface State { [for ( v : Event | e.events) ] public void [ v.getEventMethodName() /] (); [/for] enum STATES { [for ( s : State | e.states) separator ( ',n' ) after ( 'n' ) ] [ s.getStateEnumName() /] [/for] } } ... public [ e.getStateMachineClassName() /] () { setState(State.STATES. [ e.getInitialState().getStateEnumName() /] ); } public Machine() { setState(State.STATES. STATE_Stop ); }
  218. 218. Acceleo Demonstration <ul><li>Generate BadStates </li><ul><li>view errors </li></ul><li>Edit badsimple.states to correct a initial error </li><ul><li>exit, generate, inspect </li></ul><li>Edit badsimple.states to correct handled errors </li><ul><li>exit, generate, inspect </li></ul></ul><ul><li>OCL provides model validation </li><ul><li>integration within editor for early diagnosis
  219. 219. protects downstream from garbage in ... </li></ul></ul>
  220. 220. Acceleo Exercises <ul><li>Repeat demonstration
  221. 221. Change the enums from STATE to ENUM prefix
  222. 222. ReLaunch, verify changes
  223. 223. Change enums to all upper case </li><ul><li>hint String::toUpperCase() : String </li></ul><li>Change internal States interface to an abstract class with default no change event methods.
  224. 224. Eliminate the 'EveryStateIsHandled' editor validation
  225. 225. Change brace positions to your own style </li></ul>
  226. 226. Part 2: OCL in combination State Machine Example <ul><li>Xtext Editor for States Model </li><ul><li>OCL document - independent validation language exercises
  227. 227. OCLinEcore - integrated validation language exercises </li></ul><li>Acceleo States to Java Classes transformation </li><ul><li>OCL in Acceleo - navigation/evaluation aid exercises </li></ul><li>QVTo State Flattening transformation </li><ul><li>OCL in QVT Operational - navigation/evaluation aid exercises </li></ul></ul>
  228. 228. QVTo with integral OCL Main Eclipse <ul><ul><li>meta-modeling </li></ul></ul><ul><li>States Editor </li></ul><ul><li>states.ecore Model </li></ul>Run-time Eclipse <ul><ul><li>modeling </li></ul></ul><ul><li>Edit FlattenStates.qvto
  229. 229. or compound.states
  230. 230. Run Flatten States to XMI or Flatten States
  231. 231. Inspect flattened.xmi Inspect flattened.states </li></ul>FlattenStates.qvto
  232. 232. Xtext feature (as output model) <ul><li>QVTo generator runs as a Plugin </li><ul><li>no action needed </li></ul></ul><ul><li>QVTo can transform 'Xtext' to 'Xtext' </li></ul><ul><li>But to-'Xtext' must be strictly valid XMI </li><ul><li>no orphans, no missing objects </li><ul><li>obscure 'cannot write' diagnostics </li></ul><li>use 'Xtext' to *.xmi for debug </li></ul></ul>
  233. 233. QVTo feature (for re-runs) <ul><li>Output mode ill-defined by specification </li><ul><li>implemented as append by M2M/QVTo </li></ul></ul><ul><li>Transformation re-run appends </li><ul><li>additional root object in XMI file </li><ul><li>may be useful for manual comparison of old/new
  234. 234. guarantees non-serializability to Xtext </li></ul></ul></ul><ul><li>Manually delete output file before transformation </li></ul>
  235. 235. Statemachine Flattening Example module &quot;compound.states&quot; statemachine Outer { events START STOP; initial state Start value 4 { START => CompoundA } state Stop value 5 { STOP => CompoundB } compound state CompoundA machine Inner { START => Stop } compound state CompoundB machine Inner { STOP => Stop } } statemachine Inner { events LEFT RIGHT; initial state Left value 1 { LEFT => Right } state Right { RIGHT => Left } } module &quot;flattened.compound.states&quot; statemachine Outer { events STOP RIGHT LEFT START; initial state Start value 4 { START => CompoundA_Left } state Stop value 5 { STOP => CompoundB_Left } state CompoundA_Left value 1 { LEFT => CompoundA_Right START => Stop } state CompoundA_Right { RIGHT => CompoundA_Left START => Stop } state CompoundB_Left value 1 { LEFT => CompoundB_Right STOP => Stop } state CompoundB_Right { RIGHT => CompoundB_Left STOP => Stop } }
  236. 236. root mapping 1 mapping HIER :: Module :: model2FLATModel () : FLAT :: Module { name := 'flattened.' + self . name ; var name2state : Dict ( String , HIER :: State ); var allMachines : Set ( HIER :: Statemachine ) = self . machines . allMachines ()-> asSet (); var allStates : Set ( HIER :: State ) = allMachines. states -> asSet (); var allCompoundStates : Set(HIER::CompoundState) = allStates-> select ( oclIsTypeOf (HIER::CompoundState)) -> collect ( oclAsType (HIER::CompoundState))-> asSet (); var rootMachines : Set(HIER::Statemachine) = allMachines-> select (m : HIER::Statemachine | not allCompoundStates-> exists (machine = m)); machines := rootMachines-> map machine2machine (name2state); }
  237. 237. root mapping 2 var allCompoundStates : Set ( HIER :: CompoundState ) = allStates-> select ( oclIsKindOf ( HIER :: CompoundState )) -> collect ( oclAsType ( HIER :: CompoundState ))-> asSet (); oclIsKindOf(T)...oclAsType(T) ... => filter(T) in Acceleo allStates allCompoundStates
  238. 238. root mapping 3 var rootMachines : Set ( HIER :: Statemachine ) = allMachines-> select (m : HIER :: Statemachine | not allCompoundStates-> exists ( machine = m)); allMachines allCompoundStates
  239. 239. transitive closure 1 <ul><li>Find all self and all sub-statemachines - states.machine for derived CompoundStates </li></ul>
  240. 240. transitive closure 2 <ul><li>Warning because closure() not in OCL 2.0, 2.2 </li><ul><li>resolution adopted in OCL 2.3
  241. 241. due for approval this week OMG, Arlington, VA </li></ul></ul><ul><li>Eclipse OCL has and continues to pioneer solutions to OMG OCL specification issues </li></ul>
  242. 242. machine 2 machine mapping mapping HIER :: Statemachine :: machine2machine (name2state : Dict ( String , HIER :: State )) : FLAT :: Statemachine { var exitTransitions : Dict ( FLAT :: Event , FLAT :: State ); name := self . name ; events := self . allMachines (). events -> asSet (). map event2event (); states := self . states . map state2states (name2state, Sequence {}); self . states . map state2transitions (name2state, Sequence {}, exitTransitions); } <ul><ul><li>pass 1 all events flattened
  243. 243. pass 2 all states flattened </li><ul><li>name2state cache, hierarchy Sequence </li></ul><li>pass 3 transitions added to flattened states </li><ul><li>to-compound => initial
  244. 244. from-compound => all exitTransitions </li></ul></ul></ul>
  245. 245. QVTo Demonstration <ul><li>Run Flatten States </li><ul><li>inspect flattened.states </li></ul><li>Run Flatten States to XMI </li><ul><li>inspect flattened.xmi </li></ul><li>Run Flatten States again </li><ul><li>show confusion </li></ul><li>Delete flattened.states
  246. 246. Run Flatten States </li><ul><li>inspect flattened.states </li></ul></ul>
  247. 247. QVTo Exercises In Runtime Eclipse <ul><li>repeat demonstration
  248. 248. regenerate
  249. 249. add more hierachical states
  250. 250. regenerate
  251. 251. Run Generate Flattened States </li><ul><li>inspect flattened Java </li></ul><li>inspect Flattened.qvto
  252. 252. change Flattened state names from </li></ul>x_y to y_x
  253. 253. Old Eclipse OCL <ul><li>Old code - stable from Helios, MDT/OCL 3.0 </li><ul><li>LPG-based
  254. 254. org.eclipse.ocl.ecore, org.eclipse.ocl.uml </li><ul><li>templates for Ecore/UML binding </li></ul><li>no editors
  255. 255. delegate URI: </li><ul><li>http://www.eclipse.org/emf/2002/Ecore/OCL </li></ul></ul></ul><ul><ul><li>limited bug fixing </li><ul><li>fundamental problems hard to address </li></ul></ul></ul>
  256. 256. New Eclipse OCL <ul><li>Helios, MDT/OCL 3.0 Examples </li><ul><li>Partial functionality Xtext-based editors </li></ul><li>Indigo, MDT/OCL 3.1 Examples </li><ul><li>Full functionality Xtext-based editors
  257. 257. Modelled Extensible Library and Evaluator </li><ul><li>org.eclipse.ocl.examples.pivot
  258. 258. no templates, 'exact' UML-aligned OMG compliance </li></ul><li>delegate URI: </li><ul><li>http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot </li></ul></ul><li>'Juno', MDT/OCL 3.2 Core + Tools </li><ul><li>org.eclipse.ocl.pivot </li></ul></ul>
  259. 259. Summary OCL: simple, sensible, powerful modeling language Useable in isolation <ul><li>Java API, Ecore elaboration, EMF delegates </li></ul>Useavle in combination <ul><li>Xtext validation, Acceleo, QVTo core </li></ul>Tooling available <ul><li>Evaluation Console
  260. 260. OCL Editors
  261. 261. ? Papyrus Editor </li></ul>
  262. 262. OCL Resources <ul><li>OCL 2.2 Specification http://www.omg.org/spec/OCL/2.2 </li><ul><ul><li>Clause 7 is quite readable (many typos) </li></ul></ul></ul><ul><li>The Object Constraint Language: Getting Your Models Ready For MDA Jos B. Warmer, Anneke Kleppe </li></ul><ul><li>Eclipse MDT/OCL project http://www.eclipse.org/projects/project_summary.php?projectid=modeling.mdt.ocl </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×