Skip to content

Commit

Permalink
[1.0.x] Align drools 9.101.x prod (#116)
Browse files Browse the repository at this point in the history
* Bump to 1.0.7-SNAPSHOT

* [KIE-792] refactor prototypes in their own module (#97)

* [KIE-792] refactor prototypes in their own module

* use new prototype API

* get rid of fact templates

* Bump drools 8.43.0.Final to 9.101.0

---------

Co-authored-by: Mario Fusco <[email protected]>
  • Loading branch information
tkobayas and mariofusco authored Oct 6, 2024
1 parent c27f4f1 commit ad8e327
Show file tree
Hide file tree
Showing 53 changed files with 334 additions and 338 deletions.
2 changes: 1 addition & 1 deletion drools-ansible-rulebook-integration-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>drools-ansible-rulebook-integration</artifactId>
<groupId>org.drools</groupId>
<version>1.0.6-SNAPSHOT</version>
<version>1.0.7-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.drools.ansible.rulebook.integration.api.rulesengine.RulesEvaluator;
import org.drools.ansible.rulebook.integration.api.rulesengine.RulesExecutorSession;
import org.drools.ansible.rulebook.integration.api.rulesengine.SessionStats;
import org.drools.base.facttemplates.Fact;
import org.kie.api.prototype.PrototypeFactInstance;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.Match;
import org.slf4j.Logger;
Expand Down Expand Up @@ -90,7 +90,7 @@ public Collection<?> getAllFacts() {
}

public List<Map<String, Object>> getAllFactsAsMap() {
return getAllFacts().stream().map(Fact.class::cast).map(Fact::asMap).collect(Collectors.toList());
return getAllFacts().stream().map(PrototypeFactInstance.class::cast).map(PrototypeFactInstance::asMap).collect(Collectors.toList());
}

public String getAllFactsAsJson() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package org.drools.ansible.rulebook.integration.api.domain;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import org.drools.ansible.rulebook.integration.api.RuleConfigurationOption;
import org.drools.ansible.rulebook.integration.api.RuleConfigurationOptions;
import org.drools.ansible.rulebook.integration.api.domain.actions.Action;
import org.drools.ansible.rulebook.integration.api.domain.conditions.Condition;
import org.drools.ansible.rulebook.integration.api.domain.temporal.TimeConstraint;
import org.drools.ansible.rulebook.integration.api.rulesengine.RulesExecutionController;
import org.drools.model.Drools;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeVariable;
import org.drools.model.Rule;
import org.drools.model.RuleItemBuilder;
import org.drools.model.Variable;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeVariable;
import org.drools.model.view.ViewItem;

import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.getPrototype;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.getPrototypeEvent;
import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.getPrototypeFact;
import static org.drools.model.DSL.execute;
import static org.drools.model.DSL.on;
import static org.drools.model.PatternDSL.rule;
import static org.drools.model.PrototypeDSL.protoPattern;
import static org.drools.model.PrototypeDSL.variable;
import static org.drools.model.prototype.PrototypeDSL.protoPattern;
import static org.drools.model.prototype.PrototypeDSL.variable;

public class RuleGenerationContext {

Expand Down Expand Up @@ -72,8 +73,8 @@ public void addOptions(Iterable<RuleConfigurationOption> options) {
this.options.addOptions(options);
}

public PrototypeDSL.PrototypePatternDef getOrCreatePattern(String binding, String name) {
return patterns.computeIfAbsent(binding, b -> protoPattern(variable(getPrototype(name), b)));
public PrototypeDSL.PrototypePatternDef getOrCreatePattern(String binding, String name, boolean event) {
return patterns.computeIfAbsent(binding, b -> protoPattern(variable(event ? getPrototypeEvent(name) : getPrototypeFact(name), b)));
}

public PrototypeVariable getPatternVariable(String binding) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.drools.ansible.rulebook.integration.api.domain.conditions;

import java.util.ArrayList;
import java.util.List;

import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.api.domain.temporal.OnceWithinDefinition;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.model.Index;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeExpression;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.view.CombinedExprViewItem;
import org.drools.model.view.ViewItem;

import java.util.ArrayList;
import java.util.List;

import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.DEFAULT_PROTOTYPE_NAME;

public class AstCondition implements Condition {
Expand Down Expand Up @@ -145,7 +145,7 @@ protected PrototypeDSL.PrototypePatternDef getPattern(RuleGenerationContext rule

protected PrototypeDSL.PrototypePatternDef getPattern(RuleGenerationContext ruleContext, String binding) {
if (pattern == null) {
pattern = ruleContext.getOrCreatePattern(binding, DEFAULT_PROTOTYPE_NAME);
pattern = ruleContext.getOrCreatePattern(binding, DEFAULT_PROTOTYPE_NAME, ruleContext.getTimeConstraint().isPresent());
}
return pattern;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ SingleCondition withPatternBinding(RuleGenerationContext ruleContext, String pat
if (parent != null) {
parent.beforeBinding();
}
this.pattern = ruleContext.getOrCreatePattern(patternBinding, DEFAULT_PROTOTYPE_NAME);
this.pattern = ruleContext.getOrCreatePattern(patternBinding, DEFAULT_PROTOTYPE_NAME, ruleContext.getTimeConstraint().isPresent());
if (parent != null) {
parent.afterBinding();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.drools.ansible.rulebook.integration.api.domain.conditions;

import org.drools.model.PrototypeDSL;
import org.drools.model.view.ViewItem;
import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.model.view.ViewItem;

public interface Condition {
ViewItem toPattern(RuleGenerationContext ruleContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package org.drools.ansible.rulebook.integration.api.domain.conditions;

import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpressionUtils;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeVariable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpression;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpressionUtils;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeVariable;

import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.isEventOrFact;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.isKnownType;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.toJsonValue;
import static org.drools.model.PrototypeDSL.fieldName2PrototypeExpression;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.PrototypeExpression.prototypeField;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;

public class ConditionExpression {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.drools.ansible.rulebook.integration.api.domain.conditions;

import org.drools.ansible.rulebook.integration.api.io.JsonMapper;
import org.kie.api.prototype.Prototype;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.drools.ansible.rulebook.integration.api.io.JsonMapper;
import org.drools.model.Prototype;

public class ConditionParseUtil {

private static final Set<String> KNOWN_TYPES = Set.of("Integer", "Float", "String", "Boolean", "NullType");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression.map2Expr;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression.mapEntry2Expr;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;

public class MapCondition implements Condition {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.drools.ansible.rulebook.integration.api.domain.conditions;

import java.util.Arrays;
import java.util.List;

import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.model.Index;
import org.drools.model.PatternDSL;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeVariable;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeVariable;
import org.drools.model.view.CombinedExprViewItem;
import org.drools.model.view.ViewItem;
import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;

import java.util.Arrays;
import java.util.List;

import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.DEFAULT_PROTOTYPE_NAME;

Expand Down Expand Up @@ -212,7 +212,7 @@ private static ViewItem scopingCondition2Pattern(RuleGenerationContext ruleConte

private static ViewItem singleCondition2Pattern(RuleGenerationContext ruleContext, SimpleCondition condition) {
ParsedCondition parsedCondition = condition.parse();
var pattern = ruleContext.getOrCreatePattern(condition.getPatternBinding(ruleContext), DEFAULT_PROTOTYPE_NAME);
var pattern = ruleContext.getOrCreatePattern(condition.getPatternBinding(ruleContext), DEFAULT_PROTOTYPE_NAME, ruleContext.getTimeConstraint().isPresent());
if (condition.beta()) {
pattern.expr(parsedCondition.getLeft(), parsedCondition.getOperator(), ruleContext.getPatternVariable(condition.otherBinding()), parsedCondition.getRight());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.model.Prototype;
import org.kie.api.prototype.Prototype;

import java.util.Map;
import java.util.function.BiPredicate;

import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression.map2Expr;
import static org.drools.model.PrototypeExpression.thisPrototype;
import static org.drools.model.prototype.PrototypeExpression.thisPrototype;

public enum ExistsField implements RulebookOperator, ConditionFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.model.functions.Function1;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeVariable;
import org.drools.model.Prototype;
import org.drools.model.PrototypeFact;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeVariable;
import org.kie.api.prototype.Prototype;
import org.kie.api.prototype.PrototypeFactInstance;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiPredicate;

import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression.map2Expr;
import static org.drools.model.PrototypeExpression.thisPrototype;
import static org.drools.model.prototype.PrototypeExpression.thisPrototype;

public enum NegatedExistsField implements RulebookOperator, ConditionFactory {

Expand Down Expand Up @@ -49,7 +49,7 @@ private PrototypeExpressionWithAdmittedUndefined(PrototypeExpression delegate) {
}

@Override
public Function1<PrototypeFact, Object> asFunction(Prototype prototype) {
public Function1<PrototypeFactInstance, Object> asFunction(Prototype prototype) {
return delegate.asFunction(prototype).andThen( result -> result == Prototype.UNDEFINED_VALUE ? ADMITTED_UNDEFINED_VALUE : result );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression.map2Expr;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.isRegexOperator;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;

public enum SearchMatchesConstraint implements RulebookOperator, ConditionFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionExpression;
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.model.ConstraintOperator;
import org.drools.model.Prototype;
import org.kie.api.prototype.Prototype;

import java.util.Collection;
import java.util.Map;
Expand All @@ -16,7 +16,7 @@
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.mapToStringValue;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.toJsonValue;
import static org.drools.ansible.rulebook.integration.api.domain.constraints.Operators.toOperatorPredicate;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;

public enum SelectAttrConstraint implements RulebookOperator, ConditionFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpressionUtils;
import org.drools.model.ConstraintOperator;
import org.drools.model.Prototype;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeVariable;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeVariable;
import org.kie.api.prototype.Prototype;

import java.util.Collection;
import java.util.Map;
Expand All @@ -19,7 +19,7 @@
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.mapToStringValue;
import static org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil.toJsonValue;
import static org.drools.ansible.rulebook.integration.api.domain.constraints.Operators.toOperatorPredicate;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;

public enum SelectConstraint implements RulebookOperator, ConditionFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
import org.drools.ansible.rulebook.integration.protoextractor.ExtractorUtils;
import org.drools.ansible.rulebook.integration.protoextractor.ast.ExtractorNode;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpression;
import org.drools.ansible.rulebook.integration.protoextractor.prototype.ExtractorPrototypeExpressionUtils;
import org.drools.base.facttemplates.Fact;
import org.drools.model.Index;
import org.drools.model.PrototypeDSL;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeFact;
import org.drools.model.PrototypeVariable;
import org.drools.model.prototype.PrototypeDSL;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeVariable;
import org.kie.api.prototype.PrototypeFactInstance;

import java.util.List;

import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.SYNTHETIC_PROTOTYPE_NAME;
import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.getPrototype;
import static org.drools.model.PrototypeDSL.protoPattern;
import static org.drools.model.PrototypeDSL.variable;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.PrototypeExpression.prototypeField;
import static org.drools.ansible.rulebook.integration.api.rulesmodel.PrototypeFactory.getPrototypeEvent;
import static org.drools.model.prototype.PrototypeDSL.protoPattern;
import static org.drools.model.prototype.PrototypeDSL.variable;
import static org.drools.model.prototype.PrototypeExpression.prototypeField;

public abstract class OnceAbstractTimeConstraint implements TimeConstraint {

Expand All @@ -46,7 +43,7 @@ protected PrototypeVariable getControlVariable() {
}

protected PrototypeDSL.PrototypePatternDef createControlPattern() {
PrototypeDSL.PrototypePatternDef controlPattern = protoPattern(variable(getPrototype(SYNTHETIC_PROTOTYPE_NAME)));
PrototypeDSL.PrototypePatternDef controlPattern = protoPattern(variable(getPrototypeEvent(SYNTHETIC_PROTOTYPE_NAME)));
for (GroupByAttribute unique : groupByAttributes) {
controlPattern.expr( prototypeField(unique.getKey()), // intentional, the control fact has the "group by" key string as-is (not structured), so we reference it for the left part
Index.ConstraintType.EQUAL,
Expand Down Expand Up @@ -95,11 +92,7 @@ public PrototypeExpression asPrototypeExpression() {
return new ExtractorPrototypeExpression(extractor);
}

public Object evalExtractorOnFact(PrototypeFact fact) {
return evalExtractorOnFact((Fact) fact);
}

public Object evalExtractorOnFact(Fact fact) {
public Object evalExtractorOnFact(PrototypeFactInstance fact) {
return ExtractorUtils.getValueFrom(extractor, fact.asMap());
}

Expand Down
Loading

0 comments on commit ad8e327

Please sign in to comment.