Search in sources :

Example 51 with RuleNameMatchesAgendaFilter

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

the class AlwaysFalseTest method testRules.

@Test
public void testRules() 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 has an error.
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    SubPattern pp1 = new SubPattern(pattern1, 0);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    Incompatibility i1 = new Incompatibility(pp1, pp2);
    SubRule rp1 = new SubRule(rule1, 0);
    rp1.add(pp1);
    rp1.add(pp2);
    SubPattern pp3 = new SubPattern(pattern1, 2);
    SubPattern pp4 = new SubPattern(pattern1, 3);
    Incompatibility i2 = new Incompatibility(pp1, pp2);
    SubRule rp2 = new SubRule(rule1, 1);
    rp2.add(pp1);
    rp2.add(pp2);
    // This pattern does not have an error.
    VerifierRule rule2 = VerifierComponentMockFactory.createRule2();
    Pattern pattern2 = VerifierComponentMockFactory.createPattern2();
    SubPattern pp5 = new SubPattern(pattern2, 0);
    SubPattern pp6 = new SubPattern(pattern2, 1);
    SubRule rp3 = new SubRule(rule2, 2);
    rp3.add(pp5);
    rp3.add(pp6);
    SubPattern pp7 = new SubPattern(pattern2, 2);
    SubPattern pp8 = new SubPattern(pattern2, 3);
    Incompatibility i4 = new Incompatibility(pp7, pp8);
    SubRule rp4 = new SubRule(rule2, 3);
    rp4.add(pp7);
    rp4.add(pp8);
    data.add(rule1);
    data.add(pp1);
    data.add(pp2);
    data.add(pp3);
    data.add(pp4);
    data.add(i1);
    data.add(i2);
    data.add(rp1);
    data.add(rp2);
    data.add(rule2);
    data.add(pp5);
    data.add(pp6);
    data.add(pp7);
    data.add(pp8);
    data.add(i4);
    data.add(rp3);
    data.add(rp4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Rule 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(rule1)) {
                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 52 with RuleNameMatchesAgendaFilter

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

the class AlwaysTrueRuleTest method testPatternPossibilities.

@Test
public void testPatternPossibilities() 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();
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
    SubRule rp1 = new SubRule(rule1, 0);
    SubPattern pp1 = new SubPattern(pattern1, 0);
    AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);
    rp1.add(pp1);
    rp1.add(pp2);
    // This rule is okay.
    VerifierRule rule2 = VerifierComponentMockFactory.createRule2();
    Pattern pattern2 = VerifierComponentMockFactory.createPattern2();
    SubRule rp2 = new SubRule(rule2, 0);
    SubPattern pp3 = new SubPattern(pattern2, 0);
    SubPattern pp4 = new SubPattern(pattern2, 1);
    AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);
    rp2.add(pp3);
    rp2.add(pp4);
    data.add(rule1);
    data.add(rp1);
    data.add(pp1);
    data.add(pp2);
    data.add(alwaysTrue1);
    data.add(alwaysTrue2);
    data.add(rule2);
    data.add(rp2);
    data.add(pp3);
    data.add(pp4);
    data.add(alwaysTrue4);
    for (Object o : data) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Rule possibility that is always true"));
    boolean rp1true = false;
    boolean rp2true = false;
    boolean rp3true = false;
    boolean rp4true = false;
    for (Object o : session.getObjects()) {
        if (o instanceof AlwaysTrue) {
            AlwaysTrue alwaysTrue = (AlwaysTrue) o;
            if (!rp1true) {
                rp1true = alwaysTrue.getCause().equals(pp1);
            }
            if (!rp2true) {
                rp2true = alwaysTrue.getCause().equals(pp2);
            }
            if (!rp3true) {
                rp3true = alwaysTrue.getCause().equals(pp3);
            }
            if (!rp4true) {
                rp4true = alwaysTrue.getCause().equals(pp4);
            }
        }
    }
    assertTrue(rp1true);
    assertTrue(rp2true);
    assertFalse(rp3true);
    assertTrue(rp4true);
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) SubRule(org.drools.verifier.components.SubRule) VerifierReport(org.drools.verifier.data.VerifierReport) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) KieSession(org.kie.api.runtime.KieSession) SubPattern(org.drools.verifier.components.SubPattern) AlwaysTrue(org.drools.verifier.report.components.AlwaysTrue) Test(org.junit.Test)

