Search in sources :

Example 61 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class TestTextHighlighting method doTestSearchWithLiterals.

protected Map<String, Literal> doTestSearchWithLiterals(String queryString, Set<String> expectedEntityURIs) {
    Map<String, Literal> literals = new HashMap<>();
    Query query = QueryFactory.create(queryString);
    dataset.begin(ReadWrite.READ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        assertEquals(expectedEntityURIs.size() > 0, results.hasNext());
        int count;
        for (count = 0; results.hasNext(); count++) {
            QuerySolution soln = results.nextSolution();
            String entityUri = soln.getResource("s").getURI();
            assertTrue(expectedEntityURIs.contains(entityUri));
            Literal literal = soln.getLiteral("lit");
            assertNotNull(literal);
            literals.put(entityUri, literal);
        }
        assertEquals(expectedEntityURIs.size(), count);
    } finally {
        dataset.end();
    }
    return literals;
}
Also used : Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 62 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class TestTextMultilingualEnhancements02 method doTestSearchWithLiterals.

protected Map<String, Literal> doTestSearchWithLiterals(String queryString, Set<String> expectedEntityURIs) {
    Map<String, Literal> literals = new HashMap<>();
    Query query = QueryFactory.create(queryString);
    dataset.begin(ReadWrite.READ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        assertEquals(expectedEntityURIs.size() > 0, results.hasNext());
        int count;
        for (count = 0; results.hasNext(); count++) {
            QuerySolution soln = results.nextSolution();
            String entityUri = soln.getResource("s").getURI();
            assertTrue(expectedEntityURIs.contains(entityUri));
            Literal literal = soln.getLiteral("lit");
            assertNotNull(literal);
            literals.put(entityUri, literal);
        }
        assertEquals(expectedEntityURIs.size(), count);
    } finally {
        dataset.end();
    }
    return literals;
}
Also used : Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 63 with QuerySolution

use of org.apache.jena.query.QuerySolution in project Info-Evaluation by TechnionYP5777.

the class SqlRunner method getPersonalInfo.

