Search in sources :

Example 6 with Query

use of com.hp.hpl.jena.query.Query 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 7 with Query

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

the class ClerezzaAdapter method main.

public static void main(String[] args) {
    RuleAdapter ruleAdapter = new ClerezzaAdapter();
    try {
        KB kb = RuleParserImpl.parse("http://sssw.org/2012/rules/", new FileInputStream("/Users/mac/Documents/CNR/SSSW2012/construct/exercise3"));
        System.out.println("Rules: " + kb.getRuleList().size());
        Recipe recipe = new RecipeImpl(new IRI("http://sssw.org/2012/rules/"), "Recipe", kb.getRuleList());
        //List<ConstructQuery> jenaRules = (List<ConstructQuery>) ruleAdapter.adaptTo(recipe, ConstructQuery.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);
        String spqral = "CONSTRUCT " + "{ ?city a <http://www.mytravels.com/Itinerary/MovieCityItinerary> . " + "   ?city <http://www.w3.org/2000/01/rdf-schema#label> ?cLabel . " + "   ?event a <http://linkedevents.org/ontology/Event> . " + "   ?event <http://linkedevents.org/ontology/atPlace> ?location . " + "   ?location <http://www.w3.org/2000/01/rdf-schema#label> ?lLabel . " + "   ?location <http://www.w3.org/2002/07/owl#sameAs> ?city" + "} " + "WHERE " + "{ " + "   ?city a <http://www.mytravels.com/Itinerary/MovieCityItinerary> . " + "   ?city <http://www.w3.org/2000/01/rdf-schema#label> ?cLabel . " + "   ?event a <http://linkedevents.org/ontology/Event> . " + "   ?event <http://linkedevents.org/ontology/atPlace> ?location . " + "   ?location <http://www.w3.org/2000/01/rdf-schema#label> ?lLabel . " + "   FILTER(?lLabel = ?cLabel) " + "}";
        Model m = ModelFactory.createDefaultModel();
        Model model = FileManager.get().loadModel("/Users/mac/Documents/CNR/SSSW2012/datasets_new/Exercise5_tmp.rdf");
        //for(ConstructQuery constructQuery : jenaRules){
        //Query query = QueryFactory.create(constructQuery.toString(), Syntax.syntaxARQ);
        Query query = QueryFactory.create(spqral, Syntax.syntaxARQ);
        QueryExecution queryExecution = QueryExecutionFactory.create(query, model);
        //System.out.println(constructQuery.toString());
        m.add(queryExecution.execConstruct());
        //}
        FileOutputStream max = new FileOutputStream("/Users/mac/Documents/CNR/SSSW2012/datasets_new/example5.rdf");
        m.write(max);
    } 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) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) SimpleConstructQuery(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery) Recipe(org.apache.stanbol.rules.base.api.Recipe) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) QueryExecution(com.hp.hpl.jena.query.QueryExecution) KB(org.apache.stanbol.rules.manager.KB) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) FileOutputStream(java.io.FileOutputStream) Model(com.hp.hpl.jena.rdf.model.Model) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) AbstractRuleAdapter(org.apache.stanbol.rules.adapters.AbstractRuleAdapter)

Aggregations

Query (com.hp.hpl.jena.query.Query)7 QueryExecution (com.hp.hpl.jena.query.QueryExecution)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 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 Resource (com.hp.hpl.jena.rdf.model.Resource)1