hello world! an instructive case for ttc 2011 with emf henshin › staff › pvgorp › events ›...

31
Hello World! An Instructive Case for TTC 2011 with EMF Henshin Stefan Jurack 1 and Johannes Tietje 2 1 Universit¨at Marburg, Germany [email protected] 2 Technische Hochschule Mittelhessen, Gießen, Germany [email protected] Abstract. In this paper we present Henshin, a tool set for rule-based EMF model transfor- mation, along the Hello World case study of the TTC 2011. It is especially intended to show basic language concepts and constructs by means of basic operations implemented using our tool. Nevertheless, we also introduce even more sophisticated features of Henshin, e.g., control structures and parameter passing. Our transformation language is mainly presented using dedicated visual editors. 1 Introduction Model-driven software development (MDD) is a promising paradigm in software engineering since models are ideal means to cope with increasing complexity of software systems. As central arti- facts, models may be modified over time or translated into other models. Such tasks are typically performed using model transformations. We call transformations directly working on a model, i.e., without creating copies, in-place model transformations while translations are called out-place since they usually keep source models untouched and produce new models or code. The Eclipse Modeling Framework (EMF) [1] has evolved to a well-known and widely used tech- nology. It is obviously promising to provide model transformation support for EMF. In this paper we give an introduction to Henshin [2, 3], a declarative transformation language and tool envi- ronment directly operating on EMF models. As a successor of EMF Tiger [4, 5] it is based on the well-founded theory of algebraic graph transformation but extends the transformation language of EMF Tiger considerably. Basic transformation artifacts are pattern-based rules which can be enriched by nestable application conditions and attribute computation based on JavaScript. Addi- tionally, a number of predefined control structures, called transformation units, allow nesting and reuse of rules in a modular way. Among several editors, Henshin does also provide a state space generator to support reasoning by model checking which is, however, not further investigated here. The rest of the paper is structured as follows: In Sec. 2 we give an appropriate overview of Henshin’s transformation language and concepts. A representative subset of our HelloWorld solution is presented in Sec. 3 followed by a conclusion. 2 Henshin Transformation in a Nutshell In the following Henshin transformation concepts are briefly presented. Due to space constraints we omit additional simulating examples at this point and rather refer to the subsequent section containing a number of examples and comments.

Upload: others

Post on 08-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Hello World! An Instructive Case for TTC 2011with EMF Henshin

Stefan Jurack1 and Johannes Tietje2

1 Universitat Marburg, [email protected]

2 Technische Hochschule Mittelhessen, Gießen, [email protected]

Abstract. In this paper we present Henshin, a tool set for rule-based EMF model transfor-mation, along the Hello World case study of the TTC 2011. It is especially intended to showbasic language concepts and constructs by means of basic operations implemented usingour tool. Nevertheless, we also introduce even more sophisticated features of Henshin, e.g.,control structures and parameter passing. Our transformation language is mainly presentedusing dedicated visual editors.

1 Introduction

Model-driven software development (MDD) is a promising paradigm in software engineering sincemodels are ideal means to cope with increasing complexity of software systems. As central arti-facts, models may be modified over time or translated into other models. Such tasks are typicallyperformed using model transformations. We call transformations directly working on a model,i.e., without creating copies, in-place model transformations while translations are called out-placesince they usually keep source models untouched and produce new models or code.

The Eclipse Modeling Framework (EMF) [1] has evolved to a well-known and widely used tech-nology. It is obviously promising to provide model transformation support for EMF. In this paperwe give an introduction to Henshin [2, 3], a declarative transformation language and tool envi-ronment directly operating on EMF models. As a successor of EMF Tiger [4, 5] it is based on thewell-founded theory of algebraic graph transformation but extends the transformation languageof EMF Tiger considerably. Basic transformation artifacts are pattern-based rules which can beenriched by nestable application conditions and attribute computation based on JavaScript. Addi-tionally, a number of predefined control structures, called transformation units, allow nesting andreuse of rules in a modular way. Among several editors, Henshin does also provide a state spacegenerator to support reasoning by model checking which is, however, not further investigated here.

The rest of the paper is structured as follows: In Sec. 2 we give an appropriate overviewof Henshin’s transformation language and concepts. A representative subset of our HelloWorldsolution is presented in Sec. 3 followed by a conclusion.

2 Henshin Transformation in a Nutshell

In the following Henshin transformation concepts are briefly presented. Due to space constraintswe omit additional simulating examples at this point and rather refer to the subsequent sectioncontaining a number of examples and comments.

Page 2: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

The Henshin transformation meta-model is an EMF model itself. Its main artifacts are rulesconsisting of a left-hand and right-hand side graph (LHS and RHS respectively) specifying modelpatterns on abstract syntax level. The LHS describes the pattern to be found while the RHSdescribes the resulting pattern. Node mappings between LHS and RHS declare identity, i.e., suchnodes are preserved. An LHS may contain positive and negative application conditions (PAC andNAC respectively) being patterns again. While PACs can be considered analogously to the LHS,NACs specify the non-existence of model patterns. Their true power emerges in combination withlogical formulas (AND, OR, NOT) and nesting even allowing to define conditions over conditions.Furthermore, attribute conditions can be defined for rules whose expressions are evaluated by aJavaScript engine at runtime.

