Search in sources :

Example 81 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class SolversTest method testNotAnd.

/**
 * <pre>
 * when
 *     Foo( r &amp;&amp; r2 )
 *     and
 *     not Foo( r3 &amp;&amp; r4 )
 * </pre>
 *
 * result:<br>
 * r && r2<br>
 * r3 && r4
 */
@Test
public void testNotAnd() {
    PackageDescr descr = new PackageDescr("testPackage");
    RulePackage rulePackage = new RulePackage(descr);
    rulePackage.setName("testPackage");
    VerifierRule rule = new VerifierRule(descr, rulePackage, new HashMap<String, Object>());
    rule.setName("testRule");
    Pattern pattern = new Pattern(new PatternDescr(), rule);
    Restriction r = LiteralRestriction.createRestriction(pattern, "");
    Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
    Restriction r3 = LiteralRestriction.createRestriction(pattern, "");
    Restriction r4 = LiteralRestriction.createRestriction(pattern, "");
    Solvers solvers = new Solvers();
    solvers.startRuleSolver(rule);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.startPatternSolver(pattern);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.addPatternComponent(r);
    solvers.addPatternComponent(r2);
    solvers.endOperator();
    solvers.endPatternSolver();
    solvers.startNot();
    solvers.startPatternSolver(pattern);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.addPatternComponent(r3);
    solvers.addPatternComponent(r4);
    solvers.endOperator();
    solvers.endPatternSolver();
    solvers.endNot();
    solvers.endOperator();
    solvers.endRuleSolver();
    List<SubRule> list = solvers.getRulePossibilities();
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).getItems().size());
    List<Restriction> result = new ArrayList<Restriction>();
    result.add(r);
    result.add(r2);
    List<Restriction> result2 = new ArrayList<Restriction>();
    result2.add(r3);
    result2.add(r4);
    Object[] possibilies = list.get(0).getItems().toArray();
    SubPattern p1 = (SubPattern) possibilies[0];
    SubPattern p2 = (SubPattern) possibilies[1];
    /*
         * Order may change but it doesn't matter.
         */
    if (p1.getItems().containsAll(result)) {
        assertTrue(p2.getItems().containsAll(result2));
    } else if (p1.getItems().containsAll(result2)) {
        assertTrue(p2.getItems().containsAll(result));
    } else {
        fail("No items found.");
    }
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) SubRule(org.drools.verifier.components.SubRule) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) SubPattern(org.drools.verifier.components.SubPattern) RulePackage(org.drools.verifier.components.RulePackage) Restriction(org.drools.verifier.components.Restriction) LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Solvers(org.drools.verifier.solver.Solvers) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 82 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class AlwaysTruePatternTest method testPatterns.

@Test
@Ignore
public void testPatterns() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();
    session.setGlobal("result", result);
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
    // This pattern is always true.
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    SubPattern pp1 = new SubPattern(pattern1, 0);
    AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);
    // This pattern is okay.
    Pattern pattern2 = new Pattern(new PatternDescr("objectType2", "testPattern2"), rule1);
    pattern2.setName("testPattern2");
    SubPattern pp3 = new SubPattern(pattern2, 0);
    SubPattern pp4 = new SubPattern(pattern2, 1);
    AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);
    data.add(rule1);
    data.add(pattern1);
    data.add(pp1);
    data.add(pp2);
    data.add(alwaysTrue1);
    data.add(alwaysTrue2);
    data.add(pattern2);
    data.add(pp3);
    data.add(pp4);
    data.add(alwaysTrue4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always true"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.NOTE).iterator();
    boolean works = false;
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            VerifierMessage message = (VerifierMessage) o;
            if (message.getFaulty().equals(pattern1)) {
                works = true;
            } else {
                fail("There can be only one. (And this is not the one)");
            }
        }
    }
    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    assertEquals(0, result.getBySeverity(Severity.WARNING).size());
    assertEquals(1, result.getBySeverity(Severity.NOTE).size());
    assertTrue(works);
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ArrayList(java.util.ArrayList) VerifierReport(org.drools.verifier.data.VerifierReport) KieSession(org.kie.api.runtime.KieSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 83 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method parseBaseDescr.

private IPattern parseBaseDescr(final RuleModel m, final BaseDescr descr, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
    if (descr instanceof PatternDescr) {
        return parsePatternDescr(m, (PatternDescr) descr, isJavaDialect, boundParams, dmo);
    } else if (descr instanceof AndDescr) {
        AndDescr andDescr = (AndDescr) descr;
        return parseBaseDescr(m, andDescr.getDescrs().get(0), isJavaDialect, boundParams, dmo);
    } else if (descr instanceof EvalDescr) {
        FreeFormLine freeFormLine = new FreeFormLine();
        freeFormLine.setText("eval( " + ((EvalDescr) descr).getContent() + " )");
        return freeFormLine;
    } else if (descr instanceof ConditionalElementDescr) {
        return parseExistentialElementDescr(m, (ConditionalElementDescr) descr, isJavaDialect, boundParams, dmo);
    }
    return null;
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 84 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class VerifierComponentMockFactory method createPattern.

public static Pattern createPattern(int i) {
    Pattern pattern = new Pattern(new PatternDescr("objectType" + i, "testPattern" + i), createRule(i));
    pattern.setObjectTypePath("objectType" + i);
    pattern.setName("testPattern" + i);
    return pattern;
}
Also used : Pattern(org.drools.verifier.components.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr)

Example 85 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class ForallBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ForallDescr forallDescr = (ForallDescr) descr;
    final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
    final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
    if (basePattern == null) {
        return null;
    }
    final Forall forall = new Forall(basePattern);
    // adding the newly created forall CE to the build stack
    // this is necessary in case of local declaration usage
    context.getDeclarationResolver().pushOnBuildStack(forall);
    for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
        final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
        forall.addRemainingPattern(anotherPattern);
    }
    if (forallDescr.getDescrs().size() == 1) {
        // An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
        // we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
        // which we wish to ignore
        PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
        if (p.getConstraint().getDescrs().isEmpty()) {
            forall.setEmptyBetaConstraints(true);
        }
    }
    // poping the forall
    context.getDeclarationResolver().popBuildStack();
    return forall;
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Forall(org.drools.core.rule.Forall) ForallDescr(org.drools.compiler.lang.descr.ForallDescr)

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7