danysk

Kotlin graphstream experiment

Sep 11th, 2020
1,129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ExperimentalUnsignedTypes
  2. fun main() {
  3. //    val random = MersenneTwister(2)
  4.     System.setProperty("org.graphstream.ui", "swing")
  5.     val nodes = 400
  6.     val maxSize = 20
  7.     val minSize = 7
  8.     val graphs = (0..0).map {
  9.         SingleGraph("asda").also { graph ->
  10.             val layout = SpringBox(false, Random(1))
  11.             with(LobsterGenerator(2, 10)) {
  12.                 addNodeLabels(false)
  13.                 setRandomSeed(0)
  14.                 addSink(graph)
  15.                 addSink(layout)
  16.                 begin()
  17.                 (0..nodes).forEach { _ ->
  18.                     nextEvents()
  19.                 }
  20.                 end()
  21.             }
  22.             val degrees = graph.map { it.edges().count() }
  23.             val maxDeg = degrees.maxOrNull() ?: 0
  24.             val minDeg = degrees.minOrNull() ?: 0
  25.             fun Long.scaled() = minSize + (this - minDeg) * (maxSize - minSize) / (maxDeg - minDeg)
  26.             graph.forEach { it.setAttribute("ui.style", "size: ${it.edges().count().scaled()}px;") }
  27. //            graph.display(false)
  28.             var count = 0u
  29.             while (layout.stabilization < 1) {
  30.                 layout.compute()
  31.                 count++
  32.             }
  33.             println(count)
  34.             graph.forEach {
  35.                 val coordinates = it.getAttribute("xyz")
  36.                 if (coordinates is Array<*>) {
  37.                     val actualCoordinates = coordinates.filterIsInstance<Double>()
  38.                     println("${it.index} -> $actualCoordinates")
  39.                     it.setAttribute("x", actualCoordinates[0])
  40.                     it.setAttribute("y", actualCoordinates[1])
  41.                 } else {
  42.                     throw IllegalStateException("Unexpected type ${coordinates::class}, an array was expected")
  43.                 }
  44.             }
  45.             graph.display(false)
  46.         }
  47.     }
  48.     println(graphs)
RAW Paste Data