This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

Interpreter.scala 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
package de.bbisping.coupledsim.ts

import de.bbisping.coupledsim.util.Interpreting
import de.bbisping.coupledsim.util.Interpreting._
import de.bbisping.coupledsim.util.Relation
import de.bbisping.coupledsim.tool.model.NodeID
import de.bbisping.coupledsim.util.LabeledRelation

class Interpreter[S, A, L](
    tsDef: Syntax.Definition,
    stateIds: String => S,
    arrowLabeling: Option[Syntax.Label] => Interpreting.Result[A],
    nodeLabeling: Option[Syntax.NodeDeclaration] => Interpreting.Result[L]
  ) {
  
  import TransitionSystem._
  
  val defaultFactory = TransitionSystem[S, A, L](_, _)
  
  def result[R](factory: (LabeledRelation[S, A], Map[S, L]) => R = defaultFactory): Result[R] = {
    
    for {
      nodeDecls <- factorResults ( tsDef.defs collect {
        case d: Syntax.NodeDeclaration => convertNodeDeclaration(d)
      } )
      rels <- factorResults ( tsDef.defs collect {
        case r: Syntax.Relation => convertRelation(r)
      } )
      val labelMap = nodeDecls.toMap
      val trans = new LabeledRelation(rels.toSet)
      val emptyLabels = (trans.lhs ++ trans.rhs) map { n => (n, nodeLabeling(None).get) } toMap
      val labels = emptyLabels ++ labelMap
    } yield factory(trans, labels)
    
  }
  
  private def convertNodeDeclaration(n: Syntax.NodeDeclaration): Result[(S, L)] = n match {
    case d @ Syntax.NodeDeclaration(name, attribs, pos) =>
      nodeLabeling(Some(d)).map((stateIds(name), _))
    case _ =>
      Problem("Node not accepted.", List(n))
  }
  
  private def convertRelation(e: Syntax.Relation): Result[(S, A, S)] = e match {
    case Syntax.StepTo(Syntax.SingleNode(n1, _), aL @ Syntax.Label(l, _), Syntax.SingleNode(n2, _), _) => 
      arrowLabeling(Some(aL)).map((stateIds(n1), _, stateIds(n2)))
    case e =>
      Problem("Illegal Relation.", List(e))
  }
}