-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Welcome to the Cheló̱na wiki!
Cheló̱na supports the building of your own output format. This self created format can be written to a file, it can be stored in a database or transferred via actors.
The Turtle and TriG format expect a function of type List[SPOReturnValue] ⇒ Int.
One Turtle or TriG input statement can result in one to n triples respectively quadruples being generated. The generated triples respectively quadruples for a single Turtle or TriG statement are assembled in the list of SPOReturnValues. Each Turtle or TriG statement fills the list anew.
The Int return value of the output function should return the number of output statements generated for each Turtle or TriG statement.
A few examples will show how to proceed.
The Cheló̱na main routine contains the following output definition:
def tripleWriter(bo: Writer)(triple: List[SPOReturnValue]): Int = {
triple.asInstanceOf[List[SPOTriple]].map(t ⇒ bo.write(t.s + " " + t.p + " " + t.o + " .\n")).length
}
With a writer stream passed to tripleWriter the required signature for the output function is achieved
val output = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8))
val parser = ChelonaParser(input, tripleWriter(output)_, validate, base, label)
There are a few things to consider when defining a simple HTML-table output format.
Angle brackets '<', '>' have to be substituted by < and > and string literals can be followed by an iri (URI).
A complete example shows the usage of the HTML output definition:
import java.io.{StringWriter, Writer}
import org.chelona.SPOReturnValue.SPOTriple
import org.chelona.{ChelonaParser, SPOReturnValue}
import org.parboiled2.ParseError
import scala.util.{Failure, Success}
object Main extends App {
val input =
"""@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
<#green-goblin> rel:enemyOf <#spiderman> ;
a foaf:Person ; # in the context of the Marvel universe
foaf:name 'Green Goblin' ;
foaf:mail 'GreenGoblin@marvel.com' .
<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ;
foaf:name 'Spiderman', 'Человек-паук'@ru ."""
val output = new StringWriter()
def tripleHTMLWriter(bo: Writer)(triple: List[SPOReturnValue]): Int = {
triple.asInstanceOf[List[SPOTriple]].map(
t ⇒ {
val s = if (t.s.startsWith("<") && t.s.endsWith(">"))
"<" + t.s.substring(1, t.s.length - 1) + ">"
else
t.s
val p = if (t.p.startsWith("<") && t.p.endsWith(">"))
"<" + t.p.substring(1, t.p.length - 1) + ">"
else
t.p
val o = if (t.o.startsWith("<") && t.o.endsWith(">"))
"<" + t.o.substring(1, t.o.length - 1) + ">"
else if (t.o.endsWith(">")) {
val i = t.o.lastIndexOf('<')
t.o.substring(0, i) + "<" + t.o.substring(i + 1, t.o.length - 1) + ">"
}
else
t.o
bo.write("<tr><td><code>" + s + "</code></td><td><code>" + p + "</code></td><td><code>" + o + "</code></td></tr>\n")
}
).length
}
val parser = ChelonaParser(input, tripleHTMLWriter(output)_)
parser.turtleDoc.run() match {
case Success(tripleCount) ⇒
System.err.println("Input converted to " + tripleCount + " triples.")
println(output)
case Failure(e: ParseError) ⇒ System.err.println("Unexpected error during parsing run: " + parser.formatError(e))
case Failure(e) ⇒ System.err.println("Unexpected error during parsing run: " + e)
}
}
A counter is induced into the output function.
object myCount { var counter = 0 }
def tripleHTMLWriter(counterObject: myCount.type)(bo: Writer)(triple: List[SPOReturnValue]): Int = {
triple.asInstanceOf[List[SPOTriple]].map(
t ⇒ {
myCount.counter = myCount.counter + 1
val s = if (t.s.startsWith("<") && t.s.endsWith(">")) "<" + t.s.substring(1, t.s.length - 1) + ">" else t.s
val p = if (t.p.startsWith("<") && t.p.endsWith(">")) "<" + t.p.substring(1, t.p.length - 1) + ">" else t.p
val o = if (t.o.startsWith("<") && t.o.endsWith(">")) "<" + t.o.substring(1, t.o.length - 1) + ">"
else if (t.o.endsWith(">")) { val i = t.o.lastIndexOf('<'); t.o.substring(0, i) + "<" + t.o.substring(i + 1, t.o.length - 1) + ">" }
else t.o
bo.write("<tr><td><code>" + myCount.counter + "</code></td><td><code>" +
s + "</code></td><td><code>" +
p + "</code></td><td><code>" +
o + "</code></td></tr>\n")
}
).length
}
val parser = ChelonaParser(input, tripleHTMLWriter(myCount)(output)_, validate, base, label)