@alex alex / RdfDiff.scala
Created at Thu Nov 23 18:36:19 GMT 2017
RDF Diff using Jena
RdfDiff.scala
Raw
import java.io.{File, StringWriter}
import java.net.URI
import org.apache.jena.riot.RDFDataMgr

case class Config(uris: Seq[URI] = Seq.empty)

object RdfDiff extends App {
  val packageVersion: String = getClass.getPackage.getImplementationVersion()
  val parser = new scopt.OptionParser[Config]("rdfdiff") {
    head("rdfdiff", packageVersion)
    arg[URI]("<URI>") minOccurs(1) maxOccurs(2) hidden() action { (x, c) =>
      c.copy(uris = c.uris :+ x)
    }
    help("help") text("Parses each file/URL as RDF and shows the differences between the resulting set of statements.")
  }
  parser.parse(args, Config()) match {
    case Some(Config(Seq(uriA, uriB))) =>
      val a = RDFDataMgr.loadModel(uriA.toString)
      val b = RDFDataMgr.loadModel(uriB.toString)
      if (a isIsomorphicWith b) {
        println("Graphs are isomorphic.")
        System.exit(0)
      } else {
        val sw = new StringWriter()
        sw.write("<<<<\n")
        a.difference(b).write(sw, "TURTLE")
        sw.write("\n----\n")
        b.difference(a).write(sw, "TURTLE")
        sw.write("\n>>>>\n")
        println(sw.toString)
        System.exit(1)
      }
    case _ =>
      System.exit(2)
  }
}