
org.optaplanner.constraint.streams.drools.common.DetachedPatternVariable Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.optaplanner.constraint.streams.drools.common;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import org.drools.model.Variable;
import org.drools.model.view.ViewItem;
import org.optaplanner.constraint.streams.common.bi.DefaultBiJoiner;
import org.optaplanner.constraint.streams.common.quad.DefaultQuadJoiner;
import org.optaplanner.constraint.streams.common.tri.DefaultTriJoiner;
import org.optaplanner.core.api.function.QuadFunction;
import org.optaplanner.core.api.function.QuadPredicate;
import org.optaplanner.core.api.function.TriFunction;
import org.optaplanner.core.api.function.TriPredicate;
import org.optaplanner.core.impl.score.stream.JoinerType;
/**
* Represents a single variable that has no pattern on which to be bound.
* Such a variable is often the result of a Drools accumulate call.
*
*
* Consider the following simple univariate rule, in the equivalent DRL:
*
*
* {@code
* rule "Simple univariate rule"
* when
* accumulate(
* ...,
* $count: count()
* )
* $a: Something($b: someField, someOtherField > $count)
* then
* // Do something.
* end
* }
*
*
* In this rule, variable "a" would be represented by {@link DirectPatternVariable}.
* Variable "b" would be represented by {@link IndirectPatternVariable}.
* Variable "count" would be represented by this class and passed to the first variable's pattern's filter expression.
*
*
* Therefore most of its operations will throw {@link UnsupportedOperationException}, as you can not really do anything
* with the variable.
* It is only useful as an auxiliary variable in bindings or expressions on another {@link DirectPatternVariable} or
* {@link IndirectPatternVariable}.
*
*
* The pattern that produces the detached variable will be included in prerequisites for another variable.
* Therefore {@link #build()} of this class returns an empty result.
*
* @param generic type of the variable
* @param > generic type of the pattern variable, has no effect and exists only to satisfy the interface
*/
final class DetachedPatternVariable
implements PatternVariable> {
private final Variable primaryVariable;
DetachedPatternVariable(Variable variable) {
this.primaryVariable = Objects.requireNonNull(variable);
}
@Override
public Variable getPrimaryVariable() {
return primaryVariable;
}
@Override
public List> getPrerequisiteExpressions() {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public List> getDependentExpressions() {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable filter(Predicate predicate) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable filter(BiPredicate predicate,
Variable leftJoinVariable) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable filter(
TriPredicate predicate, Variable leftJoinVariableA,
Variable leftJoinVariableB) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable filter(
QuadPredicate predicate, Variable leftJoinVariableA,
Variable leftJoinVariableB, Variable leftJoinVariableC) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public PatternVariable> filterForJoin(
Variable leftJoinVar, DefaultBiJoiner joiner, JoinerType joinerType,
int mappingIndex) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public PatternVariable>
filterForJoin(Variable leftJoinVarA, Variable leftJoinVarB,
DefaultTriJoiner joiner, JoinerType joinerType, int mappingIndex) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public
PatternVariable>
filterForJoin(Variable leftJoinVarA, Variable leftJoinVarB,
Variable leftJoinVarC,
DefaultQuadJoiner joiner, JoinerType joinerType,
int mappingIndex) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable bind(Variable boundVariable,
Function bindingFunction) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable bind(Variable boundVariable,
Variable leftJoinVariable, BiFunction bindingFunction) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable bind(
Variable boundVariable, Variable leftJoinVariableA,
Variable leftJoinVariableB,
TriFunction bindingFunction) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable bind(
Variable boundVariable, Variable leftJoinVariableA,
Variable leftJoinVariableB, Variable leftJoinVariableC,
QuadFunction bindingFunction) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public DetachedPatternVariable addDependentExpression(ViewItem> expression) {
throw new UnsupportedOperationException("Impossible state: Variable (" + primaryVariable + ") is detached.");
}
@Override
public List> build() {
return Collections.emptyList(); // This variable has no related patterns.
}
}