Search in sources :

Example 6 with RuleNameMatchesAgendaFilter

use of org.drools.core.base.RuleNameMatchesAgendaFilter in project drools by kiegroup.

the class AlwaysFalseTest method testSinglePatternNoRestrictions.

/**
 * rule "test"
 * when
 * TestPattern()
 * then
 * # Nothing
 * end
 * <p/>
 * Check that a pattern with out restrictions does not raise any notifications.
 *
 * @throws Exception
 */
@Test
public void testSinglePatternNoRestrictions() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();
    session.setGlobal("result", result);
    // This pattern has an error.
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    data.add(rule1);
    data.add(pattern1);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always false"));
    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    assertEquals(0, result.getBySeverity(Severity.WARNING).size());
    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierReport(org.drools.verifier.data.VerifierReport) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 7 with RuleNameMatchesAgendaFilter

use of org.drools.core.base.RuleNameMatchesAgendaFilter in project drools by kiegroup.

the class AlwaysFalseTest method testPatterns.

@Test
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);
    // This pattern has an error.
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    Restriction r1 = LiteralRestriction.createRestriction(pattern1, "");
    Restriction r2 = LiteralRestriction.createRestriction(pattern1, "");
    Incompatibility i1 = new Incompatibility(r1, r2);
    SubPattern pp1 = new SubPattern(pattern1, 0);
    pp1.add(r1);
    pp1.add(r2);
    Restriction r3 = new VariableRestriction(pattern1);
    Restriction r4 = new VariableRestriction(pattern1);
    Incompatibility i2 = new Incompatibility(r1, r2);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    pp2.add(r1);
    pp2.add(r2);
    // This pattern does not have an error.
    Pattern pattern2 = VerifierComponentMockFactory.createPattern2();
    Restriction r5 = LiteralRestriction.createRestriction(pattern2, "");
    Restriction r6 = LiteralRestriction.createRestriction(pattern2, "");
    SubPattern pp3 = new SubPattern(pattern2, 0);
    pp3.add(r5);
    pp3.add(r6);
    Restriction r7 = new VariableRestriction(pattern2);
    Restriction r8 = new VariableRestriction(pattern2);
    Incompatibility i4 = new Incompatibility(r7, r8);
    SubPattern pp4 = new SubPattern(pattern2, 1);
    pp4.add(r7);
    pp4.add(r8);
    data.add(VerifierComponentMockFactory.createRule1());
    data.add(pattern1);
    data.add(r1);
    data.add(r2);
    data.add(r3);
    data.add(r4);
    data.add(i1);
    data.add(i2);
    data.add(pp1);
    data.add(pp2);
    data.add(pattern2);
    data.add(r5);
    data.add(r6);
    data.add(r7);
    data.add(r8);
    data.add(i4);
    data.add(pp3);
    data.add(pp4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always false"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.ERROR).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(1, result.getBySeverity(Severity.ERROR).size());
    assertEquals(0, result.getBySeverity(Severity.WARNING).size());
    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
    assertTrue(works);
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) ArrayList(java.util.ArrayList) VerifierReport(org.drools.verifier.data.VerifierReport) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with RuleNameMatchesAgendaFilter

use of org.drools.core.base.RuleNameMatchesAgendaFilter in project drools by kiegroup.

the class AlwaysTrueRuleTest method testPatterns.

@Test
public void testPatterns() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Rules.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();
    session.setGlobal("result", result);
    // This rule is always true.
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
    SubRule rp1 = new SubRule(rule1, 0);
    AlwaysTrue alwaysTrue1 = new AlwaysTrue(rp1);
    SubRule rp2 = new SubRule(rule1, 1);
    AlwaysTrue alwaysTrue2 = new AlwaysTrue(rp2);
    // This rule is okay.
    VerifierRule rule2 = VerifierComponentMockFactory.createRule2();
    SubRule rp3 = new SubRule(rule2, 0);
    SubRule rp4 = new SubRule(rule2, 1);
    AlwaysTrue alwaysTrue4 = new AlwaysTrue(rp4);
    data.add(rule1);
    data.add(rp1);
    data.add(rp2);
    data.add(alwaysTrue1);
    data.add(alwaysTrue2);
    data.add(rule2);
    data.add(rp3);
    data.add(rp4);
    data.add(alwaysTrue4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Rule that is always true"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.WARNING).iterator();
    boolean works = false;
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            VerifierMessage message = (VerifierMessage) o;
            if (message.getFaulty().equals(rule1)) {
                works = true;
            } else {
                fail("There can be only one. (And this is not the one)");
            }
        }
    }
    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    assertEquals(1, result.getBySeverity(Severity.WARNING).size());
    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
    assertTrue(works);
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierMessage(org.drools.verifier.report.components.VerifierMessage) SubRule(org.drools.verifier.components.SubRule) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierReport(org.drools.verifier.data.VerifierReport) KieSession(org.kie.api.runtime.KieSession) AlwaysTrue(org.drools.verifier.report.components.AlwaysTrue) Test(org.junit.Test)

