aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/util/VQLParser.xtend
blob: e458267cbfcf384c829494b61a0fe9940fefd75e (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
package hu.bme.mit.inf.dslreasoner.application.execution.util

import com.google.inject.Guice
import com.google.inject.Injector
import com.google.inject.Module
import hu.bme.mit.inf.dslreasoner.application.execution.ScriptConsole
import java.util.LinkedHashMap
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageRuntimeModule
import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup
import org.eclipse.viatra.query.patternlanguage.emf.GenmodelExtensionLoader
import org.eclipse.viatra.query.patternlanguage.emf.IGenmodelMappingLoader
import org.eclipse.viatra.query.patternlanguage.emf.scoping.CompoundMetamodelProviderService
import org.eclipse.viatra.query.patternlanguage.emf.scoping.IMetamodelProvider
import org.eclipse.viatra.query.patternlanguage.emf.specification.SpecificationBuilder
import org.eclipse.viatra.query.patternlanguage.emf.vql.PatternModel
import org.eclipse.viatra.query.runtime.api.IQuerySpecification
import org.eclipse.xtext.resource.XtextResourceSet

class MyModule extends EMFPatternLanguageRuntimeModule implements Module {
//    override public Class<? extends IAnnotationValidatorLoader> bindAnnotationValidatorLoader() {
//        return typeof(ExtensionBasedAnnotationValidatorLoader);
//    }
    def public Class<? extends IGenmodelMappingLoader> bindGenmodelMappingLoader() {
        return typeof(GenmodelExtensionLoader);
    }
    override Class<? extends IMetamodelProvider> bindIMetamodelProvider() {
       CompoundMetamodelProviderService
       // PreloadedMetamodelProvider
    }
}

class VQLParser {
	val Injector injector;
	val SpecificationBuilder builder = new SpecificationBuilder
	
	new() {
		EMFPatternLanguageStandaloneSetup.doSetup;
		
		injector = internalCreateInjector
	}
	
	def protected Injector internalCreateInjector() {
       new EMFPatternLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
       val Module module = new MyModule
       val newInjector = Guice.createInjector(module)
       return newInjector;
    }
    
    public def createResourceSet() {
    	injector.getInstance(XtextResourceSet);
    }
	
	/**
	 * Load patterns in two steps: first, the pattern models are are loaded to a resource set, then
	 * the linked patterns are loaded validated and translated to a {@link IQuerySpecification} object.
	 * @returns uri -> (name -> pattern)
	 */
	public def parse(List<String> uris, ResourceSet resourceSet, ScriptConsole context) {
		val res = new LinkedHashMap
		val uri2resource = new LinkedHashMap
		
		for(uri : uris) {
			var Resource resource
			var PatternModel patternModel
			try {
				resource = resourceSet.getResource(URI.createURI(uri),true);
				patternModel = resource.getContents().get(0) as PatternModel;
				uri2resource.put(uri,patternModel)
			} catch(RuntimeException e) {
				context.writeError('''Unable to load patterns from "«uri»"!''')
			}
		}
		
		for(entry : uri2resource.entrySet) {
			val uri = entry.key
			val model = entry.value
			EcoreUtil.resolveAll(model.eResource)
			model.eResource.validate(uri, context)
			
			val map = new LinkedHashMap
			for(pattern : model.patterns) {
				val IQuerySpecification<?> querySpecification = (this.builder as SpecificationBuilder).getOrCreateSpecification(pattern)
				map.put(querySpecification.fullyQualifiedName.split('.').last,querySpecification)
			}
			res.put(uri,map)
		}
		
		return res
	}
	
	def private validate(Resource resource, String URI, ScriptConsole context) {
		val errors = resource.errors
		errors.forEach[context.writeError('''Error in loading pattern "«URI»": «it»''')]
	}
}