V4_transfo_UMLClassesFlattening.t 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import org.eclipse.emf.common.util.*;
  2. import org.eclipse.emf.ecore.*;
  3. import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
  4. import org.eclipse.emf.ecore.xmi.*;
  5. import org.eclipse.emf.ecore.xmi.impl.*;
  6. import classflattening.*;
  7. import classflattening.impl.*;
  8. import java.util.*;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import tom.library.sl.*;
  12. import tom.library.emf.*;
  13. import tom.library.utils.LinkClass;
  14. public class V4_transfo_UMLClassesFlattening {
  15. %include{ sl.tom }
  16. %include{ LinkClass.tom }
  17. %include{ emf/ecore.tom }
  18. %include{ mappings/ClassflatteningPackage.tom }
  19. %typeterm UMLClassesFlattening { implement { UMLClassesFlattening }}
  20. private static classflattening.VirtualRoot virtR = null;
  21. private static LinkClass tom__linkClass;
  22. public UMLClassesFlattening() {
  23. this.tom__linkClass = new LinkClass();
  24. }
  25. /* This function is called by the transformation, like in previous
  26. * implementations */
  27. public static classflattening.Class flattening(classflattening.Class toFlatten) {
  28. classflattening.Class parent = toFlatten.getSuperclass();
  29. if(parent==null) {
  30. return toFlatten;
  31. } else {
  32. classflattening.Class flattenedParent = flattening(parent);
  33. org.eclipse.emf.common.util.EList<classflattening.Attribute> head = toFlatten.getAttributes();
  34. head.addAll(flattenedParent.getAttributes());
  35. classflattening.Class result = `cfClass(flattenedParent.getName() +
  36. toFlatten.getName(), head, flattenedParent.getSuperclass(),
  37. cfClassEList(), null);
  38. return result;
  39. }
  40. }
  41. /* VERSION4: %transformation version */
  42. %transformation FlatteningTrans(tom__linkClass:LinkClass,virtR:cfVirtualRoot) : "metamodels/Class.ecore" -> "metamodels/Class.ecore" {
  43. definition DEF1 traversal `BottomUp(DEF1(tom__linkClass, virtR)) {
  44. cfVirtualRoot(cfClassEList(_*,cl@cfClass(n,_,_,cfClassEList(),_),_*)) -> {
  45. org.eclipse.emf.common.util.EList<classflattening.Class> newChildren = virtR.getChildren();
  46. return `cfVirtualRoot(cfClassEList(flattening(cl),newChildren*));
  47. }
  48. }
  49. }
  50. /* NOTE: no resolve element needed */
  51. public static void main(String[] args) {
  52. System.out.println("\nStarting...\n");
  53. XMIResourceImpl resource = new XMIResourceImpl();
  54. classflattening.VirtualRoot source_root = `cfVirtualRoot(cfClassEList());
  55. Map opts = new HashMap();
  56. opts.put(XMIResource.OPTION_SCHEMA_LOCATION, java.lang.Boolean.TRUE);
  57. if (args.length>0) {
  58. //EMF style source model creation (loading .xmi file)
  59. classflattening.ClassflatteningPackage packageInstance = classflattening.ClassflatteningPackage.eINSTANCE;
  60. File input = new File(args[0]);
  61. try {
  62. resource.load(new FileInputStream(input),opts);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. source_root = (classflattening.VirtualRoot) resource.getContents().get(0);
  67. } else {
  68. System.out.println("No model instance given in argument. Using default hardcoded model.");
  69. //Tom style source model creation
  70. classflattening.Class clC =
  71. `cfClass("C",cfAttributeEList(),null,cfClassEList(), null);
  72. clC = `cfClass("C",cfAttributeEList(cfAttribute("attrC", clC)),null,cfClassEList(), null);
  73. classflattening.Class clD =
  74. `cfClass("D",cfAttributeEList(cfAttribute("attrD", clC)), null,
  75. cfClassEList(), null);
  76. classflattening.Class clA = `cfClass("A",cfAttributeEList(), clD,
  77. cfClassEList(), null);
  78. classflattening.Class clB =
  79. `cfClass("B",cfAttributeEList(cfAttribute("attrB1", clC),
  80. cfAttribute("attrB2", clC)), clA, cfClassEList(), null);
  81. clD = `cfClass("D",cfAttributeEList(cfAttribute("attrD", clC)), null,
  82. cfClassEList(clA), null);
  83. clA = `cfClass("A",cfAttributeEList(), clD, cfClassEList(clB), null);
  84. source_root = `cfVirtualRoot(cfClassEList(clC,clD,clA,clB));
  85. clA.setRoot(source_root);
  86. clB.setRoot(source_root);
  87. clC.setRoot(source_root);
  88. clD.setRoot(source_root);
  89. }
  90. UMLClassesFlattening translator = new UMLClassesFlattening();
  91. try {
  92. //init virtual root
  93. translator.virtR = `cfVirtualRoot(cfClassEList());
  94. System.out.println("\nSource:");
  95. `TopDown(Print()).visit(source_root, new EcoreContainmentIntrospector());
  96. System.out.print("\nFlattening");
  97. //VERSION4
  98. System.out.println(" (VERSION3) ...");
  99. Strategy transformer = `TopDown(FlatteningTrans(translator.tom__linkClass,translator.virtR));
  100. translator.virtR = transformer.visit(source_root, new EcoreContainmentIntrospector());
  101. /* As we do not use any %resolve, there is no Resolve strategy
  102. * generation, and no call of it. The following line is only the
  103. * instruction we would write if there were a Resolve strategy:
  104. `TopDown(tom__StratResolve_FlatteningTrans(translator.tom__linkClass,
  105. translator.virtR)).visit(translator.virtR, new
  106. EcoreContainmentIntrospector());
  107. */
  108. System.out.println("\nResult:");
  109. `TopDown(Print()).visit(translator.virtR, new EcoreContainmentIntrospector());
  110. } catch(VisitFailure e) {
  111. System.out.println("strategy fail!");
  112. }
  113. }
  114. %strategy Print() extends Identity() {
  115. visit cfClass {
  116. cl@cfClass(n,attr,sup,sub,root) -> {
  117. String strSup = (`sup!=null)?"\n sup="+`sup.getName():"";
  118. String strAttr = "";
  119. if(!(`attr.isEmpty())) {
  120. %match(`attr) {
  121. cfAttributeEList(head,tail*) -> {
  122. strAttr = "\n attr="+`head.getName()+":"+`head.getType().getName();
  123. %match(`tail) {
  124. cfAttributeEList(_*,a,_*) -> {
  125. strAttr += ", "+`a.getName()+":"+`a.getType().getName();
  126. }
  127. }
  128. }
  129. }
  130. }
  131. System.out.println("Class: "+`n+strAttr+strSup/*+strSub*/);
  132. }
  133. }
  134. }
  135. }