Zakir Meer | 1882c13 | 2017-07-06 09:20:59 +0200 | [diff] [blame^] | 1 | package org.eclipse.app4mc.emfutils.content.extractor.builders |
| 2 | |
| 3 | import org.eclipse.emf.common.util.BasicEMap |
| 4 | import org.eclipse.emf.common.util.EMap |
| 5 | import org.eclipse.emf.ecore.EClass |
| 6 | import org.eclipse.emf.ecore.EObject |
| 7 | import org.eclipse.emf.ecore.resource.ResourceSet |
| 8 | import org.eclipse.emf.common.util.EList |
| 9 | import java.util.ArrayList |
| 10 | import org.eclipse.emf.ecore.EPackage |
| 11 | import java.util.List |
| 12 | import org.eclipse.emf.ecore.resource.Resource |
| 13 | import org.eclipse.emf.ecore.EClassifier |
| 14 | import org.eclipse.emf.ecore.util.EcoreUtil |
| 15 | |
| 16 | class EClassHierarchyBufferBuilder2 { |
| 17 | |
| 18 | var EMap<String, Object> id2ObjectsMap = new BasicEMap |
| 19 | |
| 20 | def public setId2ObjectsMap(EMap<String, Object> id2ObjectsMap) { |
| 21 | this.id2ObjectsMap = id2ObjectsMap |
| 22 | } |
| 23 | |
| 24 | def buildBuffer(EClass selectedObjClass) ''' |
| 25 | |
| 26 | «id2ObjectsMap = new BasicEMap» |
| 27 | |
| 28 | @startuml |
| 29 | |
| 30 | ««« Generation of EClass basic information »» |
| 31 | |
| 32 | «addClassInfo(selectedObjClass)» |
| 33 | |
| 34 | ««« Generation of Hierarchy for Super types »» |
| 35 | |
| 36 | «IF selectedObjClass.ESuperTypes.size > 0» |
| 37 | |
| 38 | «FOR superClass: selectedObjClass.ESuperTypes» |
| 39 | |
| 40 | ««« UUID - which will be used for elements lookup »» |
| 41 | «var String uuid=EcoreUtil.generateUUID» |
| 42 | |
| 43 | «IF superClass.abstract && superClass.interface» |
| 44 | interface «superClass.name» "[[«uuid»]]" |
| 45 | «ELSEIF superClass.abstract» |
| 46 | abstract «superClass.name» "[[«uuid»]]" |
| 47 | «ELSEIF superClass.interface» |
| 48 | interface «superClass.name» "[[«uuid»]]" |
| 49 | «ELSE» |
| 50 | class «superClass.name» "[[«uuid»]]" |
| 51 | |
| 52 | «ENDIF» |
| 53 | |
| 54 | |
| 55 | «id2ObjectsMap.put(uuid, superClass)» |
| 56 | |
| 57 | «superClass.name» <|--«selectedObjClass.name» |
| 58 | |
| 59 | |
| 60 | |
| 61 | «ENDFOR» |
| 62 | |
| 63 | |
| 64 | «ENDIF» |
| 65 | |
| 66 | « var List<EClass> subClasses=getSubClasses(selectedObjClass) » |
| 67 | |
| 68 | «IF subClasses.size>0» |
| 69 | |
| 70 | «ENDIF» |
| 71 | |
| 72 | @enduml |
| 73 | |
| 74 | |
| 75 | |
| 76 | ''' |
| 77 | |
| 78 | /** |
| 79 | * This method is used to add EClass specific information |
| 80 | */ |
| 81 | protected def CharSequence addClassInfo(EClass selectedObjClass) '''«IF selectedObjClass.abstract && selectedObjClass.interface» |
| 82 | |
| 83 | interface «selectedObjClass.name» |
| 84 | «ELSEIF selectedObjClass.abstract» |
| 85 | abstract «selectedObjClass.name» |
| 86 | «ELSEIF selectedObjClass.interface» |
| 87 | interface «selectedObjClass.name» |
| 88 | «ELSE» |
| 89 | class «selectedObjClass.name» |
| 90 | «ENDIF»''' |
| 91 | |
| 92 | /** |
| 93 | * This method is used to get the List of sub-classes of a specific EClass |
| 94 | */ |
| 95 | |
| 96 | def private List<EClass> getSubClasses(EClass eClass) { |
| 97 | |
| 98 | var List<EClass> allSubClasses = new ArrayList |
| 99 | |
| 100 | var ResourceSet resourceSet = eClass.eResource().getResourceSet(); |
| 101 | |
| 102 | if (resourceSet != null) { |
| 103 | |
| 104 | // With this approach check is made to find the Sub-Classes for the supplied EClass in all the packages |
| 105 | // belonging to the resourceset |
| 106 | var EList<Resource> resources = resourceSet.getResources(); |
| 107 | |
| 108 | for (Resource resource : resources) { |
| 109 | |
| 110 | var EList<EObject> contents = resource.getContents(); |
| 111 | |
| 112 | if (contents.size() > 0) { |
| 113 | var EObject eObject = contents.get(0); |
| 114 | |
| 115 | if (eObject instanceof EPackage) { |
| 116 | allSubClasses.addAll(getSubClasses(eClass, eObject)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | } else { |
| 122 | |
| 123 | var EPackage ePackage = eClass.getEPackage(); |
| 124 | return getSubClasses(eClass, ePackage); |
| 125 | } |
| 126 | |
| 127 | return allSubClasses; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * This method is used to get the List of sub-classes of a specific EClass in the scope of EPackage |
| 133 | */ |
| 134 | def List<EClass> getSubClasses(EClass selectedObjClass, EPackage ePackage) { |
| 135 | |
| 136 | var List<EClass> allSubClasses = new ArrayList<EClass>(); |
| 137 | |
| 138 | var EList<EClassifier> eClassifiers = ePackage.getEClassifiers(); |
| 139 | |
| 140 | for (EClassifier eClassifier : eClassifiers) { |
| 141 | |
| 142 | if (eClassifier instanceof EClass) { |
| 143 | |
| 144 | if (!(selectedObjClass == eClassifier) && selectedObjClass.isSuperTypeOf(eClassifier)) { |
| 145 | allSubClasses.add(eClassifier); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | } |
| 150 | return allSubClasses; |
| 151 | } |
| 152 | |
| 153 | } |