Search in sources :

Example 76 with QuerySolution

use of org.apache.jena.query.QuerySolution in project legato by DOREMUS-ANR.

the class PropertyHandler method getScore.

public static double getScore(Resource property, Model model) {
    /**
     ************
     * Get distinct resources having the property "property"
     *************
     */
    List<Resource> resources = new ArrayList<Resource>();
    String sparqlQueryString = "SELECT DISTINCT ?resource WHERE {" + "?resource <" + property + "> ?object" + "}";
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    ResultSet queryResults = qexec.execSelect();
    while (queryResults.hasNext()) {
        QuerySolution qs = queryResults.nextSolution();
        resources.add(qs.getResource("?resource"));
    }
    qexec.close();
    /**
     ************
     * Get all DISTINCT objects of the property "property"
     *************
     */
    List<RDFNode> disObj = new ArrayList<RDFNode>();
    String sparqlQueryString2 = "SELECT DISTINCT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
    Query query2 = QueryFactory.create(sparqlQueryString2);
    QueryExecution qexec2 = QueryExecutionFactory.create(query2, model);
    ResultSet queryResults2 = qexec2.execSelect();
    while (queryResults2.hasNext()) {
        QuerySolution qs = queryResults2.nextSolution();
        disObj.add(qs.get("?object"));
    }
    qexec2.close();
    /**
     ************
     * Get all objects of the property "property"
     *************
     */
    List<RDFNode> obj = new ArrayList<RDFNode>();
    String sparqlQueryString3 = "SELECT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
    Query query3 = QueryFactory.create(sparqlQueryString3);
    QueryExecution qexec3 = QueryExecutionFactory.create(query3, model);
    ResultSet queryResults3 = qexec3.execSelect();
    while (queryResults3.hasNext()) {
        QuerySolution qs = queryResults3.nextSolution();
        obj.add(qs.get("?object"));
    }
    qexec3.close();
    double redondance = (obj.size() - disObj.size());
    if (redondance == 0)
        redondance = 1;
    return (resources.size() / redondance);
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 77 with QuerySolution

use of org.apache.jena.query.QuerySolution in project legato by DOREMUS-ANR.

the class ModelManager method getObjResources.

/**
 ********
 ** Get all resources having the value "object" in their path.
 *********
 */
public static List<Resource> getObjResources(Model model, RDFNode object) {
    LEGATO legato = LEGATO.getInstance();
    List<Resource> resources = new ArrayList<Resource>();
    for (String className : legato.getClassResources()) {
        String sparqlQueryString = "prefix : <urn:ex:>" + "SELECT DISTINCT ?resource WHERE {" + "{?resource a <" + className + "> ;" + "(:|!:)* \"" + object.toString() + "\"}" + "}";
        Query query = QueryFactory.create(sparqlQueryString);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        ResultSet queryResults = qexec.execSelect();
        while (queryResults.hasNext()) {
            QuerySolution qs = queryResults.nextSolution();
            resources.add(qs.getResource("?resource"));
        }
        qexec.close();
    }
    return resources;
}
Also used : LEGATO(legato.LEGATO) Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 78 with QuerySolution

use of org.apache.jena.query.QuerySolution in project autoindex by dice-group.

the class SparqlHandler method getResults.

public ArrayList<Entity> getResults(int instances_limit) {
    ResultSet results = this.getallinstances(instances_limit);
    ArrayList<Entity> entity_list = new ArrayList<Entity>();
    while (results.hasNext()) {
        QuerySolution qs = results.next();
        entity_list.add(new Entity(qs.getResource("type").getURI(), qs.getLiteral("label").getString(), Double.parseDouble(qs.getLiteral("v").getString())));
    }
    return entity_list;
}
Also used : Entity(org.aksw.simba.autoindex.es.model.Entity) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) ArrayList(java.util.ArrayList)

Example 79 with QuerySolution

use of org.apache.jena.query.QuerySolution in project autoindex by dice-group.

the class AutoIndexTest method testallproperties.

public void testallproperties() {
    seh.setLang("en");
    seh.setBaseUri(endpoint);
    ResultSet sum = seh.getallproperties();
    assertNotNull(sum);
    List<QuerySolution> asText = ResultSetFormatter.toList(sum);
    System.out.println("Properties Results" + Joiner.on("\n").join(asText));
    System.out.println("Total Visible Results = " + asText.size());
    Assert.assertTrue(asText.size() > 1);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet)

Example 80 with QuerySolution

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

the class CyRelationLoadingHandler method accept.

@Override
public void accept(Set<QuerySolution> relRecords) {
    this.renameThread("cyRelLoad:");
    log.trace("Begin of {} relations", relRecords.size());
    Map<String, List<Map<String, Object>>> cyData = new HashMap<>();
    RdfDataManager rdfMgr = this.getRdfDataManager();
    Neo4jDataManager neoMgr = this.getNeo4jDataManager();
    // 
    for (QuerySolution row : relRecords) {
        CyRelation cyRelation = rdfMgr.getCyRelation(row);
        rdfMgr.setCyRelationProps(cyRelation, this.relationPropsSparql);
        String type = cyRelation.getType();
        List<Map<String, Object>> cyRels = cyData.get(type);
        if (cyRels == null)
            cyData.put(type, cyRels = new LinkedList<>());
        Map<String, Object> cyparams = new HashMap<>();
        // We have a top map containing basic relation elements (from, to, properties)
        cyparams.put("fromIri", String.valueOf(cyRelation.getFromIri()));
        cyparams.put("toIri", String.valueOf(cyRelation.getToIri()));
        // And then we have an inner map containing the relation properties/attributes
        cyparams.put("properties", neoMgr.getCypherProperties(cyRelation));
        cyRels.add(cyparams);
    }
    // OK, ready to call Neo!
    // 
    log.trace("Sending {} relation(s) to Cypher", relRecords.size());
    // The relation type ('%2$s') is a constant wrt the underlining Cypher processor, for us it is instead a parameter
    // that is instantiated by the loop below.
    // 
    // Nodes are always identified by means of their default label ('%1$s'), which is always created for them.
    // 
    String cypherCreateRel = "UNWIND {relations} AS rel\n" + // The property map always contain the relation IRI
    "MATCH ( from:`%1$s`{ iri: rel.fromIri } ), ( to:`%1$s`{ iri: rel.toIri } )\n" + "CREATE (from)-[r:`%2$s`]->(to)\n" + "SET r = rel.properties";
    long relsCtr = 0;
    String defaultLabel = neoMgr.getDefaultLabel();
    for (Entry<String, List<Map<String, Object>>> cyDataE : cyData.entrySet()) {
        String type = cyDataE.getKey();
        String cyCreateStr = String.format(cypherCreateRel, defaultLabel, type);
        List<Map<String, Object>> props = cyDataE.getValue();
        neoMgr.runCypher(cyCreateStr, "relations", props);
        relsCtr += props.size();
    }
    log.debug("{} actual relations(s) sent to Cypher", relsCtr);
}
Also used : HashMap(java.util.HashMap) QuerySolution(org.apache.jena.query.QuerySolution) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap)

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