summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/generator/RBAModelWebGenerator.xtend
blob: 2a9d1e77461986e7bf8fc437cf6d4a634b8773c1 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package rba.tool.editor.endpoint.generator

import java.util.List
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import org.codehaus.groovy.control.CompilerConfiguration;

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext

import rba.tool.editor.model.manager.ResourceManager
import rba.tool.core.sort.SortValueManager;

import rba.core.Allocatable;
import rba.view.Area;
import rba.sound.Zone;
import rba.view.AreaSet;
import rba.sound.ZoneSet;
import rba.core.Content;
import rba.view.ViewContent;
import rba.view.ViewContentSet;
import rba.sound.SoundContent;
import rba.sound.SoundContentSet;
import rba.core.State;
import rba.core.Scene;
import rba.view.Size;
import rba.view.Display
import rba.view.PositionContainer
import rba.core.Package;
import rba.core.Constraint;
import rba.core.Variable;
import rba.core.SetOfOperator;

import rba.tool.editor.generator.z3.SortValueCodeGenerationSupporter;
import rba.tool.core.sort.ISortValueCalculation;

import java.io.OutputStreamWriter
import java.io.FileOutputStream
import java.nio.charset.StandardCharsets
import rba.tool.core.sort.SortValue
import java.util.Arrays
import org.eclipse.emf.ecore.EObject
import java.util.ArrayList
import org.eclipse.emf.ecore.util.EContentsEList
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.common.util.EList

class RBAModelWebGenerator extends AbstractGenerator {
		
	override doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		var isError = false
		
		for(r : resource.resourceSet.resources)
		{
			for(eo : r.allContents.toIterable)
			{				
				//to resolve cross reference 
				var EContentsEList.FeatureIterator<EObject> fi = eo.eCrossReferences.iterator as EContentsEList.FeatureIterator<EObject>;
				while( fi.hasNext() ) {
					var EObject o = fi.next();
					var EReference er = fi.feature() as EReference;
					if (er.getEOpposite() !== null) {
						//System.out.println(eo + "'s " + er.name + " --> " + o);
						if (er.getEOpposite().isMany) {
							var oppositeList =(o.eGet(er.getEOpposite()) as EList<EObject>);
							if (!oppositeList.contains(eo)) {
								oppositeList.add(eo)
							}
						} else {
							
						}
					}
				}
			}
			
			for(w : r.warnings)
			{
				System.err.println(w.toString)
			}
			for(e : r.errors)
			{
				var filename = new File(r.URI.toFileString).name;
				System.err.println("Error: " + filename + ":" + e.line + " " + e.message)
				isError = true
			}
		}	
		
		if(isError) 
		{
			return;	
		}
		
		runScript(WorkingDirectory.get() + "/script", "TemplateGenerator.groovy", resource.resourceSet);		
	}	
	
	def void runScript(String scriptDir, String fileName, ResourceSet resourceSet) {
		try {			
			var dir = new File(scriptDir)
			var file = new File(scriptDir + "/" + fileName)
			if (!dir.exists() || !file.exists()) {
				return;
			}
			var e = new GroovyScriptEngine(#[scriptDir], getClass().getClassLoader())
			var config = new CompilerConfiguration()
			config.setTargetDirectory(scriptDir)
			var RunScriptInternalUtil util = new RunScriptInternalUtil()
			util.setResourceManager(ResourceManager.INSTANCE)
			util.setResourceSet(resourceSet)
			util.loadZ3Lib();
			util.calculate();
			var Binding bind = new Binding()
			bind.setVariable("location", scriptDir)
			bind.setVariable("util", util)
			e.setConfig(config)
			var start_time = System.currentTimeMillis()
			e.run(fileName, bind)
			var finish_time = System.currentTimeMillis()
			removeClassFiles(scriptDir)
			new File(WorkingDirectory.get() + "/" + "success").createNewFile
		} catch (Throwable e) {
			e.printStackTrace()
			System.err.println("error:" + e.getMessage())
		}

	}
	
	def static void removeClassFiles(String folderPath) {
		var File folder = new File(folderPath)
		for (File file : folder.listFiles()) {
			var pattern = Pattern.compile(".class$")
			var Matcher m = pattern.matcher(file.getName())
			if (file.isFile() && m.find()) {
				file.delete()
			}
		}
	}

}

