V1_notom_UMLClassesFlattening.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import org.eclipse.emf.common.util.*;
  2. import org.eclipse.emf.ecore.*;
  3. import org.eclipse.emf.ecore.xmi.*;
  4. import org.eclipse.emf.ecore.xmi.impl.*;
  5. import classflattening.*;
  6. import classflattening.impl.*;
  7. import java.util.*;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. /* VERSION0: Version without any Tom code -> pure Java EMF */
  11. public class V1_notom_UMLClassesFlattening {
  12. private static classflattening.VirtualRoot virtR = null;
  13. public static classflattening.VirtualRoot v0_processClass(classflattening.VirtualRoot root) {
  14. org.eclipse.emf.common.util.EList<classflattening.Class> newChildren =
  15. new org.eclipse.emf.common.util.BasicEList<classflattening.Class>();
  16. for(classflattening.Class cl : root.getChildren()) {
  17. if(cl.getSubclass().isEmpty()) {
  18. newChildren.add(notom_flattening(cl));
  19. }
  20. }
  21. classflattening.VirtualRoot result = (classflattening.VirtualRoot)classflattening.ClassflatteningFactory.eINSTANCE.create(
  22. (EClass)classflattening.ClassflatteningPackage.eINSTANCE.getEClassifier("VirtualRoot"));
  23. result.eSet(result.eClass().getEStructuralFeature("children"),(Object)newChildren);
  24. return result;
  25. }
  26. public static classflattening.Class notom_flattening(classflattening.Class toFlatten) {
  27. classflattening.Class parent = toFlatten.getSuperclass();
  28. if(parent==null) {
  29. return toFlatten;
  30. } else {
  31. classflattening.Class flattenedParent = notom_flattening(parent);
  32. org.eclipse.emf.common.util.EList<classflattening.Attribute> head =
  33. toFlatten.getAttributes();
  34. head.addAll(flattenedParent.getAttributes());
  35. classflattening.Class result = (classflattening.Class)classflattening.ClassflatteningFactory.eINSTANCE.create(
  36. (EClass)classflattening.ClassflatteningPackage.eINSTANCE.getEClassifier("Class"));
  37. result.eSet(result.eClass().getEStructuralFeature("name"),(Object)flattenedParent.getName()+toFlatten.getName());
  38. result.eSet(result.eClass().getEStructuralFeature("attributes"),(Object)head);
  39. result.eSet(result.eClass().getEStructuralFeature("superclass"),(Object)flattenedParent.getSuperclass());
  40. result.eSet(result.eClass().getEStructuralFeature("subclass"),(Object)(
  41. new org.eclipse.emf.common.util.BasicEList<classflattening.Class>()) );
  42. result.eSet(result.eClass().getEStructuralFeature("root"),(Object)virtR);
  43. return result;
  44. }
  45. }
  46. public static void main(String[] args) {
  47. System.out.println("\nStarting...\n");
  48. XMIResourceImpl resource = new XMIResourceImpl();
  49. classflattening.VirtualRoot source_root = null;
  50. Map opts = new HashMap();
  51. opts.put(XMIResource.OPTION_SCHEMA_LOCATION, java.lang.Boolean.TRUE);
  52. if (args.length>0) {
  53. //EMF style source model creation (loading .xmi file)
  54. classflattening.ClassflatteningPackage packageInstance = classflattening.ClassflatteningPackage.eINSTANCE;
  55. File input = new File(args[0]);
  56. try {
  57. resource.load(new FileInputStream(input),opts);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. source_root = (classflattening.VirtualRoot) resource.getContents().get(0);
  62. } else {
  63. System.out.println("No model instance given in argument. Bye bye.");
  64. return;
  65. }
  66. V1_notom_UMLClassesFlattening translator = new V1_notom_UMLClassesFlattening();
  67. try {
  68. //init virtual root
  69. translator.virtR = null;
  70. System.out.println("\nSource:");
  71. printer(source_root);
  72. System.out.print("\nFlattening");
  73. //VERSION0
  74. System.out.println(" (VERSION0) ...");
  75. translator.virtR = v0_processClass(source_root);
  76. System.out.println("\nResult:");
  77. printer(translator.virtR);
  78. } catch(Exception e) {
  79. System.out.println("Ooops!");
  80. }
  81. }
  82. public static void printer(classflattening.VirtualRoot root) {
  83. for(classflattening.Class cl : root.getChildren()) {
  84. classflattening.Class sup = cl.getSuperclass();
  85. String strSup = (sup!=null)?"\n sup="+sup.getName():"";
  86. String strAttr = "";
  87. org.eclipse.emf.common.util.EList<classflattening.Attribute> attr = cl.getAttributes();
  88. if(!(attr.isEmpty())) {
  89. classflattening.Attribute head = attr.get(0);
  90. strAttr += "\n attr="+head.getName()+":"+head.getType().getName();
  91. if(attr.size()>1) {
  92. java.util.List<classflattening.Attribute> tail = attr.subList(1,attr.size());
  93. for(classflattening.Attribute at : tail) {
  94. strAttr += ", "+at.getName()+":"+at.getType().getName();
  95. }
  96. }
  97. }
  98. System.out.println("Class: "+cl.getName()+strAttr+strSup/*+strSub*/);
  99. }
  100. }
  101. }