An important concept in our approach are transformation units providing a well-defined oper-ational semantics to control the order of rule application. Transformation units may contain othertransformation units. Note that a rule can be considered an atomic transformation unit corre-sponding to its single application. Henshin offers a predefined set of units: The IndependentUnitprovides a non-deterministic rule choice, the PriorityUnit specifies priorities in which rules arechosen to be applied, a counted application is provided by the CountedUnit at which a count valueof -1 means “as often as possible”, SequentialUnits apply rules in a sequential order and perform arollback if any of its rules cannot be applied, and ConditionalUnits allow to specify an if conditionwith a corresponding then and else part. Last but not least, AmalgamationUnits can be consideredspecial units to specify forall-operations on recurring model patterns. Here, a kernel rule is appliedonce with each multi rule being applied as often as possible taking the match of the kernel ruleinto account.

Not only the control flow can be specified but the object flow as well. To this end, parameterspass objects and values from one unit to another one controlling complex transformations in aconvenient way.

Currently three different views on Henshin transformation models are provided by three differ-ent editors. A tree-based editor provides a view very close to the internal model structure, while twoother editors provide graph-like visualizations. One visual editor, in the following called complex-rule editor, shows LHS, RHS and application conditions in separate views. The second visual editor,called integrated-rule editor, shows rules in an integrated manner in a single view, i.e., graph ele-ments to be preserved, created, and deleted, due to their occurrence in and mappings between LHSand RHS are denoted by corresponding stereotypes, e.g. <<preserve>>. Each editor has its ownassets and drawbacks. For example, on the one hand the integrated-rule editor is able to representsimple rules in a very compact and clear fashion. On the other hand, more complex rules with,e.g., a number of nested application conditions and units being connected by parameters are bettervisualized in the complex-rule editor. It is conceivable to integration these editors combining theiradvantages and capabilities which is, however, future work.

Henshin has been designed in a modular way leading to an independent transformationengine which can be freely integrated into any Java project relying on EMF models. ClassesRuleApplication and UnitApplication provide appropriate methods to select and apply rulesand transformation units, respectively.

3 Case Study Tasks Solved by Henshin

In the following we present a representative subset of our solutions to all the different Hello Worldcase study tasks. As we have several editors available to show the rules we choose the one thatsuits best in each individual case. Please note, that each rule or unit contains a parameter, e.g.,

2

Page 3: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

result or counter, used to return and display/persist the outcome on code level (see Appendix C).Note furthermore, that in some cases we exploit a Henshin self-contained helper structure calledtrace model in order to keep track of the process of a transformation. This model consists of classTrace which might be connected to any EMF object by two references, source and target.

Task 2.1: Hello World! Figure 1 shows the solution for the simple and the extended Hello Worldtasks by means of our integrated-rule editor. Each rounded rectangle represents a rule with itsname at the top and its content below. Rule CreateSimple contains a node of type Greeting

assigned to parameter result. Nodes may have attributes with values assigned, e.g., text="HelloWorld". Stereotype <<create>> denotes that this node is to be created which is equivalent tothis node being in an RHS without any mappings to the LHS. Rule CreatedExtended creates amore complex greeting structure also introducing new edges and rule M2T performs a model-to-text transformation in the sense that the greeting structure remains unchanged while a newStringResult object is created. Note that we use additional parameters here, preTxt and postTxt,which are filled with its related values during matching and which are then used to calculate a newattribute value.

CreateSimple

<<create>>result:Greeting

text="Hello World"

CreateExtended

<<create>>result:Greeting

<<create>>:GreetingMessage

text="Hello"

<<create>>:Person

name="TTC Participants"

person<<create>>

greetingMessage<<create>>

M2T

<<preserve>>:Greeting

<<preserve>>:GreetingMessage

text=preTxt

<<preserve>>:Person

name=postTxt

<<create>>result:StringResult

result=preTxt + " " + postTxt

person <<preserve>>greetingMessage<<preserve>>

Fig. 1. Rules according to the Hello World! subtasks.

Task 2.2: Count Matches with certain Properties is implemented by five structurally similar se-quential units while CountDanglingEdges is exemplarily illustrated in Fig. 23. Each sequentialunit contains the rule CreateCounterObject as first sequence item which only creates an initialIntResult object used for counting. Note that all sequential units really use the same rule, i.e.,this rule has been reused a lot. The actual counting is performed by the subsequent sequenceitem being a counted unit (count=-1) and its contained rule. In fact the contained rule increasethe counter by one being applied as often as possible due to the counted unit, i.e., all sequentialunit essentially differ in this rule. The structure of the unit CountDanglingEdges is depicted inthe lower right area of Fig. 2. Furthermore, its essential rule CountDanglingEdges Increase is alsoillustrated in the upper area, showing the LHS and RHS of the rule (upper right) on the one hand,and the applications conditions (left) on the other hand. The application condition patterns incombination with their logical formula require for a match, that the Edge has not been marked bya Trace object and that the Edge must not have a target or a source. Looking at the RHS, eachEdge fulfilling these conditions leads to an increasing counter and a new Trace object serving asmarker.

3 This figure and the following of this kind are clippings and compositions of complex-rule editor viewswhich have been created in favor of space savings.

3

Page 4: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 2. Rules for counting of dangling edges with either source or target node missing (left and top right)and an overview of the related transformation unit structure (bottom right).

Task 2.3: Reverse Edges This task is solved by a counted unit (count=-1) applying rule Re-verseOneEdge (cf. Fig. 3) as often as possible. Edges and Nodes are preserved during the trans-formation while the references are not: Two are created and two others are deleted. In addition,a Trace object is used as marker again in combination with a negative application conditionforbidding such a marker to exist already in the given context.

