[add] finished internal parser

- the parser now returns a Configuration
This commit is contained in:
2020-01-29 01:19:11 -06:00
parent ab825f8bf0
commit e84a253648

View File

@@ -1,6 +1,6 @@
package scim.components 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.ConcurrentBroadcast.BroadcastObject
import scim.lib.debugUtil.{DebugType, debugf} import scim.lib.debugUtil.{DebugType, debugf}
import scim.lib.mapUtil._ import scim.lib.mapUtil._
@@ -11,10 +11,9 @@ import scim.lib.yml
import scala.collection.mutable import scala.collection.mutable
class Parser() { 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) val parser = new SimulationConfigParser(yml.parse(file), simulationBehaviours)
parser.parse() 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 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 // this will throw if the map doesn't contain what it should
@throws(classOf[Exception]) @throws(classOf[Exception])
def parse(): Unit = { def parse(): Configuration = {
val sensors = input("sensors").asInstanceOf[Vector[Map[String, Any]]] val sensors = input("sensors").asInstanceOf[Vector[Map[String, Any]]]
val groundTruths = input("groundtruths").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 //populate configuration
val configurationMapping = sensorList.map(cell => cell._1 -> cellMapper(cell._1, cell._2.toMap)).toMap 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
}
val sharedMemoryTruth = Vector.tabulate(config.duration)(t => (Time(t, config.duration), configurationMapping.map(a => truthHelper(a._1, a._2))))
//TODO: initialize shared memory val sharedMemorySensorMap = traversableToMap[Time, Map[Cell, Map[Group, Vector[SensorResult]]]](sharedMemorySensor)
//TODO: add ground truth preconfigured to shared memory
Unit 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 = { 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 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 val it = parsedFunction.toList.sortBy(_._1).iterator
println(parsedFunction.toList.sortBy(_._1))
var current = if(it.hasNext) Some(it.next) else None var current = if(it.hasNext) Some(it.next) else None
while(!current.isEmpty) { while(!current.isEmpty) {
val next = if(it.hasNext) Some(it.next) else None val next = if(it.hasNext) Some(it.next) else None