Search in sources :

Example 1 with QueryExecution

use of com.hp.hpl.jena.query.QueryExecution in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm, Object... args) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Map<String, Object> bindingMap = new HashMap<String, Object>();
    int i = 0;
    for (Object o : args) {
        String argName = "?_arg" + i++;
        sparql = sparql.replaceFirst("\\?\\?", argName);
        bindingMap.put(argName, o);
    }
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    for (String argName : bindingMap.keySet()) {
        Object argValue = bindingMap.get(argName);
        RDFNode arg;
        if (argValue instanceof URI) {
            arg = new ResourceImpl(argValue.toString());
        } else {
            arg = getLiteralNode(argValue);
        }
        initialBinding.add(argName, arg);
    }
    ParameterizedSparqlString queryString = new ParameterizedSparqlString(q1.toString(), initialBinding);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, queryString.asQuery(), false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) HashMap(java.util.HashMap) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QuerySolutionMap(com.hp.hpl.jena.query.QuerySolutionMap) Graph(com.hp.hpl.jena.graph.Graph) ResourceImpl(com.hp.hpl.jena.rdf.model.impl.ResourceImpl) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) ResultSet(com.hp.hpl.jena.query.ResultSet) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 2 with QueryExecution

use of com.hp.hpl.jena.query.QueryExecution in project stanbol by apache.

the class JenaAdapter method main.

public static void main(String[] args) {
    RuleAdapter ruleAdapter = new JenaAdapter();
    try {
        KB kb = RuleParserImpl.parse("http://sssw.org/2012/rules/", new FileInputStream("/Users/mac/Documents/CNR/SSSW2012/rules/exercise1"));
        System.out.println("Rules: " + kb.getRuleList().size());
        Recipe recipe = new RecipeImpl(new IRI("http://sssw.org/2012/rules/"), "Recipe", kb.getRuleList());
        List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = (List<com.hp.hpl.jena.reasoner.rulesys.Rule>) ruleAdapter.adaptTo(recipe, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
        String rules = "[ Exercise1: (http://dbpedia.org/resource/Madrid http://dbpedia.org/ontology/locationOf ?location) (?location rdf:type http://dbpedia.org/ontology/Museum) (?location http://dbpedia.org/ontology/numberOfVisitors ?visitors) greaterThan(?visitors '2000000'^^http://www.w3.org/2001/XMLSchema#integer) -> (?location rdf:type http://www.mytravels.com/Itinerary/MadridItinerary) ]";
        //List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = com.hp.hpl.jena.reasoner.rulesys.Rule.parseRules(rules);
        for (com.hp.hpl.jena.reasoner.rulesys.Rule jenaRule : jenaRules) {
            System.out.println(jenaRule.toString());
        }
        Model m = ModelFactory.createDefaultModel();
        Resource configuration = m.createResource();
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
        //Model model = FileManager.get().loadModel("/Users/mac/Documents/workspaceMyStanbol/sssw2012/events.rdf");
        Model model = FileManager.get().loadModel("/Users/mac/Documents/CNR/SSSW2012/datasets_new/Exercise1.rdf");
        //GenericRuleReasoner reasoner = new GenericRuleReasoner(jenaRules);
        //GenericRuleReasoner reasoner = new GenericRuleReasoner(com.hp.hpl.jena.reasoner.rulesys.Rule.parseRules(rules));
        GenericRuleReasoner reasoner = new GenericRuleReasoner(jenaRules);
        // not needed in RDFS case
        reasoner.setOWLTranslation(true);
        reasoner.setTransitiveClosureCaching(true);
        InfModel infModel = ModelFactory.createInfModel(reasoner, model);
        infModel.prepare();
        infModel.getDeductionsModel().write(System.out);
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MovieCityMuseums> }";
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/CityEventItinerary> }";
        String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MadridItinerary> }";
        //String sparql = "select * where {?s a <http://linkedevents.org/ontology/cazzo> }"; 
        //String sparql = "select * where {?s a <http://www.mytravels.com/Itinerary/MovieCityItinerary> }";
        Query query = QueryFactory.create(sparql, Syntax.syntaxARQ);
        QueryExecution queryExecution = QueryExecutionFactory.create(query, infModel);
        com.hp.hpl.jena.query.ResultSet resultSet = queryExecution.execSelect();
        ResultSetFormatter.out(System.out, resultSet);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RuleAtomCallExeption e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnavailableRuleObjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedTypeForExportException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Query(com.hp.hpl.jena.query.Query) Recipe(org.apache.stanbol.rules.base.api.Recipe) FileNotFoundException(java.io.FileNotFoundException) InfModel(com.hp.hpl.jena.rdf.model.InfModel) QueryExecution(com.hp.hpl.jena.query.QueryExecution) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) KB(org.apache.stanbol.rules.manager.KB) List(java.util.List) ArrayList(java.util.ArrayList) AtomList(org.apache.stanbol.rules.base.api.util.AtomList) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) Resource(com.hp.hpl.jena.rdf.model.Resource) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) FileInputStream(java.io.FileInputStream) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Model(com.hp.hpl.jena.rdf.model.Model) GenericRuleReasoner(com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner) Rule(org.apache.stanbol.rules.base.api.Rule) AbstractRuleAdapter(org.apache.stanbol.rules.adapters.AbstractRuleAdapter) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter)

