Folks, I have been working on a project related to semantic web and I really need help right now as I am completely struck while reading RDF N-triples file of noble prize rdf dataset.
The dataset is taken from the below links.
Nobel prize rdf data description
I want to read name, gender, nationality, discipline and year from this N-triples rdf data file using jena and want to save it in turtle format.
I am able to read name and gender so far but struck while reading other attributes.
My java code is this:
package com.tausy.rdf.query;
import java.io.InputStream;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDFS;
public class ParseNTripleRdf {
public static void main(String[] args) {
String fileNameOrUri = "././Nobeldump.nt";
Model model = ModelFactory.createDefaultModel();
InputStream is = FileManager.get().open(fileNameOrUri);
if (is != null) {
model.read(is, null, "N-TRIPLE");
//Resource person = model.getResource("http://xmlns.com/foaf/0.1/Person");
//Property firstName = model.createProperty("http://xmlns.com/foaf/0.1/name");
//String firstNameValue = person.getProperty(firstName).getString();
//System.out.println(firstNameValue);
modelStatements(model);
//model.write(System.out, "TURTLE");
} else {
System.err.println("cannot read " + fileNameOrUri);
}
}
public static void modelStatements(Model model){
// list the statements in the Model
StmtIterator iter = model.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
// Create some properties in advance for convenience.
final Property name = model.createProperty("http://xmlns.com/foaf/0.1/name");
final Property gender = model.createProperty("http://xmlns.com/foaf/0.1/gender");
final Property nationality = model.createProperty("http://dbpedia.org/ontology/country");
final Property discipline = model.createProperty("http://data.nobelprize.org/terms/category");
final Property year = model.createProperty("http://data.nobelprize.org/terms/year");
//System.out.println("Subject: "+subject.getURI());
//System.out.println("Predicate: " + predicate.getLocalName());
//System.out.println("Object: " + object.toString());
if (object instanceof Resource) {
Resource res = (Resource)object;
Statement s = res.getProperty(name);
final String nameVal = s==null ? null : s.getString();
s = res.getProperty(gender);
final String genderVal = s==null ? null : s.getString();
//s = res.getProperty(nationality);
//final String nationalityVal = s==null ? null : s.getString();
//s = res.getProperty(discipline);
//final String disciplineVal = s==null ? null : s.getString();
System.out.println(nameVal);
System.out.println(genderVal);
//System.out.println(nationalityVal);
//System.out.println(disciplineVal);
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
}
}
}
I want to query this turtle file to get simple information like:
- Find all the Nobel Laureates from the UK;
- Find all the Nobel Laureates who are female and were born after 1949;
- List all the Nobel Laureates ordering them by discipline for which they were awarded the prize. List the names in alphabetical order;
- Find all the Nobel Laureates born in the US who share the award with someone else;
Any kind of help would be highly appreciated. Thanks!