diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend new file mode 100644 index 00000000..a7e3a48b --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend | |||
@@ -0,0 +1,100 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.workspace | ||
2 | |||
3 | import java.io.FileNotFoundException | ||
4 | import java.util.Collections | ||
5 | import org.eclipse.emf.common.util.URI | ||
6 | import org.eclipse.emf.ecore.EObject | ||
7 | import org.eclipse.emf.ecore.resource.ResourceSet | ||
8 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl | ||
9 | import org.eclipse.emf.ecore.resource.Resource | ||
10 | import java.io.ByteArrayOutputStream | ||
11 | import java.io.IOException | ||
12 | |||
13 | abstract class ReasonerWorkspace{ | ||
14 | |||
15 | protected val String targetFolder; | ||
16 | protected val String prefix; | ||
17 | val ResourceSet resSet = new ResourceSetImpl(); | ||
18 | |||
19 | public new(String targetFolder, String prefix) { | ||
20 | this.targetFolder = targetFolder | ||
21 | this.prefix = prefix | ||
22 | } | ||
23 | |||
24 | public def ReasonerWorkspace subWorkspace(String targetFolder, String prefix); | ||
25 | |||
26 | def URI getWorkspaceURI(); | ||
27 | |||
28 | /** | ||
29 | * Creates the target folder and clears the workspace for the reasoning | ||
30 | */ | ||
31 | def public void initAndClear() | ||
32 | |||
33 | def protected URI getURI(String name); | ||
34 | protected def Resource getResource(String name) { | ||
35 | val prevoius = resSet.getResource(getURI(name),false); | ||
36 | if(prevoius!= null) prevoius.delete(Collections.EMPTY_MAP) | ||
37 | |||
38 | val URI resourceURI = getURI(name) | ||
39 | return resSet.createResource(resourceURI); | ||
40 | } | ||
41 | |||
42 | |||
43 | /** | ||
44 | * Writes a model | ||
45 | */ | ||
46 | def public URI writeModel(EObject modelRoot, String name) { | ||
47 | val resource = getResource(name); | ||
48 | resource.getContents().add(modelRoot); | ||
49 | resource.save(Collections.EMPTY_MAP); | ||
50 | return resource.URI | ||
51 | } | ||
52 | |||
53 | |||
54 | def public String writeModelToString(EObject modelRoot, String name) { | ||
55 | val resource = getResource(name); | ||
56 | resource.getContents().add(modelRoot); | ||
57 | val ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
58 | resource.save(outputStream, null); | ||
59 | return outputStream.toString(); | ||
60 | } | ||
61 | |||
62 | def public <RootType extends EObject> RootType reloadModel(Class<RootType> type, String name) { | ||
63 | try { | ||
64 | val resource = resSet.getResource(getURI(name),false); | ||
65 | if(resource.loaded) { | ||
66 | resource.unload | ||
67 | } | ||
68 | resource.load(Collections.EMPTY_MAP) | ||
69 | if(resource == null) throw new FileNotFoundException(getURI(name).toString) | ||
70 | else return resource.contents.get(0) as RootType | ||
71 | } catch(Exception e) { | ||
72 | throw new FileNotFoundException(getURI(name).toString) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | def public <RootType extends EObject> RootType readModel(Class<RootType> type, String name) { | ||
77 | try { | ||
78 | val resource = resSet.getResource(getURI(name),true); | ||
79 | if(resource == null) throw new FileNotFoundException(getURI(name).toString) | ||
80 | else return resource.contents.get(0) as RootType | ||
81 | } catch(Exception e) { | ||
82 | throw new FileNotFoundException(getURI(name).toString + "reason: " + e.message) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | def public deactivateModel(String name) { | ||
87 | val resource = resSet.getResource(getURI(name),true); | ||
88 | resource.unload | ||
89 | renameFile(name) | ||
90 | } | ||
91 | val static protected deactivationPostfix = ".deactivated" | ||
92 | def protected void renameFile(String name) | ||
93 | |||
94 | // def void reactivate() | ||
95 | // def void deactivate() | ||
96 | |||
97 | def public URI writeText(String name, CharSequence content); | ||
98 | |||
99 | def public String readText(String name); | ||
100 | } \ No newline at end of file | ||