Skip to content

Commit

Permalink
DROOLS-7475 Proposed feature for ['key'] accessor (#5333)
Browse files Browse the repository at this point in the history
* DROOLS-7475 Proposed feature for ['key'] accessor

see also kiegroup/drools-ansible-rulebook-integration#56 (comment)

make IndexableExpression interface so downstream can leverage this rather than specific concrete class

* fix PrototypeDSL

* implement code review feedback

---------

Co-authored-by: mariofusco <[email protected]>
  • Loading branch information
tarilabs and mariofusco authored Jun 22, 2023
1 parent 7c865cc commit ad19dc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
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);

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

0 comments on commit ad19dc5

Please sign in to comment.