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 📥

StructureOperation.scala 19.2 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
package de.bbisping.coupledsim.tool.control

import scala.scalajs.js.Date

import de.bbisping.coupledsim.algo.transform.AGraphConstruction
import de.bbisping.coupledsim.algo.AlgorithmLogging
import de.bbisping.coupledsim.algo.NaiveDivergenceFinder
import de.bbisping.coupledsim.algo.NaiveReflexiveClosure
import de.bbisping.coupledsim.algo.NaiveStrongBisimC
import de.bbisping.coupledsim.algo.NaiveTransitiveClosure
import de.bbisping.coupledsim.algo.RemoveEndoTransitions
import de.bbisping.coupledsim.algo.WeakStepAdder
import de.bbisping.coupledsim.algo.cs.BasicSACoupled
import de.bbisping.coupledsim.algo.cs.FixedPointCoupledSimilarity
import de.bbisping.coupledsim.algo.cs.GameCoupledSimilarity
import de.bbisping.coupledsim.algo.cs.SchematicCoupledSimilarity
import de.bbisping.coupledsim.algo.cs.GameCoupledSimilarityPlain
import de.bbisping.coupledsim.algo.rt2010.BasicSA
import de.bbisping.coupledsim.algo.rt2010.BasicSAWeak
import de.bbisping.coupledsim.algo.rt2010.RefinedSA
import de.bbisping.coupledsim.algo.rt2010.RefinedSimilarity
import de.bbisping.coupledsim.algo.rt2010.SA
import de.bbisping.coupledsim.algo.rt2010.SchematicSimilarity
import de.bbisping.coupledsim.algo.sigref
import de.bbisping.coupledsim.checkers.NaiveCoupledSimCheck
import de.bbisping.coupledsim.tool.control.Structure.ActionLabel
import de.bbisping.coupledsim.tool.model.NodeID
import de.bbisping.coupledsim.ts.DivergenceInformation
import de.bbisping.coupledsim.ts.TransitionSystem
import de.bbisping.coupledsim.ts.WeakTransitionSystem
import de.bbisping.coupledsim.util.Coloring
import de.bbisping.coupledsim.util.LabeledRelation
import de.bbisping.coupledsim.util.Relation
import de.bbisping.coupledsim.algo.RemoveInstableStates
import de.bbisping.coupledsim.algo.transform.TauLoopCompression
import de.bbisping.coupledsim.algo.transform.BuildQuotientSystem
import de.bbisping.coupledsim.algo.transform.PS94CSNormalForms

trait StructureOperation {
  
  val name: String
  
  val slug: String
  
  val category: String = "default"
  
  val description: String = ""
  
  def applyOperation(system: Structure): Unit
  
  override def toString() = slug
}

object StructureOperation {
    
  /** Some analyzer which returns a coloring/partition of nodes. */
  trait StructureAnalyzer extends StructureOperation {
    
    def analyze(system: Structure.TSStructure): Coloring[NodeID]
    
    /** calls the analyzer and sets the current partition for the structure. */
    override def applyOperation(structure: Structure): Unit = {
      val begin = Date.now
      val c = analyze(structure.structure)
      println("Analyzer «" + name + "» took: " + (Date.now - begin) + "ms.")
      structure.setPartition(c)
    }
  }
  
  class NaiveStrongBisimAnalyzer extends StructureAnalyzer {
    
    override val name = "Naive Strong Bisim"
    
    override val slug = "naive-strong-bisim"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the strong bisimilarity coloring, " +
      "using the naive interative approach [BGR16]."
    
    override def analyze(system: Structure.TSStructure) = new NaiveStrongBisimC(system).compute()
    
  }
  
  class NaiveDivergenceAnalyzer extends StructureAnalyzer {
    
    override val name = "Naive Divergence Finder"
    
    override val slug = "naive-divergence-finder"
    
    override def analyze(system: Structure.TSStructure) = new NaiveDivergenceFinder(system).compute()
    
  }
  
  /** Some analyzer which returns a relation on nodes. */
  trait StructureRelationAnalyzer extends StructureOperation {
    
    def analyze(system: Structure.TSStructure): (Relation[NodeID], Option[AlgorithmLogging[NodeID, Structure.ActionLabel, Structure.NodeLabel]])
    