Example 9 with RuleNameMatchesAgendaFilter

use of org.drools.core.base.RuleNameMatchesAgendaFilter in project drools by kiegroup.

the class OppositePatternsTest method testPatternsPossibilitiesOpposite.

@Test
public void testPatternsPossibilitiesOpposite() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
    Collection<Object> data = new ArrayList<Object>();
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    /*
         * Working pair
         */
    SubPattern pp1 = new SubPattern(pattern, 0);
    SubPattern pp2 = new SubPattern(pattern, 1);
    Restriction r1 = LiteralRestriction.createRestriction(pattern, "");
    pp1.add(r1);
    Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
    pp2.add(r2);
    Restriction r3 = LiteralRestriction.createRestriction(pattern, "");
    pp1.add(r3);
    Restriction r4 = LiteralRestriction.createRestriction(pattern, "");
    pp2.add(r4);
    Opposites o1 = new Opposites(r1, r2);
    Opposites o2 = new Opposites(r3, r4);
    /*
         * Pair that doesn't work.
         */
    SubPattern pp3 = new SubPattern(pattern, 2);
    SubPattern pp4 = new SubPattern(pattern, 3);
    Restriction r5 = LiteralRestriction.createRestriction(pattern, "");
    pp3.add(r5);
    Restriction r6 = LiteralRestriction.createRestriction(pattern, "");
    pp4.add(r6);
    Restriction r7 = LiteralRestriction.createRestriction(pattern, "");
    pp3.add(r7);
    Restriction r8 = LiteralRestriction.createRestriction(pattern, "");
    pp4.add(r8);
    Opposites o3 = new Opposites(r5, r6);
    data.add(r1);
    data.add(r2);
    data.add(r3);
    data.add(r4);
    data.add(r5);
    data.add(r6);
    data.add(r7);
    data.add(r8);
    data.add(pp1);
    data.add(pp2);
    data.add(pp3);
    data.add(pp4);
    data.add(o1);
    data.add(o2);
    data.add(o3);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Opposite Patterns"));
    Map<Cause, Set<Cause>> map = createOppositesMap(VerifierComponentType.SUB_PATTERN, (Iterator<Object>) session.getObjects().iterator());
    assertTrue((TestBaseOld.causeMapContains(map, pp1, pp2) ^ TestBaseOld.causeMapContains(map, pp2, pp1)));
    if (!map.isEmpty()) {
        fail("More opposites than was expected.");
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Set(java.util.Set) Opposites(org.drools.verifier.report.components.Opposites) Cause(org.drools.verifier.report.components.Cause) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 10 with RuleNameMatchesAgendaFilter

use of org.drools.core.base.RuleNameMatchesAgendaFilter in project drools by kiegroup.

the class RestrictionOrderTest method testPredicateOrderInsideOperator.

@Test
public void testPredicateOrderInsideOperator() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("RestrictionOrder.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("OptimisationRestrictionOrderTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Optimise predicates inside operator"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.NOTE).iterator();
    Collection<String> ruleNames = new ArrayList<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            String name = ((VerifierMessage) o).getCauses().toArray(new RuleComponent[2])[0].getRuleName();
            ruleNames.add(name);
        }
    }
    assertTrue(ruleNames.remove("Wrong eval order 1"));
    if (!ruleNames.isEmpty()) {
        for (String string : ruleNames) {
            fail("Rule " + string + " caused an error.");
        }
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierReport(org.drools.verifier.data.VerifierReport) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)58 Test (org.junit.Test)58 KieSession (org.kie.api.runtime.KieSession)56 VerifierReport (org.drools.verifier.data.VerifierReport)45 HashSet (java.util.HashSet)29 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)27 ArrayList (java.util.ArrayList)25 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)25 Pattern (org.drools.verifier.components.Pattern)17 Cause (org.drools.verifier.report.components.Cause)17 VerifierRule (org.drools.verifier.components.VerifierRule)12 Gap (org.drools.verifier.report.components.Gap)12 Set (java.util.Set)11 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)8 VariableRestriction (org.drools.verifier.components.VariableRestriction)5 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4 InputStream (java.io.InputStream)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 List (java.util.List)2