用Jena TDB Api查询Agrovoc

时间:2017-07-12 14:16:25

标签: java sparql apache-jena tdb

我在我的java应用程序中查询Agrovoc Agrovoc Rabbits result时遇到了问题。虽然我能够像概念一样检索关于兔子的信息。但是,当我尝试检索属性信息,如概念标签,更广泛,更广泛的标签时,我得到他们的网址而不是他们的描述。 所以我的问题是我如何获得描述或者我会说我能够检索的那些网址的值。此外,我如何查询更广泛的概念,更窄的概念,用作条目术语属性。我如何获得有关搜索查询的所有信息(如所有属性信息和说明中所示)?我已经附上了迄今为止我能够实现的目标

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();;
}
}

这个TDBCreator文件为我创建TDB文件,而Agrovoc.java查询

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;

}
}

附件是我得到from my java code implementation

的结果

我也试过运行Sparql查询我得到一个空结果

 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.");
}

2 个答案:

答案 0 :(得分:0)

该查询适用于最新的Agrovoc数据集(Core,2017年7月3日):

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'))
} 

输出(我使用Stardog三重店)

enter image description here

答案 1 :(得分:0)

@AKSW的Java代码回答

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();
    }
}