    /** calls the analyzer and sets the current relation for the structure. */
    override def applyOperation(structure: Structure): Unit = {
      val begin = Date.now
      val (r, log) = analyze(structure.structure)
      println("Analyzer «" + name + "» took: " + (Date.now - begin) + "ms.")
      structure.setRelation(r)
      for {l <- log} structure.setReplay(l.getReplay())
    }
  }
  
  class SchematicSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Schematic Similarity"
    
    override val slug = "schematic-similarity"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the strong simulation preorder relation in the simplest conceivable way [RT10]."
      
    override def analyze(system: Structure.TSStructure) = {
      val algo = new SchematicSimilarity(system)
      (algo.compute(), Some(algo))
    }
    
  }
  
  class RefinedSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Refined Similarity"
    
    override val slug = "refined-similarity"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the strong simulation preorder relation " +
      "by a refined similarity algorithm [RT10]."
    
    override def analyze(system: Structure.TSStructure) = (new RefinedSimilarity(system).compute(), None)
    
  }
  
  class BasicSASimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "BasicSA Similarity"
    
    override val slug = "basic-sa-similarity"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the strong simulation preorder relation " +
      "by the BasicSA algorithm from [RT10]."
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new BasicSA(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class BasicSAWeakSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "BasicSA Similarity Weak"
    
    override val slug = "basic-sa-similarity-weak"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the weak simulation preorder relation " +
      "by a modified version of the BasicSA algorithm from [RT10]."
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new BasicSAWeak(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class RefinedSASimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "RefinedSA Similarity"
    
    override val slug = "refined-sa-similarity"
    
    override val category = "SimBisim"
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new RefinedSA(system)
      (algo.compute(), Some(algo))
    }
  }
  
  /* not completed! */
  class SASimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "SA Similarity (incomplete!)"
    
    override val slug = "sa-similarity"
    
    override val category = "SimBisim"
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new SA(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class SigrefBisimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Sigref Bisimilarity"
    
    override val slug = "sigref-bisimilarity"
    
    override val category = "SimBisim"

    override val description =
      "Computes the strong bisimilarity relation " +
      "by iterating a signature refinement algorithm [Wim11]."
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new sigref.Bisimilarity(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class SigrefWeakBisimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Sigref Weak Bisimilarity"
    
    override val slug = "sigref-weak-bisimilarity"
    
    override val category = "SimBisim"
    
    override val description =
      "Computes the weak bisimilarity relation " +
      "by iterating a signature refinement algorithm [Wim11]."
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new sigref.WeakBisimilarity(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class SchematicCoupledSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Schematic Coupled Similarity"
    
    override val slug = "schematic-coupled-similarity"
    
    override def analyze(system: Structure.TSStructure) = (new SchematicCoupledSimilarity(system).compute(), None)
  }
  
  class FixedPointCoupledSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Fixed-Point Coupled Similarity"
    
    override val slug = "fixed-point-coupled-similarity"
    
    override val description =
      "Computes the coupled simulation preorder (relation) " +
      "by iterating to the greatest fixed point of the coupled simulation rules. (Sec 4.3 in the thesis)"
    
    override def analyze(system: Structure.TSStructure) = (new FixedPointCoupledSimilarity(system).compute(), None)
  }
  
  
  /* does not yet work */
  class BasicSACoupledSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "BasicSA Coupled Similarity (unfinished)"
    
    override val slug = "basic-sa-coupled-similarity"
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new BasicSACoupled(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class GameCoupledSimilarityAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Game Coupled Similarity"
    
    override val slug = "game-coupled-similarity"
    
    override val description =
      "Computes the coupled simulation preorder (relation) " +
      "by solving the minimized coupled simulation game. (Sec 4.5 in the thesis)"
    
    override def analyze(system: Structure.TSStructure) = {
      val algo = new GameCoupledSimilarity(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class GameCoupledSimilarityPlainAnalyzer extends StructureRelationAnalyzer {
    
    override val name = "Game Coupled Similarity Plain"
    
    override val slug = "game-coupled-similarity-plain"
    
    override val description =
      "Computes the coupled simulation preorder (relation) " +
      "by solving the full-blown coupled simulation game. (Sec 4.4 in the thesis)"
      
    override def analyze(system: Structure.TSStructure) = {
      val algo = new GameCoupledSimilarityPlain(system)
      (algo.compute(), Some(algo))
    }
  }
  
  class TauClosureCoupledSimilarityAnalyzer extends StructureAnalyzer {
    
    override val name = "Bisimulation Reduction Coupled Similarity"
    
    override val slug = "tau-closure-coupled-similarity"
    
    override val description =
      "Computes a coupled similarity partitioning (coloring) of the nodes " +
      "by reducing the problem to strong bisimilarity using the [PS94]-CS-Normalizer Closure. (Sec 4.2 in the thesis)"
    
    override def analyze(system: Structure.TSStructure) = {
      val psCSTauClosureSystem = new PS94CSNormalizer().transform(system)
      val csWeakStepSystem = new NaiveReflexiveTauClosure().transform(psCSTauClosureSystem)
      
      new sigref.Bisimilarity(csWeakStepSystem).computePartition().filter(system.nodes)
    }
  }
  
  class NaiveCoupledSimChecker extends StructureOperation {
    
    override val name = "Coupled Simulation Checker"
    
    override val slug = "naive-coupled-simulation-checker"
    
    override val description =
      "Checks whether all tuples in a relation respect coupled simulation. (If there are counter examples, they will be marked in red.)"
      
    override def applyOperation(structure: Structure): Unit = {
      val begin = Date.now
      val r = new NaiveCoupledSimCheck(structure.structure, structure.relation.toRelation).check()
      println("Analyzer «" + name + "» took: " + (Date.now - begin) + "ms.")
      val relationAnnotation = for { ((p1, p2), l) <- r } yield (p1, l, p2)
      structure.setRelation(structure.relation merge new LabeledRelation(relationAnnotation))
    }
  }
  
  trait StructureTransformer extends StructureOperation {
    
    def transform(system: Structure.TSStructure): Structure.TSStructure
    
    override def applyOperation(structure: Structure) = {
      val begin = Date.now
      val c = transform(structure.structure)
      println("Transformer «" + name + "» took: " + (Date.now - begin) + "ms.")
      structure.setStructure(c)
    }
  }
  
  class NaiveDivergenceMarker extends StructureTransformer {
    
    override val name = "Naive Divergence Marker"
    
    override val slug = "naive-divergence-marker"
    
    override def transform(system: Structure.TSStructure): Structure.TSStructure = {
      val coloring = new NaiveDivergenceFinder(system).compute()
      
      new WeakTransitionSystem(system.step, system.nodeLabeling, system.silentActions) with DivergenceInformation[NodeID] {
        def diverges(s: NodeID): Boolean =
          coloring.colors(s) == 1
      }
    }
  }
  
  class NaiveTransitiveTauClosure extends StructureTransformer {
    
    override val name = "Transitive Tau Closure"
    
    override val slug = "naive-trans-tau-closure"
    
    override val category = "Closures"
    
    override val description =
      "Adds transitive closure of tau-steps."
    
    override def transform(system: Structure.TSStructure) = {
      val trans = new NaiveTransitiveClosure(system, system.silentActions).compute()
      Structure.transitionSystemConstructor(trans, system.nodeLabeling, Some(system))
    }
  }
  
  class NaiveReflexiveTauClosure extends StructureTransformer {
    
    override val name = "Reflexive Tau Closure"
    
    override val slug = "naive-refl-tau-closure"
    
    override val category = "Closures"
    
    override val description =
      "Adds reflexive closure of tau-steps."
    
    override def transform(system: Structure.TSStructure) = {
      val newTrans = new NaiveReflexiveClosure(system, Structure.silentActionLabel).compute()
      Structure.transitionSystemConstructor(newTrans.step, newTrans.nodeLabeling, Some(system))
    }
  }
  
  
  class WeakStepRelationClosure extends StructureTransformer {
    
    override val name = "Weak Step Relation Closure"
    
    override val slug = "weak-step-relation-closure"
    
    override val category = "Closures"
    
    override val description =
      "Adds visible steps after transitive tau-closure."
    
    override def transform(system: Structure.TSStructure) = {
      val trans = new WeakStepAdder(system, system.silentActions).compute()
      Structure.transitionSystemConstructor(trans, system.nodeLabeling, Some(system))
    }
  }
  
  class RemoveTauEndoSteps extends StructureTransformer {
    
    override val name = "Remove Tau Endo-Steps"
    
    override val slug = "remove-tau-endo-steps"
    
    override val category = "Closures"
    
    override def transform(system: Structure.TSStructure) = {
      val trans = new RemoveEndoTransitions(system, Set()).compute()
      Structure.transitionSystemConstructor(trans, system.nodeLabeling, Some(system))
    }
  }
  
  class InstableStateRemover extends StructureTransformer {
    
    override val name = "Remove Instable Non-Divergent States"
    
    override val slug = "remove-instable-states"
    
    override val category = "Closures"
    
    override def transform(system: Structure.TSStructure) = {
      val trans = new RemoveInstableStates(system).compute()
      Structure.transitionSystemConstructor(trans, system.nodeLabeling, Some(system))
    }
  }
  
  class NaiveObservableDeterminismAdder extends StructureTransformer {
    
    override val name = "Naive Observable Determinism Adder"
    
    override val slug = "naive-observable-determinism-adder"
    
    override val category = "Closures"
    
    override def transform(system: Structure.TSStructure) = {
      val newNodes = for {
        ids <- system.nodes
      } yield (ids, NodeID.freshNodeID(system, ids))
      val nodeFactory = { (n: NodeID, i: Int) => NodeID.freshNodeID(system, n, i) }
      val newTrans = system // new NaiveDeterminismAdder(system, nodeFactory, system.nodes).compute()
      Structure.transitionSystemConstructor(newTrans.step, newTrans.nodeLabeling, Some(system))
    }
  }
  
  class AGraphConstructor extends StructureTransformer {
    
    override val name = "AGraph Constructor"
    
    override val slug = "agraph-constructor"
    
    override val category = "Closures"
    
    override val description =
      "Constructs 'AGraphs' as described by [CH93]. On AGraphs, bisimilar states correspond to trace equivalent states " +
      "of the initial systems. The algorithm needs an 'initial' state---we just take the upmost state (with the minimal y value)."
    
    override def transform(system: Structure.TSStructure): Structure.TSStructure = {
      
      val divColoring = new NaiveDivergenceFinder(system).compute()
      
      val systemWithDivergence = new WeakTransitionSystem(system.step, system.nodeLabeling, system.silentActions) with DivergenceInformation[NodeID] {
        def diverges(s: NodeID): Boolean =
          divColoring.colors(s) == 1
      }
      
      def newState(ts: WeakTransitionSystem[NodeID, Structure.ActionLabel, Structure.NodeLabel], acc: Set[Structure.ActionLabel], c: Boolean): (NodeID, Structure.NodeLabel) = {
        val id = NodeID.freshNodeID(ts, NodeID(acc.mkString("_") + "__" + c))
        val label = Structure.NodeLabel(acc.map(_.act))
        (id, label)
      }
      
      val constr = new AGraphConstruction(systemWithDivergence, newState)
      
      val (newTrans, newInitial) = constr.compute(systemWithDivergence.nodes.minBy(n => system.nodeLabeling(n).y.getOrElse(Double.MaxValue)))
      
      Structure.transitionSystemConstructor(newTrans.step, newTrans.nodeLabeling, Some(system))
    }
  }
  
  class PS94CSNormalizer extends StructureTransformer {
    
    override val name = "PS94 CS Normalalizer"
    
    override val slug = "ps94-cs-normalalizer"
    
    override val category = "Closures"
    
    override val description =
      "Builds CS-normal forms in the spirit of [PS94] (Sec 4.2 in the thesis). (For a full reduction to bisimulation, you must afterwards perform a reflexive tau closure)"
    
    override def transform(system: Structure.TSStructure): Structure.TSStructure = {
      
      var stateId = 0
      def newState(s: NodeID) = {
        stateId += 1
        NodeID.freshNodeID(system, NodeID(s.name + "__partial__"), stateId)
      }
      
      new PS94CSNormalForms(system, newState).compute()
    }
  }
  
  
  class TauLoopCompressor extends StructureTransformer {
    
    override val name = "Tau-loop Compression"
    
    override val slug = "tau-loop-compression"
    
    override val category = "Compressions"
    
    override val description =
      "Compresses tau loops to tau cycles (Sec 4.1.1 of the thesis)."
    
    override def transform(system: Structure.TSStructure) = {
      new TauLoopCompression(system).compute()
    }
  }
  
  class QuotientBuilder extends StructureOperation {
    
    override val name = "Quotient Builder"
    
    override val slug = "quotient-builder"
    
    override val category = "Compressions"
    
    override val description =
      "Compresses a transition system to a quotient system based on the current coloring (Sec 4.1.2 of the thesis)."
    
    override def applyOperation(structure: Structure) = {
      val begin = Date.now
      val c = new BuildQuotientSystem(structure.structure, structure.partition).build()
      println("Transformer «" + name + "» took: " + (Date.now - begin) + "ms.")
      structure.setStructure(c)
    }
  }
  
}