diff options
author | 2017-06-10 19:05:05 +0200 | |
---|---|---|
committer | 2017-06-10 19:05:05 +0200 | |
commit | 60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch) | |
tree | 5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace | |
parent | Initial commit, migrating from SVN (diff) | |
download | VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.gz VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.zst VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.zip |
Migrating Additional projects
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace')
3 files changed, 279 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/FileSystemWorkspace.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/FileSystemWorkspace.xtend new file mode 100644 index 00000000..6041fdbf --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/FileSystemWorkspace.xtend | |||
@@ -0,0 +1,74 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.workspace | ||
2 | |||
3 | import java.io.BufferedReader | ||
4 | import java.io.FileReader | ||
5 | import java.io.PrintWriter | ||
6 | import org.eclipse.emf.common.util.URI | ||
7 | import java.io.File | ||
8 | |||
9 | class FileSystemWorkspace extends ReasonerWorkspace{ | ||
10 | |||
11 | new(String targetFolder, String prefix) { | ||
12 | super(targetFolder/*.replaceAll("\\\\","/").replaceAll("/\\.","")*/, prefix) | ||
13 | } | ||
14 | |||
15 | override protected getURI(String name) { | ||
16 | URI.createFileURI(targetFolder + "/" + prefix + name) | ||
17 | } | ||
18 | |||
19 | def protected getFolderURI() { | ||
20 | URI.createFileURI(targetFolder) | ||
21 | } | ||
22 | |||
23 | override getWorkspaceURI() { | ||
24 | getFolderURI | ||
25 | } | ||
26 | |||
27 | override initAndClear() { | ||
28 | val folder = new File(folderURI.toFileString) | ||
29 | folder.mkdirs | ||
30 | for(file : folder.listFiles) { | ||
31 | file.deleteFile | ||
32 | } | ||
33 | } | ||
34 | |||
35 | def void deleteFile(File file) { | ||
36 | if (file.isDirectory()) { | ||
37 | file.listFiles().forEach[deleteFile] | ||
38 | file.delete | ||
39 | } else { | ||
40 | file.delete; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | override writeText(String name, CharSequence content) { | ||
45 | val uri = getURI(name) | ||
46 | val writer = new PrintWriter(uri.toFileString, "UTF-8"); | ||
47 | writer.println(content); | ||
48 | writer.close(); | ||
49 | return uri | ||
50 | } | ||
51 | |||
52 | override readText(String name) { | ||
53 | var String line; | ||
54 | var String result = ""; | ||
55 | val in = new BufferedReader(new FileReader(getURI(name).toFileString)) | ||
56 | while ((line = in.readLine()) != null) { | ||
57 | result = result.concat(line) | ||
58 | } | ||
59 | in.close | ||
60 | return result | ||
61 | } | ||
62 | |||
63 | override protected renameFile(String name) { | ||
64 | val uri = getURI(name) | ||
65 | val uri2 = getURI(name+deactivationPostfix) | ||
66 | val file = new File(uri. toFileString) | ||
67 | val file2 = new File(uri2.toFileString) | ||
68 | file.renameTo(file2) | ||
69 | } | ||
70 | |||
71 | override subWorkspace(String targetFolder, String prefix) { | ||
72 | return new FileSystemWorkspace(this.targetFolder + "/" + targetFolder, this.prefix + prefix) | ||
73 | } | ||
74 | } \ No newline at end of file | ||
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ProjectWorkspace.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ProjectWorkspace.xtend new file mode 100644 index 00000000..5703bd5a --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/workspace/ProjectWorkspace.xtend | |||
@@ -0,0 +1,105 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.workspace | ||
2 | |||
3 | import java.io.BufferedReader | ||
4 | import java.io.ByteArrayInputStream | ||
5 | import java.io.IOException | ||
6 | import java.io.InputStreamReader | ||
7 | import org.eclipse.core.resources.IContainer | ||
8 | import org.eclipse.core.resources.IFolder | ||
9 | import org.eclipse.core.resources.IProject | ||
10 | import org.eclipse.core.resources.IWorkspaceRoot | ||
11 | import org.eclipse.core.resources.ResourcesPlugin | ||
12 | import org.eclipse.core.runtime.NullProgressMonitor | ||
13 | import org.eclipse.emf.common.util.URI | ||
14 | |||
15 | class ProjectWorkspace extends ReasonerWorkspace{ | ||
16 | |||
17 | val monitor = new NullProgressMonitor | ||
18 | var IContainer target; | ||
19 | |||
20 | new(String targetFolder, String prefix) { | ||
21 | super(targetFolder, prefix) | ||
22 | } | ||
23 | |||
24 | override protected getURI(String name) { | ||
25 | URI.createPlatformResourceURI(targetFolder + "/" + prefix + name,true); | ||
26 | } | ||
27 | |||
28 | def protected getDirUri() { | ||
29 | URI.createPlatformResourceURI(targetFolder,true) | ||
30 | } | ||
31 | |||
32 | override getWorkspaceURI() { | ||
33 | getDirUri | ||
34 | } | ||
35 | |||
36 | override initAndClear() { | ||
37 | target = ResourcesPlugin.workspace.root | ||
38 | for(nameSegment : dirUri.segments) { | ||
39 | target = createContainer(target,nameSegment) | ||
40 | } | ||
41 | target.members.forEach[delete(false,monitor)] | ||
42 | } | ||
43 | |||
44 | def protected dispatch createContainer(IWorkspaceRoot root, String name) { | ||
45 | val project = root.getProject(name) | ||
46 | if(project.exists) { | ||
47 | if(!project.open) { | ||
48 | project.open(monitor) | ||
49 | } | ||
50 | } else { | ||
51 | project.create(monitor) | ||
52 | } | ||
53 | return project | ||
54 | } | ||
55 | |||
56 | def protected dispatch createContainer(IProject root, String name) { | ||
57 | val folder = root.getFolder(name); | ||
58 | if(folder.exists) { | ||
59 | folder.create(false,true,monitor) | ||
60 | } | ||
61 | return folder | ||
62 | } | ||
63 | |||
64 | def protected dispatch createContainer(IFolder root, String name) { | ||
65 | val folder = root.getFolder(name); | ||
66 | if(folder.exists) { | ||
67 | folder.create(false,true,monitor) | ||
68 | } | ||
69 | return folder | ||
70 | } | ||
71 | |||
72 | def dispatch getTargetFile(IFolder targetFolder, String name) { targetFolder.getFile(name) } | ||
73 | def dispatch getTargetFile(IProject targetProject, String name) { targetProject.getFile(name) } | ||
74 | |||
75 | override writeText(String name, CharSequence content) { | ||
76 | val file = target.getTargetFile(name); | ||
77 | if(!file.exists()) { | ||
78 | file.create(new ByteArrayInputStream(content.toString().getBytes()),true, new NullProgressMonitor()); | ||
79 | return URI.createPlatformResourceURI(file.projectRelativePath.toString,true) | ||
80 | } | ||
81 | else throw new IOException("The file is already existing.") | ||
82 | } | ||
83 | |||
84 | override readText(String name) { | ||
85 | val file = target.getTargetFile(name) | ||
86 | val in = new BufferedReader(new InputStreamReader(file.contents)) | ||
87 | |||
88 | var result = "" | ||
89 | var String line; | ||
90 | |||
91 | while ((line = in.readLine()) != null) { | ||
92 | result = result.concat(line) | ||
93 | } | ||
94 | |||
95 | return result | ||
96 | } | ||
97 | |||
98 | override protected renameFile(String name) { | ||
99 | throw new UnsupportedOperationException("TODO: auto-generated method stub") | ||
100 | } | ||
101 | |||
102 | override subWorkspace(String targetFolder, String prefix) { | ||
103 | throw new UnsupportedOperationException("TODO: auto-generated method stub") | ||
104 | } | ||
105 | } | ||
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 | ||