diff options
author | 2017-06-21 18:44:04 +0200 | |
---|---|---|
committer | 2017-06-21 18:44:04 +0200 | |
commit | cf0c50e4fedf219f79a462615bb0ba603ce99378 (patch) | |
tree | a87bfbd1b2cd77c66ef1ab2106f9a9295cf97cfb /Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor | |
parent | generated code for the newly added types (diff) | |
download | VIATRA-Generator-cf0c50e4fedf219f79a462615bb0ba603ce99378.tar.gz VIATRA-Generator-cf0c50e4fedf219f79a462615bb0ba603ce99378.tar.zst VIATRA-Generator-cf0c50e4fedf219f79a462615bb0ba603ce99378.zip |
Model translator and sanitiser from original yakindu files to simplified
metamodel.
Added a sample Main function.
Diffstat (limited to 'Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor')
3 files changed, 205 insertions, 0 deletions
diff --git a/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Main.java b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Main.java new file mode 100644 index 00000000..ec525fd6 --- /dev/null +++ b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Main.java | |||
@@ -0,0 +1,44 @@ | |||
1 | package hu.bme.mit.inf.yakinduModelExtractor; | ||
2 | |||
3 | import java.util.List; | ||
4 | |||
5 | import org.eclipse.emf.common.util.TreeIterator; | ||
6 | import org.eclipse.emf.ecore.EObject; | ||
7 | import org.yakindu.sct.model.sgraph.State; | ||
8 | import org.yakindu.sct.model.sgraph.Statechart; | ||
9 | |||
10 | public class Main { | ||
11 | |||
12 | public static void main(String[] args) { | ||
13 | ModelManager manager = new ModelManager(); | ||
14 | Yakindu2CommonModel converter = new Yakindu2CommonModel(); | ||
15 | |||
16 | String years[] = {"2015","2016","2017"}; | ||
17 | for(String year : years) { | ||
18 | String folderPath = "D:\\Eclipse\\GIT\\RemoHF\\"+year; | ||
19 | // Loading folder | ||
20 | List<String> paths = manager.loadAllModelPathsInDirectory(folderPath); | ||
21 | |||
22 | int index = 1; | ||
23 | for(String path : paths) { | ||
24 | Statechart s = null; | ||
25 | try{ | ||
26 | EObject root = manager.loadModel(path); | ||
27 | s = (Statechart) root; | ||
28 | } catch (Exception e) { | ||
29 | System.out.println("unable to load: "+ path); | ||
30 | } | ||
31 | |||
32 | if(s!=null) { | ||
33 | EObject s2 = converter.transform(s); | ||
34 | manager.saveModel(s2, "output/R"+year+index+".xmi"); | ||
35 | } | ||
36 | System.out.println(year + " Progress: " + 100.0*index/paths.size() + "%"); | ||
37 | index++; | ||
38 | } | ||
39 | |||
40 | System.out.println(year + " done."); | ||
41 | } | ||
42 | System.out.println("All done."); | ||
43 | } | ||
44 | } | ||
diff --git a/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java new file mode 100644 index 00000000..85fd208c --- /dev/null +++ b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java | |||
@@ -0,0 +1,79 @@ | |||
1 | package hu.bme.mit.inf.yakinduModelExtractor; | ||
2 | |||
3 | import java.io.BufferedWriter; | ||
4 | import java.io.File; | ||
5 | import java.io.FileOutputStream; | ||
6 | import java.io.IOException; | ||
7 | import java.io.OutputStreamWriter; | ||
8 | import java.io.Writer; | ||
9 | import java.util.LinkedList; | ||
10 | import java.util.List; | ||
11 | |||
12 | import org.eclipse.emf.common.util.URI; | ||
13 | import org.eclipse.emf.ecore.EObject; | ||
14 | import org.eclipse.emf.ecore.resource.Resource; | ||
15 | import org.eclipse.emf.ecore.resource.ResourceSet; | ||
16 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | ||
17 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; | ||
18 | import org.eclipse.gmf.runtime.notation.NotationPackage; | ||
19 | import org.yakindu.sct.model.sgraph.SGraphPackage; | ||
20 | |||
21 | public class ModelManager { | ||
22 | ResourceSet resourceSet; | ||
23 | |||
24 | public ModelManager() { | ||
25 | init(); | ||
26 | } | ||
27 | |||
28 | public void init() { | ||
29 | SGraphPackage.eINSTANCE.eClass(); | ||
30 | NotationPackage.eINSTANCE.eClass(); | ||
31 | resourceSet = new ResourceSetImpl(); | ||
32 | Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); | ||
33 | } | ||
34 | |||
35 | public List<String> loadAllModelPathsInDirectory(String path) { | ||
36 | File directory = new File(path); | ||
37 | if(directory.exists() && directory.isDirectory()) { | ||
38 | List<String> filePaths = new LinkedList<>(); | ||
39 | for(File f : directory.listFiles()) { | ||
40 | if(f.isFile()) { | ||
41 | String filePath = f.getPath(); | ||
42 | if(filePath.endsWith("sct")) { | ||
43 | filePaths.add(filePath); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | return filePaths; | ||
48 | } else { | ||
49 | throw new IllegalArgumentException("invalid path"); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | public EObject loadModel(String path) { | ||
54 | Resource resource = this.resourceSet.getResource(URI.createFileURI(path), true); | ||
55 | return resource.getContents().get(0); | ||
56 | } | ||
57 | |||
58 | public boolean saveModel(EObject root, String path) { | ||
59 | Resource resource = this.resourceSet.createResource(URI.createURI(path)); | ||
60 | resource.getContents().add(root); | ||
61 | try { | ||
62 | resource.save(null); | ||
63 | return true; | ||
64 | } catch (IOException e) { | ||
65 | System.err.println("Unable to save file: "+path); | ||
66 | return false; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | public boolean saveFile(String path, String content) { | ||
71 | try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "utf-8"))) { | ||
72 | writer.write(content.toString()); | ||
73 | return true; | ||
74 | } catch (IOException ex) { | ||
75 | System.err.println("Unable to save file: "+path); | ||
76 | return false; | ||
77 | } | ||
78 | } | ||
79 | } | ||
diff --git a/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Yakindu2CommonModel.xtend b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Yakindu2CommonModel.xtend new file mode 100644 index 00000000..d9219d5a --- /dev/null +++ b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/Yakindu2CommonModel.xtend | |||
@@ -0,0 +1,82 @@ | |||
1 | package hu.bme.mit.inf.yakinduModelExtractor | ||
2 | |||
3 | import org.yakindu.sct.model.sgraph.Statechart | ||
4 | import org.yakindu.sct.model.sgraph.SGraphPackage | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.YakindummPackage | ||
6 | import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.YakindummFactory | ||
7 | import org.eclipse.emf.ecore.EObject | ||
8 | import org.eclipse.emf.ecore.EClass | ||
9 | import java.util.HashMap | ||
10 | import org.eclipse.emf.ecore.EReference | ||
11 | import java.util.List | ||
12 | |||
13 | class Yakindu2CommonModel { | ||
14 | val yakinduSGraphPackage = org.yakindu.sct.model.sgraph.SGraphPackage.eINSTANCE | ||
15 | val commonSGraphPackage = YakindummPackage.eINSTANCE | ||
16 | val extension YakindummFactory factory = YakindummFactory.eINSTANCE | ||
17 | |||
18 | def transform(Statechart s) { | ||
19 | val o2o = new HashMap | ||
20 | o2o.put(s,s.copyObject) | ||
21 | for(content: s.eAllContents.toIterable) { | ||
22 | val copied = content.copyObject | ||
23 | if(copied !== null) { | ||
24 | o2o.put(content,copied) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | for(sourceObjectEntry : o2o.entrySet) { | ||
29 | val originalSource = sourceObjectEntry.key | ||
30 | val copiedSource = sourceObjectEntry.value | ||
31 | for(originalReference : originalSource.eClass.EAllReferences) { | ||
32 | if(originalReference.isMany) { | ||
33 | val originalTargets = originalSource.eGet(originalReference) as List<? extends EObject> | ||
34 | for(originalTarget : originalTargets) { | ||
35 | if(o2o.containsKey(originalTarget)) { | ||
36 | copyReference(originalReference,copiedSource,o2o.get(originalTarget)) | ||
37 | } | ||
38 | } | ||
39 | } else { | ||
40 | val originalTarget = originalSource.eGet(originalReference) as EObject | ||
41 | if(o2o.containsKey(originalTarget)) { | ||
42 | copyReference(originalReference,copiedSource,o2o.get(originalTarget)) | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | return o2o.get(s) | ||
48 | } | ||
49 | |||
50 | def private copyObject(EObject o) { | ||
51 | if(o.createCopy) { | ||
52 | val className = o.eClass.name | ||
53 | //if(className =="Exit") println("Exit") | ||
54 | val correspondingClass = commonSGraphPackage.EClassifiers.filter(EClass).filter[it.name == className].head | ||
55 | if(correspondingClass != null) { | ||
56 | return factory.create(correspondingClass) | ||
57 | } else { | ||
58 | println(className) | ||
59 | return null | ||
60 | } | ||
61 | } | ||
62 | else return null | ||
63 | } | ||
64 | |||
65 | def private copyReference(EReference reference, EObject source, EObject target) { | ||
66 | val correspondingSourceClass = commonSGraphPackage.EClassifiers.filter(EClass).filter[it.name == reference.EContainingClass.name].head | ||
67 | if(correspondingSourceClass !== null) { | ||
68 | val correspondingReference = correspondingSourceClass.EReferences.filter[it.name === reference.name].head | ||
69 | if(correspondingReference !== null) { | ||
70 | if(correspondingReference.isMany) { | ||
71 | (source.eGet(correspondingReference) as List<? super EObject>).add(target) | ||
72 | } else { | ||
73 | source.eSet(correspondingReference,target); | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | def private createCopy(EObject o) { | ||
80 | o.eClass.EPackage === yakinduSGraphPackage | ||
81 | } | ||
82 | } \ No newline at end of file | ||