diff options
Diffstat (limited to 'Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend')
-rw-r--r-- | Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend | 220 |
1 files changed, 86 insertions, 134 deletions
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend index 126dbb7d..e554cc69 100644 --- a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend | |||
@@ -1,15 +1,10 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.execution | 1 | package hu.bme.mit.inf.dslreasoner.application.execution |
2 | 2 | ||
3 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.ClassElement | ||
4 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.FeatureElement | ||
5 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelEntry | ||
6 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelSpecification | 3 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelSpecification |
7 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | 4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor |
8 | import java.util.ArrayList | 5 | import java.util.ArrayList |
9 | import java.util.HashMap | ||
10 | import java.util.LinkedList | 6 | import java.util.LinkedList |
11 | import java.util.List | 7 | import java.util.List |
12 | import java.util.Map | ||
13 | import java.util.Set | 8 | import java.util.Set |
14 | import org.eclipse.emf.common.util.URI | 9 | import org.eclipse.emf.common.util.URI |
15 | import org.eclipse.emf.ecore.EAttribute | 10 | import org.eclipse.emf.ecore.EAttribute |
@@ -24,13 +19,11 @@ import org.eclipse.emf.ecore.EcorePackage | |||
24 | import org.eclipse.emf.ecore.resource.Resource | 19 | import org.eclipse.emf.ecore.resource.Resource |
25 | import org.eclipse.emf.ecore.resource.ResourceSet | 20 | import org.eclipse.emf.ecore.resource.ResourceSet |
26 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | 21 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl |
27 | import org.eclipse.xtend.lib.annotations.Data | 22 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.AllPackageEntry |
28 | 23 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelElement | |
29 | @Data | 24 | import org.eclipse.xtext.xbase.lib.Functions.Function1 |
30 | class Metamodel { | 25 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelEntry |
31 | List<EPackage> packages | 26 | import java.util.LinkedHashSet |
32 | EcoreMetamodelDescriptor descriptor | ||
33 | } | ||
34 | 27 | ||
35 | class MetamodelLoader { | 28 | class MetamodelLoader { |
36 | 29 | ||
@@ -41,138 +34,97 @@ class MetamodelLoader { | |||
41 | 34 | ||
42 | public new() { init } | 35 | public new() { init } |
43 | 36 | ||
44 | def loadMetamodel(List<String> specification, ResourceSet rs, Context context) { | 37 | def loadMetamodel(MetamodelSpecification specification, ResourceSet rs) throws IllegalArgumentException { |
45 | 38 | ||
46 | } | 39 | val Set<EClass> classes = new LinkedHashSet |
47 | 40 | val Set<EEnum> enums = new LinkedHashSet | |
48 | def loadMetamodel(MetamodelSpecification specification, ResourceSet rs, Context context) { | 41 | val Set<EEnumLiteral> literals = new LinkedHashSet |
49 | val Map<MetamodelEntry,List<EPackage>> entry2Packages = new HashMap | 42 | val Set<EReference> references = new LinkedHashSet |
43 | val Set<EAttribute> attributes = new LinkedHashSet | ||
50 | 44 | ||
45 | /** Add all included types */ | ||
51 | for(entry : specification.entries) { | 46 | for(entry : specification.entries) { |
52 | val packagesInEntry = entry.path.path.loadPackageFromPath(rs,context) | 47 | classes+=entry.classes |
53 | entry2Packages.put(entry,packagesInEntry) | 48 | enums+=entry.enums |
49 | literals+=entry.literals | ||
50 | references+=entry.references | ||
51 | attributes+=entry.attributes | ||
54 | } | 52 | } |
55 | 53 | ||
56 | return entry2Packages | 54 | return new EcoreMetamodelDescriptor(classes.toList, emptySet, false, enums.toList, literals.toList, references.toList, attributes.toList) |
57 | } | 55 | } |
58 | 56 | ||
59 | public def pruneMetamodel(Map<MetamodelEntry,List<EPackage>> specification, ResourceSet rs, Context context) { | 57 | def <T> List<T> extractElements(MetamodelEntry entry, Function1<AllPackageEntry,Iterable<T>> packageEntryExtractor, Function1<MetamodelElement,Iterable<T>> metamodelElementExtractor) { |
60 | val List<EClass> classes = new LinkedList | 58 | if(entry instanceof MetamodelElement) { |
61 | val List<EEnum> enums = new LinkedList | 59 | return metamodelElementExtractor.apply(entry).toList |
62 | val List<EEnumLiteral> literals = new LinkedList | 60 | } else if(entry instanceof AllPackageEntry) { |
63 | val List<EReference> references = new LinkedList | 61 | val excluded = entry.exclusion.map[metamodelElementExtractor.apply(it)].flatten.toSet |
64 | val List<EAttribute> attributes = new LinkedList | 62 | return packageEntryExtractor.apply(entry).filter[!excluded.contains(it)].toList |
65 | 63 | } else throw new IllegalArgumentException('''Unsupported entry type: "«entry.class.simpleName»"!''') | |
66 | |||
67 | /** Add all included types */ | ||
68 | for(entry : specification.entrySet) { | ||
69 | val metamodelEntry = entry.key | ||
70 | val packages = entry.value | ||
71 | |||
72 | /** Excluted types */ | ||
73 | val excludedTypeNames = metamodelEntry.excluding.filter(ClassElement).map[name].toSet | ||
74 | /** Excluded features */ | ||
75 | val excludedFeatureNames = metamodelEntry.excluding.filter(FeatureElement).map[it.container.name -> it.name].toSet | ||
76 | |||
77 | /** Load the types */ | ||
78 | for(package : packages) { | ||
79 | for(class : package.EClassifiers.filter(EClass)) { | ||
80 | classes.addIfNotExcluded(class,excludedTypeNames) | ||
81 | } | ||
82 | for(enum : package.EClassifiers.filter(EEnum)) { | ||
83 | val added = enums.addIfNotExcluded(enum,excludedTypeNames) | ||
84 | if(added) { | ||
85 | for(literal : enum.ELiterals) { | ||
86 | literals.addIfNotExcluded(literal,enum,excludedFeatureNames) | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | /** Add all included references and attributes*/ | ||
94 | for(entry : specification.entrySet) { | ||
95 | val metamodelEntry = entry.key | ||
96 | val packages = entry.value | ||
97 | |||
98 | /** Excluded features */ | ||
99 | val excludedFeatureNames = metamodelEntry.excluding.filter(FeatureElement).map[it.container.name -> it.name].toSet | ||
100 | |||
101 | /** See if type is included */ | ||
102 | for(package : packages) { | ||
103 | for(class : package.EClassifiers.filter(EClass)) { | ||
104 | if(classes.contains(class)) { | ||
105 | for(reference : class.EReferences) { | ||
106 | if(classes.contains(reference.EType)) { | ||
107 | references.addIfNotExcluded(reference,class,excludedFeatureNames) | ||
108 | } | ||
109 | } | ||
110 | for(attribute : class.EAttributes) { | ||
111 | val type = attribute.EType | ||
112 | if(type instanceof EEnum) { | ||
113 | if(enums.contains(type)) { | ||
114 | attributes.addIfNotExcluded(attribute,class,excludedFeatureNames) | ||
115 | } else if(type == EcorePackage.Literals) { | ||
116 | if(enums.contains(type)) { | ||
117 | attributes.addIfNotExcluded(attribute,class,excludedFeatureNames) | ||
118 | } | ||
119 | } | ||
120 | } else if(supportedEDataType(type as EDataType)) { | ||
121 | attributes.addIfNotExcluded(attribute,class,excludedFeatureNames) | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | } | 64 | } |
129 | 65 | ||
130 | private def supportedEDataType(EDataType dataType) { | 66 | def getClasses(MetamodelEntry entry) { |
131 | val extension l = EcorePackage.eINSTANCE | 67 | return entry.extractElements( |
132 | return #[EInt, EBoolean, EString, EDouble, EFloat].contains(dataType) | 68 | [package.EClassifiers.filter(EClass)], |
69 | [val classifier = it.classifier | ||
70 | if(classifier instanceof EClass){ if(it.feature === null) { return #[classifier]} } | ||
71 | return emptyList | ||
72 | ] | ||
73 | ) | ||
133 | } | 74 | } |
134 | 75 | def getEnums(MetamodelEntry entry) { | |
135 | private def <T extends ENamedElement> addIfNotExcluded( | 76 | return entry.extractElements( |
136 | List<T> target, | 77 | [package.EClassifiers.filter(EEnum)], |
137 | T element, | 78 | [val classifier = it.classifier |
138 | Set<String> excluded) | 79 | if(classifier instanceof EEnum){ if(it.feature === null) { return #[classifier]} } |
139 | { | 80 | return emptyList |
140 | if(excluded.contains(element.name)) { | 81 | ] |
141 | target += element | 82 | ) |
142 | return true | ||
143 | } else { | ||
144 | return false | ||
145 | } | ||
146 | } | 83 | } |
147 | private def <T1 extends ENamedElement> addIfNotExcluded( | 84 | def getLiterals(MetamodelEntry entry) { |
148 | List<T1> target, | 85 | return entry.extractElements( |
149 | T1 element, | 86 | [package.EClassifiers.filter(EEnum).map[ELiterals].flatten], |
150 | ENamedElement container, | 87 | [val feature = it.feature |
151 | Set<Pair<String,String>> excluded) | 88 | if(feature instanceof EEnumLiteral){ return #[feature] } |
152 | { | 89 | return emptyList |
153 | val pair = (container.name) -> (element.name) | 90 | ] |
154 | 91 | ) | |
155 | if(excluded.contains(pair)) { | ||
156 | target += element | ||
157 | } | ||
158 | } | 92 | } |
159 | 93 | def getReferences(MetamodelEntry entry) { | |
160 | private def List<EPackage> loadPackageFromPath(String path, ResourceSet rs, Context context) throws RuntimeException { | 94 | return entry.extractElements( |
161 | var Resource resource; | 95 | [package.EClassifiers.filter(EClass).map[EReferences].flatten], |
162 | try{ | 96 | [val feature = it.feature |
163 | resource = rs.getResource(URI.createURI(path),true) | 97 | if(feature instanceof EReference) { return #[feature] } |
164 | } catch(RuntimeException e) { | 98 | return emptyList |
165 | context.writeError('''Unable to load EPackage: Error in path "«path»"!''') | 99 | ] |
166 | return #[] | 100 | ) |
167 | } | ||
168 | val res = new ArrayList<EPackage>(resource.contents.size) | ||
169 | for(content: resource.contents) { | ||
170 | if(content instanceof EPackage) { | ||
171 | res += content | ||
172 | } else { | ||
173 | context.writeError('''Unable to load EPackage: The content of "«path»" is not an EPackage, but "«content.eClass.name»"!''') | ||
174 | } | ||
175 | } | ||
176 | return res | ||
177 | } | 101 | } |
102 | def getAttributes(MetamodelEntry entry) { | ||
103 | return entry.extractElements( | ||
104 | [package.EClassifiers.filter(EClass).map[EAttributes].flatten], | ||
105 | [val feature = it.feature | ||
106 | if(feature instanceof EAttribute) {return #[feature]} | ||
107 | return emptyList | ||
108 | ] | ||
109 | ) | ||
110 | } | ||
111 | |||
112 | // public def List<EPackage> loadPackageFromPath(String path, ResourceSet rs, Context context) throws RuntimeException { | ||
113 | // var Resource resource; | ||
114 | // try{ | ||
115 | // resource = rs.getResource(URI.createURI(path),true) | ||
116 | // } catch(RuntimeException e) { | ||
117 | // context.writeError('''Unable to load EPackage: Error in path "«path»"!''') | ||
118 | // return #[] | ||
119 | // } | ||
120 | // val res = new ArrayList<EPackage>(resource.contents.size) | ||
121 | // for(content: resource.contents) { | ||
122 | // if(content instanceof EPackage) { | ||
123 | // res += content | ||
124 | // } else { | ||
125 | // context.writeError('''Unable to load EPackage: The content of "«path»" is not an EPackage, but "«content.eClass.name»"!''') | ||
126 | // } | ||
127 | // } | ||
128 | // return res | ||
129 | // } | ||
178 | } \ No newline at end of file | 130 | } \ No newline at end of file |