aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend')
-rw-r--r--Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend176
1 files changed, 115 insertions, 61 deletions
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
index 2dc329a0..4bf58c67 100644
--- a/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
+++ b/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
@@ -1,24 +1,38 @@
1package hu.bme.mit.inf.dslreasoner.application.execution 1package hu.bme.mit.inf.dslreasoner.application.execution
2 2
3import java.io.File 3import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace
4import java.io.PrintWriter 4import hu.bme.mit.inf.dslreasoner.workspace.ProjectWorkspace
5import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
6import java.util.LinkedHashMap
5import java.util.LinkedHashSet 7import java.util.LinkedHashSet
6import java.util.LinkedList 8import java.util.LinkedList
7import java.util.Map 9import java.util.Map
8import org.eclipse.emf.common.util.URI 10import org.eclipse.emf.common.util.URI
9import java.io.Closeable 11import org.eclipse.ui.IWorkbench
10import java.io.IOException 12import org.eclipse.ui.IWorkbenchPage
11import java.util.LinkedHashMap 13import org.eclipse.ui.IWorkbenchWindow
12import java.util.HashMap 14import org.eclipse.ui.PlatformUI
15import org.eclipse.ui.console.ConsolePlugin
16import org.eclipse.ui.console.IConsoleConstants
17import org.eclipse.ui.console.IConsoleView
18import org.eclipse.ui.console.MessageConsole
19import java.util.List
13 20
14class ScriptConsole implements Closeable { 21//import org.eclipse.ui.console.MessageConsole
22
23class ScriptConsole {
24 /**
25 * Console is identified with the name of this class.
26 */
27 val final consoleID = ScriptConsole.name
15 val boolean printToConsole 28 val boolean printToConsole
16 val boolean cleanFiles 29 val MessageConsole runtimeConsole;
17 30 val ReasonerWorkspace messageWorkspace;
18 val File messageConsoleFile 31 val String messageFileName;
19 val File errorConsoleFile 32 val ReasonerWorkspace errorWorkspace;
20 val File statisticsConsoleFile 33 val String errorFileName;
21 val Map<File,PrintWriter> file2Writer = new HashMap 34 val ReasonerWorkspace statisticsWorkspace;
35 val String statisticsFileName;
22 36
23 val statisticsHeaderBuffer = new LinkedHashSet<String> 37 val statisticsHeaderBuffer = new LinkedHashSet<String>
24 val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>> 38 val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>>
@@ -28,29 +42,58 @@ class ScriptConsole implements Closeable {
28 42
29 public new( 43 public new(
30 boolean printToConsole, 44 boolean printToConsole,
45 boolean printToRuntimeConsole,
31 boolean cleanFiles, 46 boolean cleanFiles,
32 URI messageConsoleURI, 47 URI messageConsoleURI,
33 URI errorConsoleURI, 48 URI errorConsoleURI,
34 URI statisticsConsoleURI) 49 URI statisticsConsoleURI)
35 { 50 {
51 val List<String> errorMessagesDuringInitialisation = new LinkedList
52
53 this.messageWorkspace = prepareWorkspace(messageConsoleURI,errorMessagesDuringInitialisation)
54 this.messageFileName = prepareFileName(messageConsoleURI)
55 this.errorWorkspace = prepareWorkspace(errorConsoleURI,errorMessagesDuringInitialisation)
56 this.errorFileName = prepareFileName(errorConsoleURI)
57 this.statisticsWorkspace = prepareWorkspace(statisticsConsoleURI,errorMessagesDuringInitialisation)
58 this.statisticsFileName = prepareFileName(statisticsConsoleURI)
59
36 this.printToConsole = printToConsole 60 this.printToConsole = printToConsole
37 this.cleanFiles = cleanFiles 61 this.runtimeConsole = if(printToRuntimeConsole) { prepareRuntimeConsole } else { null }
38 this.messageConsoleFile = messageConsoleURI.prepareFile 62
39 this.errorConsoleFile = errorConsoleURI.prepareFile 63 errorMessagesDuringInitialisation.forEach[
40 this.statisticsConsoleFile = statisticsConsoleURI.prepareFile 64 this.writeError('''Error during console initialisation: "«it»"''')
65 ]
41 } 66 }
42 67
43 public def writeMessage(String message) { 68 public def writeMessage(String message) {
44 messageConsoleFile.writeToFile(message) 69 if(messageWorkspace!=null) {
70 messageWorkspace.writeText(messageFileName,message);
71 }
72 if(printToConsole) {
73 println(message)
74 }
75 if(runtimeConsole!==null) {
76 this.writeToRuntimeConsole(message)
77 }
45 } 78 }
46 public def writeError(String message) { 79 public def writeError(String message) {
47 errorConsoleFile.writeToFile(message) 80 if(errorWorkspace!=null) {
81 errorWorkspace.writeText(errorFileName,message);
82 }
83 if(printToConsole) {
84 println(message)
85 }
86 if(runtimeConsole!==null) {
87 this.writeToRuntimeConsole(message)
88 }
48 } 89 }
49 public def writeStatistics(LinkedHashMap<String,? extends Object> statistics) { 90 public def writeStatistics(LinkedHashMap<String,? extends Object> statistics) {
50 val message = ''' 91 if(statisticsWorkspace!==null) {
92 val message = '''
51 «FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR» 93 «FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR»
52 «FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»''' 94 «FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»'''
53 statisticsConsoleFile.writeToFile(message) 95 statisticsWorkspace.writeText(statisticsFileName,message);
96 }
54 } 97 }
55 public def addStatistics(LinkedHashMap<String,? extends Object> statistics) { 98 public def addStatistics(LinkedHashMap<String,? extends Object> statistics) {
56 for(key : statistics.keySet) { 99 for(key : statistics.keySet) {
@@ -59,61 +102,72 @@ class ScriptConsole implements Closeable {
59 this.statisticsDataBuffer.add(statistics) 102 this.statisticsDataBuffer.add(statistics)
60 } 103 }
61 public def flushStatistics() { 104 public def flushStatistics() {
62 val message = ''' 105 if(statisticsWorkspace!==null) {
106 val message = '''
63 «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR» 107 «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR»
64 «FOR line : statisticsDataBuffer » 108 «FOR line : statisticsDataBuffer »
65 «FOR key : statisticsHeaderBuffer»«IF line.containsKey(key)»«empty»«ELSE»«line.get(key)»«ENDIF»«ENDFOR» 109 «FOR key : statisticsHeaderBuffer»«IF line.containsKey(key)»«empty»«ELSE»«line.get(key)»«ENDIF»«ENDFOR»
66 «ENDFOR» 110 «ENDFOR»
67 ''' 111 '''
68 statisticsConsoleFile.writeToFile(message) 112 statisticsWorkspace.writeText(statisticsFileName,message);
113 }
69 } 114 }
70 /** 115
71 * Writes a line of text to a file and the console. Initializes a writer to the file for at the first message. 116 private def prepareWorkspace(URI uri, List<String> errors) {
72 */ 117 if (uri === null) {
73 private def writeToFile(File file, String text) { 118 return null
74 if(file != null) { 119 } else {
75 val writer = if(this.file2Writer.containsKey(file)) { 120 try{
76 this.file2Writer.get(file) 121 val folderURI = uri.trimSegments(1)
77 } else { 122 if(folderURI.isFile) {
78 if(!file.exists) { 123 return new FileSystemWorkspace(folderURI.toString,"")=>[init]
79 file.createNewFile 124 } else if(folderURI.isPlatformResource) {
125 return new ProjectWorkspace(folderURI.toString,"")=>[init]
126 } else {
127 throw new UnsupportedOperationException('''Unsupported file usi: "«uri»"!''')
80 } 128 }
81 val writer = new PrintWriter(file, "UTF-8"); 129 } catch(Exception e) {
82 this.file2Writer.put(file,writer) 130 errors += e.message
83 writer 131 return null
84 } 132 }
85 writer.println(text)
86 } 133 }
87 if(printToConsole) { 134 }
88 println(text) 135 private def prepareFileName(URI uri) {
136 if(uri!==null) {
137 return uri.lastSegment
138 } else {
139 null
89 } 140 }
90 } 141 }
91 142
92 private def prepareFile(URI uri) { 143 private def MessageConsole prepareRuntimeConsole() {
93 if (uri === null) { 144 val plugin = ConsolePlugin.getDefault();
94 return null 145 val conMan = plugin.getConsoleManager();
146 val existingConsoles = conMan.getConsoles();
147 val existingConsolesWithID = existingConsoles.filter[it.name.equals(consoleID)]
148 if(existingConsolesWithID.empty) {
149 val MessageConsole res = new MessageConsole(consoleID,null)
150 conMan.addConsoles(#[res]);
151 return res
95 } else { 152 } else {
96 if(uri.isFile) { 153 return existingConsolesWithID.head as MessageConsole
97 val fileString = uri.toFileString
98 val file = new File(fileString)
99 if (this.cleanFiles && file.exists) {
100 file.delete
101 }
102 return file
103 } else if(uri.isPlatformResource) {
104 val platformString = uri.toPlatformString(true)
105 val file = new File(platformString)
106 if (this.cleanFiles && file.exists) {
107 file.delete
108 }
109 return file
110 } else {
111 throw new UnsupportedOperationException('''Unksupported file usi: "«uri»"!''')
112 }
113 } 154 }
114 } 155 }
115 156
116 override close() throws IOException { 157 private def writeToRuntimeConsole(CharSequence message) {
117 this.file2Writer.values.forEach[close] 158 // 1. reveal the console view
159// val IWorkbench wb = PlatformUI.getWorkbench();
160// val IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
161// val IWorkbenchPage page = win.getActivePage();
162// val id = IConsoleConstants.ID_CONSOLE_VIEW;
163// val view = page.showView(id) as IConsoleView;
164// view.display(this.runtimeConsole);
165
166 ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole);
167
168 // 2. write to the console
169 val stream = this.runtimeConsole.newMessageStream
170 stream.println(message.toString)
171 stream.close
118 } 172 }
119} \ No newline at end of file 173} \ No newline at end of file