Commit 9fb6b6fa by Alan White

push groovy

parent 148019a2
@Grab('org.apache.commons:commons-csv:1.2')
import org.apache.commons.csv.CSVParser
import static org.apache.commons.csv.CSVFormat.*
import java.nio.file.Paths
class MessageStatsParser {
enum Performative {
SUCCEED('succeed'),
FAIL('fail'),
CANCEL('dependencyCancel'),
REFUSE('obligantRefuse'),
ACCEPT('obligantAccept'),
CONFIRM('confirmContractWithObligant'),
UPDATE('updatedContract'),
OBLIGATION_MAINTAINED('obligationMaintained'),
DEPENDENCY_MAINTAINED('dependencyMaintained')
final String value
Performative(value) {
this.value = value
}
@Override
String toString() {
value
}
}
String root
String approachName
Map resultSet
MessageStatsParser() {
resultSet = [:]
}
Map parseMessageStats() {
new File(root).eachDir() {
dir ->
Map approachResults = [:]
dir.eachDir() {
subDir, approaches=approachResults ->
Map stats = parseMsgStats(subDir)
approaches << [(subDir.name):stats]
}
resultSet[dir.name] = approachResults
}
println "End results\n$resultSet"
}
void writeOutResults(loc) {
//assemble the ordered sequences of lines to write
def writeme = []
//parent dir (i.e. ndebil), approach, run, performatives...
String postFix = "${Performative.SUCCEED.toString()},${Performative.FAIL.toString()}"
postFix = "${postFix},${Performative.CANCEL.toString()},${Performative.REFUSE.toString()},${Performative.ACCEPT.toString()}"
postFix = "${postFix},${Performative.CONFIRM.toString()},${Performative.UPDATE.toString()},${Performative.OBLIGATION_MAINTAINED.toString()}"
postFix = "${postFix},${Performative.DEPENDENCY_MAINTAINED.toString()}"
writeme << "ndebil,approach,run,${postFix}\n" //header
//each entry by key in map
println "map is $resultSet"
resultSet.each { key, valueMap ->
println "KEY: ${key}"
valueMap.each { runId, performativeCountMap ->
def sc = performativeCountMap.get(Performative.SUCCEED.toString()) as String
def fl = performativeCountMap.get(Performative.FAIL.toString()) as String
def cl = performativeCountMap.get(Performative.CANCEL.toString()) as String
def rf = performativeCountMap.get(Performative.REFUSE.toString()) as String
def acc = performativeCountMap.get(Performative.ACCEPT.toString()) as String
def cnf = performativeCountMap.get(Performative.CONFIRM.toString()) as String
def up = performativeCountMap.get(Performative.UPDATE.toString()) as String
def ob = performativeCountMap.get(Performative.OBLIGATION_MAINTAINED.toString()) as String
def dp = performativeCountMap.get(Performative.DEPENDENCY_MAINTAINED.toString()) as String
writeme << "${approachName},${key},${runId},${sc},${fl},${cl},${rf},${acc},${cnf},${up},${ob},${dp}\n"
//need to retrieve values in fixed order of performative iterator, inserting 0 if no key is present!
}
writeme << "average\nst.dev\n"
}
println "======================================================================================="
println "${writeme}"
println "======================================================================================="
writeToFile(loc, writeme)
}
void writeToFile(loc, data) {
def target = new File(loc)
println "Writing to ${loc}"
data.each {
println "Writing ${it}"
target << "${it}"
}
}
Map parseMsgStats(sourceDir) {
Map mapOfPerformativesToValues = [:]
def filePattern = {
if (it.name ==~ /msgStats.txt/)
//note; ==~ operator will create and evaluate the LHS as a regext against the RHS
{
mapOfPerformativesToValues = fileToMap(it)
}
}
//invoke closure upon each filename found...
sourceDir.eachFileRecurse(filePattern)
return mapOfPerformativesToValues;
}
Map fileToMap(file) {
Map contents = [:]
def lines = file.text.split('\n')
List<String[]> rows = lines.collect {it.split(',')}
for(int i=0; i<rows[0].length; i++) {
println "Row:${rows[0][i].trim()} = ${rows[-1][i]}"
contents[rows[0][i].trim()] = rows[-1][i].trim()
}
return contents
}
static void main(args) {
def cli = new CliBuilder(usage: 'groovy msgStatsSummarizer r')
cli.r(longOpt: 'rootDir', 'root parsing directory', required: true , args: 1 )
OptionAccessor opt = cli.parse(args)
if(!opt) {
println "Incorrect options"
return
}
println "Root directory is \"$opt.r\""
def parser = new MessageStatsParser(root:opt.r, approachName:opt.r)
parser.parseMessageStats()
parser.writeOutResults("$opt.r/${opt.r}MsgStatsSummary.csv")
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment