Java Code Examples for org.eclipse.rdf4j.query.algebra.TupleExpr#visit()

The following examples show how to use org.eclipse.rdf4j.query.algebra.TupleExpr#visit() . 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: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitDeleteData(Resource query) throws RDF4JException {
	SingletonSet stub = new SingletonSet();
	tupleRoot = new QueryRoot(stub);
	tupleNode = stub;
	TupleExpr deleteExpr;
	Value delete = TripleSources.singleValue(query, SP.DATA_PROPERTY, store);
	if (!(delete instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.DATA_PROPERTY));
	}
	visitDelete((Resource) delete);
	deleteExpr = tupleNode;
	deleteExpr.setParentNode(null);

	DataVisitor visitor = new DataVisitor();
	deleteExpr.visit(visitor);
	updateRoot = new DeleteData(visitor.getData());
}
 
Example 2
Source File: FilterFunctionOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
private void buildQuery(final TupleExpr tupleExpr, final StatementPattern matchStatement) {
    //If our IndexerExpr (to be) is the rhs-child of LeftJoin, we can safely make that a Join:
    //  the IndexerExpr will (currently) not return results that can deliver unbound variables.
    //This optimization should probably be generalized into a LeftJoin -> Join optimizer under certain conditions. Until that
    //  has been done, this code path at least takes care of queries generated by OpenSahara SparqTool that filter on OPTIONAL
    //  projections. E.g. summary~'full text search' (summary is optional). See #379
    if (matchStatement.getParentNode() instanceof LeftJoin) {
        final LeftJoin leftJoin = (LeftJoin)matchStatement.getParentNode();
        if (leftJoin.getRightArg() == matchStatement && leftJoin.getCondition() == null) {
            matchStatement.getParentNode().replaceWith(new Join(leftJoin.getLeftArg(), leftJoin.getRightArg()));
        }
    }
    final FilterFunction fVisitor = new FilterFunction(matchStatement.getObjectVar().getName());
    tupleExpr.visit(fVisitor);
    final List<IndexingExpr> results = Lists.newArrayList();
    for(int i = 0; i < fVisitor.func.size(); i++){
        results.add(new IndexingExpr(fVisitor.func.get(i), matchStatement, Arrays.stream(fVisitor.args.get(i)).toArray()));
    }
    removeMatchedPattern(tupleExpr, matchStatement, new IndexerExprReplacer(results));
}
 
Example 3
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void render(ParsedBooleanQuery query, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();
	Resource querySubj = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(querySubj, RDF.TYPE, SP.ASK_CLASS));
	if (output.text) {
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.TEXT_PROPERTY,
				valueFactory.createLiteral(query.getSourceString())));
	}
	if (output.rdf) {
		Resource whereBNode = valueFactory.createBNode();
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.WHERE_PROPERTY, whereBNode));
		TupleExpr expr = query.getTupleExpr();
		SpinVisitor visitor = new AskVisitor(handler, whereBNode, query.getDataset());
		expr.visit(visitor);
		visitor.end();
	}
	handler.endRDF();
}
 
Example 4
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Projection node) throws RepositoryException {
	TupleExpr arg = node.getArg();
	if (arg instanceof StatementPattern && arg.getBindingNames().equals(node.getBindingNames())) {
		meetNode(node);
	} else {
		arg.visit(this);
		if (patternNode == null) {
			return;
		}
		Map<String, String> map = new HashMap<>();
		for (ProjectionElem e : node.getProjectionElemList().getElements()) {
			String source = variables.get(e.getSourceName());
			if (source == null) {
				source = safe(e.getSourceName());
			}
			map.put(e.getTargetName(), source);
		}
		this.variables = map;
		this.patternNode = node;
	}
}
 
