DescendantsByBusinessObjectRelativeReferencesQuery.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 *
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 *
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 *
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.ge.internal.query;

import java.util.Deque;
import java.util.Objects;
import java.util.function.Function;

import org.osate.ge.BusinessObjectContext;
import org.osate.ge.RelativeBusinessObjectReference;
import org.osate.ge.internal.services.ReferenceService;

public class DescendantsByBusinessObjectRelativeReferencesQuery<T> extends DefaultQuery<T> {
	private final static RelativeBusinessObjectReference[] nullBoRefs = new RelativeBusinessObjectReference[0];
	private final Function<T, Object[]> bosSupplier;
	private final int minSegments;

	private static class Match {
		BusinessObjectContext value;
		int depth = -1;
	}

	public DescendantsByBusinessObjectRelativeReferencesQuery(final DefaultQuery<T> prev,
			final Function<T, Object[]> bosSupplier) {
		this(prev, bosSupplier, -1);
	}

	public DescendantsByBusinessObjectRelativeReferencesQuery(final DefaultQuery<T> prev,
			final Function<T, Object[]> bosSupplier, final int minSegments) {
		super(prev);
		this.bosSupplier = Objects.requireNonNull(bosSupplier, "bosSupplier must not be null");
		this.minSegments = minSegments;
	}

	@Override
	void run(final Deque<DefaultQuery<T>> remainingQueries, final BusinessObjectContext ctx,
			final QueryExecutionState<T> state, final QueryResults result) {
		// Look in the cache for the reference and build a new reference string if it is not found
		RelativeBusinessObjectReference[] boRefs = (RelativeBusinessObjectReference[]) state.cache.computeIfAbsent(this,
				t -> {
					final Object[] bos = bosSupplier.apply(state.arg);
					RelativeBusinessObjectReference[] newValue = bos == null ? nullBoRefs
							: getReferences(bos, state.refBuilder);
					if (newValue == null) {
						newValue = nullBoRefs;
					}
					return newValue;
				});

		if (boRefs == nullBoRefs) {
			return;
		}

		final Match bestMatch = new Match();
		findMatchingDescendants(remainingQueries, ctx, state, result, boRefs, bestMatch, 0);

		// Return a partial match if a result has not been processed and a partial match was found
		if (!result.isDone() && allowPartialMatch() && bestMatch.depth >= minSegments
				&& bestMatch.depth < boRefs.length) {
			state.partial = true;
			processResultValue(remainingQueries, bestMatch.value, state, result);
		}
	}

	private boolean allowPartialMatch() {
		return minSegments > 0;
	}

	void findMatchingDescendants(final Deque<DefaultQuery<T>> remainingQueries, final BusinessObjectContext container,
			final QueryExecutionState<T> state, final QueryResults result,
			final RelativeBusinessObjectReference[] boRefs, final Match bestMatch, final int currentDepth) {
		if (currentDepth > bestMatch.depth) {
			bestMatch.value = container;
			bestMatch.depth = currentDepth;
		}

		if (currentDepth >= boRefs.length) {
			processResultValue(remainingQueries, container, state, result);
		} else {
			final RelativeBusinessObjectReference boRef = boRefs[currentDepth];
			for (final BusinessObjectContext child : container.getChildren()) {
				// Check the business object reference
				final RelativeBusinessObjectReference childRef = InternalQueryUtil.getRelativeReference(child,
						state.refBuilder);
				if (boRef.equals(childRef)) {
					findMatchingDescendants(remainingQueries, child, state, result, boRefs, bestMatch,
							currentDepth + 1);
				}

				if (result.isDone()) {
					return;
				}
			}
		}
	}

	/**
	 * Returns an array of references for an array of business objects.
	 * @param bos
	 * @param refBuilder
	 * @return an array of references. null if any of the business objects or their references are null.
	 */
	private static RelativeBusinessObjectReference[] getReferences(final Object[] bos,
			final ReferenceService refBuilder) {
		if (bos == null) {
			return null;
		}

		final RelativeBusinessObjectReference[] references = new RelativeBusinessObjectReference[bos.length];
		for (int i = 0; i < bos.length; i++) {
			final Object bo = bos[i];
			if (bo == null) {
				return null;
			}

			final RelativeBusinessObjectReference ref = refBuilder.getRelativeReference(bo);
			if (ref == null) {
				return null;
			}

			references[i] = ref;
		}

		return references;
	}
}