private void insertPrefixesInto(Model model) { try { if ( dataset != null ) { // Load the models prefixes first PrefixMapping m = dataset.getDefaultModel() ; model.setNsPrefixes(m) ; } // Then add the queries (just the declared mappings) // so the query declarations override the data sources. model.setNsPrefixes(query.getPrefixMapping()) ; } catch (Exception ex) { Log.warn(this, "Exception in insertPrefixes: "+ex.getMessage(), ex) ; } }
private void insertPrefixesInto(Model model) { try { if ( dataset != null ) { // Load the models prefixes first PrefixMapping m = dataset.getDefaultModel() ; model.setNsPrefixes(m) ; } // Then add the queries (just the declared mappings) // so the query declarations override the data sources. model.setNsPrefixes(query.getPrefixMapping()) ; } catch (Exception ex) { Log.warn(this, "Exception in insertPrefixes: "+ex.getMessage(), ex) ; } }
public void visitPrologue(Prologue query1) { // This is after parsing so all IRIs in the query have been made absolute. // For two queries to be equal, their explicitly set base URIs must be the same. String b1 = query1.explicitlySetBaseURI() ? query1.getBaseURI() : null ; String b2 = query2.explicitlySetBaseURI() ? query2.getBaseURI() : null ; check("Base URIs", b1, b2) ; if ( query1.getPrefixMapping() == null && query2.getPrefixMapping() == null ) return ; check("Prefixes", query1.getPrefixMapping().samePrefixMappingAs(query2.getPrefixMapping())) ; }
private void execute(String sparqlQuery) { Query sparql = QueryFactory.create(sparqlQuery); this.prefixes = new LinkedList<Prefix>(); for(Map.Entry<String,String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) { this.prefixes.add(new Prefix(e.getKey(), e.getValue())); } Rule rule = new DefaultRule(); if (sparql.isConstructType()) { Template template = sparql.getConstructTemplate(); for (Triple triple : template.getTriples()) { rule.getHead().add(SparqlUtils.triple2Atom(triple)); } } ElementVisitorImpl visitor = new ElementVisitorImpl(rule.getBody()); sparql.getQueryPattern().visit(visitor); this.rule = rule; }
private void execute(String queryString) { this.prefixes = new LinkedList<Prefix>(); List<Term> ans = new LinkedList<Term>(); Query sparql = QueryFactory.create(queryString); for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) { this.prefixes.add(new Prefix(e.getKey(), e.getValue())); } if (sparql.isSelectType()) { for (String v : sparql.getResultVars()) { ans.add(DefaultTermFactory.instance().createVariable(v)); } } ElementVisitorImpl visitor = new ElementVisitorImpl(DefaultAtomSetFactory.instance().create()); sparql.getQueryPattern().visit(visitor); // check if answer variables appear in the query body Set<Variable> bodyVars = visitor.getAtomSet().getVariables(); for(Term t : ans) { if(t.isVariable() && !bodyVars.contains(t)) { throw new ParseError("The variable ["+ t +"] of the answer list does not appear in the query body."); } } this.query = DefaultConjunctiveQueryFactory.instance().create(visitor.getAtomSet(), ans); }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public Model execConstruct(Model model) { ensureQueryType( QueryType.CONSTRUCT ); ResultSet results = exec(); if( results == null ) { QueryExecutionFactory.create( query, source, initialBinding ).execConstruct( model ); } else { model.setNsPrefixes( source.getDefaultModel() ); model.setNsPrefixes( query.getPrefixMapping() ); Set set = new HashSet(); Template template = query.getConstructTemplate(); while( results.hasNext() ) { Map bNodeMap = new HashMap(); Binding binding = results.nextBinding(); template.subst( set, bNodeMap, binding ); } for( Iterator iter = set.iterator(); iter.hasNext(); ) { Triple t = (Triple) iter.next(); Statement stmt = ModelUtils.tripleToStatement( model, t ); if( stmt != null ) model.add( stmt ); } close(); } return model; }
PrefixMapping pm = new PrefixMapping2(ARQConstants.getGlobalPrefixMap(), q.getPrefixMapping()) ; q.setPrefixMapping(pm) ; q.getPrefixMapping().setNsPrefixes(ARQConstants.getGlobalPrefixMap()) ; Q_Query query = null ; try {
String query(String query) { LOGGER.info("Using JENA:"); Query q=QueryFactory.create(query); LOGGER.info("-- Query:\n"+q); LOGGER.info("-- Base: "+q.getBaseURI()); LOGGER.info("-- Prefixes: "+q.getPrefixMapping()); LOGGER.info("-- Variables: "+q.getProjectVars()); Set<Var> variables=new HashSet<Var>(q.getProjectVars()); for(Entry<Var, Expr> entry:q.getProject().getExprs().entrySet()) { Var shadowing = entry.getKey(); VariableShadowingDetector detector = new VariableShadowingDetector(new IndentUtils(),shadowing); entry.getValue().visit(detector); if(detector.isShadowing()) { variables.remove(shadowing); variables.add(detector.getShadowed()); } } LOGGER.info("-- Graph URIS: "+q.getGraphURIs()); LOGGER.info("-- Named graphd URIS: "+q.getNamedGraphURIs()); List<String> search=q.getNamedGraphURIs(); q.getQueryPattern().visit(new ElementVisitorImplementation(search,variables)); return null; }
public static <T extends ResourceObject> Query evaluate(QueryServiceConfiguration queryServiceDTO, URI rootType) throws ParseException { Query query = QueryFactory.make(); query.setQuerySelectType(); ElementGroup elementGroup = new ElementGroup(); Var objectVar = Var.alloc("root"); // Creating and adding the first triple - could be something like: "?objectVar rdf:type oa:Annotation Triple t1 = new Triple(objectVar, RDF.type.asNode(), NodeFactory.createURI(rootType.toString())); elementGroup.addTriplePattern(t1); // Evaluating the criteria for (Criteria c : queryServiceDTO.getCriteria()) { SesameValueBackend backend = new SesameValueBackend(); LdPathParser parser = new LdPathParser(backend, queryServiceDTO.getConfiguration(), new StringReader(c.getLdpath())); Var var = LDPathEvaluator.evaluate(parser.parseSelector(queryServiceDTO.getPrefixes()), elementGroup, objectVar, queryServiceDTO.getEvaluatorConfiguration()); if (c.getConstraint() != null) { String resolvedConstraint = resolveConstraintPrefix(c.getConstraint(), queryServiceDTO, parser); EvalComparison.evaluate(elementGroup, c, var, resolvedConstraint); } } // Adding all generated patterns to the query object query.setQueryPattern(elementGroup); // Choose what we want so select - SELECT ?annotation in this case query.addResultVar(objectVar); // Setting the default prefixes, like rdf: or dc: query.getPrefixMapping().setNsPrefixes(queryServiceDTO.getPrefixes()); return query; }