[mod][fix] modified getParam to allow for None if it doesn't exist
- this allows for optional parameters - [fix] only catch NonFatal errors in getParam
This commit is contained in:
@@ -3,7 +3,9 @@ package scim.datastruct
|
||||
import scim.lib.ConcurrentBroadcast._
|
||||
import scim.lib.simInterfaces._
|
||||
import scim.lib.configInterfaces._
|
||||
|
||||
import scala.collection.parallel.ForkJoinTaskSupport
|
||||
import scala.util.control.NonFatal
|
||||
|
||||
// configuration
|
||||
//case class Configuration(simulation: Simulation, sensors: Vector[Sensor], groundTruths: Vector[GroundTruth], behaviours: Vector[Behaviour])
|
||||
@@ -122,9 +124,12 @@ case class Group(name: String, sensorParent: Option[String] = None){
|
||||
|
||||
case class Truth() extends Result[Option[Double]]
|
||||
case class FunctionArgs(args: Map[String, Any]) {
|
||||
def getParam[A](param: String): Option[A] = {
|
||||
// just try to get the param, otherwise use fallback
|
||||
try { Some(args(param).asInstanceOf[A]) } catch { case NonFatal(_) => None }
|
||||
}
|
||||
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 }
|
||||
getParam(param).getOrElse(fallback)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +146,6 @@ case class Behaviour(behaviour: BehaviourVector){
|
||||
val functionTuple = behaviour.get(time)
|
||||
val function = functionTuple._1
|
||||
val args = functionTuple._2
|
||||
// TODO: add try catch, to prevent crash in case the args are wrong
|
||||
function(cell, time, truth.get, args)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,13 +317,20 @@ package object mapUtil {
|
||||
|
||||
package object behaviourUtil {
|
||||
implicit class FunctionArgsOption(optional: Option[FunctionArgs]) {
|
||||
def getParam[B](param: String, fallback: B): B = {
|
||||
def getParam[A](param: String, fallback: A): A = {
|
||||
if(optional.isEmpty) {
|
||||
fallback
|
||||
} else {
|
||||
optional.get.getParam(param, fallback)
|
||||
}
|
||||
}
|
||||
def getParam[A](param: String): Option[A] = {
|
||||
if(optional.isEmpty) {
|
||||
None
|
||||
} else {
|
||||
optional.get.getParam(param)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user