0

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;

}
}

attached is the result i got from my java code implementation

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.");
}
Green Onyeji
  • 259
  • 5
  • 18
  • It would be easier if you show the executed SPARQL query + the sample RDF data (in Turtle or N-Triples syntax) + the expected result. – UninformedUser Jul 12 '17 at 14:38
  • It's also not clear why you don't write the SPARQL query by using a simple Java string. – UninformedUser Jul 12 '17 at 14:39
  • @AKSW i have updated my question and added the sparql query i tried. My expected result is to get similar result from the agrovoc link i posted in my question – Green Onyeji Jul 12 '17 at 14:57
  • 1. "I also tried running a SPARQL query" is confusing. The code in the TDB part also generates a SPARQL query by using the Java structures. 2. This is still not the SPARQL query that you executed to get the result in the image, right? 3. The query that is executed returns nothing because you're using a blank node `_:b1` instead of a variable. And from what I can see the predicates in your FILTER usually link to literals instead of blank nodes. – UninformedUser Jul 12 '17 at 16:37
  • @AKSW 1: What I meant was I tried running the sparql query to retrieve the information i wanted to get as asked in my question 2. The result in the image is from the first method of queryitem, the second which i later attached returns nothing 3. Please you guide me with a sample code for better understanding – Green Onyeji Jul 12 '17 at 16:58

2 Answers2

0

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)

enter image description here

UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • thank you for the guide. I will try and implement in java and see if it works. Whatever that comes up I will inform you – Green Onyeji Jul 12 '17 at 18:30
  • thank you. It worked and I will post the codes for others. What of if I want to get the definition of rabbit, its narrower concept and is used as attributes/column? – Green Onyeji Jul 12 '17 at 20:04
  • What do you mean by the "definition of rabbit"? Which property should that be? In your link there is nothing that defines the concept "Rabbits". – UninformedUser Jul 12 '17 at 23:19
  • Yes, but i was thinking of adding skos:definition, skos:narrower and skos:related but i am having problem on how to use it. Though i did made an attempt for skos:definition which I was able to get urls but got stucked up trying to get the literal forms of those urls. I couldn't get any thing from the skos:narrower & skos:related. So i felt that I didn't write the query well, so am asking if you can help – Green Onyeji Jul 13 '17 at 02:18
  • But then the principle is the same as for the other labels I returned? Didn't you understand my SPARQL query? I mean I get the node (resp. URI) of the label data and then it's lexicalform. Isn't this clear? – UninformedUser Jul 13 '17 at 03:04
  • I thought I did. I added ?defin after ?broaderConceptLabel then I added .append("?defin skos:definition ?definition.") before the filter. but i got only urls instead of literal form. Though i tried converting the way i saw you used in the query but it returns empty – Green Onyeji Jul 13 '17 at 03:22
  • The literalform is the real label - it should be clear from my query. – UninformedUser Jul 13 '17 at 07:48
  • please help me check what is its missing in this sparql https://drive.google.com/open?id=0B4Q8wpyf9f_mbzJocWJ2OTNRWTA – Green Onyeji Jul 13 '17 at 13:53
  • The query looks ok, but without seeing the data I can't tell you what's wrong. Can you share the additionally added triples? I mean, as far as I understood, you added them manually, right? – UninformedUser Jul 13 '17 at 13:59
  • I used Agrovoc core rdf 03/07/2017. Check my java code here https://drive.google.com/open?id=0B4Q8wpyf9f_mVDNZdlpDX0Z2RUE I don't know what seems to be the problem. It returns empty – Green Onyeji Jul 13 '17 at 14:18
  • I cannot follow you. I'm using the same dataset. I told you that there is no such information in the dataset for "rabbits". So what should be returned then if there is nothing that matches? – UninformedUser Jul 13 '17 at 17:22
0

Java Codes for @AKSW answer

public void searchTerm(String term) {
    StringBuilder sb = new StringBuilder();

    final String SKOS_URI = "PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>";
    final String SKOS_CORE_URI = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>";

sb.append(SKOS_URI)
        .append(SKOS_CORE_URI)
        .append("SELECT ?concept ?conceptLabel ?broaderConcept ?broaderConceptLabel WHERE {")
        .append("VALUES ?conceptLabel {\""+term+"\"@en}")
        .append("?concept skos-xl:prefLabel ?conceptLabelNode .")
        .append("?conceptLabelNode skos-xl:literalForm ?conceptLabel .")
        .append("?concept skos:broader ?broaderConcept.")
        .append("?broaderConcept skos-xl:prefLabel ?broaderConceptLabelNode .")
        .append("?broaderConceptLabelNode skos-xl:literalForm ?broaderConceptLabel .")
        .append("FILTER(LANGMATCHES(LANG(?broaderConceptLabel), 'en'))")
        .append("}");

String query = sb.toString();
//System.out.println(query);
    QueryExecution qexe = QueryExecutionFactory.create(query, model);
    try {
        ResultSet result = qexe.execSelect();
        ResultSetFormatter.out(result);
    } finally {
        qexe.close();
    }
}
Green Onyeji
  • 259
  • 5
  • 18