v1flattening.tex 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %\begin{tomcode3}
  2. \begin{codesource}[label=code:v1flattening,caption=Version 1 : Implémentation de la transformation d'aplatissement de hiérarchie de classes en Java.]
  3. public static VirtualRoot v1_processClass(VirtualRoot root) {
  4. org.eclipse.emf.common.util.EList<Class> newChildren =
  5. new org.eclipse.emf.common.util.BasicEList<Class>();
  6. for(Class cl : root.getChildren()) {
  7. if(cl.getSubclass().isEmpty()) {
  8. newChildren.add(flattening(cl));
  9. }
  10. }
  11. VirtualRoot result = (VirtualRoot) ClassflatteningFactory.eINSTANCE.create(
  12. (EClass)ClassflatteningPackage.eINSTANCE.getEClassifier("VirtualRoot"));
  13. result.eSet(result.eClass().getEStructuralFeature("children"), newChildren);
  14. return result;
  15. }
  16. public static Class flattening(Class toFlatten) {
  17. Class parent = toFlatten.getSuperclass();
  18. if(parent==null) {
  19. return toFlatten;
  20. } else {
  21. Class flattenedParent = flattening(parent);
  22. EList<Attribute> head = toFlatten.getAttributes();
  23. head.addAll(flattenedParent.getAttributes());
  24. Class result = (Class)ClassflatteningFactory.eINSTANCE.
  25. create((EClass)ClassflatteningPackage.eINSTANCE.getEClassifier("Class"));
  26. result.eSet(result.eClass().getEStructuralFeature("name"), flattenedParent.getName()+toFlatten.getName());
  27. result.eSet(result.eClass().getEStructuralFeature("attributes"), head);
  28. result.eSet(result.eClass().getEStructuralFeature("superclass"), flattenedParent.getSuperclass());
  29. result.eSet(result.eClass().getEStructuralFeature("subclass"), (new BasicEList<Class>()) );
  30. result.eSet(result.eClass().getEStructuralFeature("root"), virtR);
  31. return result;
  32. }
  33. }
  34. ...
  35. public static void main(String[] args) {
  36. ...
  37. VirtualRoot translator.virtR = v1_processClass(source_root);
  38. ...
  39. }
  40. \end{codesource}
  41. %\end{tomcode3}