diff options
author | OszkarSemerath <oszka@152.66.252.189> | 2017-06-21 18:44:04 +0200 |
---|---|---|
committer | OszkarSemerath <oszka@152.66.252.189> | 2017-06-21 18:44:04 +0200 |
commit | cf0c50e4fedf219f79a462615bb0ba603ce99378 (patch) | |
tree | a87bfbd1b2cd77c66ef1ab2106f9a9295cf97cfb /Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java | |
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/ModelManager.java')
-rw-r--r-- | Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java | 79 |
1 files changed, 79 insertions, 0 deletions
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 | } | ||