maintained by Willem Robert van Hage
This page contains notes on how to apply third-party software packages. It is a collection of practical lessons learned that can be of use for other people involved in AID or VL-e.
example code,
vital example code,
comments.
This is a piece of example code showing how you can access a Sesame server,
in this case prauw.cs.vu.nl on port 80,
from Java.
package wordnetlookup;
import org.openrdf.sesame.Sesame;
import org.openrdf.sesame.repository.SesameService;
import org.openrdf.sesame.repository.SesameRepository;
import org.openrdf.sesame.query.QueryResultsTable;
import org.openrdf.sesame.constants.QueryLanguage;
import org.openrdf.model.Value;
import java.net.URL;
public class Main {
public Main() { }
public static void main(String[] args) {
try {
/* Specify the location of the server
*/
java.net.URL sesameServerURL = new java.net.URL("http://prauw.cs.vu.nl:8080/sesame/");
SesameService service = Sesame.getService(sesameServerURL);
/* Login is not necessary for the prauw sesame server.
*/
service.login("sesame", "sesame");
/* Open a repository on the server
*/
SesameRepository myRepository = service.getRepository("wordnet-live");
/* Get all hypernyms of the english word "break" and print their URI and label.
* @en refers to the language of the label, english.
*/
String query = "SELECT Y, M " +
"FROM {X} wn:hyponymOf {Y}, {X} rdfs:label {L}, {Y} rdfs:label {M} " +
"WHERE Y = \"break\"@en " +
"USING NAMESPACE wn = <http://wordnet.princeton.edu/wn#>";
QueryResultsTable resultsTable = myRepository.performTableQuery(QueryLanguage.SERQL, query);
/* Get the number of rows and columns in the result table and iterate over them
*/
int rowCount = resultsTable.getRowCount();
int columnCount = resultsTable.getColumnCount();
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++) {
Value v = resultsTable.getValue(row, column);
System.out.print(v);
if (row == rowCount - 1) {
System.out.print("\n");
} else {
System.out.print("\t");
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
};
}
}
Data can be added to and removed from a Sesame repository by providing a piece of serialized RDF, for example a piece of XML RDF code, that encodes the triples. Another, often more convenient, way to add or remove data is by constructing a graph in Java. The following piece of code creates a graph of RDF triples and adds it to a repository. The same can be done to remove triples from a repository.
import org.openrdf.rio.ntriples.NTriplesWriter;
import org.openrdf.model.*;
import org.openrdf.model.impl.*;
String rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
String rdfs = "http://www.w3.org/2002/07/owl#";
String wn = "http://wordnet.princeton.edu/wn#";
java.net.URL sesameServerURL = new java.net.URL("http://prauw.cs.vu.nl:8080/sesame/");
SesameService service = Sesame.getService(sesameServerURL);
SesameRepository rep = service.getRepository("nalt");
Graph g = new GraphImpl();
ValueFactory vf = g.getValueFactory();
/* Create a triple that makes rdfs:subClassOf a rdf:subPropertyOf skos:broader.
*/
URI s = vf.createURI(rdfs,"subClassOf");
URI p = vf.createURI(rdf,"subPropertyOf");
URI o = vf.createURI(skos,"broader");
/* Add the triple to a graph.
* You can add as many triples as you like. (and as your memory allows)
*/
g.add(s,p,o);
/* Add the triple to the repository.
*/
rep.addGraph(g);
/* Remove it again.
*/
rep.removeGraph(g);
Prolog can be used as a powerful rule-language for RDF, RDFS,
and OWL models with the SWI-Prolog semweb package.
This package contains, amongst other things, RDFS and OWL
reasoning facilities and functions to load RDF(S) files into Prolog.
It turns RDF triples into Prolog assertions.
For example, the Turtle notation RDF triple
from the file wn:100002056-thing-n wn:hypernymOf wn:100002342-anything-n' .
'/home/wrvhage/Documents/Thesauri/Wordnet2.0/rdf/wordnet-hypernym.rdf'
using the namespace wn = http://wordnet.princeton.edu/wn# is turned
into the Prolog assertion
This makes it possible to directly manipulate RDF triples in Prolog.
rdf('http://wordnet.princeton.edu/wn#100002056-thing-n',
'http://wordnet.princeton.edu/wn#hypernymOf',
'http://wordnet.princeton.edu/wn#100002342-anything-n',
'/home/wrvhage/Thesauri/Wordnet2.0/rdf/wordnet-hypernym.rdf').
The semweb package consists of a number of separate libraries.
The library that is used to load RDF files can be loaded with the following Prolog statement:
Afterwards you can load a file like this:
use_module(library('semweb/rdf_db')).
rdf_load(path_to_file/file.rdf).