aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
blob: d49a0f2a0a5187571cab68d8205367539b8d1fdd (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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.io.File
import java.util.LinkedHashMap
import java.util.LinkedHashSet
import java.util.LinkedList
import java.util.List
import java.util.Map
import org.eclipse.emf.common.util.URI
import org.eclipse.xtend.lib.annotations.Data

abstract class ScriptConsole {
	val boolean printToConsole
	val ReasonerWorkspace messageWorkspace;
	val String messageFileName;
	val ReasonerWorkspace errorWorkspace;
	val String errorFileName;
	val ReasonerWorkspace statisticsWorkspace;
	val String statisticsFileName;
	val List<String> errorMessagesDuringInitialisation = new LinkedList;

	val statisticsHeaderBuffer = new LinkedHashSet<String>
	val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>>
	
	static val delimier = ','
	static val empty = ""

	new(boolean printToConsole, boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI,
		URI statisticsConsoleURI) {
		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
	}

	/**
	 * Writes any error messages that occurred during console initialization.
	 * 
	 * Should be called by implementations at the end of their constructors.
	 */
	protected final def writeErrorMessagesDuringInitialisation() {
		errorMessagesDuringInitialisation.forEach [
			this.writeError('''Error during console initialisation: "«it»"''')
		]
		errorMessagesDuringInitialisation.clear
	}

	def void writeMessage(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) {
		val resolvedText = this.resolveToText(message, separator, decorators)
		if (messageWorkspace !== null) {
			messageWorkspace.writeText(messageFileName, resolvedText);
		}
		if (printToConsole) {
			println(resolvedText)
		}
	}

	def void writeMessage(String message) {
		if (messageWorkspace !== null) {
			messageWorkspace.writeText(messageFileName, message);
		}
		if (printToConsole) {
			println(message)
		}
	}

	def void writeError(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) {
		val resolvedText = this.resolveToText(message, separator, decorators)
		if (errorWorkspace !== null) {
			errorWorkspace.writeText(errorFileName, resolvedText);
		}
		println(message)
	}

	def void writeError(String message) {
		if (errorWorkspace !== null) {
			errorWorkspace.writeText(errorFileName, message);
		}
		println(message)
	}

	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);
		}
	}

	def addStatistics(LinkedHashMap<String, ? extends Object> statistics) {
		for (key : statistics.keySet) {
			this.statisticsHeaderBuffer.add(key);
		}
		this.statisticsDataBuffer.add(statistics)
	}

	def flushStatistics() {
		if (statisticsWorkspace !== null) {
			val message = '''
				«FOR key : statisticsHeaderBuffer SEPARATOR delimier»«key»«ENDFOR»
				«FOR line : statisticsDataBuffer»
					«FOR key : statisticsHeaderBuffer SEPARATOR delimier»«IF line.containsKey(key)»«line.get(key)»«ELSE»«empty»«ENDIF»«ENDFOR»
				«ENDFOR»
			'''
			statisticsWorkspace.writeText(statisticsFileName, message);
			statisticsHeaderBuffer.clear
			statisticsDataBuffer.clear
		}
	}

	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 resolveToText(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) {
		val messageString = message.toString
		// 0. split the message
		val separatedMessage = if (messageString.startsWith(separator, -1)) {
				#[""] + messageString.split(separator, -1)
			} else {
				messageString.split(separator, -1).toList
			}
		if (separatedMessage.size - 1 !== decorators.size) {
			throw new IllegalArgumentException
		}

		return '''«FOR i : 0..<decorators.size»«separatedMessage.get(i)»[«decorators.get(i).text»]«ENDFOR»«separatedMessage.last»'''
	}

	@FunctionalInterface
	interface Factory {
		def ScriptConsole createScriptConsole(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI,
			URI statisticsConsoleURI)
	}
}

class StandardOutputBasedScriptConsole extends ScriptConsole {
	new(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, URI statisticsConsoleURI) {
		super(true, cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI)
		writeErrorMessagesDuringInitialisation()
	}

	public static val FACTORY = new ScriptConsole.Factory {
		override createScriptConsole(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI,
			URI statisticsConsoleURI) {
			new StandardOutputBasedScriptConsole(cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI)
		}
	}
}

@Data
class ScriptConsoleDecorator {
	String text
	File hyperlink
	int Red
	int Green
	int Blue

	new(String text) {
		this.text = text
		this.hyperlink = null
		this.Red = -1
		this.Green = -1
		this.Blue = -1
	}

	new(String text, File hyperlink) {
		this.text = text
		this.hyperlink = hyperlink
		this.Red = -1
		this.Green = -1
		this.Blue = -1
	}

	new(String text, int red, int green, int blue) {
		this.text = text
		this.hyperlink = null
		this.Red = red
		this.Green = green
		this.Blue = blue
	}

	new(String text, File hyperlink, int red, int green, int blue) {
		this.text = text
		this.hyperlink = hyperlink
		this.Red = red
		this.Green = green
		this.Blue = blue
	}
}