ReverseOneEdge

<<preserve>>:Node

<<preserve>>:Node

<<preserve>>:Edge

<<create>>:Trace

<<forbid:Already reversed>>:Trace

trg<<delete>>

source<<forbid:Already reversed>>

src

<<create>>

trg

<<create>>

src<<delete>>

source<<create>>

Fig. 3. Rule for reversing an edge’s direction.

Task 2.4: Simple Migration In its simple form the solution for this task uses techniques alreadyknown. Contained in a sequential unit, at first the rule CreateNewGraph creates a new Graph

object as initial root object. Afterwards two successive counted units follow, one containing therule dealing with the migration of nodes and the other containing the rule adding correspondingedges. Again, Trace objects mark elements already translated and a parameter carries the resultinggraph to be returned by the transformation process.

The solution for the extended task is realized a bit trickier with the help of an amalgamationunit. We skip this here and rather refer to the following task using this special kind of unit as well.

Task 2.5: Delete Node with Specific Name and its Incident Edges The delete task has been solvedusing the amalgamation unit DeleteNodeN1Simple with rule DeleteNodeN1 as kernel rule, andmulti rules DeleteNodeN1 being the identical rule to the kernel rule, DeleteIncomingRef, andDeleteOutgoingRef, shown in Fig. 4. While the kernel rule deals with the deletion of the node “n1”itself, the multi rules delete linked references. The semantics of an amalgamation unit is, that thekernel rule matches once while each multi rule is matched as often as possible taking the match of

4

Page 5: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

the kernel rule into account. Therefore, each multi rule contains the same4 elements as the kernelrule’s LHS and RHS and furthermore elements belonging to the multi rule itself, denoted by doublerectangles in Fig. 4. Consequently, the modification defined in the kernel rule is applied only oncewhile the modifications defined in each multi rule are applied as much as matches can be found.For further information on amalgamation units please see [6].

The extended task is realized in a similar way and not shown here.

Fig. 4. Kernel rule DeleteNodeN1 (left) and two multi rules (right) being part of the amalgamation unitDeleteNodeN1Simple.

Task 2.6: Insert Transitive Edges The last task is solved again by a single counted unit whichapplies its child rule InsertTransitiveEdge as often as possible to create a single transitive edge foran already existing connection between three Node objects. This is quite analog to was has beenexplained above and therefore not presented in detail here.

4 Conclusion

In this paper, Henshin was briefly introduced to solve a set of simple example tasks implementingbasic operations on models given by the Hello World case study of TTC 2011. Our solutionis especially characterized by pattern-based rules being structured by transformation unit. Theimplementation is made available under SHARE via link http://is.tm.tue.nl/staff/pvgorp/

share/?page=ConfigureNewSession&vdi=Ubuntu_10.04_TTC11_Henshin.vdi.

References

1. Steinberg, D., Budinsky, F., Paternostro, M., Merks, E.: EMF Eclipse Modeling Framework (SecondEdition). Addison-Wesley (2009)

2. Arendt, T., Biermann, E., Jurack, S., Krause, C., Taentzer, G.: Henshin: Advanced Concepts and Toolsfor In-Place EMF Model Transformation. In: Proc. of 13th Int. Conference on Model Driven EngineeringLanguages and Systems (MoDELS 2010). LNCS, Springer (2010)

3. Henshin. http://www.eclipse.org/modeling/emft/henshin (2010)4. Biermann, E., Ermel, C., Lambers, L., Prange, U., Taentzer, G.: Introduction to AGG and EMF Tiger

by modeling a conference scheduling system. Software Tools for Technology Transfer (2010)5. TFS-Group, TU Berlin: EMF Tiger. (2009) http://tfs.cs.tu-berlin.de/emftrans.6. Biermann, E., Ermel, C., Taentzer, G.: Lifting Parallel Graph Transformation Concepts to Model

Transformation based on the Eclipse Modeling Framework. ECEASST 26 (2010) http://easst.org/

eceasst.

4 In fact the elements are not the same but different elements in the transformation model. However, amapping between them declares identity.

5

Page 6: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

A Overview of the Transformation Algorithms

Note that in this section the transformation unit structures are shown at first with referred rulesbeing denoted by a bluish icon. In the subsequent section all rules are shown in full.

Fig. 5. Transformation unit CountLoopingEdges

Fig. 6. Transformation unit CountNodes

Fig. 7. Transformation unit CountIsolatedNodes

6

Page 7: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 8. Transformation unit CountCircles

Fig. 9. Transformation unit CountDanglingEdges

Fig. 10. Transformation unit ReverseEdges

Fig. 11. Transformation unit SimpleMigration

Fig. 12. Transformation unit SimpleMigrationExt

7

Page 8: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 13. Transformation unit DeleteNodeN1Simple

Fig. 14. Transformation unit DeleteNodeN1WithAllEdges

Fig. 15. Transformation unit InsertTransitiveEdges

8

Page 9: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

B Rules

Fig. 16. Rule CreateSimple

Fig. 17. Rule CreateExtended

Fig. 18. Rule M2T

9

Page 10: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 19. Rule CreateCounterObject

Fig. 20. Rule CountNodes Increase

Fig. 21. Rule CountLoopingEdges Increase

10

Page 11: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 22. Rule CountIsolatedNodes Increase

Fig. 23. Rule CountCircles Increase

Fig. 24. Rule CountDanglingEdges Increase

