aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application/src/hu/bme/mit/inf/dslreasoner/application/execution/ScriptConsole.xtend
blob: 2dc329a0cee317571c0538649030ed79ac7e136d (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
package hu.bme.mit.inf.dslreasoner.application.execution

import java.io.File
import java.io.PrintWriter
import java.util.LinkedHashSet
import java.util.LinkedList
import java.util.Map
import org.eclipse.emf.common.util.URI
import java.io.Closeable
import java.io.IOException
import java.util.LinkedHashMap
import java.util.HashMap

class ScriptConsole implements Closeable {
	val boolean printToConsole
	val boolean cleanFiles
	
	val File messageConsoleFile
	val File errorConsoleFile
	val File statisticsConsoleFile
	val Map<File,PrintWriter> file2Writer = new HashMap
	
	val statisticsHeaderBuffer = new LinkedHashSet<String>
	val statisticsDataBuffer = new LinkedList<Map<String,? extends Object>>
	
	static val delimier = ';'
	static val empty = ""
	
	public new(
		boolean printToConsole,
		boolean cleanFiles,
		URI messageConsoleURI,
		URI errorConsoleURI,
		URI statisticsConsoleURI)
	{
		this.printToConsole = printToConsole
		this.cleanFiles = cleanFiles
		this.messageConsoleFile = messageConsoleURI.prepareFile
		this.errorConsoleFile = errorConsoleURI.prepareFile
		this.statisticsConsoleFile = statisticsConsoleURI.prepareFile
	}
	
	public def writeMessage(String message) {
		messageConsoleFile.writeToFile(message)
	}
	public def writeError(String message) {
		errorConsoleFile.writeToFile(message)
	}
	public def writeStatistics(LinkedHashMap<String,? extends Object> statistics) {
		val message = '''
			«FOR key : statistics.keySet SEPARATOR delimier»«key»«ENDFOR»
			«FOR value : statistics.values SEPARATOR delimier»«value»«ENDFOR»'''
		statisticsConsoleFile.writeToFile(message)
	}
	public def addStatistics(LinkedHashMap<String,? extends Object> statistics) {
		for(key : statistics.keySet) {
			this.statisticsHeaderBuffer.add(key);
		}
		this.statisticsDataBuffer.add(statistics)
	}
	public def flushStatistics() {
		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»
			'''
		statisticsConsoleFile.writeToFile(message)
	}
	/**
	 * Writes a line of text to a file and the console. Initializes a writer to the file for at the first message.
	 */
	private def writeToFile(File file, String text) {
		if(file != null) {
			val writer = if(this.file2Writer.containsKey(file)) {
				this.file2Writer.get(file)
			} else {
				if(!file.exists) {
					file.createNewFile
				}
				val writer = new PrintWriter(file, "UTF-8");
				this.file2Writer.put(file,writer)
				writer
			}
			writer.println(text)
		}
		if(printToConsole) {
			println(text)
		}
	}
	
	private def prepareFile(URI uri) {
		if (uri === null) {
			return null
		} else {
			if(uri.isFile) {
				val fileString = uri.toFileString
				val file = new File(fileString)
				if (this.cleanFiles && file.exists) {
					file.delete
				}
				return file
			} else if(uri.isPlatformResource) {
				val platformString = uri.toPlatformString(true)
				val file = new File(platformString)
				if (this.cleanFiles && file.exists) {
					file.delete
				}
				return file
			} else {
				throw new UnsupportedOperationException('''Unksupported file usi: "«uri»"!''')
			}
		}
	}
	
	override close() throws IOException {
		this.file2Writer.values.forEach[close]
	}
}