GraphML support

graphs supports exporting and loading of graphs to GraphML XML. This format is used by the yed editor, so graphs can be edited and layouted there.

You need to add the graphs-graphml dependency to use it:

libraryDependencies += "com.flowtick" %% "graphs-graphml" % "0.8.0"

Conversion to GraphML

This creates a default graph and converts it to a GraphML graph.

sourceimport com.flowtick.graphs.Graph
import com.flowtick.graphs.defaults._
import com.flowtick.graphs.defaults.label._
import com.flowtick.graphs.graphml._
import com.flowtick.graphs.graphml.generic._

val simple: Graph[Unit, String] =
  Graph.fromEdges(Set("A" --> "B", "B" --> "C", "D" --> "A"))

val graphML = simple.asGraphML().xml
val loaded = FromGraphML[Int, String](graphML.toString)

Custom Node Types

Its possible to create GraphML graphs directly using the ml edge builder and serialize your own node types, this is implemeted using shapeless:

sourceimport com.flowtick.graphs.graphml._
import com.flowtick.graphs.graphml.generic._
import scala.xml.NodeSeq

final case class MyNode(value: Int)

val customGraph: GraphMLGraph[Unit, MyNode] =
  GraphML.fromEdges(
    Set(
      ml(MyNode(1), id = Some("one")) --> ml(MyNode(2), id = Some("two"))
    )
  )

val xml: NodeSeq = ToGraphML[Unit, MyNode](customGraph)
println(xml)
val loaded = FromGraphML[Unit, MyNode](xml.toString)
println(loaded)
The source code for this page can be found here.