diff --git a/src/main/scala/scim/components/Parser.scala b/src/main/scala/scim/components/Parser.scala index 6f818d5..0a6ab6a 100644 --- a/src/main/scala/scim/components/Parser.scala +++ b/src/main/scala/scim/components/Parser.scala @@ -1,6 +1,6 @@ package scim.components -import scim.datastruct.{Behaviour, BehaviourVector, Cell, Configuration, FunctionArgs, GroundTruth, Group, Sensor, SensorCount, SharedMemory, SimulationBehaviours, SimulationConfig, Time, Truth} +import scim.datastruct.{Behaviour, BehaviourVector, Cell, Configuration, FunctionArgs, GroundTruth, Group, Sensor, SensorCount, SensorResult, SharedMemory, SimulationBehaviours, SimulationConfig, Time, Truth} import scim.lib.ConcurrentBroadcast.BroadcastObject import scim.lib.debugUtil.{DebugType, debugf} import scim.lib.mapUtil._ @@ -11,10 +11,9 @@ import scim.lib.yml import scala.collection.mutable class Parser() { - def parse(file: String, simulationBehaviours: SimulationBehaviours): Option[Configuration] = { + def parse(file: String, simulationBehaviours: SimulationBehaviours): Configuration = { val parser = new SimulationConfigParser(yml.parse(file), simulationBehaviours) parser.parse() - None // FIXME: this should return something } } @@ -38,7 +37,7 @@ class SimulationConfigParser(input: Map[String, Any], simulationBehaviours: Simu private val groundTruthValues = mutable.Map[Time, mutable.Map[Group, Truth]]() // FIXME: are not parsed correctly // this will throw if the map doesn't contain what it should @throws(classOf[Exception]) - def parse(): Unit = { + def parse(): Configuration = { val sensors = input("sensors").asInstanceOf[Vector[Map[String, Any]]] val groundTruths = input("groundtruths").asInstanceOf[Vector[Map[String, Any]]] @@ -55,13 +54,24 @@ class SimulationConfigParser(input: Map[String, Any], simulationBehaviours: Simu //populate configuration val configurationMapping = sensorList.map(cell => cell._1 -> cellMapper(cell._1, cell._2.toMap)).toMap - + // construct shared memory sensor + val sharedMemorySensor = Vector.tabulate(config.duration)(t => (Time(t, config.duration), sensorList.map(kv => kv._1 -> kv._2.map(kv2 => kv2._1 -> Vector.tabulate(kv2._2.count)(_ => SensorResult(new BroadcastObject()))).toMap).toMap)) + def truthHelper(cell: Cell, list: Map[Group, List[(Group, Sensor, Option[GroundTruth])]]): (Cell, Map[Group, Truth]) = { + val groupList: mutable.Map[Group, Truth] = mutable.Map() + list.foreach(kv => kv._2.foreach(e => groupList(e._1) = Truth(new BroadcastObject()))) + cell -> groupList.toMap + } - //TODO: initialize shared memory - //TODO: add ground truth preconfigured to shared memory + val sharedMemoryTruth = Vector.tabulate(config.duration)(t => (Time(t, config.duration), configurationMapping.map(a => truthHelper(a._1, a._2)))) - Unit + val sharedMemorySensorMap = traversableToMap[Time, Map[Cell, Map[Group, Vector[SensorResult]]]](sharedMemorySensor) + + val sharedMemoryTruthMap = traversableToMap[Time, Map[Cell, Map[Group, Truth]]](sharedMemoryTruth) + + val sharedMemory = SharedMemory(sharedMemorySensorMap, sharedMemoryTruthMap, groundTruthValues.map(kv => kv._1 -> kv._2.toMap).toMap, config, groups.toMap, Vector.tabulate(config.duration)(_ => new BroadcastObject[Boolean]())) + + Configuration(configurationMapping, sharedMemory) } private def parseSimulationConfig(configInput: Map[String, Any]): SimulationConfig = { @@ -165,7 +175,6 @@ class SimulationConfigParser(input: Map[String, Any], simulationBehaviours: Simu } )) val timeVector: mutable.ArraySeq[Option[(SimulationBehaviour, Option[FunctionArgs])]] = new mutable.ArraySeq[Option[(SimulationBehaviour, Option[FunctionArgs])]](config.duration).map(_ => None) val it = parsedFunction.toList.sortBy(_._1).iterator - println(parsedFunction.toList.sortBy(_._1)) var current = if(it.hasNext) Some(it.next) else None while(!current.isEmpty) { val next = if(it.hasNext) Some(it.next) else None