aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ReasonerWorkspace.xtend
blob: bdf2432e8a9222ca3ac36b7e19c9877cbe1a3728 (plain) (blame)
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
package hu.bme.mit.inf.dslreasoner.workspace

import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileNotFoundException
import java.util.Collections
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl

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();
	
	def public void init()
	def public void clear()
	/**
	 * Creates the target folder and clears the workspace for the reasoning
	 */
	def public void initAndClear() {
		init()
		clear()
	}
	
	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);
	}
	public def File getFile(String name)
	public def void refreshFile(String name)
	
	/**
	 * 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 public List<String> allFiles();

	def public URI writeText(String name, CharSequence content);
	
	def public String readText(String name);
}