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 📥

Structure.scala 10.3 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
package de.bbisping.coupledsim.tool.control

import scala.collection.mutable.HashMap
import scala.scalajs.js.Date
import de.bbisping.coupledsim.tool.arch.Action
import de.bbisping.coupledsim.tool.arch.Control
import de.bbisping.coupledsim.tool.model.NodeID
import de.bbisping.coupledsim.ts.Interpreter
import de.bbisping.coupledsim.ts.Syntax
import de.bbisping.coupledsim.ts.WeakTransitionSystem
import de.bbisping.coupledsim.util.Coloring
import de.bbisping.coupledsim.util.Interpreting
import de.bbisping.coupledsim.util.Relation
import de.bbisping.coupledsim.util.LabeledRelation
import de.bbisping.coupledsim.ts.DivergenceInformation
import de.bbisping.coupledsim.algo.AlgorithmLogging

class Structure(val main: Control) extends ModelComponent {
  
  var structure: Structure.TSStructure = null
  var partition: Coloring[NodeID] = Coloring(Map())
  var relation: LabeledRelation[NodeID, String] = LabeledRelation()
  var currentReplayStep: Int = 0
  var currentReplay = List[() => AlgorithmLogging.LogEntry[NodeID, Structure.ActionLabel, Structure.NodeLabel]]()
  
  val operations = HashMap[String, StructureOperation]()
  
  def init() {
    registerOperation(new StructureOperation.NaiveStrongBisimAnalyzer)
//    registerOperation(new StructureOperation.NaiveDivergenceMarker)
    
//    registerOperation(new StructureOperation.SchematicSimilarityAnalyzer)
    registerOperation(new StructureOperation.RefinedSimilarityAnalyzer)
//    registerOperation(new StructureOperation.BasicSASimilarityAnalyzer)
//    registerOperation(new StructureOperation.RefinedSASimilarityAnalyzer)
//    registerOperation(new StructureOperation.SASimilarityAnalyzer) // does not yet work!
    
    registerOperation(new StructureOperation.BasicSAWeakSimilarityAnalyzer)
    
//    registerOperation(new StructureOperation.SchematicCoupledSimilarityAnalyzer)
    registerOperation(new StructureOperation.FixedPointCoupledSimilarityAnalyzer)
//    registerOperation(new StructureOperation.BasicSACoupledSimilarityAnalyzer)
    registerOperation(new StructureOperation.GameCoupledSimilarityAnalyzer)
    registerOperation(new StructureOperation.GameCoupledSimilarityPlainAnalyzer)
    registerOperation(new StructureOperation.TauClosureCoupledSimilarityAnalyzer)
    registerOperation(new StructureOperation.NaiveCoupledSimChecker)
    
    registerOperation(new StructureOperation.SigrefBisimilarityAnalyzer)
    registerOperation(new StructureOperation.SigrefWeakBisimilarityAnalyzer)
    
    registerOperation(new StructureOperation.NaiveTransitiveTauClosure)
    registerOperation(new StructureOperation.NaiveReflexiveTauClosure)
//    registerOperation(new StructureOperation.RemoveTauEndoSteps)
//    registerOperation(new StructureOperation.InstableStateRemover)
    
    registerOperation(new StructureOperation.WeakStepRelationClosure)
    registerOperation(new StructureOperation.AGraphConstructor)
    registerOperation(new StructureOperation.PS94CSNormalizer)
    
    registerOperation(new StructureOperation.TauLoopCompressor)
    registerOperation(new StructureOperation.QuotientBuilder)
  }
  
  def registerOperation(op: StructureOperation) = {
    operations += (op.slug -> op)
    broadcast(Structure.StructureOperationsChanged(operations))
  }
  
  override def notify(c: ModelComponent.Change) = c match {
    case Source.SourceChange(source, ast) =>
      val beginInterpret = Date.now
      val interpretationResult =
        new Interpreter(ast, NodeID(_), Structure.arrowAnnotator, Structure.nodeAnnotator)
        .result(Structure.transitionSystemConstructor(_, _))
      
      interpretationResult match {
        case p: Interpreting.Problem =>
          println("Interpretation failed after: " + (Date.now - beginInterpret) + "ms.")
          broadcast(Structure.StructureChangeFailed(p))
        case Interpreting.Success(is: Structure.TSStructure) =>
          println("Interpretation took: " + (Date.now - beginInterpret) + "ms.")
          setStructure(is)
      }
    case _ => 
  }
  
  def setPartition(p: Coloring[NodeID]) = {
    partition = p
    broadcast(Structure.StructurePartitionChange(partition))
  }
  
  def setRelation(r: Relation[NodeID]): Unit = {
    val labeled = new LabeledRelation(r.tupleSet.map {case (p1, p2) => (p1, "", p2)})
    setRelation(labeled)
  }
  
  def setRelation(r: LabeledRelation[NodeID, String]): Unit = {
    relation = r
    broadcast(Structure.StructureRelationChange(relation))
  }
  
  def setReplay(replay: List[() => AlgorithmLogging.LogEntry[NodeID, Structure.ActionLabel, Structure.NodeLabel]]): Unit = {
    currentReplay = replay
    currentReplayStep = 0
    broadcast(Structure.StructureReplayChange(currentReplay))
  }
  
  def doReplayStep(): Boolean = {
    if (currentReplayStep < currentReplay.length) {
      currentReplay(currentReplayStep)() match {
        case AlgorithmLogging.LogRelation(rel, comment) =>
          val labeled = new LabeledRelation(rel.tupleSet.map {case (p1, p2) => (p1, "", p2)})
          broadcast(Structure.StructureCommentChange(comment))
          broadcast(Structure.StructureRelationChange(labeled))
      }
      currentReplayStep += 1
      true
    } else if (currentReplayStep == currentReplay.length) {
      // reset everything
      broadcast(Structure.StructureRelationChange(relation))
      true
    } else {
      false
    }
  }
  
