According to the Drools v7.46.0.Final documentation for the null-safe dereferencing operator !., expressions of the form
Type( $data : property!.data )
are internally expanded/represented as:
Type( property != null, $data : property.data )
However, this does not seem to work in the context of OptaPlanner.
For the rule
Type( $data : property!.data )
the generated code is
package some.classes;
import static location.RulesAHASH1.*;
import org.optaplanner.core.api.score.buildin.bendablelong.BendableLongScoreHolder;
import org.drools.modelcompiler.dsl.pattern.D;
// other imports
@org.drools.compiler.kie.builder.MaterializedLambda()
public enum LambdaExtractorAHASH2 implements org.drools.model.functions.Function1<Type, java.lang.Long> {
INSTANCE;
public static final String EXPRESSION_HASH = "SOME_HASH";
@Override()
public java.lang.Long apply(Type _this) {
return _this.getProperty() != null && _this.getProperty().getData();
}
}
and given that data is not a boolean and apply has a return type of Long, this throws an exception.
The message:
java.lang.IllegalStateException: There are errors in a score DRL:
Error Messages:
Message [id=1, level=ERROR, path=blah/blah/blah/LambdaExtractorHASH.java, line=24, column=54
text=bad operand types for binary operator '&&'
first type: boolean
second type: java.lang.Long]
...
// generated code etc.
So, are we not supposed to use this notation when using Drools with OptaPlanner? Are there other language limitations?
Update:
Pulled the optaplanner repo, switched to 8.8.x, and modified a vehicleRoutingConstriants DRL rule to use the null-safe dereference operator:
rule "distanceToPreviousStandstill"
when
$customer : Customer($prevVehicle : previousStandstill!.vehicle, previousStandstill != null, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
then
System.out.println($prevVehicle);
scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end
This runs as expected.