org.eclipse.rdf4j.query.algebra.ZeroLengthPath Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.ZeroLengthPath. 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: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(ZeroLengthPath node) {
	Var subjVar = node.getSubjectVar();
	Var objVar = node.getObjectVar();
	if ((subjVar != null && subjVar.hasValue()) || (objVar != null && objVar.hasValue())) {
		// subj = obj
		cardinality = 1.0;
	} else {
		// actual cardinality = count(union(subjs, objs))
		// but cost is equivalent to ?s ?p ?o ?c (impl scans all statements)
		// so due to the lower actual cardinality we value it in preference to a fully unbound statement
		// pattern.
		cardinality = getSubjectCardinality(subjVar) * getObjectCardinality(objVar)
				* getContextCardinality(node.getContextVar());
	}
}
 
Example #2
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Switch logic appropriate for each type of {@link TupleExpr} query model node, sending each type to it's appropriate evaluation method. For example,
 * {@code UnaryTupleOperator} is sent to {@link evaluateUnaryTupleOperator()}.
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateTupleExpr(BindingSetPipe parent, TupleExpr expr, BindingSet bindings) {
    if (expr instanceof StatementPattern) {
        statementEvaluation.evaluateStatementPattern(parent, (StatementPattern) expr, bindings);
    } else if (expr instanceof UnaryTupleOperator) {
        evaluateUnaryTupleOperator(parent, (UnaryTupleOperator) expr, bindings);
    } else if (expr instanceof BinaryTupleOperator) {
        evaluateBinaryTupleOperator(parent, (BinaryTupleOperator) expr, bindings);
    } else if (expr instanceof SingletonSet) {
        evaluateSingletonSet(parent, (SingletonSet) expr, bindings);
    } else if (expr instanceof EmptySet) {
        evaluateEmptySet(parent, (EmptySet) expr, bindings);
    } else if (expr instanceof ExternalSet) {
        evaluateExternalSet(parent, (ExternalSet) expr, bindings);
    } else if (expr instanceof ZeroLengthPath) {
        evaluateZeroLengthPath(parent, (ZeroLengthPath) expr, bindings);
    } else if (expr instanceof ArbitraryLengthPath) {
        evaluateArbitraryLengthPath(parent, (ArbitraryLengthPath) expr, bindings);
    } else if (expr instanceof BindingSetAssignment) {
        evaluateBindingSetAssignment(parent, (BindingSetAssignment) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass()));
    }
}
 
Example #3
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate {@link ZeroLengthPath} query model nodes
 * @param parent
 * @param zlp
 * @param bindings
 */
private void evaluateZeroLengthPath(BindingSetPipe parent, ZeroLengthPath zlp, BindingSet bindings) {
    final Var subjectVar = zlp.getSubjectVar();
    final Var objVar = zlp.getObjectVar();
    final Var contextVar = zlp.getContextVar();
    Value subj = subjectVar.getValue() == null ? bindings.getValue(subjectVar.getName()) : subjectVar.getValue();
    Value obj = objVar.getValue() == null ? bindings.getValue(objVar.getName()) : objVar.getValue();
    if (subj != null && obj != null) {
        if (!subj.equals(obj)) {
            try {
                parent.push(null);
            } catch (InterruptedException e) {
                parent.handleException(e);
            }
            return;
        }
    }
    //temporary solution using copy of the original iterator
    //re-writing this to push model is a bit more complex task
    HalyardStatementPatternEvaluation.enqueue(parent, new ZeroLengthPathIteration(parentStrategy, subjectVar, objVar, subj, obj, contextVar, bindings), zlp);
}
 
