org.openrdf.query.algebra.TupleExpr Java Examples
The following examples show how to use
org.openrdf.query.algebra.TupleExpr.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryRewriter.java From neo4j-sparql-extension with GNU General Public License v3.0 | 6 votes |
/** * Rewrite a given query to add inference. * * @param ql the query language used for the query * @param query the query to rewrite * @param baseuri a base URI to use for the query * @return rewritten query that includes inference * @throws MalformedQueryException if the query is malformed * @throws RepositoryException if there was a problem while rewriting */ public Query rewrite(QueryLanguage ql, String query, String baseuri) throws MalformedQueryException, RepositoryException { // parse query using Sesame QueryParserFactory f = QueryParserRegistry.getInstance().get(ql); QueryParser parser = f.getParser(); ParsedQuery parsed = parser.parseQuery(query, baseuri); // get SPARQL algebra expression from parsed query TupleExpr expr = parsed.getTupleExpr(); // rewrite query using visitor pattern RuleTransformationVisitor visitor = new RuleTransformationVisitor(rules); expr.visit(visitor); // return new query based on rewritten algebra expression return getExprQuery(parsed, expr); }
Example #2
Source File: CumulusQueryOptimizer.java From cumulusrdf with Apache License 2.0 | 6 votes |
public OrderByModifier(final TupleExpr tupleExpr, final Order order) { _tupleExpr = tupleExpr; _order = order; _variables = new HashMap<Var, Boolean>(); List<OrderElem> elems = order.getElements(); Iterator<OrderElem> iter = elems.iterator(); while (iter.hasNext()) { OrderElem ele = iter.next(); boolean ascending = ele.isAscending(); ValueExpr ex = ele.getExpr(); if (ex instanceof Var) { _variables.put((Var) ex, new Boolean(ascending)); } } }
Example #3
Source File: BGPGroupGenerator.java From GeoTriples with Apache License 2.0 | 6 votes |
/** * Generate BGP groups from a SPARQL query * @param parsedQuery TupleExpr of the SPARQL query * @return DNFGrps Map of DNF groups * @throws MalformedQueryException */ public static HashMap<Integer, List<StatementPattern>> generateBgpGroups(String strQuery) throws MalformedQueryException { HashMap<Integer, List<StatementPattern>> bgpGrps = new HashMap<Integer, List<StatementPattern>>(); int grpNo = 0; SPARQLParser parser = new SPARQLParser(); ParsedQuery parsedQuery = parser.parseQuery(strQuery, null); TupleExpr query = parsedQuery.getTupleExpr(); // collect all basic graph patterns for (TupleExpr bgp : BasicGraphPatternExtractor.process(query)) { //System.out.println(bgp); List<StatementPattern> patterns = StatementPatternCollector.process(bgp); bgpGrps.put(grpNo, patterns ); grpNo++; } return bgpGrps; }
Example #4
Source File: HeuristicsBasedSelectivityEstimator.java From cumulusrdf with Apache License 2.0 | 6 votes |
@Override // TODO: create meaningful estimates/statistics for range queries ... public double rangePatternCardinality(TupleExpr expr) { RangeStatementPattern pattern = (RangeStatementPattern) expr; Value subject = pattern.getSubjectVar().getValue(); // subject is bound if (subject != null) { return 1d; } // range contained '=' comparison, i.e., no range but exact value else if (pattern.getEquals() != null) { return 1d; } // only one bound is set, i.e., either [a, infty[ or ]-infty, a] else if (pattern.getLowerBound() == null || pattern.getUpperBound() == null) { return CARD_1000M; } // both bounds are set, i.e., range is something like [a,b] else { return CARD_1000M; } }
Example #5
Source File: BasicGraphPatternExtractor.java From GeoTriples with Apache License 2.0 | 6 votes |
@Override public void meet(Join join) throws RuntimeException { boolean valid = true; // visit join arguments and check that all are valid BGPS for (TupleExpr expr : new TupleExpr[] { join.getLeftArg(), join.getRightArg() }) { expr.visit(this); if (lastBGPNode == null) { // child is not a BGP -> join is not a BGP valid = false; } else { if (!valid) { // last child is a BGP but another child was not this.bgpList.add(lastBGPNode); lastBGPNode = null; } } } if (valid) lastBGPNode = join; }
Example #6
Source File: HeuristicsBasedSelectivityEstimator.java From cumulusrdf with Apache License 2.0 | 5 votes |
@Override public synchronized double getCardinality(TupleExpr expr) { if (expr instanceof RangeStatementPattern) { return rangePatternCardinality(expr); } if (expr instanceof StatementPattern) { return triplePatternCardinality(expr); } return super.getCardinality(expr); }
Example #7
Source File: Rule.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public Rule getFreshRule() throws Exception { StatementPattern consequentClone = consequent.clone(); TupleExpr antecedentClone = antecedent.clone(); Set<Var> vars = new HashSet<Var>(); // vars.addAll(consequentClone.getVarList()); VarCollector vis = new VarCollector(); antecedentClone.visit(vis); consequentClone.visit(vis); vars.addAll(vis.getCollectedVars()); for (Var var : vars) { if (var.isConstant()) continue; // Var newVar = new Var(); UUID id = UUID.randomUUID(); // String uniqueStringForValue = Integer.toString(id.toString()); String uniqueStringForValue = Integer.toHexString(id.toString().hashCode()); // newVar.setName(var.getName() + "_" + uniqueStringForValue.toString()); // newVar.setAnonymous(var.isAnonymous()); VarRenamer renamer = new VarRenamer(var.getName(), var.getName() + "_" + uniqueStringForValue); antecedentClone.visit(renamer); consequentClone.visit(renamer); } Rule result = new Rule(); result.antecedent = antecedentClone; result.consequent = consequentClone; return result; }
Example #8
Source File: Rule.java From quetzal with Eclipse Public License 2.0 | 5 votes |
/** * */ public Rule(ParsedQuery constructQuery) { /* Getting elements of the construct part, they are always implemented * as a pair of projection and extension before the actual algebra of the query e.g., Projection ProjectionElemList ProjectionElem "x" AS "subject" ProjectionElem "_const-f5e5585a-uri" AS "predicate" ProjectionElem "_const-a31c101d-uri" AS "object" Extension ExtensionElem (_const-a31c101d-uri) ValueConstant (value=http://example.org/Company) ExtensionElem (_const-f5e5585a-uri) ValueConstant (value=http://www.w3.org/1999/02/22-rdf-syntax-ns#type) Join... */ TupleExpr expr = constructQuery.getTupleExpr(); Projection projection = (Projection)((Reduced) expr).getArg(); Extension extension = (Extension) projection.getArg(); antecedent = extension.getArg(); extractConsequent(expr, projection, extension); }
Example #9
Source File: QueryRewriter.java From neo4j-sparql-extension with GNU General Public License v3.0 | 5 votes |
/** * Creates a new query based on a tuple expression and original query. The * new query will have the same type null * ({@link org.openrdf.query.TupleQuery}, * {@link org.openrdf.query.GraphQuery} or * {@link org.openrdf.query.BooleanQuery}) as the given original query. * * @param orig the original query * @param expr the expression used for the new query * @return new query based on expression */ protected Query getExprQuery(ParsedQuery orig, TupleExpr expr) { if (orig instanceof ParsedTupleQuery) { return new SailTupleExprQuery( new ParsedTupleQuery(expr), conn); } else if (orig instanceof ParsedGraphQuery) { return new SailGraphExprQuery( new ParsedGraphQuery(expr), conn); } else { return new SailBooleanExprQuery( new ParsedBooleanQuery(expr), conn); } }
Example #10
Source File: ObjectPropertyChain.java From neo4j-sparql-extension with GNU General Public License v3.0 | 5 votes |
/** * Transform a statement pattern according to OWL-2 property chain * axiom. * * @param node the node to transform * @return list of nodes to visit next */ @Override public List<QueryModelNode> apply(StatementPattern node) { List<QueryModelNode> next = newNextList(); Var s = node.getSubjectVar(); Var o = node.getObjectVar(); Var c = node.getContextVar(); TupleExpr left = node.clone(); TupleExpr right = getChain(s, o, c); node.replaceWith(new Union(left, right)); next.add(left); next.add(right); return next; }
Example #11
Source File: PredicateVariable.java From neo4j-sparql-extension with GNU General Public License v3.0 | 5 votes |
/** * Transform a statement pattern to infer triples for a predicate variable. * * @param node the node to transform * @return list of nodes to visit next */ @Override public List<QueryModelNode> apply(StatementPattern node) { List<QueryModelNode> next = newNextList(); StatementPattern left = node.clone(); next.add(left); TupleExpr right = assignPredicates(predicates, node.clone(), next); node.replaceWith(new Union(left, right)); return next; }
Example #12
Source File: PredicateVariable.java From neo4j-sparql-extension with GNU General Public License v3.0 | 5 votes |
/** * Returns a list of expressions as a chain of {@link Union} objects, that * will unify all expressions. * * @param unions the expressions to unify * @return an expression that unifies all given expressions */ private TupleExpr listAsUnion(List<TupleExpr> unions) { // nothing to unify if (unions.isEmpty()) { return new EmptySet(); } // no need to add a union if (unions.size() == 1) { return unions.get(0); } Union last; Union tmp; // start with a union of two elements Union first = new Union(); first.setLeftArg(unions.get(0)); first.setRightArg(unions.get(1)); last = first; // for each additional element replace the right side with another // union for (int i = 2; i < unions.size(); i++) { tmp = new Union( last.getRightArg(), unions.get(i) ); last.setRightArg(tmp); last = tmp; } return first; }
Example #13
Source File: CumulusQueryOptimizer.java From cumulusrdf with Apache License 2.0 | 5 votes |
@Override public void optimize(final TupleExpr tupleExpr, final Dataset dataset, final BindingSet bindings) { // use native support for range queries if (_ranges_indexed) { tupleExpr.visit(new RangeQueryVisitor(tupleExpr)); tupleExpr.visit(new OrderByVisitor(tupleExpr)); } // use native cumulus model tupleExpr.visit(new CumulusNativeModelVisitor()); }
Example #14
Source File: CumulusRDFSailConnection.java From cumulusrdf with Apache License 2.0 | 5 votes |
@Override protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException { // Lock stLock = _sail.getStatementsReadLock(); // Clone the tuple expression to allow for more aggressive optimizations tupleExpr = tupleExpr.clone(); if (!(tupleExpr instanceof QueryRoot)) { // Add a dummy root node to the tuple expressions to allow the // optimizers to modify the actual root node tupleExpr = new QueryRoot(tupleExpr); } TripleSource tripleSource = new CumulusRDFTripleSource(); EvaluationStrategy strategy = new RangeEvaluationStrategy(tripleSource, dataset); new BindingAssigner().optimize(tupleExpr, dataset, bindings); new ConstantOptimizer(strategy).optimize(tupleExpr, dataset, bindings); new CompareOptimizer().optimize(tupleExpr, dataset, bindings); new ConjunctiveConstraintSplitter().optimize(tupleExpr, dataset, bindings); new DisjunctiveConstraintOptimizer().optimize(tupleExpr, dataset, bindings); new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings); new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings); new CumulusQueryOptimizer(_crdf.isRangeIndexesSupportEnabled()).optimize(tupleExpr, dataset, bindings); new QueryJoinOptimizer(_select_est).optimize(tupleExpr, dataset, bindings); // new FilterOptimizer().optimize(tupleExpr, dataset, bindings); new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings); new OrderLimitOptimizer().optimize(tupleExpr, dataset, bindings); try { return strategy.evaluate(tupleExpr, EmptyBindingSet.getInstance()); } catch (QueryEvaluationException e) { e.printStackTrace(); throw new SailException(e); } }
Example #15
Source File: BasicGraphPatternExtractor.java From GeoTriples with Apache License 2.0 | 5 votes |
/** * Handles binary nodes with potential BGPs as children (e.g. union, left join). */ @Override public void meetBinaryTupleOperator(BinaryTupleOperator node) throws RuntimeException { for (TupleExpr expr : new TupleExpr[] { node.getLeftArg(), node.getRightArg() }) { expr.visit(this); if (lastBGPNode != null) { // child is a BGP node but this node is not this.bgpList.add(lastBGPNode); lastBGPNode = null; } } }
Example #16
Source File: BigdataParsedTupleQuery.java From database with GNU General Public License v2.0 | 5 votes |
/** * @param tupleExpr */ public BigdataParsedTupleQuery(final TupleExpr tupleExpr // final QueryType queryType // final Properties queryHints ) { super(tupleExpr); // this.queryType = queryType; // // this.queryHints = queryHints; }
Example #17
Source File: BigdataSail.java From database with GNU General Public License v2.0 | 5 votes |
/** * Bigdata now uses an internal query model which differs significantly * from the Sesame query model. Support is no longer provided for * {@link TupleExpr} evaluation. SPARQL queries must be prepared and * evaluated using a {@link BigdataSailRepositoryConnection}. * * @throws SailException * <em>always</em>. */ public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate( final TupleExpr tupleExpr, // final Dataset dataset,// final BindingSet bindings,// final boolean includeInferred// ) throws SailException { throw new SailException(ERR_OPENRDF_QUERY_MODEL); }
Example #18
Source File: CumulusQueryOptimizer.java From cumulusrdf with Apache License 2.0 | 4 votes |
public RangeStatementModifier(final TupleExpr tupleExpr, final CompareOp comp, final Var var, final Literal bound) { _tupleExpr = tupleExpr; _comp = comp; _var = var; _bound = bound; }
Example #19
Source File: TestJoinScope.java From database with GNU General Public License v2.0 | 4 votes |
public void testJoinScope() throws Exception { final BigdataSail sail = getSail(); sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final BigdataSailRepositoryConnection cxn = (BigdataSailRepositoryConnection) repo.getConnection(); cxn.setAutoCommit(false); try { final BNode a = new BNodeImpl("_:a"); final BNode b = new BNodeImpl("_:b"); final URI graphA = new URIImpl("http://www.bigdata.com/rdf#graphA"); final URI graphB = new URIImpl("http://www.bigdata.com/rdf#graphB"); final URI s = new URIImpl("http://www.bigdata.com/rdf#s"); final URI p1 = new URIImpl("http://www.bigdata.com/rdf#p1"); final URI o1 = new URIImpl("http://www.bigdata.com/rdf#o1"); final URI p2 = new URIImpl("http://www.bigdata.com/rdf#p2"); final URI o2 = new URIImpl("http://www.bigdata.com/rdf#o2"); URL url = new URL("file:/C:/DOCUME~1/mike/LOCALS~1/Temp/sparql2303/testcases-dawg/data-r2/algebra/var-scope-join-1.ttl"); cxn.add(url, "", RDFFormat.TURTLE); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.flush();//commit(); /**/ if (log.isInfoEnabled()) { log.info(cxn.getTripleStore().dumpStore()); } String query = "PREFIX : <http://example/> " + " SELECT * " + " { " + " ?X :name \"paul\" . " + " ?X :name \"sue\" . " + " ?Y :name \"george\" . " + " OPTIONAL { ?X :email ?Z } " + " OPTIONAL { ?X :address ?A } " + " }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); TupleQueryResult result = tupleQuery.evaluate(); TupleExpr tupleExpr = tupleQuery.getParsedQuery().getTupleExpr(); Collection<BindingSet> answer = new LinkedList<BindingSet>(); compare(result, answer); } finally { cxn.close(); sail.__tearDownUnitTest(); } }
Example #20
Source File: Rule.java From quetzal with Eclipse Public License 2.0 | 4 votes |
private void extractConsequent(TupleExpr constructQuery, Projection projection, Extension extension) { StatementPattern consequent = new StatementPattern(); Var sub = new Var(); Var pred = new Var(); Var obj = new Var(); HashMap<String, ValueConstant> index = new HashMap<String, ValueConstant>(); for (ExtensionElem extElem: extension.getElements()) { try { index.put(extElem.getName(), (ValueConstant) extElem.getExpr()); } catch (ClassCastException e) { e.printStackTrace(); throw new RuntimeException("Unsupported construct query rule: \n" + constructQuery.toString()); } } for (ProjectionElem elem: projection.getProjectionElemList().getElements()) { Var currentComponent = null; if (elem.getTargetName().equals("subject")) { currentComponent = sub; } else if (elem.getTargetName().equals("predicate")) { currentComponent = pred; } else if (elem.getTargetName().equals("object")) { currentComponent = obj; } else { throw new RuntimeException("Unsupported construct query rule: " + constructQuery.toString()); } ValueConstant valueConstant = index.get(elem.getSourceName()); if (valueConstant != null) { currentComponent.setConstant(true); currentComponent.setValue(valueConstant.getValue()); currentComponent.setName(elem.getSourceName()); } else { currentComponent.setConstant(false); currentComponent.setName(elem.getSourceName()); } } consequent.setSubjectVar(sub); consequent.setPredicateVar(pred); consequent.setObjectVar(obj); this.consequent = consequent; }
Example #21
Source File: BigdataParsedUpdate.java From database with GNU General Public License v2.0 | 4 votes |
/** * Unsupported operation. */ public BigdataParsedUpdate(TupleExpr tupleExpr) { throw new UnsupportedOperationException(); }
Example #22
Source File: ResolutionEngine.java From quetzal with Eclipse Public License 2.0 | 4 votes |
/**** * If the visited pattern unifies with rule, and the node has not been * visited, it will replace the node (in the nodes parent) with a union * consisting of the node unified with the body of all the rules that * unified with the node. */ @Override public void meet(StatementPattern node) { if (visited.contains(node)) return; visited.add(node); QueryModelNode parent = node.getParentNode(); List<TupleExpr> nodeAlternatives = new LinkedList<TupleExpr>(); nodeAlternatives.add(node); for (Rule rule : rules) { try { rule = rule.getFreshRule(); } catch (Exception e) { throw new RuntimeException(e); } Substitution s = StatementUnifier.getMGU(node, rule.consequent); if (s == null) continue; // The rule matches the node, replacing (executing a resolution // step) // Preparing the body of the rule TupleExpr body = rule.antecedent; SubstitutionApplier app = new SubstitutionApplier(s); body.visit(app); /* If the substitution affects any variables in the domain of the original query then we need to add a BIND(...) element, using extensions, for example. If x is in the original query, and we have the substition x/<example.org> we need the extension BIND(<example.org> as ?x) */ Extension ex = new Extension(); for (Var var: s.getMap().keySet()) { String name = var.getName(); if (!queryBindingNames.contains(name)) { continue; } Var expr = s.get(var); ex.addElement(new ExtensionElem(expr, name)); } TupleExpr newExpression; if (!ex.getElements().isEmpty()) { ex.setArg(body); newExpression = ex; } else { newExpression = body; } nodeAlternatives.add(newExpression); } if (nodeAlternatives.size() == 1) { // There was no resolution, no change return; } // constructing UNION operator, a binary tree, removing 2 at a time Union union = null; while (!nodeAlternatives.isEmpty()) { Union newunion = null; if (union == null) { newunion = new Union(nodeAlternatives.remove(0), nodeAlternatives.remove(0)); } else { newunion = new Union(nodeAlternatives.remove(0), union); } union = newunion; } // replacing the node with the constructed union parent.replaceChildNode(node, union); producedChange = true; }
Example #23
Source File: ResolutionEngine.java From quetzal with Eclipse Public License 2.0 | 4 votes |
/*** * Takes a SELECT query and * @param query */ public void unfold(ParsedTupleQuery query) { ResolutionVisitor visitor = new ResolutionVisitor(rules); TupleExpr body = query.getTupleExpr(); VarNameCollector collector = new VarNameCollector(); body.visit(collector); Set<String> bindingNames = collector.getVarNames(); visitor.setBindingNames(bindingNames); body.visit(visitor); while (visitor.producedChange) { visitor.producedChange = false; body.visit(visitor); } }
Example #24
Source File: BigdataParsedUpdate.java From database with GNU General Public License v2.0 | 4 votes |
/** * Unsupported operation. */ public BigdataParsedUpdate(TupleExpr tupleExpr, Dataset dataset) { throw new UnsupportedOperationException(); }
Example #25
Source File: BigdataParsedQuery.java From database with GNU General Public License v2.0 | 4 votes |
/** * Unsupported operation. */ public BigdataParsedQuery(TupleExpr tupleExpr) { throw new UnsupportedOperationException(); }
Example #26
Source File: BigdataParsedQuery.java From database with GNU General Public License v2.0 | 4 votes |
/** * Unsupported operation. */ public BigdataParsedQuery(TupleExpr tupleExpr, Dataset dataset) { throw new UnsupportedOperationException(); }
Example #27
Source File: CumulusQueryOptimizer.java From cumulusrdf with Apache License 2.0 | 4 votes |
public OrderByVisitor(final TupleExpr tupleExpr) { _tupleExpr = tupleExpr; }
Example #28
Source File: BasicGraphPatternExtractor.java From GeoTriples with Apache License 2.0 | 4 votes |
public static List<TupleExpr> process(QueryModelNode node) { BasicGraphPatternExtractor ex = new BasicGraphPatternExtractor(); node.visit(ex); return ex.bgpList; }
Example #29
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 4 votes |
@Test public void testQueryBindings() throws Exception { // Add some data to the repository con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.addStatement(picasso, paints, guernica, context1); con.commit(); // Query 1 ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class}", null); TupleExpr tupleExpr = tupleQuery.getTupleExpr(); MapBindingSet bindings = new MapBindingSet(2); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleExpr, null, bindings, false); int resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 2, resultCount); bindings.addBinding("Y", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 1, resultCount); bindings.addBinding("Z", painting); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 1, resultCount); bindings.removeBinding("Y"); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 2, resultCount); // Query 2 tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class} where Y = Z", null); tupleExpr = tupleQuery.getTupleExpr(); bindings.clear(); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 0, resultCount); bindings.addBinding("Z", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); assertEquals("Wrong number of query results", 1, resultCount); }
Example #30
Source File: HeuristicsBasedSelectivityEstimator.java From cumulusrdf with Apache License 2.0 | 4 votes |
@Override public double equiJoinCardinality(TupleExpr expr) { // TODO: create meaningful estimates for simple equi-joins return 0; }