Search in sources :

Example 1 with Predicate2

use of org.drools.model.functions.Predicate2 in project drools by kiegroup.

the class ToStringTest method testToString.

/**
 * Users may depend on seeing {@link Rule#toString()} in log files giving useful information, in order to understand
 * the rules that are being created. The format is not required to be backwards compatible - this test merely checks
 * that it does not change unknowingly.
 */
@Test
public void testToString() {
    Variable<Person> markV = declarationOf(Person.class);
    Variable<Integer> markAge = declarationOf(Integer.class);
    Variable<Person> olderV = declarationOf(Person.class);
    Variable<Double> resultAvg = declarationOf(Double.class);
    Variable<Integer> age = declarationOf(Integer.class);
    String person = "Mark";
    Function1<Person, String> nameGetter = Person::getName;
    Function1<Person, Integer> ageGetter = Person::getAge;
    Predicate1<Person> markPredicate = p -> p.getName().equals(person);
    PatternDSL.PatternDef<Person> pattern1 = pattern(markV).expr("exprA", markPredicate, alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, nameGetter, person)).bind(markAge, ageGetter);
    Predicate1<Person> notMarkPredicate = markPredicate.negate();
    Predicate2<Person, Integer> agePredicate = (p1, someAge) -> p1.getAge() > someAge;
    Function1<Integer, Integer> ageCaster = int.class::cast;
    PatternDSL.PatternDef<Person> pattern2 = pattern(olderV).expr("exprB", notMarkPredicate, alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, nameGetter, person)).expr("exprC", markAge, agePredicate, betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, ageGetter, ageCaster));
    AccumulateFunction<AverageAccumulateFunction.AverageData> f = new AverageAccumulateFunction();
    Supplier<AccumulateFunction<AverageAccumulateFunction.AverageData>> accumulateSupplier = () -> f;
    org.drools.model.functions.accumulate.AccumulateFunction actualAccumulate = accFunction(accumulateSupplier, age);
    ExprViewItem<Person> accumulate = accumulate(pattern(olderV).expr("exprD", notMarkPredicate).bind(age, ageGetter), actualAccumulate.as(resultAvg));
    Rule rule = rule("beta").build(pattern1, pattern2, accumulate, on(olderV, markV).execute((drools, p1, p2) -> drools.insert(p1.getName() + " is older than " + p2.getName())));
    String pattern1toString = "PatternImpl (type: PATTERN, inputVars: null, " + "outputVar: " + markV + ", " + "constraint: Constraint for 'exprA' (index: AlphaIndex #0 (EQUAL, left: lambda " + System.identityHashCode(nameGetter) + ", right: " + person + ")))";
    String pattern2toString = "PatternImpl (type: PATTERN, inputVars: null, " + "outputVar: " + olderV + ", " + "constraint: MultipleConstraints (constraints: [" + "Constraint for 'exprB' (index: AlphaIndex #1 (NOT_EQUAL, left: lambda " + System.identityHashCode(nameGetter) + ", right: " + person + ")), " + "Constraint for 'exprC' (index: BetaIndex #0 (GREATER_THAN, left: lambda " + System.identityHashCode(ageGetter) + ", right: lambda " + System.identityHashCode(ageCaster) + "))]))";
    String accumulatePatternToString = "PatternImpl (type: PATTERN, inputVars: null, " + "outputVar: " + olderV + ", " + "constraint: Constraint for 'exprD' (index: null))";
    String accumulateToString = "AccumulatePatternImpl (functions: [" + actualAccumulate + "], " + "condition: " + accumulatePatternToString + ", " + "pattern: " + accumulatePatternToString + ")";
    String consequenceToString = "ConsequenceImpl (variables: [" + olderV + ", " + markV + "], language: java, breaking: false)";
    String expectedToString = "Rule: defaultpkg.beta (" + "view: CompositePatterns of AND (vars: null, patterns: [" + pattern1toString + ", " + pattern2toString + ", " + accumulateToString + ", NamedConsequenceImpl 'default' (breaking: false)], " + "consequences: {default=" + consequenceToString + "}), consequences: {default=" + consequenceToString + "})";
    Assertions.assertThat(rule).hasToString(expectedToString);
}
Also used : PatternDSL.rule(org.drools.model.PatternDSL.rule) Variable(org.drools.model.Variable) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) PatternDSL(org.drools.model.PatternDSL) Test(org.junit.Test) DSL.on(org.drools.model.DSL.on) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) Supplier(java.util.function.Supplier) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) Predicate2(org.drools.model.functions.Predicate2) DSL.accumulate(org.drools.model.DSL.accumulate) Function1(org.drools.model.functions.Function1) DSL.accFunction(org.drools.model.DSL.accFunction) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) Rule(org.drools.model.Rule) Assertions(org.assertj.core.api.Assertions) ExprViewItem(org.drools.model.view.ExprViewItem) Person(org.drools.modelcompiler.domain.Person) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) DSL.declarationOf(org.drools.model.DSL.declarationOf) DSL.accumulate(org.drools.model.DSL.accumulate) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) PatternDSL(org.drools.model.PatternDSL) Rule(org.drools.model.Rule) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

Supplier (java.util.function.Supplier)1 Assertions (org.assertj.core.api.Assertions)1 AverageAccumulateFunction (org.drools.core.base.accumulators.AverageAccumulateFunction)1 DSL.accFunction (org.drools.model.DSL.accFunction)1 DSL.accumulate (org.drools.model.DSL.accumulate)1 DSL.declarationOf (org.drools.model.DSL.declarationOf)1 DSL.on (org.drools.model.DSL.on)1 Index (org.drools.model.Index)1 PatternDSL (org.drools.model.PatternDSL)1 PatternDSL.alphaIndexedBy (org.drools.model.PatternDSL.alphaIndexedBy)1 PatternDSL.betaIndexedBy (org.drools.model.PatternDSL.betaIndexedBy)1 PatternDSL.pattern (org.drools.model.PatternDSL.pattern)1 PatternDSL.rule (org.drools.model.PatternDSL.rule)1 Rule (org.drools.model.Rule)1 Variable (org.drools.model.Variable)1 Function1 (org.drools.model.functions.Function1)1 Predicate1 (org.drools.model.functions.Predicate1)1 Predicate2 (org.drools.model.functions.Predicate2)1 ExprViewItem (org.drools.model.view.ExprViewItem)1 Person (org.drools.modelcompiler.domain.Person)1