[add][mod] added getKey and getKeyOrElse for Map[String, Any] in mapUtil

- [mod] use getKey and getKeyOrElse from mapUtil in FunctionArgs
This commit is contained in:
2020-01-29 01:19:11 -06:00
parent c7013eddd4
commit 4e5d9570ad
2 changed files with 16 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package scim.datastruct
import scim.lib.ConcurrentBroadcast._ import scim.lib.ConcurrentBroadcast._
import scim.lib.simInterfaces._ import scim.lib.simInterfaces._
import scim.lib.configInterfaces._ import scim.lib.configInterfaces._
import scim.lib.mapUtil._
import scala.collection.parallel.ForkJoinTaskSupport import scala.collection.parallel.ForkJoinTaskSupport
import scala.util.control.NonFatal import scala.util.control.NonFatal
@@ -125,23 +126,22 @@ 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): Option[A] = { def getParam[A](param: String): Option[A] = {
// just try to get the param, otherwise use fallback args.getKey[A](param)
try { Some(args(param).asInstanceOf[A]) } catch { case NonFatal(_) => None }
} }
def getParam[A](param: String, fallback: A): A = { def getParam[A](param: String, fallback: A): A = {
getParam(param).getOrElse(fallback) args.getKeyOrElse[A](param, 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,
defaultArgs: Option[FunctionArgs] = None){ defaultArgs: Option[FunctionArgs] = None) {
def get(t: Time): (SimulationBehaviour, Option[FunctionArgs]) = { def get(t: Time): (SimulationBehaviour, Option[FunctionArgs]) = {
function(t.time).getOrElse((defaultFunc, defaultArgs)) function(t.time).getOrElse((defaultFunc, defaultArgs))
} }
} }
case class Behaviour(behaviour: BehaviourVector){ case class Behaviour(behaviour: BehaviourVector) {
def run(time: Time, cell: Cell, truth: Truth): Option[Double] = { def run(time: Time, cell: Cell, truth: Truth): Option[Double] = {
val functionTuple = behaviour.get(time) val functionTuple = behaviour.get(time)
val function = functionTuple._1 val function = functionTuple._1

View File

@@ -6,6 +6,7 @@ import java.io.{File, FileInputStream}
import scim.datastruct.FunctionArgs import scim.datastruct.FunctionArgs
import scala.collection.immutable.ListMap import scala.collection.immutable.ListMap
import scala.util.control.NonFatal
@@ -117,6 +118,16 @@ package object convertUtil {
package object mapUtil { package object mapUtil {
implicit class MapGet(map: Map[String, Any]) {
def getKey[A](key: String): Option[A] = {
// just try to get the param, otherwise use fallback
try { Some(map(key).asInstanceOf[A]) } catch { case NonFatal(_) => None }
}
def getKeyOrElse[A](key: String, fallback: A): A = {
getKey[A](key).getOrElse(fallback)
}
}
def traversableToMap[A, B](traversable: Traversable[Any], getTuple: (Any) => (A, B) = (e: Any) => e.asInstanceOf[(A, B)]._1 -> e.asInstanceOf[(A, B)]._2): Map[A, B] = { def traversableToMap[A, B](traversable: Traversable[Any], getTuple: (Any) => (A, B) = (e: Any) => e.asInstanceOf[(A, B)]._1 -> e.asInstanceOf[(A, B)]._2): Map[A, B] = {
// Traversable[Tuple[A, B]] can be converted via .map // Traversable[Tuple[A, B]] can be converted via .map
// .foldLeft used in favor of .map().toMap as the latter needs two traversals // .foldLeft used in favor of .map().toMap as the latter needs two traversals