Search in sources :

Example 1 with EvaluatorConstraint

use of org.drools.core.rule.constraint.EvaluatorConstraint in project drools by kiegroup.

the class AlphaNode method calculateDeclaredMask.

public BitMask calculateDeclaredMask(Class modifiedClass, List<String> settableProperties) {
    boolean typeBit = false;
    if (constraint instanceof EvaluatorConstraint && ((EvaluatorConstraint) constraint).isSelf()) {
        Operator op = ((EvaluatorConstraint) constraint).getEvaluator().getOperator();
        if (op == IsAEvaluatorDefinition.ISA || op == IsAEvaluatorDefinition.NOT_ISA) {
            typeBit = true;
        }
    }
    BitMask mask = constraint.getListenedPropertyMask(modifiedClass, settableProperties);
    return typeBit ? mask.set(PropertySpecificUtil.TRAITABLE_BIT) : mask;
}
Also used : Operator(org.kie.api.runtime.rule.Operator) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) BitMask(org.drools.core.util.bitmask.BitMask)

Example 2 with EvaluatorConstraint

use of org.drools.core.rule.constraint.EvaluatorConstraint in project drools by kiegroup.

the class MVELConstraintBuilder method buildLiteralConstraint.

public Constraint buildLiteralConstraint(RuleBuildContext context, Pattern pattern, ValueType vtype, FieldValue field, String expression, String leftValue, String operator, String rightValue, InternalReadAccessor extractor, LiteralRestrictionDescr restrictionDescr, Map<String, OperatorDescr> aliases) {
    if (!isMvelOperator(operator)) {
        Evaluator evaluator = buildLiteralEvaluator(context, extractor, restrictionDescr, vtype);
        if (evaluator != null && evaluator.isTemporal()) {
            try {
                field = context.getCompilerFactory().getFieldFactory().getFieldValue(field.getValue(), ValueType.DATE_TYPE);
            } catch (Exception e) {
                context.addError(new DescrBuildError(context.getParentDescr(), restrictionDescr, null, e.getMessage()));
            }
        }
        return new EvaluatorConstraint(field, evaluator, extractor);
    }
    String mvelExpr = normalizeMVELLiteralExpression(vtype, field, expression, leftValue, operator, rightValue, restrictionDescr);
    IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operator);
    MVELCompilationUnit compilationUnit = buildCompilationUnit(context, pattern, mvelExpr, aliases);
    EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, restrictionDescr, aliases));
    return new MvelConstraint(context.getPkg().getName(), mvelExpr, compilationUnit, constraintType, field, extractor, operators);
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) IndexUtil(org.drools.core.util.index.IndexUtil) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Evaluator(org.drools.core.spi.Evaluator)

Example 3 with EvaluatorConstraint

use of org.drools.core.rule.constraint.EvaluatorConstraint in project drools by kiegroup.

the class MVELConstraintBuilder method buildVariableConstraint.

