|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.federated.evaluation; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Supplier; |
| 15 | + |
| 16 | +import org.eclipse.rdf4j.common.iteration.CloseableIteration; |
| 17 | +import org.eclipse.rdf4j.federated.algebra.FedXZeroLengthPath; |
| 18 | +import org.eclipse.rdf4j.federated.algebra.StatementSource; |
| 19 | +import org.eclipse.rdf4j.federated.evaluation.iterator.FedXZeroLengthPathIteration; |
| 20 | +import org.eclipse.rdf4j.federated.structures.QueryInfo; |
| 21 | +import org.eclipse.rdf4j.model.Value; |
| 22 | +import org.eclipse.rdf4j.query.BindingSet; |
| 23 | +import org.eclipse.rdf4j.query.QueryEvaluationException; |
| 24 | +import org.eclipse.rdf4j.query.algebra.Var; |
| 25 | +import org.eclipse.rdf4j.query.algebra.evaluation.EvaluationStrategy; |
| 26 | +import org.eclipse.rdf4j.query.algebra.evaluation.QueryEvaluationStep; |
| 27 | +import org.eclipse.rdf4j.query.algebra.evaluation.QueryValueEvaluationStep; |
| 28 | +import org.eclipse.rdf4j.query.algebra.evaluation.impl.QueryEvaluationContext; |
| 29 | +import org.eclipse.rdf4j.query.algebra.evaluation.impl.evaluationsteps.ZeroLengthPathEvaluationStep; |
| 30 | + |
| 31 | +/** |
| 32 | + * An evaluation step used for {@link FedXZeroLengthPath}. |
| 33 | + * |
| 34 | + * @see ZeroLengthPathEvaluationStep |
| 35 | + */ |
| 36 | +public final class FedXZeroLengthPathEvaluationStep implements QueryEvaluationStep { |
| 37 | + private final Var subjectVar; |
| 38 | + private final Var objVar; |
| 39 | + private final Var contextVar; |
| 40 | + private final QueryValueEvaluationStep subPrep; |
| 41 | + private final QueryValueEvaluationStep objPrep; |
| 42 | + private final EvaluationStrategy strategy; |
| 43 | + private final QueryEvaluationContext context; |
| 44 | + |
| 45 | + private final Supplier<List<StatementSource>> statementSources; |
| 46 | + private final Supplier<QueryInfo> queryInfo; |
| 47 | + |
| 48 | + public FedXZeroLengthPathEvaluationStep(Var subjectVar, Var objVar, Var contextVar, |
| 49 | + QueryValueEvaluationStep subPrep, |
| 50 | + QueryValueEvaluationStep objPrep, EvaluationStrategy strategy, QueryEvaluationContext context, |
| 51 | + Supplier<List<StatementSource>> statementSources, Supplier<QueryInfo> queryInfo) { |
| 52 | + this.subjectVar = subjectVar; |
| 53 | + this.objVar = objVar; |
| 54 | + this.contextVar = contextVar; |
| 55 | + this.subPrep = subPrep; |
| 56 | + this.objPrep = objPrep; |
| 57 | + this.strategy = strategy; |
| 58 | + this.context = context; |
| 59 | + |
| 60 | + this.statementSources = statementSources; |
| 61 | + this.queryInfo = queryInfo; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) { |
| 66 | + Value subj = null; |
| 67 | + try { |
| 68 | + subj = subPrep.evaluate(bindings); |
| 69 | + } catch (QueryEvaluationException ignored) { |
| 70 | + } |
| 71 | + |
| 72 | + Value obj = null; |
| 73 | + try { |
| 74 | + obj = objPrep.evaluate(bindings); |
| 75 | + } catch (QueryEvaluationException ignored) { |
| 76 | + } |
| 77 | + |
| 78 | + if (subj != null && obj != null) { |
| 79 | + if (!subj.equals(obj)) { |
| 80 | + return EMPTY_ITERATION; |
| 81 | + } |
| 82 | + } |
| 83 | + return getZeroLengthPathIterator(bindings, subjectVar, objVar, contextVar, subj, obj, context); |
| 84 | + } |
| 85 | + |
| 86 | + protected FedXZeroLengthPathIteration getZeroLengthPathIterator(final BindingSet bindings, final Var subjectVar, |
| 87 | + final Var objVar, final Var contextVar, Value subj, Value obj, QueryEvaluationContext context) { |
| 88 | + return new FedXZeroLengthPathIteration(strategy, subjectVar, objVar, subj, obj, contextVar, bindings, context, |
| 89 | + queryInfo.get(), statementSources.get()); |
| 90 | + } |
| 91 | +} |
0 commit comments