aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
blob: 4bf58c67c1912e14a0cab8f24d5a8f752a5e5836 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package hu.bme.mit.inf.dslreasoner.application.execution

import hu.bme.mit.inf.dslreasoner.workspace.FileSystemWorkspace
import hu.bme.mit.inf.dslreasoner.workspace.ProjectWorkspace
import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
import java.util.LinkedHashMap
import java.util.LinkedHashSet
import java.util.LinkedList
import java.util.Map
import org.eclipse.emf.common.util.URI
import org.eclipse.ui.IWorkbench
import org.eclipse.ui.IWorkbenchPage
import org.eclipse.ui.IWorkbenchWindow
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.console.ConsolePlugin
import org.eclipse.ui.console.IConsoleConstants
import org.eclipse.ui.console.IConsoleView
import org.eclipse.ui.console.MessageConsole
import java.util.List

//import org.eclipse.ui.console.MessageConsole

class ScriptConsole {
	/**
	 * Console is identified with the name of this class.
	 */
	val final consoleID = ScriptConsole.name
	val boolean printToConsole
	val MessageConsole runtimeConsole;
	val ReasonerWorkspace messageWorkspace;
	val String messageFileName;
	val ReasonerWorkspace errorWorkspace;
	val String errorFileName;
	val ReasonerWorkspace statisticsWorkspace;
	val String statisticsFileName;
	
	val statisticsHeaderBuffer = new LinkedHashSet<String>
	val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>>
	
	static val delimier = ';'
	static val empty = ""
	
	public new(
		boolean printToConsole,
		boolean printToRuntimeConsole,
		boolean cleanFiles,
		URI messageConsoleURI,
		URI errorConsoleURI,
		URI statisticsConsoleURI)
	{
		val List<String> errorMessagesDuringInitialisation = new LinkedList
		
		this.messageWorkspace = prepareWorkspace(messageConsoleURI,errorMessagesDuringInitialisation)
		this.messageFileName = prepareFileName(messageConsoleURI)
		this.errorWorkspace = prepareWorkspace(errorConsoleURI,errorMessagesDuringInitialisation)
		this.errorFileName = prepareFileName(errorConsoleURI)
		this.statisticsWorkspace = prepareWorkspace(statisticsConsoleURI,errorMessagesDuringInitialisation)
		this.statisticsFileName = prepareFileName(statisticsConsoleURI)
		
		this.printToConsole = printToConsole
		this.runtimeConsole = if(printToRuntimeConsole) { prepareRuntimeConsole } else { null }
		
		errorMessagesDuringInitialisation.forEach[
			this.writeError('''Error during console initialisation: "«it»"''')
		]
	}
	
	public def writeMessage(String message) {
		if(messageWorkspace!=null) {
			messageWorkspace.writeText(messageFileName,message);
		}
		if(printToConsole) {
			println(message)
		}
		if(runtimeConsole!==null) {
			this.writeToRuntimeConsole(message)
		}
	}
	public def writeError(String message) {
		if(errorWorkspace!=null) {
			errorWorkspace.writeText(errorFileName,message);
		}
		if(printToConsole) {
			println(message)
		}
		if(runtimeConsole!==null) {
			this.writeToRuntimeConsole(message)
		}
	}
	public def writeStatistics(LinkedHashMap<String,? extends Object> statistics) {
		if(statisticsWorkspace!==null) {
			val message = '''
			«FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR»
			«FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»'''
			statisticsWorkspace.writeText(statisticsFileName,message);
		}
	}
	public def addStatistics(LinkedHashMap<String,? extends Object> statistics) {
		for(key : statistics.keySet) {
			this.statisticsHeaderBuffer.add(key);
		}
		this.statisticsDataBuffer.add(statistics)
	}
	public def flushStatistics() {
		if(statisticsWorkspace!==null) {
			val message = '''
			«FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR»
			«FOR line : statisticsDataBuffer »
				«FOR key : statisticsHeaderBuffer»«IF line.containsKey(key)»«empty»«ELSE»«line.get(key)»«ENDIF»«ENDFOR»
			«ENDFOR»
			'''
			statisticsWorkspace.writeText(statisticsFileName,message);
		}
	}

	private def prepareWorkspace(URI uri, List<String> errors) {
		if (uri === null) {
			return null
		} else {
			try{
				val folderURI = uri.trimSegments(1)
				if(folderURI.isFile) {
					return new FileSystemWorkspace(folderURI.toString,"")=>[init]
				} else if(folderURI.isPlatformResource) {
					return new ProjectWorkspace(folderURI.toString,"")=>[init]
				} else {
					throw new UnsupportedOperationException('''Unsupported file usi: "«uri»"!''')
				}
			} catch(Exception e) {
				errors += e.message
				return null
			}
		}
	}
	private def prepareFileName(URI uri) {
		if(uri!==null) {
			return uri.lastSegment
		} else {
			null
		}
	}
	
	private def MessageConsole prepareRuntimeConsole() {
		val plugin = ConsolePlugin.getDefault();
		val conMan = plugin.getConsoleManager();
		val existingConsoles = conMan.getConsoles();
		val existingConsolesWithID = existingConsoles.filter[it.name.equals(consoleID)]
		if(existingConsolesWithID.empty) {
			val MessageConsole res = new MessageConsole(consoleID,null)
			conMan.addConsoles(#[res]);
			return res
		} else {
			return existingConsolesWithID.head as MessageConsole
		}
	}
	
	private def writeToRuntimeConsole(CharSequence message) {
		// 1. reveal the console view
//		val IWorkbench wb = PlatformUI.getWorkbench();
//		val IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
//		val IWorkbenchPage page = win.getActivePage();
//		val id = IConsoleConstants.ID_CONSOLE_VIEW;
//		val view = page.showView(id) as IConsoleView;
//		view.display(this.runtimeConsole);	
		
		ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole);
		
		// 2. write to the console
		val stream = this.runtimeConsole.newMessageStream
		stream.println(message.toString)
		stream.close
	}
}