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
|
package hu.bme.mit.inf.dslreasoner.workspace
import java.io.FileNotFoundException
import java.util.Collections
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.resource.Resource
import java.io.ByteArrayOutputStream
import java.io.IOException
abstract class ReasonerWorkspace{
protected val String targetFolder;
protected val String prefix;
val ResourceSet resSet = new ResourceSetImpl();
public new(String targetFolder, String prefix) {
this.targetFolder = targetFolder
this.prefix = prefix
}
public def ReasonerWorkspace subWorkspace(String targetFolder, String prefix);
def URI getWorkspaceURI();
/**
* Creates the target folder and clears the workspace for the reasoning
*/
def public void initAndClear()
def protected URI getURI(String name);
protected def Resource getResource(String name) {
val prevoius = resSet.getResource(getURI(name),false);
if(prevoius!= null) prevoius.delete(Collections.EMPTY_MAP)
val URI resourceURI = getURI(name)
return resSet.createResource(resourceURI);
}
/**
* Writes a model
*/
def public URI writeModel(EObject modelRoot, String name) {
val resource = getResource(name);
resource.getContents().add(modelRoot);
resource.save(Collections.EMPTY_MAP);
return resource.URI
}
def public String writeModelToString(EObject modelRoot, String name) {
val resource = getResource(name);
resource.getContents().add(modelRoot);
val ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
resource.save(outputStream, null);
return outputStream.toString();
}
def public <RootType extends EObject> RootType reloadModel(Class<RootType> type, String name) {
try {
val resource = resSet.getResource(getURI(name),false);
if(resource.loaded) {
resource.unload
}
resource.load(Collections.EMPTY_MAP)
if(resource == null) throw new FileNotFoundException(getURI(name).toString)
else return resource.contents.get(0) as RootType
} catch(Exception e) {
throw new FileNotFoundException(getURI(name).toString)
}
}
def public <RootType extends EObject> RootType readModel(Class<RootType> type, String name) {
try {
val resource = resSet.getResource(getURI(name),true);
if(resource == null) throw new FileNotFoundException(getURI(name).toString)
else return resource.contents.get(0) as RootType
} catch(Exception e) {
throw new FileNotFoundException(getURI(name).toString + "reason: " + e.message)
}
}
def public deactivateModel(String name) {
val resource = resSet.getResource(getURI(name),true);
resource.unload
renameFile(name)
}
val static protected deactivationPostfix = ".deactivated"
def protected void renameFile(String name)
// def void reactivate()
// def void deactivate()
def public URI writeText(String name, CharSequence content);
def public String readText(String name);
}
|