11

Page 12: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

ReverseOneEdge

<<preserve>>:Node

<<preserve>>:Node

<<preserve>>:Edge

<<create>>:Trace

<<forbid:Already reversed>>:Trace

trg<<delete>>

source<<forbid:Already reversed>>

src

<<create>>

trg

<<create>>

src<<delete>>

source<<create>>

Fig. 25. Rule ReverseOneEdge

Fig. 26. Rule CreateNewGraph

Fig. 27. Rule MigrateNode

12

Page 13: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 28. Rule MigrateEdge

Fig. 29. Rule SelectNode

13

Page 14: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 30. Rule MigrateOutgoingEdge

Fig. 31. Rule DeleteNodeN1

Fig. 32. Rule DeleteIncomingRefs

14

Page 15: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 33. Rule DeleteOutgoingRefs

15

Page 16: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 34. Rule DeleteIncodmingEdges

Fig. 35. Rule DeleteOutgoingEdges

C Java Code of the Transformation Application

This section show the code to trigger all transformations. Each task is represented by a classcontaining related code. Note that the classes contain some redundant code in order to allowthe reader to concentrate on certain parts without hiding functionality by inheritance and callhierarchies.

Listing 1.1. Abstract transformation base class

1 package de . unimarburg . swt . t tc2011 ;2

3 import java . i o . F i l e ;4 import java . i o . IOException ;5 import java . u t i l . HashMap ;6

7 import org . e c l i p s e . emf . common . u t i l . URI ;8 import org . e c l i p s e . emf . e co re . EObject ;

16

Page 17: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

Fig. 36. Rule InsertTransitiveEdges

9 import org . e c l i p s e . emf . e co re . r e s ou r c e . Resource ;10 import org . e c l i p s e . emf . e co re . r e s ou r c e . ResourceSet ;11 import org . e c l i p s e . emf . e co re . r e s ou r c e . impl . ResourceSetImpl ;12 import org . e c l i p s e . emf . e co re . xmi . XMIResource ;13 import org . e c l i p s e . emf . e co re . xmi . impl . EcoreResourceFactoryImpl ;14 import org . e c l i p s e . emf . e co re . xmi . impl . XMIResourceFactoryImpl ;15 import org . e c l i p s e . emf . henshin . model . impl . HenshinPackageImpl ;16 import org . e c l i p s e . emf . henshin . model . r e s ou r c e . HenshinResourceFactory ;17 import org . e c l i p s e . emf . henshin . t r a c e . impl . TracePackageImpl ;18

19 /∗∗20 ∗ This f i l e s conta in s some gene ra l methods and v a r i a b l e s use again and

again .21 ∗ Al l other t rans fo rmat ion r e l a t e d c l a s s e s i n h e r i t from that to use i t s

