From c7013eddd4f9990803acd64ea7e040ec8047e700 Mon Sep 17 00:00:00 2001 From: Pascal Date: Wed, 29 Jan 2020 01:19:11 -0600 Subject: [PATCH] [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 --- src/main/scala/scim/datastruct/Configuration.scala | 10 +++++++--- src/main/scala/scim/lib/Library.scala | 9 ++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/scala/scim/datastruct/Configuration.scala b/src/main/scala/scim/datastruct/Configuration.scala index 8b011c2..0a7538f 100644 --- a/src/main/scala/scim/datastruct/Configuration.scala +++ b/src/main/scala/scim/datastruct/Configuration.scala @@ -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) } } diff --git a/src/main/scala/scim/lib/Library.scala b/src/main/scala/scim/lib/Library.scala index 7c9e43d..b429d8d 100644 --- a/src/main/scala/scim/lib/Library.scala +++ b/src/main/scala/scim/lib/Library.scala @@ -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) + } + } } }