This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added RDF/JSON syntax support for writing OWL data
- Loading branch information
1 parent
4802ee7
commit fa97f40
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
sansa-owl-spark/src/main/scala/net/sansa_stack/owl/spark/writers/RDFJSonWriter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.sansa_stack.owl.spark.writers | ||
|
||
import java.io.{BufferedWriter, ByteArrayOutputStream, OutputStreamWriter, PrintWriter} | ||
import java.util.Collections | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
import org.eclipse.rdf4j.rio.RDFFormat | ||
import org.eclipse.rdf4j.rio.rdfjson.RDFJSONWriter | ||
import org.semanticweb.owlapi.apibinding.OWLManager | ||
import org.semanticweb.owlapi.formats.RDFJsonDocumentFormat | ||
import org.semanticweb.owlapi.model.{OWLAxiom, OWLOntology} | ||
import org.semanticweb.owlapi.rio.RioRenderer | ||
|
||
import net.sansa_stack.owl.spark.rdd.OWLAxiomsRDD | ||
|
||
|
||
protected class SANSARioRenderer(ont: OWLOntology, rdfJsonWriter: RDFJSONWriter, docFormat: RDFJsonDocumentFormat) | ||
extends RioRenderer(ont, rdfJsonWriter, docFormat) { | ||
|
||
override def renderOntologyHeader(): Unit = None | ||
} | ||
|
||
object RDFJSonWriter extends OWLWriterBase { | ||
override def save(filePath: String, owlAxioms: OWLAxiomsRDD): Unit = { | ||
owlAxioms.mapPartitionsWithIndex((idx: Int, partition: Iterator[OWLAxiom]) => if (partition.hasNext) { | ||
val snippets = partition.map(axiom => { | ||
// writer stuff... | ||
val os = new ByteArrayOutputStream() | ||
val osWriter = new OutputStreamWriter(os) | ||
val buffPrintWriter = new PrintWriter(new BufferedWriter(osWriter)) | ||
|
||
val man = OWLManager.createOWLOntologyManager() | ||
val ont = man.createOntology(Seq(axiom).asJava) | ||
|
||
val renderer = new SANSARioRenderer( | ||
ont, | ||
new RDFJSONWriter(osWriter, RDFFormat.RDFJSON), | ||
new RDFJsonDocumentFormat) | ||
|
||
renderer.render() | ||
buffPrintWriter.flush() | ||
|
||
os.toString("UTF-8") | ||
.replaceFirst("\\{", "") // remove per-partition opening curly brace | ||
.reverse.replaceFirst("\\}", "").reverse // remove per-partition closing curly brace | ||
// make blank node IDs unique (by appending the partition ID) | ||
.replaceAll("_:genid([0-9]+)", "_:genid$1" + s"_$idx") | ||
.replaceAll("\\s+$", "") // trim end | ||
}) | ||
Collections.singleton(snippets.mkString(",")).iterator().asScala | ||
|
||
} else { | ||
Iterator() | ||
}).saveAsTextFile(filePath) | ||
} | ||
} |