[add][fix] added numberUtil and behaviourUtil to library
- numberUtil adds functions to Double - behaviourUtil adds functions to Option[FunctionArgs] - [add] getParam function to FunctionArgs case class - [fix] fixed typo in config
This commit is contained in:
@@ -47,19 +47,19 @@ groundtruths:
|
|||||||
- [7, 10.0]
|
- [7, 10.0]
|
||||||
- [12, 16.0]
|
- [12, 16.0]
|
||||||
delta:
|
delta:
|
||||||
- name: flactuate
|
- name: fluctuate
|
||||||
args: {range: 1.0, center: [3, 3], distOffset: 0.5, time:[0, 12, 18]}
|
args: {range: 1.0, center: [3, 3], distOffset: -0.5, time:[0, 12, 18]}
|
||||||
timeline:
|
timeline:
|
||||||
- [0, 7]
|
- [0, 7]
|
||||||
- [12, 16]
|
- [12, 16]
|
||||||
- [18, 24]
|
- [18, 24]
|
||||||
- [7, change, {target: 16.0, targetTime: 12, center: [3, 3], distOffset: 0.5, time:[7]}]
|
- [7, change, {target: 16.0, targetTime: 12, center: [3, 3], distOffset: -0.5, time:[7]}]
|
||||||
- [16, change, {target: 10.0, targetTime: 18, center: [3, 3], distOffset: 0.5, time:[16]}]
|
- [16, change, {target: 10.0, targetTime: 18, center: [3, 3], distOffset: -0.5, time:[16]}]
|
||||||
|
|
||||||
- group: humidity
|
- group: humidity
|
||||||
parent: temperature
|
parent: temperature
|
||||||
delta:
|
delta:
|
||||||
- [0, humidFromTemp, {dewpoint: 12.0, range: 1.0, center: [3, 3], distOffset: 0.5, time:[0, 7, 12, 16, 18]}]
|
- [0, humidFromTemp, {dewpoint: 12.0,}]
|
||||||
|
|
||||||
behaviours:
|
behaviours:
|
||||||
- group: temperature
|
- group: temperature
|
||||||
|
|||||||
@@ -117,7 +117,12 @@ case class Group(name: String, sensorParent: Option[String] = None){
|
|||||||
}
|
}
|
||||||
|
|
||||||
case class Truth() extends Result[Option[Double]]
|
case class Truth() extends Result[Option[Double]]
|
||||||
case class FunctionArgs(args: Map[String, Any])
|
case class FunctionArgs(args: Map[String, Any]) {
|
||||||
|
def getParam[A](param: String, fallback: A): A = {
|
||||||
|
// just bruteforce try to get the param, otherwise use fallback
|
||||||
|
try { args(param).asInstanceOf[A] } catch { case _: Throwable => fallback }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case class BehaviourVector(function: Vector[Option[(SimulationBehaviour, Option[FunctionArgs])]],
|
case class BehaviourVector(function: Vector[Option[(SimulationBehaviour, Option[FunctionArgs])]],
|
||||||
defaultFunc: SimulationBehaviour = (_: Cell, _: Time, v: Option[Double], _: Option[FunctionArgs]) => v,
|
defaultFunc: SimulationBehaviour = (_: Cell, _: Time, v: Option[Double], _: Option[FunctionArgs]) => v,
|
||||||
|
|||||||
@@ -313,6 +313,53 @@ package object mapUtil {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
package object behaviourUtil {
|
||||||
|
implicit class FunctionArgsOption(optional: Option[FunctionArgs]) {
|
||||||
|
def getParam[B](param: String, fallback: B): B = {
|
||||||
|
if(optional.isEmpty) {
|
||||||
|
fallback
|
||||||
|
} else {
|
||||||
|
optional.get.getParam(param, fallback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package object numberUtil {
|
||||||
|
implicit class ExtendedDouble(number: Double) {
|
||||||
|
def truncate(decimals: Int): Double = {
|
||||||
|
val n = Math.pow(10, decimals)
|
||||||
|
(number * n).toLong.toDouble / n
|
||||||
|
}
|
||||||
|
|
||||||
|
def range(min: Double, max: Double): Double = {
|
||||||
|
if(number < min) return min
|
||||||
|
if(number > max) return max
|
||||||
|
number
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is not perfect if the number has accuracy problems
|
||||||
|
def getNumOfDecimals: Int = {
|
||||||
|
val limit = 1000 // just a safeguard
|
||||||
|
def recursiveDecimals(n: Double, run: Int): Int = {
|
||||||
|
if (n % 1d == 0 || run >= limit) {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
1 + recursiveDecimals(n * 10, run + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recursiveDecimals(number, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
def resolution(res: Double): Double = {
|
||||||
|
val result = (number / res).toLong.toDouble * res
|
||||||
|
// remove any accuracy artifacts by truncating (if resolution does not have problems)
|
||||||
|
result.truncate(res.getNumOfDecimals)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package object yml {
|
package object yml {
|
||||||
val yaml = new Yaml
|
val yaml = new Yaml
|
||||||
// TODO: handle errors (invalid file/format)
|
// TODO: handle errors (invalid file/format)
|
||||||
|
|||||||
Reference in New Issue
Block a user