diff options
author | OszkarSemerath <oszka@SEMERATH-LAPTOP> | 2018-01-13 19:33:26 +0100 |
---|---|---|
committer | OszkarSemerath <oszka@SEMERATH-LAPTOP> | 2018-01-13 19:33:26 +0100 |
commit | 7e50434905cbb7f5d03636033b698e17a9075e9d (patch) | |
tree | 10e0968c20ac55dae4f5dc6077f39cf9947e3cb9 /Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend | |
parent | Test and measurement runners (diff) | |
download | VIATRA-Generator-7e50434905cbb7f5d03636033b698e17a9075e9d.tar.gz VIATRA-Generator-7e50434905cbb7f5d03636033b698e17a9075e9d.tar.zst VIATRA-Generator-7e50434905cbb7f5d03636033b698e17a9075e9d.zip |
Initial commit of the configuration language and application
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 | 178 |
1 files changed, 178 insertions, 0 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 new file mode 100644 index 00000000..126dbb7d --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend | |||
@@ -0,0 +1,178 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.execution | ||
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 | ||
7 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
8 | import java.util.ArrayList | ||
9 | import java.util.HashMap | ||
10 | import java.util.LinkedList | ||
11 | import java.util.List | ||
12 | import java.util.Map | ||
13 | import java.util.Set | ||
14 | import org.eclipse.emf.common.util.URI | ||
15 | import org.eclipse.emf.ecore.EAttribute | ||
16 | import org.eclipse.emf.ecore.EClass | ||
17 | import org.eclipse.emf.ecore.EDataType | ||
18 | import org.eclipse.emf.ecore.EEnum | ||
19 | import org.eclipse.emf.ecore.EEnumLiteral | ||
20 | import org.eclipse.emf.ecore.ENamedElement | ||
21 | import org.eclipse.emf.ecore.EPackage | ||
22 | import org.eclipse.emf.ecore.EReference | ||
23 | import org.eclipse.emf.ecore.EcorePackage | ||
24 | import org.eclipse.emf.ecore.resource.Resource | ||
25 | import org.eclipse.emf.ecore.resource.ResourceSet | ||
26 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
27 | import org.eclipse.xtend.lib.annotations.Data | ||
28 | |||
29 | @Data | ||
30 | class Metamodel { | ||
31 | List<EPackage> packages | ||
32 | EcoreMetamodelDescriptor descriptor | ||
33 | } | ||
34 | |||
35 | class MetamodelLoader { | ||
36 | |||
37 | def private static init() { | ||
38 | EcorePackage.eINSTANCE.EClass | ||
39 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("ecore",new XMIResourceFactoryImpl) | ||
40 | } | ||
41 | |||
42 | public new() { init } | ||
43 | |||
44 | def loadMetamodel(List<String> specification, ResourceSet rs, Context context) { | ||
45 | |||
46 | } | ||
47 | |||
48 | def loadMetamodel(MetamodelSpecification specification, ResourceSet rs, Context context) { | ||
49 | val Map<MetamodelEntry,List<EPackage>> entry2Packages = new HashMap | ||
50 | |||
51 | for(entry : specification.entries) { | ||
52 | val packagesInEntry = entry.path.path.loadPackageFromPath(rs,context) | ||
53 | entry2Packages.put(entry,packagesInEntry) | ||
54 | } | ||
55 | |||
56 | return entry2Packages | ||
57 | } | ||
58 | |||
59 | public def pruneMetamodel(Map<MetamodelEntry,List<EPackage>> specification, ResourceSet rs, Context context) { | ||
60 | val List<EClass> classes = new LinkedList | ||
61 | val List<EEnum> enums = new LinkedList | ||
62 | val List<EEnumLiteral> literals = new LinkedList | ||
63 | val List<EReference> references = new LinkedList | ||
64 | val List<EAttribute> attributes = new LinkedList | ||
65 | |||
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 | } | ||
129 | |||
130 | private def supportedEDataType(EDataType dataType) { | ||
131 | val extension l = EcorePackage.eINSTANCE | ||
132 | return #[EInt, EBoolean, EString, EDouble, EFloat].contains(dataType) | ||
133 | } | ||
134 | |||
135 | private def <T extends ENamedElement> addIfNotExcluded( | ||
136 | List<T> target, | ||
137 | T element, | ||
138 | Set<String> excluded) | ||
139 | { | ||
140 | if(excluded.contains(element.name)) { | ||
141 | target += element | ||
142 | return true | ||
143 | } else { | ||
144 | return false | ||
145 | } | ||
146 | } | ||
147 | private def <T1 extends ENamedElement> addIfNotExcluded( | ||
148 | List<T1> target, | ||
149 | T1 element, | ||
150 | ENamedElement container, | ||
151 | Set<Pair<String,String>> excluded) | ||
152 | { | ||
153 | val pair = (container.name) -> (element.name) | ||
154 | |||
155 | if(excluded.contains(pair)) { | ||
156 | target += element | ||
157 | } | ||
158 | } | ||
159 | |||
160 | private def List<EPackage> loadPackageFromPath(String path, ResourceSet rs, Context context) throws RuntimeException { | ||
161 | var Resource resource; | ||
162 | try{ | ||
163 | resource = rs.getResource(URI.createURI(path),true) | ||
164 | } catch(RuntimeException e) { | ||
165 | context.writeError('''Unable to load EPackage: Error in path "«path»"!''') | ||
166 | return #[] | ||
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 | } | ||
178 | } \ No newline at end of file | ||