org.eclipse.rdf4j.query.algebra.TupleExpr Java Examples
The following examples show how to use
org.eclipse.rdf4j.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: IndexedExecutionPlanGenerator.java From rya with Apache License 2.0 | 6 votes |
private List<ExternalTupleSet> getNormalizedIndices(List<ExternalTupleSet> indexSet) { ExternalTupleSet tempIndex; final List<ExternalTupleSet> normalizedIndexSet = Lists.newArrayList(); for (final ExternalTupleSet e : indexSet) { List<TupleExpr> tupList = null; try { tupList = QueryVariableNormalizer.getNormalizedIndex(query, e.getTupleExpr()); } catch (final Exception e1) { e1.printStackTrace(); throw new Error(e1); } for (final TupleExpr te : tupList) { tempIndex = (ExternalTupleSet) e.clone(); tempIndex.setProjectionExpr((Projection) te); normalizedIndexSet.add(tempIndex); } } return normalizedIndexSet; }
Example #2
Source File: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testSimpleVarGraph() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q22, null); ParsedQuery pq2 = parser2.parseQuery(q23, null); List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertTrue(normalize.size() == 1); for (TupleExpr s : normalize) { Assert.assertTrue(tupleEquals(s, pq1.getTupleExpr())); } }
Example #3
Source File: TopologyFactory.java From rya with Apache License 2.0 | 6 votes |
@Override public void meet(final Projection node) throws TopologyBuilderException { final String id = PROJECTION_PREFIX + UUID.randomUUID(); final Optional<Side> side = getSide(node); // If the arg is an Extension, there are rebindings that need to be // ignored since they do not have a processor node. TupleExpr downstreamNode = node.getArg(); if (downstreamNode instanceof Extension) { downstreamNode = ((Extension) downstreamNode).getArg(); } final ProjectionProcessorSupplier supplier = new ProjectionProcessorSupplier( ProjectionEvaluator.make(node), result -> getResult(side, result)); entries.add(new ProcessorEntry(node, id, side, supplier, Lists.newArrayList(downstreamNode))); idMap.put(node, id); super.meet(node); }
Example #4
Source File: MongoPcjQueryNode.java From rya with Apache License 2.0 | 6 votes |
/** * Creates a new {@link MongoPcjQueryNode}. * * @param sparql - sparql query whose results will be stored in PCJ document. (not empty of null) * @param pcjId - name of an existing PCJ. (not empty or null) * @param pcjDocs - {@link MongoPcjDocuments} used to maintain PCJs in mongo. (not null) * * @throws MalformedQueryException - The SPARQL query needs to contain a projection. */ public MongoPcjQueryNode(final String sparql, final String pcjId, final MongoPcjDocuments pcjDocs) throws MalformedQueryException { checkArgument(!Strings.isNullOrEmpty(sparql)); checkArgument(!Strings.isNullOrEmpty(pcjId)); this.pcjDocs = checkNotNull(pcjDocs); this.pcjId = pcjId; final SPARQLParser sp = new SPARQLParser(); final ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(sparql, null); final TupleExpr te = pq.getTupleExpr(); Preconditions.checkArgument(PCJOptimizerUtilities.isPCJValid(te), "TupleExpr is an invalid PCJ."); final Optional<Projection> projection = new ParsedQueryUtil().findProjection(pq); if (!projection.isPresent()) { throw new MalformedQueryException("SPARQL query '" + sparql + "' does not contain a Projection."); } setProjectionExpr(projection.get()); }
Example #5
Source File: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testSES1927UnequalLiteralValueConstants1() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} "); ParsedQuery q = parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); Slice s = (Slice) te; assertTrue(s.getArg() instanceof Join); Join j = (Join) s.getArg(); assertTrue(j.getLeftArg() instanceof StatementPattern); assertTrue(j.getRightArg() instanceof StatementPattern); StatementPattern leftArg = (StatementPattern) j.getLeftArg(); StatementPattern rightArg = (StatementPattern) j.getRightArg(); assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar())); assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName()); }
Example #6
Source File: TupleReArranger.java From rya with Apache License 2.0 | 6 votes |
private static List<TupleExpr> getFilterChain(List<Filter> filters) { List<TupleExpr> filterTopBottom = Lists.newArrayList(); Filter filterChainTop = null; Filter filterChainBottom = null; for (Filter filter : filters) { if (filterChainTop == null) { filterChainTop = filter.clone(); } else if (filterChainBottom == null) { filterChainBottom = filter.clone(); filterChainTop.setArg(filterChainBottom); } else { Filter newFilter = filter.clone(); filterChainBottom.setArg(newFilter); filterChainBottom = newFilter; } } if (filterChainTop != null) { filterTopBottom.add(filterChainTop); } if (filterChainBottom != null) { filterTopBottom.add(filterChainBottom); } return filterTopBottom; }
Example #7
Source File: TupleExprs.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Returns {@link TupleExpr} children of the given node. * * @param t a tuple expression. * @return a list of TupleExpr children. */ public static List<TupleExpr> getChildren(TupleExpr t) { final List<TupleExpr> children = new ArrayList<>(4); t.visitChildren(new AbstractQueryModelVisitor<RuntimeException>() { @Override public void meetNode(QueryModelNode node) { if (node instanceof TupleExpr) { children.add((TupleExpr) node); } } }); return children; }
Example #8
Source File: UpdateExprBuilder.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public TupleExpr visit(ASTDeleteClause node, Object data) throws VisitorException { TupleExpr result = (TupleExpr) data; // Collect construct triples GraphPattern parentGP = graphPattern; graphPattern = new GraphPattern(); // inherit scope & context graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope()); graphPattern.setContextVar(parentGP.getContextVar()); for (int i = 0; i < node.jjtGetNumChildren(); i++) { node.jjtGetChild(i).jjtAccept(this, data); } TupleExpr deleteExpr = graphPattern.buildTupleExpr(); Map<String, Object> tripleVars = TripleRefCollector.process(where); // FIXME we should adapt the grammar so we can avoid doing this in // post-processing. VarCollector collector = new VarCollector(); deleteExpr.visit(collector); for (Var var : collector.getCollectedVars()) { // skip vars that are provided by ValueExprTripleRef - added as Extentsion if (tripleVars.containsKey(var.getName())) { continue; } if (var.isAnonymous() && !var.hasValue()) { // blank node in delete pattern, not allowed by SPARQL spec. throw new VisitorException("DELETE clause may not contain blank nodes"); } } graphPattern = parentGP; return deleteExpr; }
Example #9
Source File: QueryJoinOptimizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected <L extends List<TupleExpr>> L getJoinArgs(TupleExpr tupleExpr, L joinArgs) { if (tupleExpr instanceof Join) { Join join = (Join) tupleExpr; getJoinArgs(join.getLeftArg(), joinArgs); getJoinArgs(join.getRightArg(), joinArgs); } else { joinArgs.add(tupleExpr); } return joinArgs; }
Example #10
Source File: ParallelJoinTask.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public ParallelJoinTask(ParallelExecutor<BindingSet> joinControl, FederationEvalStrategy strategy, TupleExpr expr, BindingSet bindings) { this.strategy = strategy; this.expr = expr; this.bindings = bindings; this.joinControl = joinControl; }
Example #11
Source File: ConstantOptimizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Applies generally applicable optimizations to the supplied query: variable assignments are inlined. */ @Override public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { ConstantVisitor visitor = new ConstantVisitor(); tupleExpr.visit(visitor); Set<String> varsBefore = visitor.varNames; VarNameCollector varCollector = new VarNameCollector(); tupleExpr.visit(varCollector); Set<String> varsAfter = varCollector.varNames; if (varsAfter.size() < varsBefore.size()) { varsBefore.removeAll(varsAfter); for (ProjectionElemList projElems : visitor.projElemLists) { for (ProjectionElem projElem : projElems.getElements()) { String name = projElem.getSourceName(); if (varsBefore.contains(name)) { UnaryTupleOperator proj = (UnaryTupleOperator) projElems.getParentNode(); Extension ext = new Extension(proj.getArg()); proj.setArg(ext); Var lostVar = new Var(name); Value value = bindings.getValue(name); if (value != null) { lostVar.setValue(value); } ext.addElement(new ExtensionElem(lostVar, name)); } } } } }
Example #12
Source File: ContextCollector.java From semagrow with Apache License 2.0 | 5 votes |
private void sameCtxCheck(TupleExpr theCurrentExpr, TupleExpr theLeftExpr, Var theLeftCtx, TupleExpr theRightExpr, Var theRightCtx) { if ((theLeftCtx != null) && (theRightCtx != null) && isSameCtx(theLeftCtx, theRightCtx)) { mContexts.remove(theLeftExpr); mContexts.remove(theRightExpr); mContexts.put(theCurrentExpr, theLeftCtx); } }
Example #13
Source File: VOIDSourceSelector.java From semagrow with Apache License 2.0 | 5 votes |
@Loggable public Collection<SourceMetadata> getSources(TupleExpr expr, Dataset dataset, BindingSet bindings) { if (expr instanceof StatementPattern) return getSources((StatementPattern)expr, dataset, bindings); List<StatementPattern> patterns = StatementPatternCollector.process(expr); return getSources(patterns, dataset, bindings); }
Example #14
Source File: SailRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SailBooleanQuery prepareBooleanQuery(QueryLanguage ql, String queryString, String baseURI) throws MalformedQueryException { Optional<TupleExpr> sailTupleExpr = sailConnection.prepareQuery(ql, Query.QueryType.BOOLEAN, queryString, baseURI); ParsedBooleanQuery parsedQuery = sailTupleExpr .map(expr -> new ParsedBooleanQuery(queryString, expr)) .orElse(QueryParserUtil.parseBooleanQuery(ql, queryString, baseURI)); return new SailBooleanQuery(parsedQuery, this); }
Example #15
Source File: QueryAlgebraUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Convert the given {@link ArbitraryLengthPath} to a fresh {@link TupleExpr} where all provided bindings are bound. * * @param node * @param varNames * @param bindings * @return the fresh and bound expression */ public static TupleExpr toTupleExpr(ArbitraryLengthPath node, Set<String> varNames, BindingSet bindings) { TupleExpr clone = node.clone(); InsertBindingsVisitor bindingsInserter = new InsertBindingsVisitor(bindings); bindingsInserter.meetOther(clone); varNames.addAll(bindingsInserter.freeVars); return clone; }
Example #16
Source File: SailRepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testPrepareTupleQuery_not_bypassed() throws Exception { Optional<TupleExpr> response = Optional.empty(); when(sailConnection.prepareQuery(any(), eq(Query.QueryType.TUPLE), any(), any())).thenReturn(response); when(sailConnection.evaluate(any(), any(), any(), anyBoolean())).thenReturn(new EmptyIteration<>()); TupleQuery query = subject.prepareTupleQuery("SELECT * WHERE { ?s ?p ?o }"); query.evaluate(); // check that evaluation is still called, and not with an empty TupleExpr verify(sailConnection).evaluate(any(TupleExpr.class), any(), any(), anyBoolean()); }
Example #17
Source File: SailRepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testPrepareGraphQuery_bypassed() throws Exception { TupleExpr expr = mock(TupleExpr.class); Optional<TupleExpr> response = Optional.of(expr); when(sailConnection.prepareQuery(any(), eq(Query.QueryType.GRAPH), any(), any())).thenReturn(response); when(sailConnection.evaluate(eq(expr), any(), any(), anyBoolean())).thenReturn(new EmptyIteration<>()); GraphQuery query = subject.prepareGraphQuery("CONSTRUCT WHERE { ?s ?p ?o }"); query.evaluate(); // check that the TupleExpr implementation created by the underlying sail was passed to the evaluation verify(sailConnection).evaluate(eq(expr), any(), any(), anyBoolean()); }
Example #18
Source File: BasicRater.java From rya with Apache License 2.0 | 5 votes |
private double getConnectedComponentRating(List<QueryModelNode> eNodes) { Multimap<String, Integer> commonVarBin = HashMultimap.create(); // bin QueryModelNode positions according to variable names for (int i = 0; i < eNodes.size(); i++) { QueryModelNode node = eNodes.get(i); if (node instanceof TupleExpr) { TupleExpr tup = (TupleExpr) node; Set<String> bindingNames = tup.getAssuredBindingNames(); for (String name : bindingNames) { if (!VarNameUtils.isConstant(name)) { commonVarBin.put(name, i); } } } } Set<List<Integer>> pairs = new HashSet<>(); for (String var : commonVarBin.keySet()) { Set<Integer> pos = Sets.newHashSet(commonVarBin.get(var)); pairs.addAll(Sets.cartesianProduct(pos, pos)); } int numComponents = countComponents(eNodes.size(), pairs); return ((double) numComponents) / eNodes.size(); }
Example #19
Source File: BasicGroup.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private TupleExpr filteredTuple(TupleExpr theExpr) { TupleExpr aExpr = theExpr; for (ValueExpr aValEx : mFilters) { Filter aFilter = new Filter(); aFilter.setCondition(aValEx); aFilter.setArg(aExpr); aExpr = aFilter; } return aExpr; }
Example #20
Source File: SailBooleanQuery.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean evaluate() throws QueryEvaluationException { ParsedBooleanQuery parsedBooleanQuery = getParsedQuery(); TupleExpr tupleExpr = parsedBooleanQuery.getTupleExpr(); Dataset dataset = getDataset(); if (dataset == null) { // No external dataset specified, use query's own dataset (if any) dataset = parsedBooleanQuery.getDataset(); } CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter1 = null; CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter2 = null; try { SailConnection sailCon = getConnection().getSailConnection(); bindingsIter1 = sailCon.evaluate(tupleExpr, dataset, getBindings(), getIncludeInferred()); bindingsIter2 = enforceMaxQueryTime(bindingsIter1); return bindingsIter2.hasNext(); } catch (SailException e) { throw new QueryEvaluationException(e.getMessage(), e); } finally { // Always cleanup all iterators, as they are not persistently visible outside of this method try { if (bindingsIter2 != null) { bindingsIter2.close(); } } finally { if (bindingsIter1 != null) { bindingsIter1.close(); } } } }
Example #21
Source File: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testParsedBooleanQueryRootNode() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?a <foo:bar> \"test\"}"); ParsedBooleanQuery q = (ParsedBooleanQuery) parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); assertNull(te.getParentNode()); }
Example #22
Source File: SemagrowSailConnection.java From semagrow with Apache License 2.0 | 5 votes |
public Publisher<? extends BindingSet> evaluateOnlyReactive(TupleExpr expr, Dataset dataset, BindingSet bindings, boolean b, boolean p) throws SailException { try { //QueryExecutorImpl executor = new QueryExecutorImpl(); FederatedEvaluationStrategyImpl strategy = new FederatedEvaluationStrategyImpl(SemagrowValueFactory.getInstance()); strategy.setBatchSize(semagrowSail.getBatchSize()); return strategy.evaluate(expr, bindings); } catch(QueryEvaluationException e) { throw new SailException(e); } }
Example #23
Source File: SemagrowSailConnection.java From semagrow with Apache License 2.0 | 5 votes |
public final CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate( TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred, boolean includeProvenance, Collection<IRI> includeOnlySources, Collection<IRI> excludeSources) throws SailException { //FIXME: flushPendingUpdates(); connectionLock.readLock().lock(); try { verifyIsOpen(); boolean registered = false; CloseableIteration<? extends BindingSet, QueryEvaluationException> iteration = evaluateInternal(tupleExpr, dataset, bindings, includeInferred, includeProvenance, includeOnlySources, excludeSources); try { CloseableIteration<? extends BindingSet, QueryEvaluationException> registeredIteration = registerIteration(iteration); registered = true; return registeredIteration; } finally { if (!registered) { try { iteration.close(); } catch (QueryEvaluationException e) { throw new SailException(e); } } } } finally { connectionLock.readLock().unlock(); } }
Example #24
Source File: SparqlFluoQueryBuilder.java From rya with Apache License 2.0 | 5 votes |
/** * Get the non-constant variables from a {@link TupleExpr}. * * @param node - The node to inspect for variables. (not null) * @return The non-constant variables that were part of the node. */ private Set<String> getVars(final TupleExpr node) { checkNotNull(node); final Set<String> vars = Sets.newHashSet(); for(final String bindingName : node.getBindingNames()) { if (!VarNameUtils.isConstant(bindingName)) { vars.add(bindingName); } } return vars; }
Example #25
Source File: EntityIndexOptimizer.java From rya with Apache License 2.0 | 5 votes |
@Override public void optimize(TupleExpr tupleExpr, final Dataset dataset, final BindingSet bindings) { checkNotNull(tupleExpr); checkNotNull(indexer); // first standardize query by pulling all filters to top of query if // they exist using TopOfQueryFilterRelocator tupleExpr = TopOfQueryFilterRelocator.moveFiltersToTop(tupleExpr); super.optimize(tupleExpr, null, null); }
Example #26
Source File: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 5 votes |
/** * @throws Exception * Tests QueryVariableNormalizerContext to see if it recognizes * that no substitution exists for two moderate, similar queries * q5 and q1 that are structurally different */ @Test public void testEqNEQ() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q1, null); ParsedQuery pq2 = parser2.parseQuery(q5, null); List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertTrue(normalize.size() == 0); pq1 = parser1.parseQuery(q5, null); pq2 = parser2.parseQuery(q1, null); List<TupleExpr> normalize2 = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertEquals(1, normalize2.size()); for (TupleExpr s : normalize2) { List<TupleExpr> testList = QueryVariableNormalizer.getNormalizedIndex(pq2.getTupleExpr(), s); Assert.assertEquals(1, testList.size()); for (TupleExpr t : testList) { Assert.assertTrue(t.equals(pq2.getTupleExpr())); } } }
Example #27
Source File: HalyardQueryJoinOptimizerTest.java From Halyard with Apache License 2.0 | 5 votes |
@Test public void testQueryJoinOptimizer() { final TupleExpr expr = new SPARQLParser().parseQuery("select * where {?a ?b ?c, \"1\".}", "http://baseuri/").getTupleExpr(); new HalyardQueryJoinOptimizer(new HalyardEvaluationStatistics(null, null)).optimize(expr, null, null); expr.visit(new AbstractQueryModelVisitor<RuntimeException>(){ @Override public void meet(Join node) throws RuntimeException { assertTrue(expr.toString(), ((StatementPattern)node.getLeftArg()).getObjectVar().hasValue()); assertEquals(expr.toString(), "c", ((StatementPattern)node.getRightArg()).getObjectVar().getName()); } }); }
Example #28
Source File: SpinRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private ExtensionContext meetExtension(TupleExpr expr) { ExtensionContext extVisitor = new ExtensionContext(); expr.visit(extVisitor); ExtensionContext oldInlineBindings = inlineBindings; inlineBindings = (extVisitor.extension) != null ? extVisitor : null; return oldInlineBindings; }
Example #29
Source File: CardinalityVisitor.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
public static Collection<String> getCommonVars(Collection<String> vars, TupleExpr tupleExpr) { Collection<String> commonvars = null; Collection<String> exprVars = OptimizerUtil.getFreeVars(tupleExpr); for (String argvar : exprVars) { if (vars.contains(argvar)) { if (commonvars == null) { commonvars = new HashSet<String>(); } commonvars.add(argvar); } } return commonvars; }
Example #30
Source File: ContextCollector.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void sameCtxCheck(TupleExpr theCurrentExpr, TupleExpr theLeftExpr, Var theLeftCtx, TupleExpr theRightExpr, Var theRightCtx) { if ((theLeftCtx != null) && (theRightCtx != null) && isSameCtx(theLeftCtx, theRightCtx)) { mContexts.remove(theLeftExpr); mContexts.remove(theRightExpr); mContexts.put(theCurrentExpr, theLeftCtx); } }