g iven22 ∗ t o o l s e t as l oad ing and sav ing .23 ∗24 ∗ @author Ste fan Jurack25 ∗26 ∗/27 public abstract class ATrafo {28

29 public stat ic f ina l St r ing MODELS PATH = "HelloWorld/models/" ;30 public stat ic f ina l St r ing HENSHIN PATH = "HelloWorld/henshin/" ;31 public stat ic f ina l St r ing OUTPUT PATH = "HelloWorld/output/" ;32

33 /∗∗34 ∗ The shared r e sou r c e s e t f o r l oad ing XMI f i l e s .35 ∗/36 protected f ina l ResourceSet r e s ou r c eSe t = new ResourceSetImpl ( ) ;37

38 /∗∗39 ∗ To be overr idden !40 ∗/

17

Page 18: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

41 public void s t a r t ( ) {42 // P r e r e q u i s i t e s43 i n i t i a l i z e F a c t o r i e s ( ) ;44 // −−−−−−45 }// s t a r t46

47 /∗∗48 ∗ Loads the model and re tu rn s the root element .49 ∗50 ∗ @param modelFilePath51 ∗ @return52 ∗/53 protected EObject loadModel ( S t r ing modelFilePath ) {54 URI modelUri = URI . createFi l eURI (new

F i l e ( modelFilePath ) . getAbsolutePath ( ) ) ;55 Resource modelResource = re sou r c eSe t . getResource ( modelUri ,

true ) ;56 i f ( modelResource . isLoaded ( ) ) {57 modelResource . unload ( ) ; // make sure that we work

with a v i r g i n58 //

in s t anceo fthemodel

59 try {60 modelResource . load ( null ) ;61 } catch ( IOException e ) {62 }// try catch63 }// i f64 return modelResource . getContents ( ) . get (0 ) ;65 }// loadModel66

67 /∗∗68 ∗ Saves the content o f the g iven <code>root</code> {@link EObject}

to the69 ∗ s p e c i f i e d f i l e path .70 ∗71 ∗ @param f i l ename72 ∗ @param root73 ∗/74 protected void saveModel ( S t r ing f i l ename , EObject root ) {75 URI modelUri = URI . createFi l eURI (new F i l e (OUTPUT PATH +

f i l ename ) . getAbsolutePath ( ) ) ;76 Resource r e s = re sou r c eSe t . c reateResource ( modelUri ) ;77 HashMap<Str ing , Object> opts = new HashMap<Str ing ,

Object >() ;78 opts . put ( XMIResource .OPTION SCHEMA LOCATION, true ) ;79 try {80 r e s . getContents ( ) . add ( root ) ;81 r e s . save ( opts ) ;

18

Page 19: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

82 } catch ( IOException e ) {83 e . pr intStackTrace ( ) ;84 }// try catch85 }// saveModel86

87 /∗∗88 ∗ I n i t i a l i z e s r e l a t e d f a c t o r i e s and packages .89 ∗/90 protected void i n i t i a l i z e F a c t o r i e s ( ) {91 Resource . Factory . Reg i s t ry .INSTANCE. getExtensionToFactoryMap ( ) . put ("ecore" ,92 new EcoreResourceFactoryImpl ( ) ) ;93 Resource . Factory . Reg i s t ry .INSTANCE. getExtensionToFactoryMap ( ) . put ("xmi" ,94 new XMIResourceFactoryImpl ( ) ) ;95 Resource . Factory . Reg i s t ry .INSTANCE. getExtensionToFactoryMap ( ) . put ("henshin" ,96 new HenshinResourceFactory ( ) ) ;97

98 HenshinPackageImpl . i n i t ( ) ;99 TracePackageImpl . i n i t ( ) ;

100 }// i n i t i a l i z e F a c t o r i e s101

102 }// c l a s s

Listing 1.2. Count matches transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;7 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;8 import org . e c l i p s e . emf . henshin . model . TransformationUnit ;9

10 /∗∗11 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld task at

the TTC12 ∗ 2011.<br>13 ∗ Task 2 .214 ∗15 ∗ @author Ste fan Jurack16 ∗17 ∗/18 public class CountMatchesTrafo extends ATrafo {19

20 public stat ic f ina l St r ing INSTANCE GRAPH1 = MODELS PATH +"Graph1.xmi" ;

21

22 public stat ic f ina l St r ing HENSHIN COUNT MATCHES = HENSHIN PATH23 + "countMatches.henshin" ;24

25 /∗26 ∗ ( non−Javadoc )

19

Page 20: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

27 ∗28 ∗ @see de . unimarburg . swt . t tc2011 . ATrafo#s t a r t ( )29 ∗/30 @Override31 public void s t a r t ( ) {32 super . s t a r t ( ) ; // p r e r e q u i s i t e s33

34 System . out . p r i n t l n ("Task 2.2" ) ;35

36 performCountNumberOfNodes ( ) ;37 performCountLoopingEdges ( ) ;38 performCountIsolatedNodes ( ) ;39 per formCountCirc les ( ) ;40 performCountDanglingEdges ( ) ;41

42 }// s t a r t43

44 private void performCountNumberOfNodes ( ) {45 St r ing unitName = "CountNodes" ;46 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN COUNT MATCHES) ;47 EObject rootObject = loadModel (INSTANCE GRAPH1) ;48 EmfGraph emfGraph = new EmfGraph ( ) ;49 emfGraph . addRoot ( rootObject ) ;50 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;51 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;52 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;53 boolean s u c c e s s = unitApp . execute ( ) ;54 i f ( s u c c e s s ) {55 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("counter" ) ;56 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;57 } else {58 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;59 }60 }// performCountNumberOfNodes61

62 private void performCountLoopingEdges ( ) {63 St r ing unitName = "CountLoopingEdges" ;64 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN COUNT MATCHES) ;65 EObject rootObject = loadModel (INSTANCE GRAPH1) ;66 EmfGraph emfGraph = new EmfGraph ( ) ;67 emfGraph . addRoot ( rootObject ) ;68 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;69 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;70 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;71 boolean s u c c e s s = unitApp . execute ( ) ;72 i f ( s u c c e s s ) {

20

Page 21: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

73 EObject r e s u l t = ( EObject )unitApp . getParameterValue ("counter" ) ;

74 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;75 } else {76 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;77 }78 }// performCountLoopingEdges79

80 private void performCountIsolatedNodes ( ) {81 St r ing unitName = "CountIsolatedNodes" ;82 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN COUNT MATCHES) ;83 EObject rootObject = loadModel (INSTANCE GRAPH1) ;84 EmfGraph emfGraph = new EmfGraph ( ) ;85 emfGraph . addRoot ( rootObject ) ;86 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;87 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;88 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;89 boolean s u c c e s s = unitApp . execute ( ) ;90 i f ( s u c c e s s ) {91 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("counter" ) ;92 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;93 } else {94 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;95 }96 }// performCountIsolatedNodes97

98 private void per formCountCirc les ( ) {99 St r ing unitName = "CountCircles" ;

100 TransformationSystem t s = ( TransformationSystem )loadModel (HENSHIN COUNT MATCHES) ;

101 EObject rootObject = loadModel (INSTANCE GRAPH1) ;102 EmfGraph emfGraph = new EmfGraph ( ) ;103 emfGraph . addRoot ( rootObject ) ;104 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;105 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;106 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;107 boolean s u c c e s s = unitApp . execute ( ) ;108 i f ( s u c c e s s ) {109 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("counter" ) ;110 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;111 } else {112 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;113 }114 }// per formCountCirc les115

21

Page 22: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

116 private void performCountDanglingEdges ( ) {117 St r ing unitName = "CountDanglingEdges" ;118 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN COUNT MATCHES) ;119 EObject rootObject = loadModel (INSTANCE GRAPH1) ;120 EmfGraph emfGraph = new EmfGraph ( ) ;121 emfGraph . addRoot ( rootObject ) ;122 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;123 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;124 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;125 boolean s u c c e s s = unitApp . execute ( ) ;126 i f ( s u c c e s s ) {127 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("counter" ) ;128 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;129 } else {130 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;131 }132 }// performCountDanglingEdges133

