aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/MetamodelLoader.xtend
blob: 126dbb7dc0fc785c6ca94554bd8bb4f6ae142333 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package hu.bme.mit.inf.dslreasoner.application.execution

import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.ClassElement
import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.FeatureElement
import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelEntry
import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelSpecification
import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor
import java.util.ArrayList
import java.util.HashMap
import java.util.LinkedList
import java.util.List
import java.util.Map
import java.util.Set
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EDataType
import org.eclipse.emf.ecore.EEnum
import org.eclipse.emf.ecore.EEnumLiteral
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EcorePackage
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.xtend.lib.annotations.Data

@Data
class Metamodel {
	List<EPackage> packages
	EcoreMetamodelDescriptor descriptor
} 

class MetamodelLoader {
	
	def private static init() {
		EcorePackage.eINSTANCE.EClass
		Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("ecore",new XMIResourceFactoryImpl)
	}
	
	public new() { init }
	
	def loadMetamodel(List<String> specification, ResourceSet rs, Context context) {
		
	}
	
	def loadMetamodel(MetamodelSpecification specification, ResourceSet rs, Context context) {
		val Map<MetamodelEntry,List<EPackage>> entry2Packages = new HashMap
		
		for(entry : specification.entries) {
			val packagesInEntry = entry.path.path.loadPackageFromPath(rs,context)
			entry2Packages.put(entry,packagesInEntry)
		}
		
		return entry2Packages
	}
	
	public def pruneMetamodel(Map<MetamodelEntry,List<EPackage>> specification, ResourceSet rs, Context context) {
		val List<EClass> classes = new LinkedList
		val List<EEnum> enums = new LinkedList
		val List<EEnumLiteral> literals = new LinkedList
		val List<EReference> references = new LinkedList
		val List<EAttribute> attributes = new LinkedList
		
		
		/** Add all included types */
		for(entry : specification.entrySet) {
			val metamodelEntry = entry.key
			val packages = entry.value
			
			/** Excluted types */
			val excludedTypeNames = metamodelEntry.excluding.filter(ClassElement).map[name].toSet
			/** Excluded features */
			val excludedFeatureNames = metamodelEntry.excluding.filter(FeatureElement).map[it.container.name -> it.name].toSet
			
			/** Load the types */
			for(package : packages) {
				for(class : package.EClassifiers.filter(EClass)) {
					classes.addIfNotExcluded(class,excludedTypeNames)
				}
				for(enum : package.EClassifiers.filter(EEnum)) {
					val added = enums.addIfNotExcluded(enum,excludedTypeNames)
					if(added) {
						for(literal : enum.ELiterals) {
							literals.addIfNotExcluded(literal,enum,excludedFeatureNames)
						}
					}
				}
			}
		}
		
		/** Add all included references and attributes*/
		for(entry : specification.entrySet) {
			val metamodelEntry = entry.key
			val packages = entry.value
			
			/** Excluded features */
			val excludedFeatureNames = metamodelEntry.excluding.filter(FeatureElement).map[it.container.name -> it.name].toSet
			
			/** See if type is included */
			for(package : packages) {
				for(class : package.EClassifiers.filter(EClass)) {
					if(classes.contains(class)) {
						for(reference : class.EReferences) {
							if(classes.contains(reference.EType)) {
								references.addIfNotExcluded(reference,class,excludedFeatureNames)
							}
						}
						for(attribute : class.EAttributes) {
							val type = attribute.EType
							if(type instanceof EEnum) {
								if(enums.contains(type)) {
									attributes.addIfNotExcluded(attribute,class,excludedFeatureNames)
								} else if(type == EcorePackage.Literals) {
									if(enums.contains(type)) {
										attributes.addIfNotExcluded(attribute,class,excludedFeatureNames)
									}
								}
							} else if(supportedEDataType(type as EDataType)) {
								attributes.addIfNotExcluded(attribute,class,excludedFeatureNames)
							}
						}
					}
				}
			}
		}
	}
	
	private def supportedEDataType(EDataType dataType) {
		val extension l = EcorePackage.eINSTANCE
		return #[EInt, EBoolean, EString, EDouble, EFloat].contains(dataType)
	}
	
	private def <T extends ENamedElement> addIfNotExcluded(
		List<T> target,
		T element,
		Set<String> excluded)
	{
		if(excluded.contains(element.name)) {
			target += element
			return true
		} else {
			return false
		}
	}
	private def <T1 extends ENamedElement> addIfNotExcluded(
		List<T1> target,
		T1 element,
		ENamedElement container,
		Set<Pair<String,String>> excluded)
	{
		val pair = (container.name) -> (element.name)
		
		if(excluded.contains(pair)) {
			target += element
		}
	}
	
	private def List<EPackage> loadPackageFromPath(String path, ResourceSet rs, Context context) throws RuntimeException {
		var Resource resource;
		try{
			resource = rs.getResource(URI.createURI(path),true)
		} catch(RuntimeException e) {
			context.writeError('''Unable to load EPackage: Error in path "«path»"!''')
			return #[]
		}
		val res = new ArrayList<EPackage>(resource.contents.size)
		for(content: resource.contents) {
			if(content instanceof EPackage) {
				res += content
			} else {
				context.writeError('''Unable to load EPackage: The content of "«path»" is not an EPackage, but "«content.eClass.name»"!''')
			}
		}
		return res
	}
}