Search in sources :

Example 16 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class KnowledgeBuilderTest method testReturnValueMethodCompare.

@Test
public void testReturnValueMethodCompare() {
    final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr1 = new PackageDescr("package1");
    createReturnValueRule(packageDescr1, " x + y ");
    builder1.addPackage(packageDescr1);
    if (builder1.hasErrors()) {
        fail(builder1.getErrors().toString());
    }
    final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue1 = pattern1.getConstraints().get(0);
    final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr2 = new PackageDescr("package2");
    createReturnValueRule(packageDescr2, " x + y ");
    builder2.addPackage(packageDescr2);
    final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue2 = pattern2.getConstraints().get(0);
    final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr3 = new PackageDescr("package3");
    createReturnValueRule(packageDescr3, " x - y ");
    builder3.addPackage(packageDescr3);
    final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue3 = pattern3.getConstraints().get(0);
    assertEquals(returnValue1, returnValue2);
    assertFalse(returnValue1.equals(returnValue3));
    assertFalse(returnValue2.equals(returnValue3));
}
Also used : Pattern(org.drools.core.rule.Pattern) PredicateConstraint(org.drools.core.rule.PredicateConstraint) Constraint(org.drools.core.spi.Constraint) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 17 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class JavaAccumulateBuilder method bindReaderToDeclaration.

private void bindReaderToDeclaration(RuleBuildContext context, AccumulateDescr accumDescr, Pattern pattern, AccumulateFunctionCallDescr fc, InternalReadAccessor readAccessor, Class<?> resultType, int index) {
    if (fc.getBind() != null) {
        if (context.getDeclarationResolver().isDuplicated(context.getRule(), fc.getBind(), resultType.getName())) {
            if (!fc.isUnification()) {
                context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + fc.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
            } else {
                Declaration inner = context.getDeclarationResolver().getDeclaration(fc.getBind());
                Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), index >= 0 ? "this[ " + index + " ] == " + fc.getBind() : "this == " + fc.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(fc.getBind()), index >= 0 ? new ArrayElementReader(readAccessor, index, resultType) : readAccessor, true);
                ((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
                pattern.addConstraint(c);
            }
        } else {
            Declaration declr = pattern.addDeclaration(fc.getBind());
            declr.setReadAccessor(readAccessor);
        }
    }
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration)

Example 18 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class IndexUtil method compositeAllowed.