public Constraint buildVariableConstraint(RuleBuildContext context, Pattern pattern, String expression, Declaration[] declarations, String leftValue, OperatorDescr operatorDescr, String rightValue, InternalReadAccessor extractor, Declaration requiredDeclaration, RelationalExprDescr relDescr, Map<String, OperatorDescr> aliases) {
    if (!isMvelOperator(operatorDescr.getOperator())) {
        if (requiredDeclaration == null) {
            return null;
        }
        EvaluatorDefinition.Target right = getRightTarget(extractor);
        EvaluatorDefinition.Target left = (requiredDeclaration.isPatternDeclaration() && !(Date.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()) || Number.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()))) ? EvaluatorDefinition.Target.HANDLE : EvaluatorDefinition.Target.FACT;
        final Evaluator evaluator = getEvaluator(context, relDescr, extractor.getValueType(), operatorDescr.getOperator(), relDescr.isNegated(), relDescr.getParametersText(), left, right);
        return new EvaluatorConstraint(new Declaration[] { requiredDeclaration }, evaluator, extractor);
    }
    boolean isUnification = requiredDeclaration != null && requiredDeclaration.getPattern().getObjectType().equals(new ClassObjectType(DroolsQuery.class)) && Operator.EQUAL.getOperatorString().equals(operatorDescr.getOperator());
    if (isUnification && leftValue.equals(rightValue)) {
        expression = resolveUnificationAmbiguity(expression, declarations, leftValue, rightValue);
    }
    expression = normalizeMVELVariableExpression(expression, leftValue, rightValue, relDescr);
    IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operatorDescr.getOperator());
    MVELCompilationUnit compilationUnit = isUnification ? null : buildCompilationUnit(context, pattern, expression, aliases);
    EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, relDescr, aliases));
    return new MvelConstraint(Collections.singletonList(context.getPkg().getName()), expression, declarations, operators, compilationUnit, constraintType, requiredDeclaration, extractor, isUnification);
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) ClassObjectType(org.drools.core.base.ClassObjectType) IndexUtil(org.drools.core.util.index.IndexUtil) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Evaluator(org.drools.core.spi.Evaluator) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 4 with EvaluatorConstraint

use of org.drools.core.rule.constraint.EvaluatorConstraint in project drools by kiegroup.

the class BuildUtilsTest method testCalculateTemporalDistance.

/**
 * Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
 */
@Test
public void testCalculateTemporalDistance() {
    // input is here just for "documentation" purposes
    Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
    Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
    AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
    ClassObjectType ot = new ClassObjectType(StockTick.class, true);
    Pattern a = new Pattern(0, ot, "$a");
    Pattern b = new Pattern(1, ot, "$b");
    b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern c = new Pattern(2, ot, "$c");
    c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern d = new Pattern(3, ot, "$d");
    d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
    d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern e = new Pattern(4, ot, "$e");
    e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
    GroupElement not = new GroupElement(Type.NOT);
    not.addChild(e);
    GroupElement and = new GroupElement(Type.AND);
    and.addChild(a);
    and.addChild(b);
    and.addChild(c);
    and.addChild(d);
    and.addChild(not);
    TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
    // printMatrix( matrix.getMatrix() );
    assertEqualsMatrix(expected, matrix.getMatrix());
    assertEquals(15, matrix.getExpirationOffset(a));
    assertEquals(11, matrix.getExpirationOffset(d));
    assertEquals(1, matrix.getExpirationOffset(e));
}
Also used : Pattern(org.drools.core.rule.Pattern) AfterEvaluatorDefinition(org.drools.core.base.evaluators.AfterEvaluatorDefinition) ClassObjectType(org.drools.core.base.ClassObjectType) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) TemporalDependencyMatrix(org.drools.core.time.TemporalDependencyMatrix) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) GroupElement(org.drools.core.rule.GroupElement) Declaration(org.drools.core.rule.Declaration) Interval(org.drools.core.time.Interval) Test(org.junit.Test)

Aggregations

EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)4 ClassObjectType (org.drools.core.base.ClassObjectType)2 EvaluatorWrapper (org.drools.core.base.EvaluatorWrapper)2 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)2 Evaluator (org.drools.core.spi.Evaluator)2 IndexUtil (org.drools.core.util.index.IndexUtil)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 DroolsQuery (org.drools.core.base.DroolsQuery)1 AfterEvaluatorDefinition (org.drools.core.base.evaluators.AfterEvaluatorDefinition)1 EvaluatorDefinition (org.drools.core.base.evaluators.EvaluatorDefinition)1 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)1 Declaration (org.drools.core.rule.Declaration)1 GroupElement (org.drools.core.rule.GroupElement)1 Pattern (org.drools.core.rule.Pattern)1 Interval (org.drools.core.time.Interval)1 TemporalDependencyMatrix (org.drools.core.time.TemporalDependencyMatrix)1 BitMask (org.drools.core.util.bitmask.BitMask)1 Test (org.junit.Test)1 Operator (org.kie.api.runtime.rule.Operator)1