i have an issue querying Agrovoc Agrovoc Rabbits result in my java application. While am able to retrieve information about rabbits like concept. But when i try retrieving attribute information like concept label, broader, broader label I get their urls instead of their descriptions. So my question is how do I get the descriptions or would i say the values of those urls i am able retrieve. And as well how do I query for broader concept, narrower concepts, is used as, entry terms attributes. And how do I get all information about the search query (as in all attribute information and description)? I have attached what i have been able achieve so far
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.tdb.TDBFactory;
import org.apache.jena.util.FileManager;
public class TDBCreator {
public static final String THESAURUS_FILE = "C:\\Users\\GREEN\\Desktop\\wordnet\\agrovoc_rdf/agrovoc_2017-05-04_core.rdf";
public static final String TDB_DIRECTORY = "C:\\Users\\GREEN\\Desktop\\wordnet\\agrovoc_rdf/tdb";
public Model createTDB() {
Model model = ModelFactory.createDefaultModel();
model = TDBFactory.createDataset(TDB_DIRECTORY).getDefaultModel();
InputStream thesaurusStream = FileManager.get().open(THESAURUS_FILE);
model.read(thesaurusStream, null, "RDF/XML");
return model;
}
public static void main(String[] args) {
new TDBCreator().createTDB().close();;
}
}
This TDBCreator file creates the TDB file for me while the Agrovoc.java queries the
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.query.Syntax;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.sparql.algebra.Algebra;
import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.algebra.op.OpBGP;
import org.apache.jena.sparql.core.BasicPattern;
import org.apache.jena.sparql.core.TriplePath;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.engine.QueryIterator;
import org.apache.jena.sparql.engine.ResultSetStream;
import org.apache.jena.sparql.path.PathFactory;
import org.apache.jena.sparql.syntax.ElementPathBlock;
import org.apache.jena.tdb.TDBFactory;
import org.apache.log4j.Logger;
public class Agrovoc {
private static Agrovoc instance;
private final Logger LOGGER = Logger.getLogger(getClass());
private Model model;
private Agrovoc() {
loadModel();
}
public static Agrovoc getInstance() {
if (instance == null) {
instance = new Agrovoc();
}
return instance;
}
private void loadModel() {
File directory = new File(TDBCreator.TDB_DIRECTORY);
if (!directory.exists()) {
directory.mkdirs();
}
model = TDBFactory.createDataset(TDBCreator.TDB_DIRECTORY).getDefaultModel();
if (model.isEmpty()) {
model = new TDBCreator().createTDB();
}
}
public void queryTerm(String label) {
ElementPathBlock pattern = new ElementPathBlock();
Var varConceptLabel = Var.alloc("conceptLabel");
Var varConcept = Var.alloc("concept");
Var varBroader = Var.alloc("broader");
Var varBroaderLabel = Var.alloc("broaderLabel");
Var varLiteralForm = Var.alloc("literalForm");
final String SKOS_URI = "http://www.w3.org/2008/05/skos-xl#";
final String SKOS_CORE_URI = "http://www.w3.org/2004/02/skos/core#";
pattern.addTriple(new Triple(varConceptLabel, NodeFactory.createURI(SKOS_URI + "literalForm"), NodeFactory.createLiteral(label, "en")));
pattern.addTriple(new Triple(varConcept, NodeFactory.createURI(SKOS_URI + "prefLabel"), varConceptLabel));
pattern.addTriplePath(new TriplePath(varConcept, PathFactory.pathZeroOrMoreN(PathFactory.pathLink(NodeFactory.createURI(SKOS_CORE_URI + "broader"))), varBroader));
pattern.addTriple(new Triple(varBroader, NodeFactory.createURI(SKOS_URI + "prefLabel"), varBroaderLabel));
pattern.addTriple(new Triple(varBroaderLabel, NodeFactory.createURI(SKOS_URI + "literalForm"), varLiteralForm));
Op op = Algebra.compile(pattern);
QueryIterator queryIterator = Algebra.exec(op, model);
List<String> resultVars = new ArrayList<String>();
resultVars.add("conceptLabel");
resultVars.add("concept");
resultVars.add("broader");
resultVars.add("broaderLabel");
resultVars.add("literalForm");
ResultSet rs = new ResultSetStream(resultVars, model, queryIterator);
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
Literal literal = qs.getLiteral("literalForm");
RDFNode type = qs.get("conceptLabel");
RDFNode type2 = qs.get("concept");
RDFNode type3 = qs.get("broader");
RDFNode type4 = qs.get("broaderLabel");
System.out.println(type.toString());
System.out.println(type2.toString());
System.out.println(type3.toString());
System.out.println(type4.toString());
System.out.println(literal.getString());
}
//return false;
}
}
I also tried running Sparql query i got an empty result
public void queryTerm(String term) {
//String tdb = "/usr/local/src/javadev/onld/tdb";
Dataset dataset = TDBFactory.createDataset(TDBCreator.TDB_DIRECTORY);
StringBuilder sb = new StringBuilder();
sb.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ");
sb.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ");
sb.append("PREFIX skos: <http://www.w3.org/2004/02/skos/core#> ");
sb.append("PREFIX foaf: <http://xmlns.com/foaf/0.1/> ");
sb.append("PREFIX text: <http://jena.apache.org/text#> ");
//sb.append("SELECT * ");
//sb.append("WHERE { ?s ?p ?o } LIMIT 10 ");
//sb.append("SELECT ?label ");
//sb.append("{ ?s text:query (skos:altLabel '" + term + "' 3) ; ");
//sb.append("skos:prefLabel ?label ");
//sb.append("}");
//sb.append("SELECT ?definition ");
//sb.append("WHERE{");
//sb.append(term).append(" skos: definition ?definition.");
//sb.append("FILTER (lang(?definition) = 'en'");
//sb.append("}");
sb.append("SELECT DISTINCT ?concept ?searchLabel ");
sb.append("WHERE {");
sb.append("{ ?concept skos:prefLabel ?searchLabel. } UNION ");
sb.append("{ ?concept skos:altLabel ?searchLabel. } ");
sb.append("FILTER (regex(str(?searchLabel),
\"").append(term).append("\",").append("\"i\")) ");
sb.append("FILTER (lang(?searchLabel) = \"en\")");
sb.append("} LIMIT 10");
// sb.append("SELECT ?uri ?em { ");
// sb.append("?uri skos:prefLabel \"Japan\"@en . ");
// sb.append("OPTIONAL { <http://aims.fao.org/aos/agrovoc/c_4039>
//skos:exactMatch ?em } .");
// sb.append("}");
String queryo = "SELECT ?conceptURI ?label "
+ "\nWHERE { "
+ "\n ?conceptURI ?pred1 _:b1 . "
+ "\nFILTER( ?conceptURI = <" +
"http://aims.fao.org/aos/agrovoc/" + term + "> )"
+ "\n FILTER(?pred1 = <" + SKOSXLALTLABEL + "> || "
+ "?pred1 = <" + SKOSXLHIDDENLABEL + "> || "
+ "?pred1 = <" + SKOSXLPREFLABEL + "> ) ."
+ "\n_:b1 <" + SKOSXLLITERALFORM + "> ?label ."
+ "}";
String queryString = queryo;//sb.toString();
System.out.println (queryString);
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query,
dataset.getDefaultModel());
try {
int resultSetSize = 0;
ResultSet results = qexec.execSelect();
// Output query results
ResultSetFormatter.out(System.out, results, query);
System.out.println(results.getRowNumber());
while (results.hasNext()) {
resultSetSize++;
QuerySolution solution = results.nextSolution();
Iterator varnames = solution.varNames();
System.out.println(varnames.next());
HashMap<String, String> hm = new HashMap<String, String>();
while (varnames.hasNext()) {
String name = (String) varnames.next();
RDFNode rdfnode = solution.get(name);
System.out.println("rdf node name, type: " + name);
if (rdfnode.isLiteral()) {
Literal literal = rdfnode.asLiteral();
String nodeval = literal.getString();
hm.put(name, nodeval);
System.out.println(nodeval);
} else if (rdfnode.isResource()) {
Resource resource = rdfnode.asResource();
String nodeval = resource.toString();
hm.put(name, nodeval);
System.out.println(nodeval);
}
}
}
}
finally {
qexec.close();
}
System.out.println ("Done.");
}
That query works on the latest Agrovoc dataset (Core, 03 Jul 2017):
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?concept ?conceptLabel ?broaderConcept ?broaderConceptLabel WHERE {
# set the search term
VALUES ?conceptLabel {"rabbits"@en}
# get the concept matching the term
?concept skos-xl:prefLabel ?conceptLabelNode .
?conceptLabelNode skos-xl:literalForm ?conceptLabel .
# get the broader concepts
?concept skos:broader ?broaderConcept.
# and their labels
?broaderConcept skos-xl:prefLabel ?broaderConceptLabelNode .
?broaderConceptLabelNode skos-xl:literalForm ?broaderConceptLabel .
# in English language only
FILTER(LANGMATCHES(LANG(?broaderConceptLabel), 'en'))
}
Output (I used Stardog triple store)