public static boolean compositeAllowed(BetaNodeFieldConstraint[] constraints, short betaNodeType) {
    // 1) If there is 1 or more unification restrictions it cannot be composite
    // 2) Ensures any non unification restrictions are first
    int firstUnification = -1;
    int firstNonUnification = -1;
    for (int i = 0, length = constraints.length; i < length; i++) {
        if (isIndexable(constraints[i], betaNodeType)) {
            final boolean isUnification = ((IndexableConstraint) constraints[i]).isUnification();
            if (isUnification && firstUnification == -1) {
                firstUnification = i;
            } else if (!isUnification && firstNonUnification == -1) {
                firstNonUnification = i;
            }
        }
        if (firstUnification != -1 && firstNonUnification != -1) {
            break;
        }
    }
    if (firstNonUnification != -1 && firstNonUnification > 0) {
        // Make sure a nonunification indexable constraint is first
        swap(constraints, 0, firstNonUnification);
    }
    return (firstUnification == -1);
}
Also used : IndexableConstraint(org.drools.core.rule.IndexableConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 19 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class PatternBuilder method createConstraints.

private Constraints createConstraints(BuildContext context, Pattern pattern) {
    Constraints constraints = new Constraints();
    // check if cross products for identity patterns should be disabled
    checkRemoveIdentities(context, pattern, constraints.betaConstraints);
    // checks if this pattern is nested inside a NOT CE
    final boolean isNegative = isNegative(context);
    for (Constraint constraint : pattern.getConstraints()) {
        switch(constraint.getType()) {
            case ALPHA:
                linkAlphaConstraint((AlphaNodeFieldConstraint) constraint, constraints.alphaConstraints);
                break;
            case BETA:
                linkBetaConstraint((BetaNodeFieldConstraint) constraint, constraints.betaConstraints);
                if (isNegative && context.getKnowledgeBase().getConfiguration().getEventProcessingMode() == EventProcessingOption.STREAM && pattern.getObjectType().isEvent() && constraint.isTemporal()) {
                    checkDelaying(context, constraint);
                }
                break;
            case XPATH:
                constraints.xpathConstraints.add((XpathConstraint) constraint);
                break;
            default:
                throw new RuntimeException("Unknown constraint type: " + constraint.getType() + ". This is a bug. Please contact development team.");
        }
    }
    return constraints;
}
Also used : InstanceNotEqualsConstraint(org.drools.core.common.InstanceNotEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint)

Example 20 with Constraint

use of org.drools.core.spi.Constraint in project drools by kiegroup.

the class LogicTransformer method processElement.

/**
 * recurse through the rule condition elements updating the declaration objecs
 */
private void processElement(final DeclarationScopeResolver resolver, final Stack<RuleConditionElement> contextStack, final RuleConditionElement element) {
    if (element instanceof Pattern) {
        Pattern pattern = (Pattern) element;
        for (RuleConditionElement ruleConditionElement : pattern.getNestedElements()) {
            processElement(resolver, contextStack, ruleConditionElement);
        }
        for (Constraint constraint : pattern.getConstraints()) {
            if (constraint instanceof Declaration) {
                continue;
            }
            replaceDeclarations(resolver, pattern, constraint);
        }
    } else if (element instanceof EvalCondition) {
        processEvalCondition(resolver, (EvalCondition) element);
    } else if (element instanceof Accumulate) {
        for (RuleConditionElement rce : element.getNestedElements()) {
            processElement(resolver, contextStack, rce);
        }
        Accumulate accumulate = (Accumulate) element;
        replaceDeclarations(resolver, accumulate);
    } else if (element instanceof From) {
        DataProvider provider = ((From) element).getDataProvider();
        Declaration[] decl = provider.getRequiredDeclarations();
        for (Declaration aDecl : decl) {
            Declaration resolved = resolver.getDeclaration(aDecl.getIdentifier());
            if (resolved != null && resolved != aDecl) {
                provider.replaceDeclaration(aDecl, resolved);
            } else if (resolved == null) {
                // it is probably an implicit declaration, so find the corresponding pattern
                Pattern old = aDecl.getPattern();
                Pattern current = resolver.findPatternByIndex(old.getIndex());
                if (current != null && old != current) {
                    resolved = new Declaration(aDecl.getIdentifier(), aDecl.getExtractor(), current);
                    provider.replaceDeclaration(aDecl, resolved);
                }
            }
        }
    } else if (element instanceof QueryElement) {
        QueryElement qe = (QueryElement) element;
        Pattern pattern = qe.getResultPattern();
        for (Entry<String, Declaration> entry : pattern.getInnerDeclarations().entrySet()) {
            Declaration resolved = resolver.getDeclaration(entry.getValue().getIdentifier());
            if (resolved != null && resolved != entry.getValue() && resolved.getPattern() != pattern) {
                entry.setValue(resolved);
            }
        }
        List<Integer> varIndexes = asList(qe.getVariableIndexes());
        for (int i = 0; i < qe.getArguments().length; i++) {
            if (!(qe.getArguments()[i] instanceof QueryArgument.Declr)) {
                continue;
            }
            Declaration declr = ((QueryArgument.Declr) qe.getArguments()[i]).getDeclaration();
            Declaration resolved = resolver.getDeclaration(declr.getIdentifier());
            if (resolved != declr && resolved.getPattern() != pattern) {
                qe.getArguments()[i] = new QueryArgument.Declr(resolved);
            }
            if (ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom(resolved.getPattern().getObjectType())) {
                // if the resolved still points to DroolsQuery, we know this is the first unification pattern, so redeclare it as the visible Declaration
                declr = pattern.addDeclaration(declr.getIdentifier());
                // this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
                ArrayElementReader reader = new ArrayElementReader(new SelfReferenceClassFieldReader(Object[].class), i, resolved.getDeclarationClass());
                declr.setReadAccessor(reader);
                varIndexes.add(i);
            }
        }
        qe.setVariableIndexes(toIntArray(varIndexes));
    } else if (element instanceof ConditionalBranch) {
        processBranch(resolver, (ConditionalBranch) element);
    } else {
        contextStack.push(element);
        for (RuleConditionElement ruleConditionElement : element.getNestedElements()) {
            processElement(resolver, contextStack, ruleConditionElement);
        }
        contextStack.pop();
    }
}
Also used : Constraint(org.drools.core.spi.Constraint) Constraint(org.drools.core.spi.Constraint) DataProvider(org.drools.core.spi.DataProvider) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader)

Aggregations

Constraint (org.drools.core.spi.Constraint)21 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)16 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)14 PredicateConstraint (org.drools.core.rule.PredicateConstraint)13 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)11 NegConstraint (org.drools.core.rule.constraint.NegConstraint)11 Declaration (org.drools.core.rule.Declaration)6 TypeDeclaration (org.drools.core.rule.TypeDeclaration)6 Pattern (org.drools.core.rule.Pattern)5 ArrayList (java.util.ArrayList)4 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)3 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)3 Test (org.junit.Test)3 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 MVELDumper (org.drools.compiler.lang.MVELDumper)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)2 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)2