diff options
author | 2018-01-24 11:42:29 +0100 | |
---|---|---|
committer | 2018-01-24 11:42:29 +0100 | |
commit | 7b40388ea6bb96ab3adacf338b1bc30d06474df4 (patch) | |
tree | e925aafff7f40a6f1a82a1f5b653d625f3308878 /Application/hu.bme.mit.inf.dslreasoner.application/src | |
parent | New Scope format: #String += 2..* (diff) | |
download | VIATRA-Generator-7b40388ea6bb96ab3adacf338b1bc30d06474df4.tar.gz VIATRA-Generator-7b40388ea6bb96ab3adacf338b1bc30d06474df4.tar.zst VIATRA-Generator-7b40388ea6bb96ab3adacf338b1bc30d06474df4.zip |
Loading services
Diffstat (limited to 'Application/hu.bme.mit.inf.dslreasoner.application/src')
5 files changed, 327 insertions, 150 deletions
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/Context.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/Context.xtend index 8f72caed..c00c0d31 100644 --- a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/Context.xtend +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/Context.xtend | |||
@@ -1,9 +1,5 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.execution | 1 | package hu.bme.mit.inf.dslreasoner.application.execution |
2 | 2 | ||
3 | import java.io.OutputStream | ||
4 | import java.util.LinkedList | ||
5 | import java.util.List | ||
6 | import java.io.FileWriter | ||
7 | import org.eclipse.xtend.lib.annotations.Accessors | 3 | import org.eclipse.xtend.lib.annotations.Accessors |
8 | 4 | ||
9 | class Context { | 5 | class Context { |
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 |
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/QueryLoader.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/QueryLoader.xtend index 8c064cd3..6e725086 100644 --- a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/QueryLoader.xtend +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/QueryLoader.xtend | |||
@@ -1,28 +1,128 @@ | |||
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.AllPatternEntry | ||
4 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.ConfigurationScript | ||
5 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.PatternElement | ||
6 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.PatternEntry | ||
3 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.PatternSpecification | 7 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.PatternSpecification |
4 | import hu.bme.mit.inf.dslreasoner.application.execution.util.VQLParser | 8 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.ViatraImport |
5 | import java.util.LinkedHashMap | 9 | import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor |
6 | import org.eclipse.emf.ecore.resource.ResourceSet | 10 | import java.util.HashMap |
11 | import java.util.LinkedHashSet | ||
12 | import java.util.List | ||
13 | import java.util.Set | ||
14 | import org.eclipse.emf.ecore.EClass | ||
15 | import org.eclipse.emf.ecore.EPackage | ||
16 | import org.eclipse.viatra.query.patternlanguage.emf.eMFPatternLanguage.PatternModel | ||
17 | import org.eclipse.viatra.query.patternlanguage.emf.specification.SpecificationBuilder | ||
18 | import org.eclipse.viatra.query.patternlanguage.patternLanguage.Pattern | ||
19 | import org.eclipse.viatra.query.patternlanguage.patternLanguage.PatternCompositionConstraint | ||
20 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification | ||
21 | import org.eclipse.xtext.EcoreUtil2 | ||
7 | 22 | ||
8 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | 23 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* |
9 | 24 | ||
10 | class QueryLoader { | 25 | class QueryLoader { |
11 | val parser = new VQLParser | 26 | //val parser = new VQLParser |
27 | val builder = new SpecificationBuilder | ||
12 | 28 | ||
13 | def loadQueries(PatternSpecification specification, ResourceSet rs, Context context) { | 29 | def loadQueries(PatternSpecification specification) { |
14 | val uris = specification.entries.map[path].toList | 30 | val patterns = new LinkedHashSet |
15 | val models = parser.parse(uris,rs,context) | ||
16 | 31 | ||
17 | val res = new LinkedHashMap | 32 | for(entry: specification.entries) { |
18 | for(entry : specification.entries) { | 33 | patterns += getPatterns(entry) |
19 | res.put(entry,entry.path.lookup(models)) | ||
20 | } | 34 | } |
21 | 35 | ||
22 | return res | 36 | val allConcernedPatterns = patterns.allReferredPatterns |
37 | val pattern2Specification = allConcernedPatterns.translatePatterns() | ||
38 | |||
39 | val List<IQuerySpecification<?>> patternsToTranslate = | ||
40 | pattern2Specification.values.toList | ||
41 | val Set<IQuerySpecification<?>> validationPatterns = | ||
42 | patterns.filter[it.annotations.exists[it.name.equals("Constraint")]] | ||
43 | .map[lookup(pattern2Specification)] | ||
44 | .toSet | ||
45 | val derivedFeatures = calculateDerivedFeatures(patterns.referredEcoreModels,patterns.map[it.lookup(pattern2Specification)]) | ||
46 | |||
47 | return new ViatraQuerySetDescriptor( | ||
48 | patternsToTranslate, | ||
49 | validationPatterns, | ||
50 | derivedFeatures | ||
51 | ) -> allConcernedPatterns | ||
23 | } | 52 | } |
24 | 53 | ||
25 | def pruneQueries(PatternSpecification specification, ResourceSet rs, Context context) { | 54 | def dispatch List<Pattern> getPatterns(AllPatternEntry entry) { |
55 | val excluded = entry.exclusuion.map[getPatterns] | ||
56 | val referredPatternModels = allPatternsWithSamePackage(entry,entry.package as PatternModel).toSet | ||
57 | val patterns = referredPatternModels.map[patterns].flatten | ||
58 | return patterns.filter[!excluded.contains(it)].toList | ||
59 | } | ||
60 | |||
61 | def dispatch List<Pattern> getPatterns(PatternElement entry) { | ||
62 | return #[entry.pattern] | ||
63 | } | ||
64 | |||
65 | def private allPatternsWithSamePackage(PatternEntry entry, PatternModel model) { | ||
66 | val packageURI = model.packageName | ||
67 | val document = EcoreUtil2.getContainerOfType(entry,ConfigurationScript) | ||
68 | val viatraImportsWithSamePackage = document.imports | ||
69 | .filter(ViatraImport) | ||
70 | .map[it.importedViatra] | ||
71 | .filterNull | ||
72 | .filter[packageName.equals(packageURI)] | ||
73 | return viatraImportsWithSamePackage | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Adds all referred patterns to a given set of patterns. | ||
78 | */ | ||
79 | def private Set<Pattern> allReferredPatterns(Set<Pattern> patterns) { | ||
80 | val res = new LinkedHashSet | ||
81 | res+=patterns | ||
26 | 82 | ||
83 | var boolean changed | ||
84 | do{ | ||
85 | changed = false | ||
86 | val newElements = res.map[directlyReferredPatterns].flatten.filter[!res.contains(it)] | ||
87 | if(!newElements.empty) { | ||
88 | changed = true | ||
89 | res.addAll(newElements) | ||
90 | } | ||
91 | } while(changed) | ||
92 | |||
93 | return res | ||
94 | } | ||
95 | |||
96 | def private directlyReferredPatterns(Pattern pattern) { | ||
97 | return pattern | ||
98 | .bodies | ||
99 | .map[constraints] | ||
100 | .flatten | ||
101 | .filter(PatternCompositionConstraint) | ||
102 | .map[call.patternRef].toSet | ||
103 | } | ||
104 | |||
105 | def private referredEcoreModels(Set<Pattern> patterns) { | ||
106 | patterns.map[eContainer as PatternModel].map[it.importPackages.packageImport.map[it.EPackage].filterNull].flatten.toSet | ||
107 | } | ||
108 | |||
109 | def private translatePatterns(Set<Pattern> xtextPattern) { | ||
110 | xtextPattern.toInvertedMap[builder.getOrCreateSpecification(it) as IQuerySpecification<?>] | ||
111 | } | ||
112 | |||
113 | def private calculateDerivedFeatures(Set<EPackage> packages, Iterable<IQuerySpecification<?>> patterns) { | ||
114 | val features = packages.map[EClassifiers].flatten.filter(EClass).map[it.EStructuralFeatures].flatten | ||
115 | val res = new HashMap | ||
116 | for(feature : features) { | ||
117 | val QBFAnnotation = feature.EAnnotations.filter[it.source.equals("org.eclipse.viatra.query.querybasedfeature")].head | ||
118 | if(QBFAnnotation !== null) { | ||
119 | val targetFQN = QBFAnnotation.details.get("patternFQN") | ||
120 | val referredPattern = patterns.filter[it.fullyQualifiedName.equals(targetFQN)].head | ||
121 | if(referredPattern!== null) { | ||
122 | res.put(referredPattern, feature) | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | return res | ||
27 | } | 127 | } |
28 | } \ No newline at end of file | 128 | } \ No newline at end of file |
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/MetamodelValidator.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/MetamodelValidator.xtend new file mode 100644 index 00000000..aae47c83 --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/MetamodelValidator.xtend | |||
@@ -0,0 +1,76 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.execution.validation | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
4 | import java.util.LinkedList | ||
5 | import java.util.List | ||
6 | import org.eclipse.emf.ecore.EAttribute | ||
7 | import org.eclipse.emf.ecore.EClass | ||
8 | import org.eclipse.emf.ecore.EDataType | ||
9 | import org.eclipse.emf.ecore.EEnum | ||
10 | import org.eclipse.emf.ecore.EEnumLiteral | ||
11 | import org.eclipse.emf.ecore.EReference | ||
12 | import org.eclipse.emf.ecore.EcorePackage | ||
13 | |||
14 | class MetamodelValidator { | ||
15 | def validateMetamodel(EcoreMetamodelDescriptor descriptor) { | ||
16 | val errors = new LinkedList | ||
17 | this.allSupertypesAreIncluded(descriptor.classes,errors) | ||
18 | this.allEnumTypesareIncluded(descriptor.enums,descriptor.literals,errors) | ||
19 | this.referenceEndpointsAreIncluded(descriptor.classes,descriptor.references,errors) | ||
20 | this.attributeSourcePontIsIncluded(descriptor.classes,descriptor.attributes,errors) | ||
21 | this.attributeTargetIsSupported(descriptor.attributes,errors) | ||
22 | return errors | ||
23 | } | ||
24 | |||
25 | |||
26 | private def supportedEDataType(EDataType dataType) { | ||
27 | val extension l = EcorePackage.eINSTANCE | ||
28 | return #[EInt, EBoolean, EString, EDouble, EFloat].contains(dataType) | ||
29 | } | ||
30 | |||
31 | def allSupertypesAreIncluded(List<EClass> classes, List<String> errors) { | ||
32 | for(c : classes) { | ||
33 | for(s : c.ESuperTypes) { | ||
34 | if(!classes.contains(s)) { | ||
35 | errors.add('''Metamodel contains "«c.name»" but not contains its supertype "«s.name»"!''') | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | def allEnumTypesareIncluded(List<EEnum> enums, List<EEnumLiteral> literals, List<String> errors) { | ||
41 | for(l: literals) { | ||
42 | if(!enums.contains(l.EEnum)) { | ||
43 | errors.add('''Metamodel contains literal "«l.name»" but does not contains enum "«l.EEnum.name»"!''') | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | def referenceEndpointsAreIncluded(List<EClass> classes, List<EReference> references, List<String> errors) { | ||
48 | for(reference : references) { | ||
49 | val src = reference.EContainingClass | ||
50 | if(!classes.contains(src)) { | ||
51 | errors.add('''Metamodel contains reference "«reference.name»" but does not contains its source type "«src.name»"!''') | ||
52 | } | ||
53 | |||
54 | val trg = reference.EReferenceType | ||
55 | if(!classes.contains(trg)) { | ||
56 | errors.add('''Metamodel contains reference "«reference.name»" but does not contains its target type "«trg.name»"!''') | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | def attributeSourcePontIsIncluded(List<EClass> classes, List<EAttribute> attributes, List<String> errors) { | ||
61 | for(attribute : attributes) { | ||
62 | val src = attribute.containerClass | ||
63 | if(!classes.contains(src)) { | ||
64 | errors.add('''Metamodel contains attribute "«attribute.name»" but does not contains its source type "«src.name»"!''') | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | def attributeTargetIsSupported(List<EAttribute> attributes, List<String> errors) { | ||
69 | for(attribute: attributes) { | ||
70 | val trg = attribute.EAttributeType | ||
71 | if(!supportedEDataType(trg)) { | ||
72 | errors.add('''Metamodels contains attribute "«attribute.name»" with unsupported type "«trg.name»"!''') | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | } \ No newline at end of file | ||
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/QueryAndMetamodelValidator.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/QueryAndMetamodelValidator.xtend new file mode 100644 index 00000000..e1a20ffd --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/validation/QueryAndMetamodelValidator.xtend | |||
@@ -0,0 +1,53 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.execution.validation | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.EcoreMetamodelDescriptor | ||
4 | import hu.bme.mit.inf.dslreasoner.viatra2logic.ViatraQuerySetDescriptor | ||
5 | import java.util.List | ||
6 | import java.util.Set | ||
7 | import org.eclipse.emf.ecore.ENamedElement | ||
8 | import org.eclipse.viatra.query.patternlanguage.patternLanguage.Pattern | ||
9 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification | ||
10 | import java.util.LinkedList | ||
11 | |||
12 | class QueryAndMetamodelValidator { | ||
13 | |||
14 | def public validateQueryAndMetamodel( | ||
15 | EcoreMetamodelDescriptor metamodel, | ||
16 | Pair<ViatraQuerySetDescriptor, Set<Pattern>> viatra) | ||
17 | { | ||
18 | val patterns = viatra.value | ||
19 | val elementsInMetamodel = | ||
20 | (metamodel.classes + | ||
21 | metamodel.enums + | ||
22 | metamodel.literals+ | ||
23 | metamodel.attributes + | ||
24 | metamodel.references) | ||
25 | .toSet | ||
26 | val errors = new LinkedList | ||
27 | for(pattern : patterns) { | ||
28 | val elements = getReferredNamedElements(pattern) | ||
29 | for(element : elements) { | ||
30 | if(!elementsInMetamodel.contains(element)) { | ||
31 | errors+='''Pattern "«pattern.name»" refers to an element "«element»" that is not included to the selected metamodel!''' | ||
32 | } | ||
33 | } | ||
34 | } | ||
35 | return errors | ||
36 | } | ||
37 | |||
38 | def getReferredNamedElements(Pattern pattern) { | ||
39 | val elements = pattern.eAllContents.toList | ||
40 | return elements.map[element| | ||
41 | val references = element.eClass.EAllReferences | ||
42 | return references.map[r | | ||
43 | if(r.isMany) { | ||
44 | return (element.eGet(r) as List<?>).filter(ENamedElement) | ||
45 | } else { | ||
46 | val value = element.eGet(r) | ||
47 | if(value instanceof ENamedElement) return #[value] | ||
48 | else return #[] | ||
49 | } | ||
50 | ].flatten.filter(ENamedElement) | ||
51 | ].flatten | ||
52 | } | ||
53 | } \ No newline at end of file | ||