Example #4
Source File: ReflexivePropertyVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether any solution for the {@link StatementPattern} could be derived from
 * reflexive property inference, and if so, replace the pattern with a union of itself and the
 * reflexive solution.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    // Only applies when the predicate is defined and reflexive
    final Var predVar = node.getPredicateVar();
    if (predVar.getValue() != null && inferenceEngine.isReflexiveProperty((IRI) predVar.getValue())) {
        final StatementPattern originalSP = node.clone();
        // The reflexive solution is a ZeroLengthPath between subject and
        // object: they can be matched to one another, whether constants or
        // variables.
        final Var subjVar = node.getSubjectVar();
        final Var objVar = node.getObjectVar();
        final ZeroLengthPath reflexiveSolution = new ZeroLengthPath(subjVar, objVar);
        node.replaceWith(new InferUnion(originalSP, reflexiveSolution));
    }
}
 
Example #5
Source File: ReflexivePropertyVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testReflexiveProperty() throws Exception {
    // Define a reflexive property
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    when(inferenceEngine.isReflexiveProperty(HAS_FAMILY)).thenReturn(true);
    // Construct a query, then visit it
    final StatementPattern sp = new StatementPattern(new Var("s", ALICE), new Var("p", HAS_FAMILY), new Var("o"));
    final Projection query = new Projection(sp, new ProjectionElemList(new ProjectionElem("o", "member")));
    query.visit(new ReflexivePropertyVisitor(conf, inferenceEngine));
    // Expected structure after rewriting SP(:Alice :hasFamilyMember ?member):
    //
    // Union(
    //     originalSP(:Alice :hasFamilyMember ?member),
    //     ZeroLengthPath(:Alice, ?member)
    // )
    Assert.assertTrue(query.getArg() instanceof Union);
    final TupleExpr left = ((Union) query.getArg()).getLeftArg();
    final TupleExpr right = ((Union) query.getArg()).getRightArg();
    Assert.assertEquals(sp, left);
    Assert.assertTrue(right instanceof ZeroLengthPath);
    Assert.assertEquals(sp.getSubjectVar(), ((ZeroLengthPath) right).getSubjectVar());
    Assert.assertEquals(sp.getObjectVar(), ((ZeroLengthPath) right).getObjectVar());
}
 
Example #6
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings)
		throws QueryEvaluationException {

	CloseableIteration<BindingSet, QueryEvaluationException> ret;

	if (expr instanceof StatementPattern) {
		ret = evaluate((StatementPattern) expr, bindings);
	} else if (expr instanceof UnaryTupleOperator) {
		ret = evaluate((UnaryTupleOperator) expr, bindings);
	} else if (expr instanceof BinaryTupleOperator) {
		ret = evaluate((BinaryTupleOperator) expr, bindings);
	} else if (expr instanceof SingletonSet) {
		ret = evaluate((SingletonSet) expr, bindings);
	} else if (expr instanceof EmptySet) {
		ret = evaluate((EmptySet) expr, bindings);
	} else if (expr instanceof ExternalSet) {
		ret = evaluate((ExternalSet) expr, bindings);
	} else if (expr instanceof ZeroLengthPath) {
		ret = evaluate((ZeroLengthPath) expr, bindings);
	} else if (expr instanceof ArbitraryLengthPath) {
		ret = evaluate((ArbitraryLengthPath) expr, bindings);
	} else if (expr instanceof BindingSetAssignment) {
		ret = evaluate((BindingSetAssignment) expr, bindings);
	} else if (expr instanceof TripleRef) {
		ret = evaluate((TripleRef) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass());
	}

	if (trackTime) {
		// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
		expr.setTotalTimeNanosActual(Math.max(0, expr.getTotalTimeNanosActual()));
		ret = new TimedIterator(ret, expr);
	}

	if (trackResultSize) {
		// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
		expr.setResultSizeActual(Math.max(0, expr.getResultSizeActual()));
		ret = new ResultSizeCountingIterator(ret, expr);
	}

	return ret;
}
 
Example #7
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(ZeroLengthPath node) throws X {
	meetNode(node);
}
 
Example #8
Source File: HalyardEvaluationStatistics.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(ZeroLengthPath node) {
    super.meet(node);
    updateMap(node);
}