V3_stratnotransfo_UMLClassesFlattening.t 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* VERSION3: %strategy version, no %transformation */
  14. public class V3_stratnotransfo_UMLClassesFlattening {
  15. %include{ sl.tom }
  16. %include{ emf/ecore.tom }
  17. %include{ mappings/ClassflatteningPackage.tom }
  18. %typeterm V3_stratnotransfo_UMLClassesFlattening { implement { V3_stratnotransfo_UMLClassesFlattening }}
  19. private static classflattening.VirtualRoot virtR = null;
  20. public static classflattening.Class flattening(classflattening.Class toFlatten) {
  21. classflattening.Class parent = toFlatten.getSuperclass();
  22. if(parent==null) {
  23. return toFlatten;
  24. } else {
  25. classflattening.Class flattenedParent = flattening(parent);
  26. org.eclipse.emf.common.util.EList<classflattening.Attribute> head = toFlatten.getAttributes();
  27. head.addAll(flattenedParent.getAttributes());
  28. classflattening.Class result = `cfClass(flattenedParent.getName() +
  29. toFlatten.getName(), head, flattenedParent.getSuperclass(),
  30. cfClassEList(), null);
  31. return result;
  32. }
  33. }
  34. %strategy FlatteningStrat(translator:V3_stratnotransfo_UMLClassesFlattening) extends Identity() {
  35. visit cfVirtualRoot {
  36. cfVirtualRoot(cfClassEList(_*,cl@cfClass(n,_,_,cfClassEList(),_),_*)) -> {
  37. org.eclipse.emf.common.util.EList<classflattening.Class> newChildren = translator.virtR.getChildren();
  38. translator.virtR = `cfVirtualRoot(cfClassEList(flattening(cl),newChildren*));
  39. }
  40. }
  41. }
  42. public static void main(String[] args) {
  43. System.out.println("\nStarting...\n");
  44. XMIResourceImpl resource = new XMIResourceImpl();
  45. classflattening.VirtualRoot source_root = `cfVirtualRoot(cfClassEList());
  46. Map opts = new HashMap();
  47. opts.put(XMIResource.OPTION_SCHEMA_LOCATION, java.lang.Boolean.TRUE);
  48. if (args.length>0) {
  49. //EMF style source model creation (loading .xmi file)
  50. classflattening.ClassflatteningPackage packageInstance = classflattening.ClassflatteningPackage.eINSTANCE;
  51. File input = new File(args[0]);
  52. try {
  53. resource.load(new FileInputStream(input),opts);
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. source_root = (classflattening.VirtualRoot) resource.getContents().get(0);
  58. } else {
  59. System.out.println("No model instance given in argument. Using default hardcoded model.");
  60. //Tom style source model creation
  61. classflattening.Class clC =
  62. `cfClass("C",cfAttributeEList(),null,cfClassEList(), null);
  63. clC = `cfClass("C",cfAttributeEList(cfAttribute("attrC", clC)),null,cfClassEList(), null);
  64. classflattening.Class clD =
  65. `cfClass("D",cfAttributeEList(cfAttribute("attrD", clC)), null,
  66. cfClassEList(), null);
  67. classflattening.Class clA = `cfClass("A",cfAttributeEList(), clD,
  68. cfClassEList(), null);
  69. classflattening.Class clB =
  70. `cfClass("B",cfAttributeEList(cfAttribute("attrB1", clC),
  71. cfAttribute("attrB2", clC)), clA, cfClassEList(), null);
  72. clD = `cfClass("D",cfAttributeEList(cfAttribute("attrD", clC)), null,
  73. cfClassEList(clA), null);
  74. clA = `cfClass("A",cfAttributeEList(), clD, cfClassEList(clB), null);
  75. source_root = `cfVirtualRoot(cfClassEList(clC,clD,clA,clB));
  76. clA.setRoot(source_root);
  77. clB.setRoot(source_root);
  78. clC.setRoot(source_root);
  79. clD.setRoot(source_root);
  80. }
  81. V3_stratnotransfo_UMLClassesFlattening translator = new V3_stratnotransfo_UMLClassesFlattening();
  82. try {
  83. //init virtual root
  84. translator.virtR = `cfVirtualRoot(cfClassEList());
  85. System.out.println("\nSource:");
  86. `TopDown(Print()).visit(source_root, new EcoreContainmentIntrospector());
  87. System.out.print("\nFlattening");
  88. //VERSION3
  89. System.out.println(" (VERSION2) ...");
  90. Strategy transformer = `BottomUp(FlatteningStrat(translator));
  91. transformer.visit(source_root, new EcoreContainmentIntrospector());
  92. System.out.println("\nResult:");
  93. `TopDown(Print()).visit(translator.virtR, new EcoreContainmentIntrospector());
  94. } catch(VisitFailure e) {
  95. System.out.println("strategy fail!");
  96. }
  97. }
  98. %strategy Print() extends Identity() {
  99. visit cfClass {
  100. cl@cfClass(n,attr,sup,sub,root) -> {
  101. String strSup = (`sup!=null)?"\n sup="+`sup.getName():"";
  102. String strAttr = "";
  103. if(!(`attr.isEmpty())) {
  104. %match(`attr) {
  105. cfAttributeEList(head,tail*) -> {
  106. strAttr = "\n attr="+`head.getName()+":"+`head.getType().getName();
  107. %match(`tail) {
  108. cfAttributeEList(_*,a,_*) -> {
  109. strAttr += ", "+`a.getName()+":"+`a.getType().getName();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. System.out.println("Class: "+`n+strAttr+strSup/*+strSub*/);
  116. }
  117. }
  118. }
  119. }