134 }// c l a s s

Listing 1.3. Delete nodes transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . common . u t i l . TransformationOptions ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;7 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;8 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;9 import org . e c l i p s e . emf . henshin . model . TransformationUnit ;

10

11 /∗∗12 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld task at

the TTC13 ∗ 2011.<br>14 ∗ Task 2 .515 ∗16 ∗17 ∗ @author Ste fan Jurack18 ∗19 ∗/20 public class DeleteNodeTrafo extends ATrafo {21

22 public stat ic f ina l St r ing INSTANCE GRAPH1 = MODELS PATH +"Graph1.xmi" ;

23

24 public stat ic f ina l St r ing HENSHIN DELETE NODE = HENSHIN PATH25 + "deleteNode.henshin" ;

22

Page 23: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

26

27 @Override28 public void s t a r t ( ) {29 super . s t a r t ( ) ; // p r e r e q u i s i t e s30

31 System . out . p r i n t l n ("Task 2.5" ) ;32

33 performDeleteNode ( ) ;34 performDeleteNodeWithAllEdges ( ) ;35 }// s t a r t36

37 private void performDeleteNode ( ) {38 St r ing unitName = "DeleteNodeN1Simple" ;39 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN DELETE NODE) ;40 EObject rootObject = loadModel (INSTANCE GRAPH1) ;41 EmfGraph emfGraph = new EmfGraph ( ) ;42 emfGraph . addRoot ( rootObject ) ;43 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;44 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;45 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;46 boolean s u c c e s s = unitApp . execute ( ) ;47 i f ( s u c c e s s ) {48 saveModel ( unitName + "Result.xmi" , rootObject ) ;49 } else {50 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;51 }// i f e l s e52 }// performDeleteNode53

54 private void performDeleteNodeWithAllEdges ( ) {55 St r ing unitName = "DeleteNodeN1WithAllEdges" ;56 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN DELETE NODE) ;57 EObject rootObject = loadModel (INSTANCE GRAPH1) ;58 EmfGraph emfGraph = new EmfGraph ( ) ;59 emfGraph . addRoot ( rootObject ) ;60 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;61

62 /∗63 ∗ Note that we use non− i n j e c t i v e matching here . Thi i s not

r equ i r ed f o r64 ∗ ”n1” as the re are no c y c l i c edges , but in gene ra l t h i s

should be65 ∗ enabled f o r such ca s e s ( or a l t e r n a t i v e l y cons ide r ed

during r u l e66 ∗ c r e a t i o n ) .67 ∗/68 TransformationOptions opt ions = new TransformationOptions ( ) ;69 opt ions . s e t I n j e c t i v e ( fa l se ) ;70 eng ine . setOpt ions ( opt ions ) ;

23

Page 24: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

71

72 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;73 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;74 boolean s u c c e s s = unitApp . execute ( ) ;75 i f ( s u c c e s s ) {76 saveModel ( unitName + "Result.xmi" , rootObject ) ;77 } else {78 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;79 }// i f e l s e80 }// performDeleteNodeWithAllEdges81

82 }// c l a s s

Listing 1.4. Hello World transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;7 import org . e c l i p s e . emf . henshin . model . Rule ;8 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;9

10 /∗∗11 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld case at

