Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-7475 Proposed feature for ['key'] accessor #5333

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,18 @@ public PrototypePatternDef expr(PrototypeExpression left, ConstraintOperator ope
}

Prototype prototype = getPrototype();
Function1<PrototypeFact, Object> leftExtractor;
Function1<PrototypeFact, Object> leftExtractor = left.asFunction(prototype);
AlphaIndex alphaIndex = null;
if (left instanceof PrototypeExpression.PrototypeFieldValue && right instanceof PrototypeExpression.FixedValue && operator instanceof Index.ConstraintType) {
String fieldName = ((PrototypeExpression.PrototypeFieldValue) left).getFieldName();
if (left.getIndexingKey().isPresent() && right instanceof PrototypeExpression.FixedValue && operator instanceof Index.ConstraintType) {
String fieldName = left.getIndexingKey().get();
Index.ConstraintType constraintType = (Index.ConstraintType) operator;
Prototype.Field field = prototype.getField(fieldName);
Object value = ((PrototypeExpression.FixedValue) right).getValue();
leftExtractor = getFieldValueExtractor(prototype, fieldName);

Class<Object> fieldClass = (Class<Object>) (field != null && field.isTyped() ? field.getType() : value != null ? value.getClass() : null);
if (fieldClass != null) {
alphaIndex = alphaIndexedBy(fieldClass, constraintType, getFieldIndex(prototype, fieldName, field), leftExtractor, value);
}
} else {
leftExtractor = left.asFunction(prototype);
}

Set<String> reactOnFields = new HashSet<>();
Expand Down Expand Up @@ -186,14 +183,12 @@ other, asPredicate2(left.asFunction(prototype), operator, right.asFunction(other
}

private BetaIndex createBetaIndex(PrototypeExpression left, ConstraintOperator operator, PrototypeExpression right, Prototype prototype, Prototype otherPrototype) {
if (left instanceof PrototypeExpression.PrototypeFieldValue && operator instanceof Index.ConstraintType && right instanceof PrototypeExpression.PrototypeFieldValue) {
String fieldName = ((PrototypeExpression.PrototypeFieldValue) left).getFieldName();
if (left.getIndexingKey().isPresent() && operator instanceof Index.ConstraintType && right.getIndexingKey().isPresent()) {
String fieldName = left.getIndexingKey().get();
Index.ConstraintType constraintType = (Index.ConstraintType) operator;
Prototype.Field field = prototype.getField(fieldName);
Function1<PrototypeFact, Object> extractor = getFieldValueExtractor(prototype, fieldName);

String otherFieldName = ((PrototypeExpression.PrototypeFieldValue) right).getFieldName();
Function1<PrototypeFact, Object> otherExtractor = getFieldValueExtractor(otherPrototype, otherFieldName);
Function1<PrototypeFact, Object> extractor = left.asFunction(prototype);
Function1<PrototypeFact, Object> otherExtractor = right.asFunction(otherPrototype);
Comment on lines -193 to +191
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mariofusco
I see you did this change, are you sure the two versions of the code are the same? getFieldValueExtractor uses fieldName while asFunction not so it is not clear to me if they are equivalent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested that they're equivalent, or in other words I ran the whole ansible integration test suite with this change only and everything was ok. In all honesty I tried to remember why I developed and went through that getFieldValueExtractor method instead of using asFunction directly but I couldn't :(


Class<Object> fieldClass = (Class<Object>) (field != null && field.isTyped() ? field.getType() : Object.class);
return betaIndexedBy( fieldClass, constraintType, getFieldIndex(prototype, fieldName, field), extractor, otherExtractor );
Expand Down Expand Up @@ -272,10 +267,6 @@ private PrototypeVariable[] findRightPrototypeVariables(PrototypeExpression righ
private static boolean evaluateConstraint(Object leftValue, ConstraintOperator operator, Object rightValue) {
return leftValue != Prototype.UNDEFINED_VALUE && rightValue != Prototype.UNDEFINED_VALUE && operator.asPredicate().test(leftValue, rightValue);
}

private Function1<PrototypeFact, Object> getFieldValueExtractor(Prototype prototype, String fieldName) {
return prototype.getFieldValueExtractor(fieldName)::apply;
}
}

public static class PrototypeSubPatternDefImpl<T> extends PrototypePatternDefImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.drools.model.functions.Function1;
Expand All @@ -35,6 +36,13 @@ interface EvaluableExpression {
Function1<PrototypeFact, Object> asFunction(Prototype prototype);

Collection<String> getImpactedFields();

/**
* if indexable, return a key for alpha/beta indexing
*/
default Optional<String> getIndexingKey() {
return Optional.empty();
}

static PrototypeExpression fixedValue(Object value) {
return new FixedValue(value);
Expand Down Expand Up @@ -150,8 +158,9 @@ public Function1<PrototypeFact, Object> asFunction(Prototype prototype) {
return prototype.getFieldValueExtractor(fieldName)::apply;
}

public String getFieldName() {
return fieldName;
@Override
public Optional<String> getIndexingKey() {
return Optional.of(fieldName);
}

@Override
Expand Down Expand Up @@ -189,12 +198,12 @@ public Collection<PrototypeVariable> getPrototypeVariables() {

@Override
public Object evaluate(Map<PrototypeVariable, PrototypeFact> factsMap) {
return protoVar.getPrototype().getFieldValueExtractor(getFieldName()).apply(factsMap.get(protoVar));
return protoVar.getPrototype().getFieldValueExtractor(getIndexingKey().get()).apply(factsMap.get(protoVar));
}

@Override
public String toString() {
return "PrototypeFieldValue{" + getFieldName() + " on " + protoVar + "}";
return "PrototypeFieldValue{" + getIndexingKey() + " on " + protoVar + "}";
}
}

Expand Down