  def setStructure(is: Structure.TSStructure) = {
    structure = is
    broadcast(Structure.StructureChange(structure))
    setPartition(Coloring.fromPartition(Set(is.nodes)))
    setRelation(Relation[NodeID]())
  }
}

object Structure {
  
  type TSStructure = WeakTransitionSystem[NodeID, ActionLabel, NodeLabel]
  
  abstract sealed class StructureAction extends Action {
    override def implement(target: ModelComponent): Boolean = target match {
      case s: Structure => 
        implementStructure(s)
      case _ =>
        false
    }
    
    def implementStructure(structure: Structure): Boolean
  }
  
  case class StructureChange(tsStructure: TSStructure) extends ModelComponent.Change
  
  case class StructureChangeFailed(problem: Interpreting.Problem) extends ModelComponent.Change
  
  case class StructurePartitionChange(partition: Coloring[NodeID]) extends ModelComponent.Change
  
  case class StructureRelationChange(relation: LabeledRelation[NodeID, String]) extends ModelComponent.Change
  
  case class StructureCommentChange(comment: String) extends ModelComponent.Change
  
  case class StructureReplayChange(replay: List[() => AlgorithmLogging.LogEntry[NodeID, Structure.ActionLabel, Structure.NodeLabel]])
    extends ModelComponent.Change
  
  case class StructureOperationsChanged(operations: HashMap[String, StructureOperation]) extends ModelComponent.Change {
    override def toString() = operations.map(_._1).mkString
  }
  
  case class ActionLabel(
      val act: Symbol,
      val x: Option[Double] = None,
      val y: Option[Double] = None) {
    
    def this(name: String) = {
      this(Symbol(name), None, None)
    }
    
    val hash = act.hashCode()
    
    override def hashCode() = hash
    
    override def equals(other: Any) = other match {
      case o: ActionLabel => this.act == o.act
      case _ => false
    }
    
    def toActString = {
      act.name
    }
    
    override def toString() = toActString
    
    def toStringPairList = {
      act.name ++
      x.toList.map(v => ("x", v.round.toString)) ++
      y.toList.map(v => ("y", v.round.toString))
    }
  }
  
  case class NodeLabel(
      val act: Set[Symbol],
      val x: Option[Double] = None,
      val y: Option[Double] = None) {
    
    val hash = act.hashCode()
    
    override def hashCode() = hash
    
    override def equals(other: Any) = other match {
      case o: NodeLabel => this.act == o.act
      case _ => false
    }
    
    def toStringPairList = {
      act.toList.map(v => (v.name, "")) ++
      x.toList.map(v => ("x", v.round.toString)) ++
      y.toList.map(v => ("y", v.round.toString))
    }
    
  }
  
  val emptyLabel = NodeLabel(Set())
  
  val silentLabel = NodeLabel(Set('tau))
  
  val emptyActionLabel = new ActionLabel("")
  
  val silentActionLabel = ActionLabel('tau)
  
  def nodeAnnotator(nodeDecl: Option[Syntax.NodeDeclaration]): Interpreting.Result[NodeLabel] = nodeDecl match {
    case Some(nD @ Syntax.NodeDeclaration(name, attribs, pos)) =>
      val labels = attribs collect { case (k, _) if k != "x" && k != "y" => Symbol(k) }
      try {
        val l = NodeLabel(
            act = labels.toSet,
            x = attribs.find(_._1 == "x").map(_._2.toDouble),
            y = attribs.find(_._1 == "y").map(_._2.toDouble)
        )
        Interpreting.Success(l)
      } catch {
        case e: Exception => Interpreting.Problem(e.toString(), List(nD))
      }
    case None =>
      Interpreting.Success(emptyLabel)
  }
  
  def arrowAnnotator(arrowLabel: Option[Syntax.Label]): Interpreting.Result[ActionLabel] = arrowLabel match {
    case Some(aL @ Syntax.Label(name, pos)) =>
      try {
        val l = ActionLabel(
            act = Symbol(name)
        )
        Interpreting.Success(l)
      } catch {
        case e: Exception => Interpreting.Problem(e.toString(), List(aL))
      }
    case None =>
      Interpreting.Success(emptyActionLabel)
  }
  
  
  def transitionSystemConstructor(
      rel: LabeledRelation[NodeID, ActionLabel],
      labels: Map[NodeID, Structure.NodeLabel],
      oldTs: Option[Structure.TSStructure] = None) = {
    
    val silentActions = rel.labels filter (_.act == 'tau)
    
    oldTs match {
      case Some(d: DivergenceInformation[_]) =>
        new WeakTransitionSystem(rel, labels, silentActions.toSet) with DivergenceInformation[NodeID] {
          def diverges(s: NodeID): Boolean = d.asInstanceOf[DivergenceInformation[NodeID]].diverges(s)
        }
      case _ =>
        new WeakTransitionSystem(rel, labels, silentActions.toSet)
    }
    
  }
  
  case class StructureCallOperation(slug: String, resetReplay: Boolean = true) extends StructureAction {
    override def implementStructure(structure: Structure) = {
      val op = structure.operations.get(slug)
      for (o <- op) {
        if (resetReplay) {
          structure.setReplay(List())
        }
        o applyOperation structure
      }
      op.isDefined
    }
  }
  
  case class StructureDoReplayStep(goToStep: Int = -1) extends StructureAction {
    override def implementStructure(structure: Structure) = {
      if (goToStep >= 0) structure.currentReplayStep = goToStep
      structure.doReplayStep()
    }
  }
    
}