public TableEntry getPersonalInfo(int wikiPageID) throws ClassNotFoundException, SQLException, IOException, ParseException {
    Extractor ext = new Extractor(wikiPageID);
    ext.executeQuery(QueryTypes.ABSTRACT_BY_WIKI_PAGE_ID);
    ResultSetRewindable results = ext.getResults();
    results.reset();
    QuerySolution solution = results.nextSolution();
    RDFNode overview = solution.get("abstract");
    String overviewStr = "No Abstract";
    if (overview != null)
        if (overview.isResource())
            overviewStr = (overview.asResource() + "").split("resource/")[1];
        else if (overview.isLiteral())
            overviewStr = (overview.asLiteral() + "").split("@")[0];
    ext.executeQuery(QueryTypes.BASIC_INFO_BY_WIKI_PAGE_ID);
    ResultSetRewindable basicInfoByIdResults = ext.getResults();
    basicInfoByIdResults.reset();
    TableEntry te = SqlTablesFiller.getBasicInfo(basicInfoByIdResults);
    TableEntry result = new TableEntry(te);
    result.setUrl(wikiURL + wikiPageID);
    result.setOverview(overviewStr);
    String photoLink = result.getPhotoLink();
    photoLink.replaceAll("'", "\'");
    int containsQuesmark = photoLink.indexOf("?");
    if (containsQuesmark != -1) {
        photoLink = photoLink.substring(0, containsQuesmark);
    }
    result.setPhotoLink(photoLink);
    return result;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 64 with QuerySolution

use of org.apache.jena.query.QuerySolution in project opentheso by miledrousset.

the class AlignmentQuery method queryBNF.

/**
 * cette fonction permet de récuperer les alignements de la BNF
 * requête de type Sparql
 * @param idC
 * @param idTheso
 * @param lexicalValue
 * @param lang
 * @param requete
 * @param source
 * @return
 */
public ArrayList<NodeAlignment> queryBNF(String idC, String idTheso, String lexicalValue, String lang, String requete, String source) {
    listeAlign = new ArrayList<>();
    // lexicalValue = lexicalValue.replaceAll(" ", "%20");
    requete = requete.replace("##lang##", "\"" + lang + "\"");
    if (lexicalValue.contains(" ")) {
        String[] valueTemp = lexicalValue.split(" ");
        boolean first = true;
        for (String valuetemp : valueTemp) {
            requete = requete.substring(0, requete.length() - 1);
            if (first) {
                requete = requete.replace("##value##", valuetemp.trim());
                first = false;
            } else {
                requete = requete.concat(" && regex(?value, \"" + valuetemp.trim() + "\",\"i\")");
            }
        }
        requete = requete + ")";
    } else {
        requete = requete.replace("##value##", "\"" + lexicalValue + "\"");
    }
    /*requete = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>" +
                " PREFIX xml: <http://www.w3.org/XML/1998/namespace>" +
                " SELECT ?instrument ?prop ?value" +
                " where {  <http://data.bnf.fr/ark:/12148/cb119367821> skos:narrower+ ?instrument.  ?instrument ?prop ?value.  FILTER( (regex(?prop,skos:prefLabel) || regex(?prop,skos:altLabel))  && regex(?value, \"cornemuse\",\"i\"))   filter(lang(?value) =\"fr\")} LIMIT 10";
      */
    // System.out.println(requete);
    Query query = QueryFactory.create(requete);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://data.bnf.fr/sparql", query);
    ResultSet results = qexec.execSelect();
    while (results.hasNext()) {
        QuerySolution qs = results.next();
        NodeAlignment na = new NodeAlignment();
        na.setInternal_id_concept(idC);
        na.setInternal_id_thesaurus(idTheso);
        na.setConcept_target(qs.get("value").toString());
        // qs.get("comment").toString());
        na.setDef_target("");
        na.setThesaurus_target(source);
        na.setUri_target(qs.get("instrument").toString());
        listeAlign.add(na);
    }
    qexec.close();
    return listeAlign;
}
Also used : NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 65 with QuerySolution

use of org.apache.jena.query.QuerySolution in project opentheso by miledrousset.

the class AlignmentQuery method queryDBPedia.

/**
 * Cette fonction permet de récupérer les alignements présents sur DBPedia
 * pour un concept passé en paramètre
 *
 * @param idC
 * @param idTheso
 * @param lexicalValue
 * @param lang
 * @param source
 * @return
 */
public ArrayList<NodeAlignment> queryDBPedia(String idC, String idTheso, String lexicalValue, String lang, String source) {
    listeAlign = new ArrayList<>();
    if (lexicalValue.contains(" ")) {
        lexicalValue = lexicalValue.substring(0, lexicalValue.indexOf(" "));
    }
    lexicalValue = String.valueOf(lexicalValue.charAt(0)).toUpperCase() + lexicalValue.substring(1);
    String sparqlQueryString1 = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" + "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT * WHERE {" + "       ?s rdfs:label ?label ." + "       ?s rdfs:comment ?comment ." + "       ?s dbpedia-owl:thumbnail ?thumbnail ." + "       ?s foaf:isPrimaryTopicOf ?primaryTopicOf ." + "       FILTER(regex(?s,\"resource/" + lexicalValue + "*\"))" + "       FILTER(lang(?label) = \"" + lang + "\")" + "       FILTER(lang(?comment) = \"" + lang + "\")" + "   }";
    // System.out.println(sparqlQueryString1);
    Query query = QueryFactory.create(sparqlQueryString1);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
    ResultSet results = qexec.execSelect();
    while (results.hasNext()) {
        QuerySolution qs = results.next();
        NodeAlignment na = new NodeAlignment();
        na.setInternal_id_concept(idC);
        na.setInternal_id_thesaurus(idTheso);
        na.setConcept_target(qs.get("label").toString());
        na.setDef_target(qs.get("comment").toString());
        na.setThesaurus_target(source);
        na.setUri_target(qs.get("primaryTopicOf").toString());
        listeAlign.add(na);
    }
    qexec.close();
    return listeAlign;
}
Also used : NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)125 ResultSet (org.apache.jena.query.ResultSet)103 QueryExecution (org.apache.jena.query.QueryExecution)96 ArrayList (java.util.ArrayList)70 Test (org.junit.Test)69 Dataset (org.apache.jena.query.Dataset)44 Query (org.apache.jena.query.Query)44 Resource (org.apache.jena.rdf.model.Resource)43 Literal (org.apache.jena.rdf.model.Literal)21 RDFNode (org.apache.jena.rdf.model.RDFNode)20 Model (org.apache.jena.rdf.model.Model)12 HashMap (java.util.HashMap)7 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)6 Extractor (infoeval.main.WikiData.Extractor)4 Node (org.apache.jena.graph.Node)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 IOException (java.io.IOException)3 Date (java.sql.Date)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LinkedList (java.util.LinkedList)3