diff options
author | 2019-03-19 20:13:18 +0100 | |
---|---|---|
committer | 2019-03-19 20:13:18 +0100 | |
commit | d0a243527533ab589d4b7e69cc361945af2e8953 (patch) | |
tree | fbb78ba178b223d8c062f27f5b9c8a0e88dcdab0 /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme | |
parent | Created new module to load referred patterns. hacked metamodel linking. (diff) | |
download | VIATRA-Generator-d0a243527533ab589d4b7e69cc361945af2e8953.tar.gz VIATRA-Generator-d0a243527533ab589d4b7e69cc361945af2e8953.tar.zst VIATRA-Generator-d0a243527533ab589d4b7e69cc361945af2e8953.zip |
ParseUtil hijacked original vql parser and caused errors - fix by kris7t
https://github.com/kris7t
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/util/ParseUtil.xtend | 209 |
1 files changed, 140 insertions, 69 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/util/ParseUtil.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/util/ParseUtil.xtend index b6eddcd2..8e264488 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/util/ParseUtil.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/util/ParseUtil.xtend | |||
@@ -2,10 +2,11 @@ package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.util | |||
2 | 2 | ||
3 | import com.google.inject.Binder | 3 | import com.google.inject.Binder |
4 | import com.google.inject.Guice | 4 | import com.google.inject.Guice |
5 | import com.google.inject.Inject | ||
5 | import com.google.inject.Injector | 6 | import com.google.inject.Injector |
6 | import com.google.inject.Module | ||
7 | import com.google.inject.multibindings.Multibinder | 7 | import com.google.inject.multibindings.Multibinder |
8 | import com.google.inject.name.Names | 8 | import com.google.inject.name.Names |
9 | import com.google.inject.util.Modules | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage | 10 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage |
10 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage | 11 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage |
11 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage | 12 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage |
@@ -19,6 +20,9 @@ import org.eclipse.emf.common.util.URI | |||
19 | import org.eclipse.emf.ecore.EClassifier | 20 | import org.eclipse.emf.ecore.EClassifier |
20 | import org.eclipse.emf.ecore.EObject | 21 | import org.eclipse.emf.ecore.EObject |
21 | import org.eclipse.emf.ecore.EPackage | 22 | import org.eclipse.emf.ecore.EPackage |
23 | import org.eclipse.emf.ecore.EValidator | ||
24 | import org.eclipse.emf.ecore.impl.EPackageRegistryImpl | ||
25 | import org.eclipse.emf.ecore.impl.EValidatorRegistryImpl | ||
22 | import org.eclipse.emf.ecore.resource.Resource | 26 | import org.eclipse.emf.ecore.resource.Resource |
23 | import org.eclipse.emf.ecore.resource.ResourceSet | 27 | import org.eclipse.emf.ecore.resource.ResourceSet |
24 | import org.eclipse.emf.ecore.util.EcoreUtil | 28 | import org.eclipse.emf.ecore.util.EcoreUtil |
@@ -37,123 +41,190 @@ import org.eclipse.xtext.resource.EObjectDescription | |||
37 | import org.eclipse.xtext.resource.IResourceFactory | 41 | import org.eclipse.xtext.resource.IResourceFactory |
38 | import org.eclipse.xtext.resource.IResourceServiceProvider | 42 | import org.eclipse.xtext.resource.IResourceServiceProvider |
39 | import org.eclipse.xtext.resource.XtextResourceSet | 43 | import org.eclipse.xtext.resource.XtextResourceSet |
44 | import org.eclipse.xtext.resource.impl.ResourceServiceProviderRegistryImpl | ||
40 | import org.eclipse.xtext.scoping.IScope | 45 | import org.eclipse.xtext.scoping.IScope |
41 | import org.eclipse.xtext.scoping.IScopeProvider | 46 | import org.eclipse.xtext.scoping.IScopeProvider |
42 | import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider | 47 | import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider |
43 | import org.eclipse.xtext.scoping.impl.SimpleScope | 48 | import org.eclipse.xtext.scoping.impl.SimpleScope |
44 | import org.eclipse.viatra.query.patternlanguage.emf.scoping.MetamodelProviderService | 49 | import org.eclipse.xtext.service.AbstractGenericModule |
45 | import org.eclipse.viatra.query.patternlanguage.emf.scoping.ResourceSetMetamodelProviderService | 50 | import org.eclipse.xtext.service.SingletonBinding |
51 | import org.eclipse.xtext.workspace.IProjectConfigProvider | ||
46 | 52 | ||
47 | class MyModule extends StandaloneParserWithSeparateModules implements Module{ | 53 | package class StandaloneParserWithFixedMetamodelProviderModule extends StandaloneParserWithSeparateModules { |
48 | 54 | ||
49 | /** | 55 | /** |
50 | * Overrided binder | 56 | * Overrided binder |
51 | * <code>Multibinder</code>IMetamodelProviderInstance.class); | 57 | * <code>Multibinder</code>IMetamodelProviderInstance.class); |
52 | metamodelProviderBinder.addBinding().to(MetamodelProviderService.class); | 58 | * metamodelProviderBinder.addBinding().to(MetamodelProviderService.class); |
53 | metamodelProviderBinder.addBinding().to(ResourceSetMetamodelProviderService.class); | 59 | * metamodelProviderBinder.addBinding().to(ResourceSetMetamodelProviderService.class); |
54 | * </code> | 60 | * </code> |
55 | */ | 61 | */ |
56 | override configureIScopeProviderDelegate(Binder binder) { | 62 | override configureIScopeProviderDelegate(Binder binder) { |
57 | binder.bind(IScopeProvider).annotatedWith(Names.named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)) | 63 | binder.bind(IScopeProvider).annotatedWith(Names.named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).to( |
58 | .to(EMFPatternLanguageImportNamespaceProvider); | 64 | EMFPatternLanguageImportNamespaceProvider); |
59 | Multibinder::newSetBinder(binder, IMetamodelProviderInstance); | 65 | Multibinder::newSetBinder(binder, IMetamodelProviderInstance); |
60 | } | 66 | } |
61 | 67 | ||
62 | override Class<? extends IMetamodelProvider> bindIMetamodelProvider() { | 68 | } |
63 | FixedMetamodelProvider | 69 | |
64 | } | 70 | package class StandaloneParserOverridesModule extends AbstractGenericModule { |
71 | |||
72 | def Class<? extends IMetamodelProvider> bindIMetamodelProvider() { | ||
73 | FixedMetamodelProvider | ||
74 | } | ||
75 | |||
76 | def Class<? extends IProjectConfigProvider> bindProjectConfigProvider() { | ||
77 | NullProjectConfigProvider | ||
78 | } | ||
79 | |||
80 | @SingletonBinding | ||
81 | def Class<? extends IResourceServiceProvider.Registry> bindIResourceServiceProvider$Registry() { | ||
82 | VqlDeactivatedServiceProviderRegistry | ||
83 | } | ||
84 | |||
85 | def EValidator.Registry bindEValidator$Registry() { | ||
86 | // org.eclipse.xtext.validation.EValidatorRegistrar modifies EValidators already in the registry, | ||
87 | // so it is not safe to populate the registry from the EValidator.Registry.INSTANCE singleton. | ||
88 | // There is no need to execute any EValiator other than EMFPatternLanguageValidator, | ||
89 | // so we can start with a blank registry instead. | ||
90 | new EValidatorRegistryImpl() | ||
91 | } | ||
92 | |||
93 | def EPackage.Registry bindEPackage$Registry() { | ||
94 | new EPackageRegistryImpl(EPackage.Registry.INSTANCE) | ||
95 | } | ||
96 | |||
97 | } | ||
98 | |||
99 | package class NullProjectConfigProvider implements IProjectConfigProvider { | ||
100 | |||
101 | override getProjectConfig(ResourceSet context) { | ||
102 | null | ||
103 | } | ||
104 | |||
105 | } | ||
106 | |||
107 | package class VqlDeactivatedServiceProviderRegistry extends ResourceServiceProviderRegistryImpl { | ||
108 | |||
109 | new() { | ||
110 | val delegateRegistry = IResourceServiceProvider.Registry.INSTANCE | ||
111 | contentTypeToFactoryMap.putAll(delegateRegistry.contentTypeToFactoryMap) | ||
112 | extensionToFactoryMap.putAll(delegateRegistry.extensionToFactoryMap) | ||
113 | protocolToFactoryMap.putAll(delegateRegistry.protocolToFactoryMap) | ||
114 | } | ||
115 | |||
116 | @Inject | ||
117 | def void setResourceServiceProvider(IResourceServiceProvider vqlDeactivatedResourceServiceProvider) { | ||
118 | extensionToFactoryMap.put(ParseUtil.SIMPLIFIED_PARSER_EXTENSION, vqlDeactivatedResourceServiceProvider) | ||
119 | } | ||
65 | } | 120 | } |
66 | 121 | ||
67 | class ParseUtil { | 122 | class ParseUtil { |
123 | public static val PARSER_EXTENSION = "vql" | ||
68 | public static val SIMPLIFIED_PARSER_EXTENSION = "vql_deactivated" | 124 | public static val SIMPLIFIED_PARSER_EXTENSION = "vql_deactivated" |
69 | 125 | ||
70 | val Injector injector; | 126 | val Injector injector |
127 | val IResourceFactory resourceFactory | ||
128 | |||
71 | new() { | 129 | new() { |
72 | PartialinterpretationPackage.eINSTANCE.class; | 130 | PartialinterpretationPackage.eINSTANCE.class; |
73 | LogicproblemPackage.eINSTANCE.class; | 131 | LogicproblemPackage.eINSTANCE.class; |
74 | LogiclanguagePackage.eINSTANCE.class; | 132 | LogiclanguagePackage.eINSTANCE.class; |
75 | |||
76 | injector = internalCreateInjector | 133 | injector = internalCreateInjector |
77 | 134 | resourceFactory = injector.getInstance(IResourceFactory) | |
78 | val IResourceFactory resourceFactory = injector.getInstance(IResourceFactory); | ||
79 | val IResourceServiceProvider serviceProvider = injector.getInstance(IResourceServiceProvider); | ||
80 | Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(SIMPLIFIED_PARSER_EXTENSION, resourceFactory); | ||
81 | IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put(SIMPLIFIED_PARSER_EXTENSION, serviceProvider); | ||
82 | } | 135 | } |
83 | 136 | ||
84 | def protected Injector internalCreateInjector() { | 137 | def protected Injector internalCreateInjector() { |
85 | var newInjector = new EMFPatternLanguageStandaloneSetup().createInjectorAndDoEMFRegistration(); | 138 | ensureViatraInitialized(); |
86 | val Module module = new MyModule | 139 | val runtimeModulemodule = new StandaloneParserWithFixedMetamodelProviderModule |
87 | newInjector = Guice.createInjector(module) | 140 | val overridesModule = new StandaloneParserOverridesModule |
88 | return newInjector; | 141 | val module = Modules.override(runtimeModulemodule).with(overridesModule) |
89 | } | 142 | val newInjector = Guice.createInjector(module) |
90 | 143 | return newInjector | |
144 | } | ||
145 | |||
146 | protected def ensureViatraInitialized() { | ||
147 | val extensionToFactoryMap = Resource.Factory.Registry.INSTANCE.extensionToFactoryMap | ||
148 | if (!extensionToFactoryMap.containsKey(PARSER_EXTENSION)) { | ||
149 | EMFPatternLanguageStandaloneSetup.doSetup | ||
150 | } | ||
151 | } | ||
152 | |||
153 | protected def createResourceSet() { | ||
154 | val XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet) | ||
155 | resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put(SIMPLIFIED_PARSER_EXTENSION, resourceFactory) | ||
156 | resourceSet | ||
157 | } | ||
158 | |||
91 | val builder = new SpecificationBuilder | 159 | val builder = new SpecificationBuilder |
92 | 160 | ||
93 | public def parse(CharSequence vqlFileContent) { | 161 | def parse(CharSequence vqlFileContent) { |
94 | val XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet); | 162 | val resourceSet = createResourceSet(); |
95 | val Resource resource = resourceSet.createResource(URI.createURI("dummy:/example."+SIMPLIFIED_PARSER_EXTENSION)); | 163 | val Resource resource = resourceSet.createResource( |
164 | URI.createURI("dummy:/example." + SIMPLIFIED_PARSER_EXTENSION)); | ||
96 | val InputStream in = new ByteArrayInputStream(vqlFileContent.toString.getBytes()); | 165 | val InputStream in = new ByteArrayInputStream(vqlFileContent.toString.getBytes()); |
97 | resource.load(in, resourceSet.getLoadOptions()); | 166 | resource.load(in, resourceSet.getLoadOptions()); |
98 | val patternModel = resource.getContents().get(0) as PatternModel; | 167 | val patternModel = resource.getContents().get(0) as PatternModel; |
99 | 168 | ||
100 | EcoreUtil.resolveAll(resource) | 169 | EcoreUtil.resolveAll(resource) |
101 | resource.validate | 170 | resource.validate |
102 | val res = new LinkedHashMap<String,IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> | 171 | val res = new LinkedHashMap<String, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> |
103 | val patterns = new LinkedList<IQuerySpecification<?>> | 172 | val patterns = new LinkedList<IQuerySpecification<?>> |
104 | for(pattern : patternModel.patterns) { | 173 | for (pattern : patternModel.patterns) { |
105 | val querySpecification = createSpecification(pattern,patterns) | 174 | val querySpecification = createSpecification(pattern, patterns) |
106 | res.put(querySpecification.fullyQualifiedName,querySpecification) | 175 | res.put(querySpecification.fullyQualifiedName, querySpecification) |
107 | patterns += querySpecification | 176 | patterns += querySpecification |
108 | } | 177 | } |
109 | resource.validate | 178 | resource.validate |
110 | 179 | ||
111 | return res | 180 | return res |
112 | } | 181 | } |
113 | 182 | ||
114 | def IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> createSpecification( | 183 | def IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> createSpecification(Pattern pattern, |
115 | Pattern pattern,List<IQuerySpecification<?>> createdPatternList) | 184 | List<IQuerySpecification<?>> createdPatternList) { |
116 | { | 185 | builder.getOrCreateSpecification(pattern, createdPatternList, true) |
117 | builder.getOrCreateSpecification(pattern,createdPatternList,true) | ||
118 | } | 186 | } |
119 | 187 | ||
120 | def private validate(Resource resource) { | 188 | def private validate(Resource resource) { |
121 | val errors = resource.errors | 189 | val errors = resource.errors |
122 | if(!errors.isEmpty) { | 190 | if (!errors.isEmpty) { |
123 | println('''-- Errors --''') | 191 | println('''-- Errors --''') |
124 | errors.forEach[println('''> «it»''')] | 192 | errors.forEach[println('''> «it»''')] |
125 | println('''------------''') | 193 | println('''------------''') |
126 | } | 194 | } |
127 | } | 195 | } |
128 | } | 196 | } |
129 | 197 | ||
130 | class FixedMetamodelProvider implements IMetamodelProvider { | 198 | class FixedMetamodelProvider implements IMetamodelProvider { |
131 | 199 | ||
132 | protected val List<EPackage> packages = new LinkedList | 200 | protected val List<EPackage> packages = new LinkedList |
201 | |||
133 | new() { | 202 | new() { |
134 | packages += PartialinterpretationPackage.eINSTANCE | 203 | packages += PartialinterpretationPackage.eINSTANCE |
135 | packages += LogicproblemPackage.eINSTANCE | 204 | packages += LogicproblemPackage.eINSTANCE |
136 | packages += LogiclanguagePackage.eINSTANCE | 205 | packages += LogiclanguagePackage.eINSTANCE |
137 | } | 206 | } |
138 | 207 | ||
139 | override getAllMetamodelObjects(IScope delegateScope, EObject context) { | 208 | override getAllMetamodelObjects(IScope delegateScope, EObject context) { |
140 | val metamodels = packages.map[EObjectDescription.create(it.nsURI, it, Collections.singletonMap("nsURI", "true"))] | 209 | val metamodels = packages.map [ |
141 | return new SimpleScope(delegateScope, metamodels); | 210 | EObjectDescription.create(it.nsURI, it, Collections.singletonMap("nsURI", "true")) |
142 | } | 211 | ] |
143 | 212 | return new SimpleScope(delegateScope, metamodels); | |
144 | override boolean isGeneratedCodeAvailable(EPackage ePackage, ResourceSet set) { | 213 | } |
145 | true | 214 | |
146 | } | 215 | override boolean isGeneratedCodeAvailable(EPackage ePackage, ResourceSet set) { |
147 | 216 | true | |
148 | override String getModelPluginId(EPackage ePackage, ResourceSet set) { | 217 | } |
149 | return "hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage" | 218 | |
150 | } | 219 | override String getModelPluginId(EPackage ePackage, ResourceSet set) { |
151 | 220 | return "hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage" | |
152 | override String getQualifiedClassName(EClassifier classifier, EObject context) { | 221 | } |
153 | classifier.name | 222 | |
154 | } | 223 | override String getQualifiedClassName(EClassifier classifier, EObject context) { |
155 | 224 | classifier.name | |
225 | } | ||
226 | |||
156 | override loadEPackage(String uri, ResourceSet resourceSet) { | 227 | override loadEPackage(String uri, ResourceSet resourceSet) { |
157 | return packages.filter[it.nsURI.equals(uri)].head | 228 | return packages.filter[it.nsURI.equals(uri)].head |
158 | } | 229 | } |
159 | } \ No newline at end of file | 230 | } |