diff options
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.xtend | 290 |
1 files changed, 98 insertions, 192 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 cfb1f09b..d49a0f2a 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 | |||
@@ -9,128 +9,108 @@ import java.util.LinkedHashSet | |||
9 | import java.util.LinkedList | 9 | import java.util.LinkedList |
10 | import java.util.List | 10 | import java.util.List |
11 | import java.util.Map | 11 | import java.util.Map |
12 | import java.util.concurrent.CompletableFuture | ||
13 | import javax.swing.text.BadLocationException | ||
14 | import org.eclipse.emf.common.util.URI | 12 | import org.eclipse.emf.common.util.URI |
15 | import org.eclipse.jface.text.DocumentEvent | ||
16 | import org.eclipse.jface.text.IDocumentListener | ||
17 | import org.eclipse.swt.graphics.Color | ||
18 | import org.eclipse.ui.console.ConsolePlugin | ||
19 | import org.eclipse.ui.console.MessageConsole | ||
20 | import org.eclipse.ui.console.MessageConsoleStream | ||
21 | import org.eclipse.xtend.lib.annotations.Data | 13 | import org.eclipse.xtend.lib.annotations.Data |
22 | 14 | ||
23 | //import org.eclipse.ui.console.MessageConsole | 15 | abstract class ScriptConsole { |
24 | |||
25 | class ScriptConsole { | ||
26 | /** | ||
27 | * Console is identified with the name of this class. | ||
28 | */ | ||
29 | val final consoleID = ScriptConsole.name | ||
30 | val boolean printToConsole | 16 | val boolean printToConsole |
31 | val MessageConsole runtimeConsole; | ||
32 | val ReasonerWorkspace messageWorkspace; | 17 | val ReasonerWorkspace messageWorkspace; |
33 | val String messageFileName; | 18 | val String messageFileName; |
34 | val ReasonerWorkspace errorWorkspace; | 19 | val ReasonerWorkspace errorWorkspace; |
35 | val String errorFileName; | 20 | val String errorFileName; |
36 | val ReasonerWorkspace statisticsWorkspace; | 21 | val ReasonerWorkspace statisticsWorkspace; |
37 | val String statisticsFileName; | 22 | val String statisticsFileName; |
38 | 23 | val List<String> errorMessagesDuringInitialisation = new LinkedList; | |
24 | |||
39 | val statisticsHeaderBuffer = new LinkedHashSet<String> | 25 | val statisticsHeaderBuffer = new LinkedHashSet<String> |
40 | val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>> | 26 | val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>> |
41 | 27 | ||
42 | static val delimier = ',' | 28 | static val delimier = ',' |
43 | static val empty = "" | 29 | static val empty = "" |
44 | 30 | ||
45 | public new( | 31 | new(boolean printToConsole, boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, |
46 | boolean printToRuntimeConsole, | 32 | URI statisticsConsoleURI) { |
47 | boolean cleanFiles, | 33 | this.messageWorkspace = prepareWorkspace(messageConsoleURI, errorMessagesDuringInitialisation) |
48 | URI messageConsoleURI, | ||
49 | URI errorConsoleURI, | ||
50 | URI statisticsConsoleURI) | ||
51 | { | ||
52 | val List<String> errorMessagesDuringInitialisation = new LinkedList | ||
53 | |||
54 | this.messageWorkspace = prepareWorkspace(messageConsoleURI,errorMessagesDuringInitialisation) | ||
55 | this.messageFileName = prepareFileName(messageConsoleURI) | 34 | this.messageFileName = prepareFileName(messageConsoleURI) |
56 | this.errorWorkspace = prepareWorkspace(errorConsoleURI,errorMessagesDuringInitialisation) | 35 | this.errorWorkspace = prepareWorkspace(errorConsoleURI, errorMessagesDuringInitialisation) |
57 | this.errorFileName = prepareFileName(errorConsoleURI) | 36 | this.errorFileName = prepareFileName(errorConsoleURI) |
58 | this.statisticsWorkspace = prepareWorkspace(statisticsConsoleURI,errorMessagesDuringInitialisation) | 37 | this.statisticsWorkspace = prepareWorkspace(statisticsConsoleURI, errorMessagesDuringInitialisation) |
59 | this.statisticsFileName = prepareFileName(statisticsConsoleURI) | 38 | this.statisticsFileName = prepareFileName(statisticsConsoleURI) |
60 | 39 | ||
61 | this.printToConsole = (ConsolePlugin.getDefault() === null) | 40 | this.printToConsole = printToConsole |
62 | this.runtimeConsole = if(printToRuntimeConsole) { prepareRuntimeConsole } else { null } | 41 | } |
63 | 42 | ||
64 | errorMessagesDuringInitialisation.forEach[ | 43 | /** |
44 | * Writes any error messages that occurred during console initialization. | ||
45 | * | ||
46 | * Should be called by implementations at the end of their constructors. | ||
47 | */ | ||
48 | protected final def writeErrorMessagesDuringInitialisation() { | ||
49 | errorMessagesDuringInitialisation.forEach [ | ||
65 | this.writeError('''Error during console initialisation: "«it»"''') | 50 | this.writeError('''Error during console initialisation: "«it»"''') |
66 | ] | 51 | ] |
52 | errorMessagesDuringInitialisation.clear | ||
67 | } | 53 | } |
68 | 54 | ||
69 | public def writeMessage(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | 55 | def void writeMessage(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { |
70 | val resolvedText = this.resolveToText(message, separator, decorators) | 56 | val resolvedText = this.resolveToText(message, separator, decorators) |
71 | if(messageWorkspace!==null) { | 57 | if (messageWorkspace !== null) { |
72 | messageWorkspace.writeText(messageFileName,resolvedText); | 58 | messageWorkspace.writeText(messageFileName, resolvedText); |
73 | } | 59 | } |
74 | if(printToConsole) { | 60 | if (printToConsole) { |
75 | println(resolvedText) | 61 | println(resolvedText) |
76 | } | 62 | } |
77 | if(runtimeConsole!==null) { | ||
78 | writeToRuntimeConsole(message, separator, decorators) | ||
79 | } | ||
80 | } | 63 | } |
81 | public def writeMessage(String message) { | 64 | |
82 | if(messageWorkspace!==null) { | 65 | def void writeMessage(String message) { |
83 | messageWorkspace.writeText(messageFileName,message); | 66 | if (messageWorkspace !== null) { |
67 | messageWorkspace.writeText(messageFileName, message); | ||
84 | } | 68 | } |
85 | if(printToConsole) { | 69 | if (printToConsole) { |
86 | println(message) | 70 | println(message) |
87 | } | 71 | } |
88 | if(runtimeConsole!==null) { | ||
89 | writeToRuntimeConsole(message) | ||
90 | } | ||
91 | } | 72 | } |
92 | public def writeError(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | 73 | |
74 | def void writeError(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | ||
93 | val resolvedText = this.resolveToText(message, separator, decorators) | 75 | val resolvedText = this.resolveToText(message, separator, decorators) |
94 | if(errorWorkspace!==null) { | 76 | if (errorWorkspace !== null) { |
95 | errorWorkspace.writeText(errorFileName,resolvedText); | 77 | errorWorkspace.writeText(errorFileName, resolvedText); |
96 | } | 78 | } |
97 | println(message) | 79 | println(message) |
98 | if(runtimeConsole!==null) { | ||
99 | writeToRuntimeConsole(message, separator, decorators) | ||
100 | } | ||
101 | } | 80 | } |
102 | public def writeError(String message) { | 81 | |
103 | if(errorWorkspace!==null) { | 82 | def void writeError(String message) { |
104 | errorWorkspace.writeText(errorFileName,message); | 83 | if (errorWorkspace !== null) { |
84 | errorWorkspace.writeText(errorFileName, message); | ||
105 | } | 85 | } |
106 | println(message) | 86 | println(message) |
107 | if(runtimeConsole!==null) { | ||
108 | writeToRuntimeConsole(message) | ||
109 | } | ||
110 | } | 87 | } |
111 | public def writeStatistics(LinkedHashMap<String,? extends Object> statistics) { | 88 | |
112 | if(statisticsWorkspace!==null) { | 89 | def writeStatistics(LinkedHashMap<String, ? extends Object> statistics) { |
113 | val message = ''' | 90 | if (statisticsWorkspace !== null) { |
91 | val message = ''' | ||
114 | «FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR» | 92 | «FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR» |
115 | «FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»''' | 93 | «FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»''' |
116 | statisticsWorkspace.writeText(statisticsFileName,message); | 94 | statisticsWorkspace.writeText(statisticsFileName, message); |
117 | } | 95 | } |
118 | } | 96 | } |
119 | public def addStatistics(LinkedHashMap<String,? extends Object> statistics) { | 97 | |
120 | for(key : statistics.keySet) { | 98 | def addStatistics(LinkedHashMap<String, ? extends Object> statistics) { |
99 | for (key : statistics.keySet) { | ||
121 | this.statisticsHeaderBuffer.add(key); | 100 | this.statisticsHeaderBuffer.add(key); |
122 | } | 101 | } |
123 | this.statisticsDataBuffer.add(statistics) | 102 | this.statisticsDataBuffer.add(statistics) |
124 | } | 103 | } |
125 | public def flushStatistics() { | 104 | |
126 | if(statisticsWorkspace!==null) { | 105 | def flushStatistics() { |
127 | val message = ''' | 106 | if (statisticsWorkspace !== null) { |
128 | «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR» | 107 | val message = ''' |
129 | «FOR line : statisticsDataBuffer» | 108 | «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR» |
130 | «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«IF line.containsKey(key)»«line.get(key)»«ELSE»«empty»«ENDIF»«ENDFOR» | 109 | «FOR line : statisticsDataBuffer» |
131 | «ENDFOR» | 110 | «FOR key : statisticsHeaderBuffer SEPARATOR delimier»«IF line.containsKey(key)»«line.get(key)»«ELSE»«empty»«ENDIF»«ENDFOR» |
111 | «ENDFOR» | ||
132 | ''' | 112 | ''' |
133 | statisticsWorkspace.writeText(statisticsFileName,message); | 113 | statisticsWorkspace.writeText(statisticsFileName, message); |
134 | statisticsHeaderBuffer.clear | 114 | statisticsHeaderBuffer.clear |
135 | statisticsDataBuffer.clear | 115 | statisticsDataBuffer.clear |
136 | } | 116 | } |
@@ -140,143 +120,66 @@ class ScriptConsole { | |||
140 | if (uri === null) { | 120 | if (uri === null) { |
141 | return null | 121 | return null |
142 | } else { | 122 | } else { |
143 | try{ | 123 | try { |
144 | val folderURI = uri.trimSegments(1) | 124 | val folderURI = uri.trimSegments(1) |
145 | if(folderURI.isFile) { | 125 | if (folderURI.isFile) { |
146 | return new FileSystemWorkspace(folderURI.toString,"")=>[init] | 126 | return new FileSystemWorkspace(folderURI.toString, "") => [init] |
147 | } else if(folderURI.isPlatformResource) { | 127 | } else if (folderURI.isPlatformResource) { |
148 | return new ProjectWorkspace(folderURI.toString,"")=>[init] | 128 | return new ProjectWorkspace(folderURI.toString, "") => [init] |
149 | } else { | 129 | } else { |
150 | throw new UnsupportedOperationException('''Unsupported file usi: "«uri»"!''') | 130 | throw new UnsupportedOperationException('''Unsupported file usi: "«uri»"!''') |
151 | } | 131 | } |
152 | } catch(Exception e) { | 132 | } catch (Exception e) { |
153 | errors += e.message | 133 | errors += e.message |
154 | return null | 134 | return null |
155 | } | 135 | } |
156 | } | 136 | } |
157 | } | 137 | } |
138 | |||
158 | private def prepareFileName(URI uri) { | 139 | private def prepareFileName(URI uri) { |
159 | if(uri!==null) { | 140 | if (uri !== null) { |
160 | return uri.lastSegment | 141 | return uri.lastSegment |
161 | } else { | 142 | } else { |
162 | null | 143 | null |
163 | } | 144 | } |
164 | } | 145 | } |
165 | 146 | ||
166 | private def MessageConsole prepareRuntimeConsole() { | ||
167 | val plugin = ConsolePlugin.getDefault(); | ||
168 | if(plugin === null) { | ||
169 | return null | ||
170 | } else { | ||
171 | val conMan = plugin.getConsoleManager(); | ||
172 | val existingConsoles = conMan.getConsoles(); | ||
173 | val existingConsolesWithID = existingConsoles.filter[it.name.equals(consoleID)] | ||
174 | if(existingConsolesWithID.empty) { | ||
175 | val MessageConsole res = new MessageConsole(consoleID,null) | ||
176 | conMan.addConsoles(#[res]); | ||
177 | |||
178 | return res | ||
179 | } else { | ||
180 | return existingConsolesWithID.head as MessageConsole | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | private def resolveToText(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | 147 | private def resolveToText(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { |
185 | val messageString = message.toString | 148 | val messageString = message.toString |
186 | // 0. split the message | 149 | // 0. split the message |
187 | val separatedMessage = if(messageString.startsWith(separator,-1)) { | 150 | val separatedMessage = if (messageString.startsWith(separator, -1)) { |
188 | #[""]+messageString.split(separator,-1) | 151 | #[""] + messageString.split(separator, -1) |
189 | } else { | 152 | } else { |
190 | messageString.split(separator,-1).toList | 153 | messageString.split(separator, -1).toList |
191 | } | 154 | } |
192 | if(separatedMessage.size-1 !== decorators.size) { | 155 | if (separatedMessage.size - 1 !== decorators.size) { |
193 | throw new IllegalArgumentException | 156 | throw new IllegalArgumentException |
194 | } | 157 | } |
195 | 158 | ||
196 | return '''«FOR i : 0..<decorators.size»«separatedMessage.get(i)»[«decorators.get(i).text»]«ENDFOR»«separatedMessage.last»''' | 159 | return '''«FOR i : 0..<decorators.size»«separatedMessage.get(i)»[«decorators.get(i).text»]«ENDFOR»«separatedMessage.last»''' |
197 | } | 160 | } |
198 | private def writeToRuntimeConsole(CharSequence message) { | 161 | |
199 | // 1. reveal the console view | 162 | @FunctionalInterface |
200 | ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole); | 163 | interface Factory { |
201 | val stream = this.runtimeConsole.newMessageStream | 164 | def ScriptConsole createScriptConsole(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, |
202 | stream.println(message.toString) | 165 | URI statisticsConsoleURI) |
203 | stream.close | ||
204 | } | 166 | } |
205 | private def writeToRuntimeConsole(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | 167 | } |
206 | val messageString = message.toString | 168 | |
207 | // 0. split the message | 169 | class StandardOutputBasedScriptConsole extends ScriptConsole { |
208 | val separatedMessage = if(messageString.startsWith(separator)) { | 170 | new(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, URI statisticsConsoleURI) { |
209 | #[""]+messageString.split(separator,-1) | 171 | super(true, cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI) |
210 | } else { | 172 | writeErrorMessagesDuringInitialisation() |
211 | messageString.split(separator,-1).toList | ||
212 | } | ||
213 | if(separatedMessage.size-1 !== decorators.size) { | ||
214 | throw new IllegalArgumentException | ||
215 | } | ||
216 | |||
217 | // 1. reveal the console view | ||
218 | ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole); | ||
219 | val stream = this.runtimeConsole.newMessageStream | ||
220 | |||
221 | // 2. print the segments of the view | ||
222 | for(i : 0..<decorators.size) { | ||
223 | stream.print(separatedMessage.get(i)) | ||
224 | writeDecoratedTextToRuntimeConsole(decorators.get(i),stream) | ||
225 | |||
226 | } | ||
227 | // 2.1 write the last segment of | ||
228 | stream.println(separatedMessage.last) | ||
229 | |||
230 | //stream.println(message.toString) | ||
231 | stream.close | ||
232 | } | 173 | } |
233 | private def writeDecoratedTextToRuntimeConsole(ScriptConsoleDecorator message, MessageConsoleStream stream) { | 174 | |
234 | val originalBackgroundColor = this.runtimeConsole.background | 175 | public static val FACTORY = new ScriptConsole.Factory { |
235 | var Color newColor = null; | 176 | override createScriptConsole(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, |
236 | 177 | URI statisticsConsoleURI) { | |
237 | val text = '''[«message.text»]''' | 178 | new StandardOutputBasedScriptConsole(cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI) |
238 | if(message.red >= 0 && message.green >= 0 && message.blue >= 0) { | ||
239 | newColor = new Color(originalBackgroundColor.device,message.red,message.green,message.blue) | ||
240 | this.runtimeConsole.setBackground(newColor) | ||
241 | } | ||
242 | stream.flush | ||
243 | val CompletableFuture<Boolean> finished = new CompletableFuture<Boolean> | ||
244 | val listener = new IDocumentListener() { | ||
245 | override documentAboutToBeChanged(DocumentEvent event) { } | ||
246 | override documentChanged(DocumentEvent event) { | ||
247 | //println('''ftext="«event.fText»", message="«message.text»" endswith=«event.fText.endsWith(message.text)»''') | ||
248 | if(event.fText.endsWith(text)) { | ||
249 | val from = event.fDocument.length-text.length+1 | ||
250 | val length = message.text.length | ||
251 | //println('''from: «from» length «length»''') | ||
252 | try{ | ||
253 | runtimeConsole.addHyperlink( | ||
254 | new ScriptConsoleFileHiperlink(message.hyperlink), | ||
255 | from, | ||
256 | length | ||
257 | ) | ||
258 | //println("link added") | ||
259 | } catch(BadLocationException e) { | ||
260 | |||
261 | } finally { | ||
262 | runtimeConsole.document.removeDocumentListener(this) | ||
263 | finished.complete(true) | ||
264 | } | ||
265 | } | ||
266 | |||
267 | } | ||
268 | } | ||
269 | runtimeConsole.document.addDocumentListener(listener) | ||
270 | stream.print(text) | ||
271 | stream.flush | ||
272 | finished.get | ||
273 | //stream.console.new | ||
274 | if(message.red >= 0 && message.green >= 0 && message.blue >= 0) { | ||
275 | newColor.dispose | ||
276 | this.runtimeConsole.setBackground(originalBackgroundColor) | ||
277 | } | 179 | } |
278 | } | 180 | } |
279 | } | 181 | } |
182 | |||
280 | @Data | 183 | @Data |
281 | class ScriptConsoleDecorator { | 184 | class ScriptConsoleDecorator { |
282 | String text | 185 | String text |
@@ -284,33 +187,36 @@ class ScriptConsoleDecorator { | |||
284 | int Red | 187 | int Red |
285 | int Green | 188 | int Green |
286 | int Blue | 189 | int Blue |
287 | 190 | ||
288 | public new(String text) { | 191 | new(String text) { |
289 | this.text = text | 192 | this.text = text |
290 | this.hyperlink = null | 193 | this.hyperlink = null |
291 | this.Red = -1 | 194 | this.Red = -1 |
292 | this.Green = -1 | 195 | this.Green = -1 |
293 | this.Blue = -1 | 196 | this.Blue = -1 |
294 | } | 197 | } |
295 | public new(String text, File hyperlink) { | 198 | |
199 | new(String text, File hyperlink) { | ||
296 | this.text = text | 200 | this.text = text |
297 | this.hyperlink = hyperlink | 201 | this.hyperlink = hyperlink |
298 | this.Red = -1 | 202 | this.Red = -1 |
299 | this.Green = -1 | 203 | this.Green = -1 |
300 | this.Blue = -1 | 204 | this.Blue = -1 |
301 | } | 205 | } |
302 | public new(String text, int red, int green, int blue) { | 206 | |
207 | new(String text, int red, int green, int blue) { | ||
303 | this.text = text | 208 | this.text = text |
304 | this.hyperlink = null | 209 | this.hyperlink = null |
305 | this.Red = red | 210 | this.Red = red |
306 | this.Green = green | 211 | this.Green = green |
307 | this.Blue = blue | 212 | this.Blue = blue |
308 | } | 213 | } |
309 | public new(String text, File hyperlink, int red, int green, int blue) { | 214 | |
215 | new(String text, File hyperlink, int red, int green, int blue) { | ||
310 | this.text = text | 216 | this.text = text |
311 | this.hyperlink = hyperlink | 217 | this.hyperlink = hyperlink |
312 | this.Red = red | 218 | this.Red = red |
313 | this.Green = green | 219 | this.Green = green |
314 | this.Blue = blue | 220 | this.Blue = blue |
315 | } | 221 | } |
316 | } \ No newline at end of file | 222 | } |