class WorkingDirectory {
	def static String get() {
		return new File(RBAModelWebGenerator.getProtectionDomain().getCodeSource().getLocation().toURI()).parent;
	}
}

class RunScriptInternalUtil {
	ResourceSet resourceSet;
	ResourceManager resourceManager
	private static SortValueCodeGenerationSupporter generationSupporter = new SortValueCodeGenerationSupporter();

	private SortValue areaZorderSortValue = new SortValue();
    private SortValue visibilitySortValue = new SortValue();
    private SortValue csPrioritySortValue = new SortValue();
    
    static String OS_NAME = System.getProperty("os.name").toLowerCase();
    def boolean isLinux() {
        return OS_NAME.startsWith("linux");
    }
    def boolean isWindows() {
        return OS_NAME.startsWith("windows");
    }

	def protected void setSortValueManager(SortValueManager sortValueManager) {
		this.sortValueManager = sortValueManager
	}

	def protected void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager
	}
	
	def protected void setResourceSet(ResourceSet resourceSet) {
		this.resourceSet = resourceSet
	}
	
	def boolean loadZ3Lib() {
		var z3Path = WorkingDirectory.get();
		
		if(isLinux()) {
			z3Path = z3Path + "/lib/linux/z3/bin";
		}
		else if(isWindows()) {
			z3Path = z3Path + "/lib/windows/z3";
		}
		else {
			print("error: Not supported OS");
			return false;
		}
        
        try {
            loadLibs(z3Path);
            addLibraryPath(z3Path);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    def void loadLibs(String z3Path) throws Exception {
    	if(isLinux()) {
    		System.load(z3Path + "/libz3.so");
        	System.load(z3Path + "/libz3java.so");
    	}
    	else if(isWindows()) {
	        System.load(z3Path + "/Microsoft.Z3.dll");
	        System.load(z3Path + "/msvcr110.dll");
	        System.load(z3Path + "/msvcp110.dll");
	        System.load(z3Path + "/vcomp110.dll");
	        System.load(z3Path + "/libz3.dll");
	        System.load(z3Path + "/libz3java.dll");
        }
    }

    def void addLibraryPath(String pathToAdd) throws Exception {
        val usrPathsField = ClassLoader.getDeclaredField("usr_paths");
        usrPathsField.setAccessible(true);
        val paths = usrPathsField.get(null) as String[];
        for (String path : paths) {
            if (path.equals(pathToAdd)) {
                return;
            }
        }
        val newPaths = Arrays.copyOf(paths, paths.length + 1);
        newPaths.set(newPaths.length - 1, pathToAdd);
        usrPathsField.set(null, newPaths);
    }
	
	def void calculate() {
		var code = resourceSet.compile.toString();
		val createdFile = new File("./SortValueCalculation.java");
		try { 
			var writer = new OutputStreamWriter(new FileOutputStream(createdFile), StandardCharsets.UTF_8);
			writer.write(code, 0, code.length());
	    	writer.flush();
	    	writer.close();
		}
		catch(Exception e) {
			System.out.println("error: " + e.toString());
		}
		
		var e = new GroovyScriptEngine("./", getClass().getClassLoader());
        e.loadScriptByName("./SortValueCalculation.java");
        var s = e.getGroovyClassLoader().loadClass("SortValueCalculation");

        var calc = s.newInstance() as ISortValueCalculation;
        calc.setUp();
        areaZorderSortValue.setSortValue(calc.calculateArea_zorder());
        visibilitySortValue.setSortValue(calc.calculateAllocatable_visibility());
        csPrioritySortValue.setSortValue(calc.calculateContentState_priority());
        calc.close();
        
        createdFile.delete();
	}
	
	def compile(ResourceSet resourceSet) '''
		«val allAllocatables = ResourceManager.INSTANCE.getRbaAllocatables(resourceSet)»
		
		«val allAreas = ResourceManager.INSTANCE.getRbaAreas(resourceSet)»
«««		«FOR area : allAreas»
«««			«area.name» : [ «FOR content : area.contents»«content.name», «ENDFOR»]
«««		«ENDFOR»
		«val allContents = ResourceManager.INSTANCE.getRbaContents(resourceSet)»
«««		«FOR content : allContents»
«««			«content.name» : [ «FOR area : content.allocatable»«area.name», «ENDFOR»]
«««		«ENDFOR»
		«generationSupporter.generate(allContents, allAllocatables, allAreas)»
	'''

	def void print(String message) {
		System.out.println(message)
	}

	def List<Allocatable> getAllocatables() {
		return resourceManager.getRbaAllocatables(resourceSet)
	}
	
	def List<Area> getAreas() {
		return resourceManager.getRbaAreas(resourceSet)
	}
	
	def List<Zone> getZones() {		
		return resourceManager.getRbaZones(resourceSet)
	}
	
	def List<AreaSet> getAreaSets() {
		return resourceManager.getRbaAreaSets(resourceSet)
	}
	
	def List<ZoneSet> getZoneSets() {
		return resourceManager.getRbaZoneSets(resourceSet)
	}

	def List<Content> getContents() {
		return resourceManager.getRbaContents(resourceSet)
	}
	
	def List<ViewContent> getViewContents() {
		return resourceManager.getRbaViewContents(resourceSet)
	}

	def List<ViewContentSet> getViewContentSets() {
		return resourceManager.getRbaViewContentSets(resourceSet)
	}
	
	def List<SoundContent> getSoundContents() {
		return resourceManager.getRbaSoundContents(resourceSet)
	}

	def List<SoundContentSet> getSoundContentSets() {
		return resourceManager.getRbaSoundContentSets(resourceSet)
	}
	
	def List<State> getStates() {
		return resourceManager.getRbaState(resourceSet)
	}
	
	def List<Scene> getScenes() {
		return resourceManager.getRbaScenes(resourceSet)
	}
	
	def List<Size> getSizes() {
		return resourceManager.getRbaSizes(resourceSet)
	} 
	
	def List<Display> getDisplays() {
		return resourceManager.getRbaDisplays(resourceSet)
	}

	def List<PositionContainer> getPositionContainers() {
		return resourceManager.getRbaPositionContainers(resourceSet)
	}

	def List<Package> getPackages() {
		return resourceManager.getRbaPackages(resourceSet)
	}

	def List<Package> getRootPackages() {
		return resourceManager.getRbaRootPackages(resourceSet)
	}

	def List<Constraint> getConstraints() {
		return resourceManager.getRbaConstraints(resourceSet)
	}

	def List<Constraint> getOnlineConstraints() {
		return resourceManager.getRbaOnlineConstraints(resourceSet)
	}

	def List<Constraint> getOfflineConstraints() {
		return resourceManager.getRbaOfflineConstraints(resourceSet)
	}
	
	def List<Constraint> getViewConstraints() {
		return resourceManager.getRbaViewConstraints(resourceSet)
	}

	def List<Constraint> getSoundConstraints() {
		return resourceManager.getRbaSoundConstraints(resourceSet)
	}


	def List<Variable> getVariables() {
		return resourceManager.getRbaVariables(resourceSet)
	}
	
	def List<SetOfOperator> getSetOfOperators() {
		return resourceManager.getRbaSetOfOperators(resourceSet)
	}	

	def int getVisibility(String areaName) {
		return visibilitySortValue.getValue(areaName);
	}

	def int getZorder(String areaName) {
		return areaZorderSortValue.getValue(areaName);
	}

	def int getPriority(String contentName, String contentStateName) {
		return csPrioritySortValue.getValue(contentName + "_" + contentStateName);
	}
	
	def <T extends EObject> List<T> collect(Class<T> clazz) {
       var results = new ArrayList<T>();
       var ite = resourceManager.getRbaAllContents(resourceSet).iterator();
       while (ite.hasNext()) {
           var obj = ite.next();
           if (clazz.isAssignableFrom(obj.getClass())) {
               results.add(obj as T);
           }
       }
       return results;
   }
}