V2_nostrat_UMLClassesFlattening.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.emf.*;
  12. /* VERSION1: recursive version, no %strategy, no %transformation */
  13. public class V2_nostrat_UMLClassesFlattening {
  14. %include{ emf/ecore.tom }
  15. %include{ mappings/ClassflatteningPackage.tom }
  16. %typeterm V2_nostrat_UMLClassesFlattening { implement { V2_nostrat_UMLClassesFlattening }}
  17. private static classflattening.VirtualRoot virtR = null;
  18. public static classflattening.VirtualRoot v1_processClass(classflattening.VirtualRoot root) {
  19. org.eclipse.emf.common.util.EList<classflattening.Class> children = root.getChildren();
  20. org.eclipse.emf.common.util.EList<classflattening.Class> newChildren = `cfClassEList();
  21. %match(children) {
  22. cfClassEList(_*,cl@cfClass(_,_,_,cfClassEList(),_),_*) -> {
  23. newChildren = `cfClassEList(flattening(cl),newChildren*);
  24. }
  25. }
  26. return `cfVirtualRoot(newChildren);
  27. }
  28. public static classflattening.Class flattening(classflattening.Class toFlatten) {
  29. classflattening.Class parent = toFlatten.getSuperclass();
  30. if(parent==null) {
  31. return toFlatten;
  32. } else {
  33. classflattening.Class flattenedParent = flattening(parent);
  34. org.eclipse.emf.common.util.EList<classflattening.Attribute> head = toFlatten.getAttributes();
  35. head.addAll(flattenedParent.getAttributes());
  36. classflattening.Class result = `cfClass(flattenedParent.getName() +
  37. toFlatten.getName(), head, flattenedParent.getSuperclass(),
  38. cfClassEList(), null);
  39. return result;
  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. V2_nostrat_UMLClassesFlattening translator = new V2_nostrat_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. //VERSION1
  89. System.out.println(" (VERSION1) ...");
  90. translator.virtR = v1_processClass(source_root);
  91. System.out.println("\nResult:");
  92. `TopDown(Print()).visit(translator.virtR, new EcoreContainmentIntrospector());
  93. } catch(VisitFailure e) {
  94. System.out.println("strategy fail!");
  95. }
  96. }
  97. %strategy Print() extends Identity() {
  98. visit cfClass {
  99. cl@cfClass(n,attr,sup,sub,root) -> {
  100. String strSup = (`sup!=null)?"\n sup="+`sup.getName():"";
  101. String strAttr = "";
  102. if (!(`attr.isEmpty())) {
  103. %match(`attr) {
  104. cfAttributeEList(head,tail*) -> {
  105. strAttr = "\n attr="+`head.getName()+":"+`head.getType().getName();
  106. %match(`tail) {
  107. cfAttributeEList(_*,a,_*) -> {
  108. strAttr += ", "+`a.getName()+":"+`a.getType().getName();
  109. }
  110. }
  111. }
  112. }
  113. }
  114. System.out.println("Class: "+`n+strAttr+strSup/*+strSub*/);
  115. }
  116. }
  117. }
  118. }