Search in sources :

Example 86 with Pattern

use of org.drools.core.rule.Pattern 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) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 87 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class KnowledgeBuilderTest method testExists.

@Test
public void testExists() throws Exception {
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // Make sure we can't accessa  variable bound inside the not node
    RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
    assertTrue(builder.hasErrors());
    builder = new KnowledgeBuilderImpl();
    rule = createRule(new ExistsDescr(), builder, "");
    assertEquals(0, builder.getErrors().getErrors().length);
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, exists.getChildren());
    final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 88 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class PatternBuilder method build.

private List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ConstraintConnectiveDescr descr, DumperContext mvelCtx) {
    List<Constraint> constraints = new ArrayList<>();
    List<BaseDescr> initialDescrs = new ArrayList<>(descr.getDescrs());
    for (BaseDescr d : initialDescrs) {
        boolean isXPath = isXPathDescr(d);
        if (isXPath && pattern.hasXPath()) {
            registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
            return constraints;
        }
        context.setXpathOffsetadjustment(xpathStartDeclaration == null ? 0 : -1);
        Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, xpathStartDeclaration, pattern, (ExpressionDescr) d, mvelCtx) : buildCcdDescr(context, patternDescr, xpathStartDeclaration, pattern, d, descr, mvelCtx);
        if (constraint != null) {
            Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
            if (declCorrXpath == null) {
                constraints.add(constraint);
            } else {
                // A constraint is using a declration bound to an xpath in the same pattern
                // Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
                Pattern modifiedPattern = pattern.clone();
                modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
                constraint = buildCcdDescr(context, patternDescr, xpathStartDeclaration, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
                if (constraint != null) {
                    pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
                }
            }
        }
    }
    if (descr.getDescrs().size() > initialDescrs.size()) {
        // The initial build process may have generated other constraint descrs.
        // This happens when null-safe references or inline-casts are used
        // These additional constraints must be built, and added as
        List<BaseDescr> additionalDescrs = new ArrayList<>(descr.getDescrs());
        additionalDescrs.removeAll(initialDescrs);
        if (!additionalDescrs.isEmpty()) {
            List<Constraint> additionalConstraints = new ArrayList<>();
            for (BaseDescr d : additionalDescrs) {
                Constraint constraint = buildCcdDescr(context, patternDescr, xpathStartDeclaration, pattern, d, descr, mvelCtx);
                if (constraint != null) {
                    additionalConstraints.add(constraint);
                }
            }
            constraints.addAll(0, additionalConstraints);
        }
    }
    return constraints;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) ArrayList(java.util.ArrayList) ExpressionDescr(org.drools.drl.ast.descr.ExpressionDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration)

Example 89 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class CollectBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final CollectDescr collectDescr = (CollectDescr) descr;
    final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
    final Pattern sourcePattern = (Pattern) patternBuilder.build(context, collectDescr.getInputPattern());
    if (sourcePattern == null) {
        return null;
    }
    final String className = "collect" + context.getNextId();
    collectDescr.setClassMethodName(className);
    Pattern resultPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
    return new Collect(sourcePattern, resultPattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.drl.ast.descr.PatternDescr) CollectDescr(org.drools.drl.ast.descr.CollectDescr) Collect(org.drools.core.rule.Collect)

Example 90 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class AnnotationsTest method testNestedAnnotations.

@Test
public void testNestedAnnotations() {
    String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
    assertNotNull(outer);
    Object val = outer.getPropertyValue("value");
    assertNotNull(val);
    assertTrue(val instanceof AnnotationDefinition);
    AnnotationDefinition inner = (AnnotationDefinition) val;
    assertEquals("world", inner.getPropertyValue("text"));
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Aggregations

Pattern (org.drools.core.rule.Pattern)190 ClassObjectType (org.drools.core.base.ClassObjectType)103 Test (org.junit.Test)98 Declaration (org.drools.core.rule.Declaration)79 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)69 GroupElement (org.drools.core.rule.GroupElement)49 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)45 ArrayList (java.util.ArrayList)35 IOException (java.io.IOException)33 ObjectType (org.drools.core.spi.ObjectType)32 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)28 Variable (org.drools.model.Variable)25 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)25 List (java.util.List)24 Accumulate (org.drools.core.rule.Accumulate)24 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)23 KieBase (org.kie.api.KieBase)23 Collection (java.util.Collection)21 QueryImpl (org.drools.core.rule.QueryImpl)21 KieSession (org.kie.api.runtime.KieSession)21