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

import hu.bme.mit.inf.dslreasoner.application.ApplicationConfigurationStandaloneSetup
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.ConfigurationScript
import org.eclipse.core.runtime.NullProgressMonitor
import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup
import org.eclipse.emf.ecore.util.EcoreUtil
import java.io.FileNotFoundException
import org.eclipse.emf.ecore.resource.Resource
import java.io.IOException
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.viatra.query.runtime.matchers.backend.IQueryBackendFactoryProvider
import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactoryProvider
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineOptions
import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint
import org.eclipse.viatra.query.runtime.rete.util.ReteHintOptions
import org.eclipse.viatra.query.runtime.rete.matcher.ReteEngine
import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneCompilerSetup
import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetupGenerated
import org.eclipse.xtext.resource.XtextResourceSet
import com.google.inject.Injector
import com.google.inject.Guice

class StandaloneScriptExecutor {
	def static void main(String[] args) {
		if(args.size == 1) {
			val message = executeScript(args.head)
			if(message !== null) {
				println(message)
			}	
		} else if(args.size == 0){
			println('''Run generator with script file path as parameter!''')
		} else { // args.size > 1
			println('''Multiple arguments are not supported! Run generator with script file path as parameter!''')
		}
	}
	
	static def protected Injector internalCreateInjector() {
        var newInjector = new EMFPatternLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
		val PatternLanguageWithRSModule module = new PatternLanguageWithRSModule
		newInjector = Guice.createInjector(module)
		//val XtextInjectorProvider x = null
		return newInjector;
    }
	
	
	def static loadScript(String path) {
		//Initialise extensions
		val i = (new PatternLanguageWithRSSetup).createInjectorAndDoEMFRegistration
		//PatternLanguageWithRSSetup.createInjectorAndDoEMFRegistration
		ApplicationConfigurationStandaloneSetup.doSetup
		Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi",new XMIResourceFactoryImpl)
		Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("logicproblem",new XMIResourceFactoryImpl)
		Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("partialmodel",new XMIResourceFactoryImpl)
		ReteEngine.getClass
		
		val ext = path.split("\\.").last
		if(ext.equals("vsconfig")) {
			
			val resourceSet = //new ResourceSetImpl
				i.getInstance(XtextResourceSet);
			var Resource resource 
			
			try{
				resource = resourceSet.getResource(URI.createURI(path),true)
			} catch(Exception e) {
				val message = '''Unable to load Configuration Script! «e.message»'''
				throw new IllegalArgumentException(message)
			}
			
			EcoreUtil::resolveAll(resource)
			val errors = resource.errors
			if(errors.empty) {
				val content = resource.contents.head
				if(content instanceof ConfigurationScript) {
					return content
				} else {
					val message = '''Content is not a Configuration Script! (Found : «content.class.simpleName»)'''
					throw new IllegalArgumentException(message)
				}
			} else {
				val message =  '''
				The Configuration Script contains «errors.size» error«IF errors.size>1»s«ENDIF»:
					«FOR error : errors»
					«"\t"»«error.message»
					«ENDFOR»
				'''
				throw new IllegalArgumentException(message)
			}
		} else {
			throw new IllegalArgumentException('''Unsupported file extension: «ext»''')
		}
	}
	
	def static executeScript(String path){
		val executor = new ScriptExecutor
		try{
			val content = loadScript(path)
			executor.executeScript(content,new NullProgressMonitor)
			return null
		} catch(Exception e) {
			return e.message
		}
	}
}