Example 3 with QueryExecution

use of com.hp.hpl.jena.query.QueryExecution in project goci by EBISPOT.

the class SparqlTemplate method ask.

public boolean ask(String sparql) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        return execute.execAsk();
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute ask '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 4 with QueryExecution

use of com.hp.hpl.jena.query.QueryExecution in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 5 with QueryExecution

use of com.hp.hpl.jena.query.QueryExecution in project stanbol by apache.

the class RunSingleSPARQLTest method testCreateSPARQLQueryExecutionFactory.

/**
     * Test of testCreateSPARQLQueryExecutionFactory() method, of class RunSingleSPARQL.
     */
@Test
public void testCreateSPARQLQueryExecutionFactory() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("rdfs", "<http://www.w3.org/2000/01/rdf-schema#>");
    map.put("xsd", "<http://www.w3.org/2000/01/rdf-schema#>");
    map.put("owl", "<http://www.w3.org/2000/01/rdf-schema#>");
    map.put("rdf", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
    map.put("ex", "<http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#>");
    String query = "SELECT * WHERE {?p rdf:type ex:Person .}";
    RunSingleSPARQL instance = new RunSingleSPARQL(owl, map);
    QueryExecution queryExecution = instance.createSPARQLQueryExecutionFactory(query);
    if (queryExecution == null) {
        fail("Some errors occurred in createSPARQLQueryExecutionFactory of KReSRunSPARQL");
    }
    ResultSet result = queryExecution.execSelect();
    if (result != null) {
        int m = 0;
        while (result.hasNext()) {
            result.next();
            m++;
        }
        queryExecution.close();
        assertEquals(3, m);
    // TODO review the generated test code and remove the default call to fail.
    } else {
        fail("Some errors occur in createSPARQLQueryExecutionFactory of KReSRunSPARQL");
    }
}
Also used : HashMap(java.util.HashMap) RunSingleSPARQL(org.apache.stanbol.commons.owl.RunSingleSPARQL) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution) Test(org.junit.Test)

Aggregations

QueryExecution (com.hp.hpl.jena.query.QueryExecution)6 Query (com.hp.hpl.jena.query.Query)5 Graph (com.hp.hpl.jena.graph.Graph)3 ResultSet (com.hp.hpl.jena.query.ResultSet)3 LodeException (uk.ac.ebi.fgpt.lode.exception.LodeException)3 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)3 Model (com.hp.hpl.jena.rdf.model.Model)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 AbstractRuleAdapter (org.apache.stanbol.rules.adapters.AbstractRuleAdapter)2 Recipe (org.apache.stanbol.rules.base.api.Recipe)2 RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)2 KB (org.apache.stanbol.rules.manager.KB)2 RecipeImpl (org.apache.stanbol.rules.manager.RecipeImpl)2 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)1 QuerySolutionMap (com.hp.hpl.jena.query.QuerySolutionMap)1 InfModel (com.hp.hpl.jena.rdf.model.InfModel)1 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)1