Example 5
Source File: ValidIndexCombinationGenerator.java    From rya with Apache License 2.0 6 votes vote down vote up
private int indicesDisjoint(List<Integer> combo,
		List<ExternalTupleSet> indexList) {

	Set<QueryModelNode> indexNodes = Sets.newHashSet();
	Set<QueryModelNode> tempNodes;
	TupleExpr temp;

	int j = 0;
	for (Integer i : combo) {
		temp = indexList.get(i).getTupleExpr();
		SpFilterCollector spf = new SpFilterCollector();
		temp.visit(spf);
		tempNodes = spf.getSpFilterSet();
		if (Sets.intersection(indexNodes, tempNodes).size() == 0) {
			indexNodes = Sets.union(indexNodes, tempNodes);
			if (indexNodes.size() > spFilterSet.size()) {
				return j;
			}
		} else {
			return j;
		}
		j++;
	}

	return -1;
}
 
Example 6
Source File: RestarterIteration.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
public void meet(BindJoin bj)  {
	TupleExpr rightArg = bj.getRightArg();
	if (rightArg instanceof TopKSourceStatementPattern) {
		TopKSourceStatementPattern r = (TopKSourceStatementPattern)rightArg;
		if (r.hasEntry(curEntry)) {
			long modL = r.getCachedBindingCount(); // |L|/B
			cost = rCost * modL;
			// additional results min(|L|, curEntry.card)
			additionalResults = Math.min((long)(modL * nBindingsCfg), curEntry.card);
			//CVisitor cv = new CVisitor();
			//bj.getLeftArg().visit(cv);
			//additionalResults = Math.min(modL * nBindingsCfg /*cv.getCardinality()*/, curEntry.card);
			return;
		}
	}
	meetNode(bj.getLeftArg());
	cost += additionalResults/nBindingsCfg * rCost;
	CVisitor cv = new CVisitor();
	rightArg.visit(cv);
	additionalResults = Math.min(additionalResults, cv.getCardinality());
}
 
Example 7
Source File: ContextCollector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
static Map<TupleExpr, Var> collectContexts(TupleExpr theTupleExpr)
        throws Exception
{
    ContextCollector aContextVisitor = new ContextCollector();

    theTupleExpr.visit(aContextVisitor);

    return aContextVisitor.mContexts;
}
 
Example 8
Source File: SpinFunctionInterpreter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) {
	try {
		tupleExpr.visit(new FunctionScanner());
	} catch (RDF4JException e) {
		logger.warn("Failed to parse function");
	}
}
 
Example 9
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
private void setVarOrderAndQueryType(final QueryMetadata.Builder builder, final TupleExpr te) {
    final QueryMetadataLocator locator = new QueryMetadataLocator();
    try {
        te.visit(locator);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }

    builder.setVarOrder(locator.getVarOrder());
    builder.setQueryType(locator.getQueryType());
}
 
Example 10
Source File: HalyardQueryJoinOptimizerTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryJoinOptimizerWithSplitFunction() {
    final TupleExpr expr = new SPARQLParser().parseQuery("select * where {?a a \"1\";?b ?d. filter (<" + HALYARD.PARALLEL_SPLIT_FUNCTION + ">(10, ?d))}", "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 {
            assertEquals(expr.toString(), "d", ((StatementPattern)node.getLeftArg()).getObjectVar().getName());
            assertTrue(expr.toString(), ((StatementPattern)node.getRightArg()).getObjectVar().hasValue());
        }
    });
}
 
Example 11
Source File: TopOfQueryFilterRelocator.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 *
 * This method moves the Filters of a specified {@link TupleExpr} to the top
 * of the TupleExpr.
 *
 * @param query
 *            - query whose filters will be relocated
 * @return - TupleExpr with filters relocated to top
 */
public static TupleExpr moveFiltersToTop(TupleExpr query) {

    ProjectionAndFilterGatherer fg = new ProjectionAndFilterGatherer();
    query.visit(fg);
    List<ValueExpr> filterCond = new ArrayList<>(fg.filterCond);
    Projection projection = fg.projection;

    if (filterCond.size() == 0) {
        return query;
    }

    Filter first = new Filter();
    first.setCondition(filterCond.remove(0));
    Filter current = first;
    for (ValueExpr cond : filterCond) {
        Filter filter = new Filter(null, cond);
        current.setArg(filter);
        current = filter;
    }

    TupleExpr te = projection.getArg();
    projection.setArg(first);
    current.setArg(te);

    return query;

}
 