Example 53 with RuleNameMatchesAgendaFilter

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

the class RestrictionOrderTest method testRestrictionOrderInsideOperator.

@Test
public void testRestrictionOrderInsideOperator() 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());
    // for ( Object object : testData ) {
    // if ( object instanceof SubPattern ) {
    // SubPattern s = (SubPattern) object;
    // System.out.println( " - " + s );
    // for ( PatternComponent o : s.getItems() ) {
    // System.out.println( " -- " + o + " : " + o.getOrderNumber() );
    // }
    // }
    // }
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Optimise restrictions 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 Restriction[2])[0].getRuleName();
            ruleNames.add(name);
        }
    }
    assertTrue(ruleNames.remove("Wrong descr 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)

Example 54 with RuleNameMatchesAgendaFilter

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

the class NotesTest method testRedundantRestrictionsInPatternPossibilities.

@Test
public void testRedundantRestrictionsInPatternPossibilities() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Notes.drl"));
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    Collection<Object> objects = new ArrayList<Object>();
    LiteralRestriction left = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction right = LiteralRestriction.createRestriction(pattern, "");
    Redundancy redundancy = new Redundancy(left, right);
    SubPattern possibility = new SubPattern(pattern, 0);
    possibility.add(left);
    possibility.add(right);
    objects.add(left);
    objects.add(right);
    objects.add(redundancy);
    objects.add(possibility);
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    session.setGlobal("result", result);
    for (Object o : objects) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Find redundant restrictions from pattern possibilities"));
    Collection<VerifierMessageBase> notes = result.getBySeverity(Severity.NOTE);
    // Has at least one item.
    assertEquals(1, notes.size());
    VerifierMessageBase note = notes.iterator().next();
    Iterator<Cause> causes = note.getCauses().iterator();
    assertEquals(left, causes.next());
    assertEquals(right, causes.next());
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Redundancy(org.drools.verifier.report.components.Redundancy) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierReport(org.drools.verifier.data.VerifierReport) Cause(org.drools.verifier.report.components.Cause) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 55 with RuleNameMatchesAgendaFilter

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

the class NotesTest method testRedundantPatternPossibilitiesInRulePossibilities.

@Test
public void testRedundantPatternPossibilitiesInRulePossibilities() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Notes.drl"));
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    Collection<Object> objects = new ArrayList<Object>();
    SubPattern left = new SubPattern(pattern, 0);
    SubPattern right = new SubPattern(pattern, 1);
    Redundancy redundancy = new Redundancy(left, right);
    SubRule possibility = new SubRule(rule, 0);
    possibility.add(left);
    possibility.add(right);
    objects.add(left);
    objects.add(right);
    objects.add(redundancy);
    objects.add(possibility);
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    session.setGlobal("result", result);
    for (Object o : objects) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Find redundant pattern possibilities from rule possibilities"));
    Collection<VerifierMessageBase> notes = result.getBySeverity(Severity.NOTE);
    // Has at least one item.
    assertEquals(1, notes.size());
    VerifierMessageBase note = notes.iterator().next();
    Iterator<Cause> causes = note.getCauses().iterator();
    assertEquals(left, causes.next());
    assertEquals(right, causes.next());
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Redundancy(org.drools.verifier.report.components.Redundancy) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierReport(org.drools.verifier.data.VerifierReport) Cause(org.drools.verifier.report.components.Cause) 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