the TTC12 ∗ 2011 . <br>13 ∗ Task 2 .114 ∗15 ∗16 ∗ @author Ste fan Jurack17 ∗/18 public f ina l class HelloWorldTrafo extends ATrafo {19

20 public stat ic f ina l St r ing HENSHIN HELLOWORLD = HENSHIN PATH21 + "helloworld.henshin" ;22 public stat ic f ina l St r ing HENSHIN HELLOWORLD EXT = HENSHIN PATH23 + "helloworldext.henshin" ;24

25 /∗∗26 ∗27 ∗/28 @Override29 public void s t a r t ( ) {30 super . s t a r t ( ) ; // p r e r e q u i s i t e s31

32 System . out . p r i n t l n ("Task 2.1" ) ;33

34 performHelloWord ( ) ;35 performHelloWordExt ( ) ;

24

Page 25: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

36

37 }// s t a r t38

39 /∗∗40 ∗ Creates an example ” He l lo World” in s t anc e .41 ∗/42 private void performHelloWord ( ) {43

44 TransformationSystem t s = ( TransformationSystem )loadModel (HENSHIN HELLOWORLD) ;

45 EmfGraph emfGraph = new EmfGraph ( ) ;46 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;47 Rule r u l e = t s . findRuleByName ("CreateSimple" ) ;48 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , r u l e ) ;49 boolean s u c c e s s = unitApp . execute ( ) ;50 i f ( s u c c e s s ) {51 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("result" ) ;52 saveModel ("HelloWorldSimple.xmi" , r e s u l t ) ;53 } else {54 System . e r r . p r i n t l n ("’Hello World’ could not be

applied!" ) ;55 }56 }// performHelloWord57

58 /∗∗59 ∗ Creates an example ” He l lo World” in s t anc e f o r the extended

metamodel and60 ∗ subsequent ly performs a M2T trans fo rmat ion on that newly c rea ted

model .61 ∗/62 private void performHelloWordExt ( ) {63

64 TransformationSystem t s = ( TransformationSystem )loadModel (HENSHIN HELLOWORLD EXT) ;

65 EmfGraph emfGraph = new EmfGraph ( ) ;66 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;67 Rule ru l e 1 = t s . findRuleByName ("CreateExtended" ) ;68 UnitAppl i cat ion unitApp1 = new UnitAppl i cat ion ( engine ,

ru l e 1 ) ;69 boolean s u c c e s s = unitApp1 . execute ( ) ;70 i f ( s u c c e s s ) {71 EObject r e s u l t = ( EObject )

unitApp1 . getParameterValue ("result" ) ;72 saveModel ("HelloWorldExt.xmi" , r e s u l t ) ;73 } else {74 System . e r r . p r i n t l n ("’Hello World Ext’ could not be

applied!" ) ;75 return ;76 }// i f e l s e77

25

Page 26: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

78 /∗79 ∗ We do now reuse the EmfGraph/Engine f o r the M2T task

s i n c e then we80 ∗ a l r eady have an appropr ia t e s t a r t graph at hand .81 ∗/82

83 Rule ru l e 2 = t s . findRuleByName ("M2T" ) ;84 UnitAppl i cat ion unitApp2 = new UnitAppl i cat ion ( engine ,

ru l e 2 ) ;85 s u c c e s s = unitApp2 . execute ( ) ;86 i f ( s u c c e s s ) {87 EObject r e s u l t = ( EObject )

unitApp2 . getParameterValue ("result" ) ;88 saveModel ("HelloWorldM2T.xmi" , r e s u l t ) ;89 } else {90 System . e r r . p r i n t l n ("’Hello World M2T’ could not be

applied!" ) ;91 }// i f e l s e92

93 }// performHelloWordExt94

95 }// c l a s s

Listing 1.5. Insert transitive edges transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;7 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;8 import org . e c l i p s e . emf . henshin . model . TransformationUnit ;9

10 /∗∗11 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld task at

the TTC 2011 .12 ∗13 ∗ @author Ste fan Jurack14 ∗15 ∗/16 public class In s e r tTrans i t i v eEdge sTra fo extends ATrafo {17

18 public stat ic f ina l St r ing INSTANCE GRAPH1 = MODELS PATH +"Graph1.xmi" ;

19

20 public stat ic f ina l St r ing HENSHIN INSERT EDGES = HENSHIN PATH21 + "insertTransitiveEdges.henshin" ;22

23 @Override24 public void s t a r t ( ) {25 super . s t a r t ( ) ; // p r e r e q u i s i t e s

26

Page 27: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

26

27 per fo rmInse r tTrans i t i v eEdges ( ) ;28 }// s t a r t29

30 private void per fo rmInse r tTrans i t i v eEdges ( ) {31 St r ing unitName = "InsertTransitiveEdges" ;32 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN INSERT EDGES) ;33 EObject rootObject = loadModel (INSTANCE GRAPH1) ;34 EmfGraph emfGraph = new EmfGraph ( ) ;35 emfGraph . addRoot ( rootObject ) ;36 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;37 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;38 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;39 boolean s u c c e s s = unitApp . execute ( ) ;40 i f ( s u c c e s s ) {41 saveModel ( unitName + "Result.xmi" , rootObject ) ;42 } else {43 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;44 }45 }// per fo rmInse r tTrans i t i v eEdges46

47 }// c l a s s

Listing 1.6. Reverse edges transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;7 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;8 import org . e c l i p s e . emf . henshin . model . TransformationUnit ;9

10 /∗∗11 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld task at

the TTC12 ∗ 2011.<br>13 ∗ Task 2 .314 ∗15 ∗ @author Ste fan Jurack16 ∗17 ∗/18 public class ReverseEdgesTrafo extends ATrafo {19

20 public stat ic f ina l St r ing INSTANCE GRAPH1 = MODELS PATH +"Graph1.xmi" ;

21

22 public stat ic f ina l St r ing HENSHIN REVERSE EDGES = HENSHIN PATH23 + "reverseEdges.henshin" ;

27

Page 28: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

24

25 @Override26 public void s t a r t ( ) {27 super . s t a r t ( ) ; // p r e r e q u i s i t e s28

29 System . out . p r i n t l n ("Task 2.3" ) ;30

31 performReverseEdges ( ) ;32 }// s t a r t33

34 private void performReverseEdges ( ) {35 St r ing unitName = "ReverseEdges" ;36 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN REVERSE EDGES) ;37 EObject rootObject = loadModel (INSTANCE GRAPH1) ;38 EmfGraph emfGraph = new EmfGraph ( ) ;39 emfGraph . addRoot ( rootObject ) ;40 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;41 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;42 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;43 boolean s u c c e s s = unitApp . execute ( ) ;44 i f ( s u c c e s s ) {45 saveModel ( unitName + "Result.xmi" , rootObject ) ;46 } else {47 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;48 }// i f e l s e49 }// performReverseEdges50

51 }// c l a s s

Listing 1.7. Simple migrations transformation class

1 package de . unimarburg . swt . t tc2011 ;2

3 import org . e c l i p s e . emf . e co re . EObject ;4 import org . e c l i p s e . emf . henshin . common . u t i l . EmfGraph ;5 import org . e c l i p s e . emf . henshin . common . u t i l . TransformationOptions ;6 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . EmfEngine ;7 import org . e c l i p s e . emf . henshin . i n t e r p r e t e r . Uni tAppl i cat ion ;8 import org . e c l i p s e . emf . henshin . model . TransformationSystem ;9 import org . e c l i p s e . emf . henshin . model . TransformationUnit ;

10

11 /∗∗12 ∗ This c l a s s performs t rans f o rmat i ons r e l a t e d to the HelloWorld task at

the TTC13 ∗ 2011.<br>14 ∗ Task 2 .415 ∗16 ∗ @author Ste fan Jurack17 ∗18 ∗/

28

Page 29: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

19 public class SimpleMigrat ionsTrafo extends ATrafo {20

21 public stat ic f ina l St r ing INSTANCE GRAPH1 = MODELS PATH +"Graph1.xmi" ;

22

23 public stat ic f ina l St r ing HENSHIN SIMPLE MIGRATION = HENSHIN PATH24 + "simpleMigration.henshin" ;25 public stat ic f ina l St r ing HENSHIN SIMPLE MIGRATION EXT =

HENSHIN PATH26 + "simpleMigrationExt.henshin" ;27

28 @Override29 public void s t a r t ( ) {30 super . s t a r t ( ) ; // p r e r e q u i s i t e s31

32 System . out . p r i n t l n ("Task 2.4" ) ;33

34 performSimpleMigrat ion ( ) ;35 performSimpleMigrationExt ( ) ;36 }// s t a r t37

38 private void performSimpleMigrat ion ( ) {39 St r ing unitName = "SimpleMigration" ;40 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN SIMPLE MIGRATION) ;41 EObject rootObject = loadModel (INSTANCE GRAPH1) ;42 EmfGraph emfGraph = new EmfGraph ( ) ;43 emfGraph . addRoot ( rootObject ) ;44 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;45

46 /∗47 ∗ Note , that we s e t matching to be ”non− i n j e c t i v e ” here .

Thus , we can48 ∗ migrate Edges having equal source and t a r g e t nodes .

A l t e rna t i v e l y , we49 ∗ j u s t could have added one a d d i t i o n a l r u l e dea l i ng with

t h i s s p e c i a l50 ∗ case .51 ∗/52 TransformationOptions opt ions = new TransformationOptions ( ) ;53 opt ions . s e t I n j e c t i v e ( fa l se ) ;54 eng ine . setOpt ions ( opt ions ) ;55

56 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;57 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;58 boolean s u c c e s s = unitApp . execute ( ) ;59 i f ( s u c c e s s ) {60 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("newGraph" ) ;61 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;62 } else {

29

Page 30: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

63 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;64 }65 }// performSimpleMigrat ion66

67 private void performSimpleMigrationExt ( ) {68 St r ing unitName = "SimpleMigrationExt" ;69 TransformationSystem t s = ( TransformationSystem )

loadModel (HENSHIN SIMPLE MIGRATION EXT) ;70 EObject rootObject = loadModel (INSTANCE GRAPH1) ;71 EmfGraph emfGraph = new EmfGraph ( ) ;72 emfGraph . addRoot ( rootObject ) ;73 EmfEngine eng ine = new EmfEngine ( emfGraph ) ;74

75 /∗76 ∗ Note , that we s e t matching to be ”non− i n j e c t i v e ” here .

Thus , we can77 ∗ migrate Edges having equal source and t a r g e t nodes .

A l t e rna t i v e l y , we78 ∗ j u s t could have added one a d d i t i o n a l r u l e dea l i ng with

t h i s s p e c i a l79 ∗ case .80 ∗/81 TransformationOptions opt ions = new TransformationOptions ( ) ;82 opt ions . s e t I n j e c t i v e ( fa l se ) ;83 eng ine . setOpt ions ( opt ions ) ;84

85 TransformationUnit un i t = t s . findUnitByName ( unitName ) ;86 UnitAppl i cat ion unitApp = new UnitAppl i cat ion ( engine , un i t ) ;87 boolean s u c c e s s = unitApp . execute ( ) ;88 i f ( s u c c e s s ) {89 EObject r e s u l t = ( EObject )

unitApp . getParameterValue ("newGraph" ) ;90 saveModel ( unitName + "Result.xmi" , r e s u l t ) ;91 } else {92 System . e r r . p r i n t l n ("’" + unitName + "’ could not be

applied!" ) ;93 }94 }// performSimpleMigrationExt95

96 }// c l a s s

Listing 1.8. Starter for the transformations

1 package de . unimarburg . swt . t tc2011 ;2

3 public class S t a r t e r {4

5 public stat ic void main ( St r ing args [ ] ) {6 ATrafo t r a f o ;7 // −−−−−8

30

Page 31: Hello World! An Instructive Case for TTC 2011 with EMF Henshin › staff › pvgorp › events › TTC2011 › ... · Task 2.5: Delete Node with Speci c Name and its Incident Edges

9 t r a f o = new HelloWorldTrafo ( ) ;10 t r a f o . s t a r t ( ) ;11 // −−−−−12 t r a f o = new CountMatchesTrafo ( ) ;13 t r a f o . s t a r t ( ) ;14 // −−−−−15 t r a f o = new ReverseEdgesTrafo ( ) ;16 t r a f o . s t a r t ( ) ;17 // −−−−−18 t r a f o = new SimpleMigrat ionsTrafo ( ) ;19 t r a f o . s t a r t ( ) ;20 // −−−−−21 t r a f o = new DeleteNodeTrafo ( ) ;22 t r a f o . s t a r t ( ) ;23 // −−−−−24 t r a f o = new In s e r tTrans i t i v eEdge sTra fo ( ) ;25 t r a f o . s t a r t ( ) ;26

27

28 }// main29

30 }// c l a s s

31