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 📥

GraphRenderer.scala 8.47 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
package de.bbisping.coupledsim.tool.view

import scala.annotation.migration
import scala.scalajs.js.Any.fromFunction1
import scala.scalajs.js.Any.fromFunction2
import scala.scalajs.js.Any.jsArrayOps
import scala.scalajs.js.Any.wrapArray
import scala.scalajs.js.Tuple2.fromScalaTuple2
import scala.scalajs.js.UndefOr.undefOr2ops
import scala.scalajs.js.|.from
import org.scalajs.dom
import org.scalajs.dom.raw.EventTarget
import org.scalajs.dom.raw.HTMLInputElement
import org.singlespaced.d3js.Ops.asPrimitive
import org.singlespaced.d3js.Ops.fromFunction1To2
import org.singlespaced.d3js.Ops.fromFunction1To3
import org.singlespaced.d3js.Ops.fromFunction2To3
import org.singlespaced.d3js.Ops.fromFunction2To3DoublePrimitive
import org.singlespaced.d3js.Ops.fromFunction2To3StringPrimitive
import org.singlespaced.d3js.Selection
import org.singlespaced.d3js.d3
import de.bbisping.coupledsim.tool.arch.Control
import de.bbisping.coupledsim.tool.control.ModelComponent
import de.bbisping.coupledsim.tool.control.Structure
import de.bbisping.coupledsim.util.Partition
import de.bbisping.coupledsim.tool.model.NodeID
import de.bbisping.coupledsim.util.Coloring
import de.bbisping.coupledsim.util.Relation
import de.bbisping.coupledsim.util.LabeledRelation