Example 12
Source File: FederationJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * If the argument can be sent to a single member.
 */
public RepositoryConnection getSingleOwner(TupleExpr arg) throws RepositoryException {
	boolean pre_shared = shared;
	RepositoryConnection pre_owner = owner;
	try {
		shared = false;
		owner = null; // NOPMD
		arg.visit(this);
		return owner;
	} finally {
		// restore
		shared = pre_shared;
		owner = pre_owner;
	}
}
 
Example 13
Source File: GenericInfoOptimizer.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void optimize(TupleExpr tupleExpr) {
	tupleExpr.visit(this);
	if (!curStmts.isEmpty()) {
		stmts.add(curStmts);
		curStmts = null;
	}
}
 
Example 14
Source File: AbstractExternalSetOptimizer.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) {
    QuerySegmentMatchVisitor visitor = new QuerySegmentMatchVisitor();
    tupleExpr.visit(visitor);
}
 
Example 15
Source File: FilterCollector.java    From semagrow with Apache License 2.0 4 votes vote down vote up
public static Collection<ValueExpr> process(TupleExpr expr){
    FilterCollector filterCollector = new FilterCollector();
    expr.visit(filterCollector);
    return filterCollector.filters;
}
 
Example 16
Source File: ThreshholdPlanSelectorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleIndex() throws Exception {

	SPARQLParser parser = new SPARQLParser();

	ParsedQuery pq1 = parser.parseQuery(q15, null);
	ParsedQuery pq2 = parser.parseQuery(q7, null);
	ParsedQuery pq3 = parser.parseQuery(q8, null);
	ParsedQuery pq4 = parser.parseQuery(q9, null);

	SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
			(Projection) pq2.getTupleExpr());
	SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
			(Projection) pq3.getTupleExpr());
	SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
			(Projection) pq4.getTupleExpr());

	List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

	list.add(extTup1);

	List<QueryModelNode> optTupNodes = Lists.newArrayList();
	optTupNodes.add(extTup2);
	optTupNodes.add(extTup3);

	IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
			pq1.getTupleExpr(), list);

	Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
			.getPlans(iep.getIndexedTuples());

	IndexPlanValidator ipv = new IndexPlanValidator(false);

	Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);

	ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
			pq1.getTupleExpr());

	TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .1, 1, 0,
			0);

	NodeCollector nc = new NodeCollector();
	optimalTup.visit(nc);

	List<QueryModelNode> qNodes = nc.getNodes();

	Assert.assertEquals(qNodes.size(), optTupNodes.size());
	for (QueryModelNode node : qNodes) {
		Assert.assertTrue(optTupNodes.contains(node));
	}

}
 
Example 17
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Applies generally applicable optimizations: path expressions are sorted from more to less specific.
 */
@Override
public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) {
	tupleExpr.visit(new TreeSanitizer());
}
 
Example 18
Source File: OptionalJoinSegmentPCJMatcherTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public static Set<QueryModelNode> getNodes(TupleExpr te) {
	nodes = new HashSet<>();
	te.visit(new LeftJoinQueryNodeGatherer());
	return nodes;
}
 
Example 19
Source File: VariableScopeOptimizer.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void optimize(TupleExpr tupleExpr) {
	tupleExpr.visit(this);
}
 
Example 20
Source File: ContextCollector.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
private void binaryOpMeet(TupleExpr theCurrentExpr, TupleExpr theLeftExpr, TupleExpr theRightExpr)
		throws Exception {
	theLeftExpr.visit(this);

	Var aLeftCtx = mContexts.get(theLeftExpr);

	theRightExpr.visit(this);

	Var aRightCtx = mContexts.get(theRightExpr);

	sameCtxCheck(theCurrentExpr, theLeftExpr, aLeftCtx, theRightExpr, aRightCtx);
}