Start models

This commit is contained in:
Matt Soucy 2020-06-20 22:18:45 -04:00
parent 214fd22b8b
commit 68699b4532
2 changed files with 88 additions and 3 deletions

View File

@ -56,8 +56,8 @@ class GbatArgs(parser: ArgParser) {
val not_interesting by parser.adding("--not-interesting", "-N",
help="Regular expression to determine which files should not be included in calculations.")
val case_sensitive by parser.flagging("Use case sensitive regexps when determining interesting files (default is case-insensitive)")
val departed by parser.storing("--departed-file", "-D", help="File listing departed devs, one per line").default<String?>(null)
val risk_file by parser.storing("--bus-risk-file", help="File of dev=float lines (e.g. ejorgensen=0.4) with custom bus risks for devs").default<String?>(null)
val departed by parser.storing("--departed-file", "-D", help="File listing departed devs, one per line", transform=::File).default<File?>(null)
val risk_file by parser.storing("--bus-risk-file", help="File of dev=float lines (e.g. ejorgensen=0.4) with custom bus risks for devs", transform=::File).default<File?>(null)
val default_bus_risk by parser.storing("--default-bus-risk", help="Default risk that a dev will be hit by a bus in your analysis timeframe (defaults to 0.1).") { toDouble() }.default(0.1)
// Multiprocessing options

View File

@ -0,0 +1,85 @@
package me.msoucy.gbat
import java.io.File
import kotlin.io.forEachLine
class RiskModel(val threshold : Double,
val default : Double,
val busRiskFile : File?,
val departedFile : File?) {
val departed = mutableSetOf<String>()
val risks = mutableMapOf<String, Double>().withDefault {default}
init {
parseBusRisks()
parseDeparted()
}
operator fun get(author : String) : Double {
val name = author.trim()
if(name.isEmpty()) {
return threshold
}
return risks.getOrPut(name) { default }
}
fun isDeparted(author : String) = author.trim() in departed
fun jointBusProb(vararg authors : String) =
authors.map { this[it] }.reduce { a, b -> a * b }
fun jointBusProbBelowThreshold(vararg authors : String) =
jointBusProb(*authors) <= threshold
private fun parseBusRisks() {
busRiskFile?.forEachLine { line ->
val sline = line.trim()
if(sline.isNotEmpty()) {
val segments = sline.split("=")
val risk = segments.last()
val author = segments.dropLast(1).joinToString(separator="=")
risks[author] = risk.toDouble()
}
}
}
private fun parseDeparted() {
departedFile?.forEachLine { line ->
val author = line.trim()
if(author.isNotEmpty()) {
risks[author] = 1.0
departed.add(author)
}
}
}
}
class LineModel() {
inner class Line(var num : Int, var text : String)
val model = mutableSetOf<Line>()
fun add(line : Line) {
model.onEach { entry ->
if(entry.num >= line.num) {
entry.num++
}
}
model.add(line)
}
fun del(line : Line) {
model.removeIf { it.num == line.num }
model.onEach { entry ->
if(entry.num > line.num) {
entry.num--
}
}
}
fun change(line : Line) {
model.removeIf { it.num == line.num }
model.add(line)
}
fun get() = model.sortedBy { it.num }.map { it.text }
}