class GraphRenderer(val main: Control)
  extends GraphView
  with GraphEditing
  with ViewComponent {
  
  import GraphView._
  
  registerEditingBehavior("es-graph-move", new GraphMoveNode(this))
  registerEditingBehavior("es-graph-rename", new GraphEditNode(this))
  registerEditingBehavior("es-graph-delete", new GraphDeleteNodeOrLink(this))
  registerEditingBehavior("es-graph-connect-stepto", new GraphConnectStepTo(this))
  
  behaviors.foreach{ case (n: String, b: GraphEditBehavior) =>
    d3.select("#"+n).on("click", {_: EventTarget => setEditingBehavior(n)})
  }
  
  setEditingBehavior("es-graph-move")
  
  val force = d3.layout.force[GraphNode, NodeLink] ()
      .charge(-300.0)
      .chargeDistance(200.0)
      .linkStrength(0.3)
      .size((400.0, 400.0))
      .gravity(.2)
      .nodes(nodes)
      .links(links)
      
  val graphBendingButton = d3.selectAll("#es-graph-arrowbending")
  graphBendingButton
    .on("change", (a: EventTarget, b: Int) => {
      GraphView.graphBending = graphBendingButton.node.asInstanceOf[HTMLInputElement].checked
      force.resume()
      println("set bending: " + GraphView.graphBending)
    }
  )
  
  val layerLinks = sceneRoot.append("g").classed("layer-links", true)
  val layerNodes = sceneRoot.append("g").classed("layer-nodes", true)
  val layerMeta = sceneRoot.append("g").classed("layer-meta", true)
      
  var linkViews: Selection[NodeLink] = layerLinks.selectAll(".link")
  var linkLabelViews: Selection[NodeLink] = layerMeta.selectAll(".link-label")
  
  var nodeViews: Selection[GraphNode] = layerNodes.selectAll(".node")
  var nodeLabelViews: Selection[GraphNode] = layerMeta.selectAll(".node-label")
  
  var relationViews: Selection[NodeLink] = layerMeta.selectAll(".relation")
  
  def buildGraph(ts: Structure.TSStructure) = {
    
    val nodes = ts.nodes.map { e =>
      (e, new GraphNode(e, ts.nodeLabeling.getOrElse(e, Structure.emptyLabel)))
    }.toMap
    
    val nodeLinks = for {
      ((e1, e2), ees) <- ts.step.tupleSet.groupBy(t => (t._1, t._3))
      en1 = nodes(e1)
      en2 = nodes(e2)
    } yield new NodeLink('stepto, ees.map(_._2.toActString).mkString(", "), en1, en2)
    
    (nodes, nodeLinks)
  }
  
  def setStructure(ts: Structure.TSStructure) {
    
    force.stop()
    
    val (sysNodes, nodeLinks) = buildGraph(ts)
    
    val newNodes = sysNodes.values.filter(n => !nodes.exists(n.sameNode(_)))
    val deletedNodes = nodes.filter(n => !sysNodes.isDefinedAt(n.nameId))
    deletedNodes.foreach(n => nodes.remove(nodes.indexOf(n)))
    newNodes.foreach(nodes.push(_))
    nodes.foreach { n => n.updateMeta(ts.nodeLabeling.getOrElse(n.nameId, Structure.emptyLabel), force = true)}
    
    val newLinks = nodeLinks.flatMap(_.integrate(nodes)).filter(l => !links.exists(l.sameLink(_)))
    val deletedLinks = links.filter(l => !nodeLinks.exists(l.sameLink(_)))
    deletedLinks.foreach(l => links.remove(links.indexOf(l)))
    newLinks.foreach(links.push(_))
    
    val linkUp = linkViews.data(links, (_: NodeLink).toString)
    linkUp.enter()
        .append("path")
        .attr("class", (d: NodeLink, i: Int) => "link " + d.kind.name)
        .attr("marker-end", (d: NodeLink, i: Int) => "url(#" + d.kind.name.split(" ")(0) + ")")
        .call(dragLink)
        .on("mousemove", onHover _)
        .on("mouseout", onHoverEnd _)
    linkUp.exit().remove()
    linkViews = layerLinks.selectAll(".link")
    
    val linkLabelUp = linkLabelViews.data(links, (_: NodeLink).toString)
    linkLabelUp.enter()
        .append("text")
        .attr("class", (d: NodeLink, i: Int) => "link-label " + d.kind.name)
        .text((_: NodeLink).label)
    linkLabelUp.exit().remove()
    linkLabelViews = layerMeta.selectAll(".link-label")
    
    val nodeUp = nodeViews.data(nodes, (_:GraphNode).nameId.name)
    nodeUp.enter()
        .append("circle")
        .attr("cx", ((d: GraphNode, i: Int) => d.x))
        .attr("cy", ((d: GraphNode, i: Int) => d.y))
        .attr("r", 7)
        .on("mousemove", onHover _)
        .on("mouseout", onHoverEnd _)
        .on("click", onClick _)
        .call(drag)
    nodeUp.attr("class", ((d: GraphNode, i: Int) => "node " + d.meta.act.map(_.name).mkString(" ")))
    nodeUp.exit().remove()
    nodeViews = layerNodes.selectAll(".node")
        
    val nodeLabelUp = nodeLabelViews
        .data(nodes, (_:GraphNode).nameId.name)
    nodeLabelUp.enter()
        .append("text")
        .attr("class", "node-label")
        .text((_: GraphNode).nameId.name)
    nodeLabelUp.exit().remove()
    nodeLabelViews = layerMeta.selectAll(".node-label")
    
    force.linkDistance((l: NodeLink, d: Double) => l.kind match {
      case _ => 150.0
    })
    
    force.on("tick", updateViews _)
    
    force.start()
    
  }
  
  def setRelation(rel: LabeledRelation[NodeID, String]) {
    force.stop()
    val relationLinks = for {
      (e1, l, e2) <- rel.tupleSet
      en1 <- nodes.find(_.nameId == e1)
      en2 <- nodes.find(_.nameId == e2)
    } yield new NodeLink(Symbol("relation " + l), "", en1, en2)
    
    val newRealtionLinks = relationLinks.filter(l => !relation.exists(l.sameLink(_)))
    val deletedRelationLinks = relation.filter(l => !relationLinks.exists(l.sameLink(_)))
    deletedRelationLinks.foreach(l => relation.remove(relation.indexOf(l)))
    newRealtionLinks.foreach(relation.push(_))
    
    val relationUp = relationViews.data(relation, (_: NodeLink).toString)
    relationUp.enter()
        .append("path")
        .attr("class", (d: NodeLink, i: Int) => "relation " + d.kind.name)
        .attr("marker-end", (d: NodeLink, i: Int) => "url(#" + d.kind.name.split(" ")(0) + ")")

    relationUp.exit().remove()
    relationViews = layerMeta.selectAll(".relation")
    
    force.start()
  }
  
  def setComment(comment: String) = {
    d3.select("#es-graph-comment")
      .text(comment)
      .classed("hidden", comment.isEmpty())
  }
  
  def colorize(partition: Coloring[NodeID]) {
    val colorScale = d3.scale.category20()
    nodeViews.style("stroke", { (d: GraphNode, i: Int) =>
      val repHash = partition(d.nameId)
      colorScale(repHash.toString)
    })
  }
  
  def updateViews(event: dom.Event) {
    nodes.foreach(_.updatePos())
    links foreach { d: NodeLink =>
      d.updateDirAndCenter()
    }
    relation foreach { d: NodeLink =>
      d.updateDirAndCenter()
    }
    nodeViews
      .attr("cx", ((d: GraphNode, i: Int) => d.x))
      .attr("cy", ((d: GraphNode, i: Int) => d.y))
    nodeLabelViews
      .attr("x", ((d: GraphNode, i: Int) => d.x.get + 4))
      .attr("y", ((d: GraphNode, i: Int) => d.y.get - 10))
    linkViews
      .attr("d", (_: NodeLink).toSVGPathString)
    linkLabelViews
      .attr("x", (_: NodeLink).center._1)
      .attr("y", (_: NodeLink).center._2)
    relationViews
      .attr("d", (_: NodeLink).toSVGPathString)
  }
  
  override def onSelectionChange() {
    nodeViews.classed("selected", { n: GraphNode =>
      n.selected
    })
  }
  
  def notify(change: ModelComponent.Change) = change match {
    case Structure.StructureChange(structure) =>
      setComment("")
      setStructure(structure)
    case Structure.StructurePartitionChange(partition) =>
      colorize(partition)
    case Structure.StructureRelationChange(relation) =>
      setRelation(relation)
    case Structure.StructureCommentChange(comment) =>
      setComment(comment)
    case _ =>
      
  }
  
}