Search in sources :

Example 1 with VerifierMessage

use of org.drools.verifier.report.components.VerifierMessage in project drools by kiegroup.

the class MissingEqualityTest method testMissingEqualityInLiteralRestrictions.

@Test
public void testMissingEqualityInLiteralRestrictions() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("MissingEquality.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("MissingEqualityTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Missing restriction in LiteralRestrictions"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.WARNING).iterator();
    Collection<String> ruleNames = new ArrayList<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            Cause cause = ((VerifierMessage) o).getFaulty();
            String name = ((LiteralRestriction) cause).getRuleName();
            ruleNames.add(name);
        }
    }
    assertTrue(ruleNames.remove("Missing equality 1"));
    assertTrue(ruleNames.remove("Missing equality 2"));
    if (!ruleNames.isEmpty()) {
        for (String string : ruleNames) {
            fail("Rule " + string + " caused an error.");
        }
    }
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) VerifierMessage(org.drools.verifier.report.components.VerifierMessage) 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 2 with VerifierMessage

use of org.drools.verifier.report.components.VerifierMessage in project drools by kiegroup.

the class MissingEqualityTest method testMissingEqualityInVariableRestrictions2.

@Test
public void testMissingEqualityInVariableRestrictions2() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("MissingEquality.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("MissingEqualityTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Missing restriction in VariableRestrictions, unequal operator"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.WARNING).iterator();
    Set<String> ruleNames = new HashSet<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            Cause cause = ((VerifierMessage) o).getFaulty();
            String name = ((VariableRestriction) cause).getRuleName();
            ruleNames.add(name);
        }
    }
    assertTrue(ruleNames.remove("Missing equality 7"));
    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) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VariableRestriction(org.drools.verifier.components.VariableRestriction) VerifierReport(org.drools.verifier.data.VerifierReport) Cause(org.drools.verifier.report.components.Cause) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with VerifierMessage

use of org.drools.verifier.report.components.VerifierMessage 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)

Example 4 with VerifierMessage

use of org.drools.verifier.report.components.VerifierMessage in project drools by kiegroup.

the class VerifierTestStandalone method main.

public static final void main(String[] args) {
    try {
        Collection<String> fileNames = new ArrayList<String>();
        // Test data
        // fileNames.add("MissingRangesForDates.drl");
        // fileNames.add("MissingRangesForDoubles.drl");
        // fileNames.add("MissingRangesForInts.drl");
        // fileNames.add("MissingRangesForVariables.drl");
        // fileNames.add("Misc.drl");
        // fileNames.add("Misc2.drl");
        // fileNames.add("Misc3.drl");
        fileNames.add("Enums.drl");
        // fileNames.add("ConsequenceTest.drl");
        // fileNames.add("optimisation/OptimisationRestrictionOrderTest.drl");
        // fileNames.add("optimisation/OptimisationPatternOrderTest.drl");
        Verifier verifier = VerifierBuilderFactory.newVerifierBuilder().newVerifier();
        for (String s : fileNames) {
            verifier.addResourcesToVerify(ResourceFactory.newClassPathResource(s, Verifier.class), ResourceType.DRL);
        }
        verifier.fireAnalysis();
        // System.out.print(a.getResultAsPlainText());
        // System.out.print(a.getResultAsXML());
        // a.writeComponentsHTML("/stash/");
        // a.writeComponentsHTML("/Users/michaelneale/foo.html");
        // a.writeComponentsHTML("/home/trikkola/");
        // a.writeComponentsHTML("c:/");
        VerifierReport result = verifier.getResult();
        VerifierReportWriter reportwriter = VerifierReportWriterFactory.newHTMLReportWriter();
        FileOutputStream out = new FileOutputStream("/Users/rikkola/Desktop/testReport.zip");
        reportwriter.writeReport(out, result);
        Collection<VerifierMessageBase> msgs = result.getBySeverity(Severity.ERROR);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("ERR: " + msg.getMessage());
        }
        msgs = result.getBySeverity(Severity.WARNING);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("WARN (" + msg.getClass().getSimpleName() + "): " + msg.getMessage());
            System.out.println("\t FAULT: [" + msg.getClass().getSimpleName() + "] " + msg.getFaulty());
            if (msg instanceof VerifierMessage) {
                System.out.println("\t CAUSES (message):");
                VerifierMessage amsg = (VerifierMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t [" + c.getClass().getSimpleName() + "]" + c);
                }
            } else if (msg instanceof VerifierRangeCheckMessage) {
                System.out.println("\t CAUSES (range):");
                VerifierRangeCheckMessage amsg = (VerifierRangeCheckMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t" + c);
                }
            }
        }
        msgs = result.getBySeverity(Severity.NOTE);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("NOTE: " + msg.getMessage());
            System.out.println("\t" + msg.getFaulty());
        }
        Collection<ObjectType> classes = result.getVerifierData().getAll(VerifierComponentType.OBJECT_TYPE);
        for (Iterator iterator = classes.iterator(); iterator.hasNext(); ) {
            ObjectType c = (ObjectType) iterator.next();
            Collection<VerifierRule> cr = result.getVerifierData().getRulesByObjectTypePath(c.getPath());
            System.err.println("Class rules:" + cr);
            Set<Field> flds = c.getFields();
            for (Iterator iterator2 = flds.iterator(); iterator2.hasNext(); ) {
                Field f = (Field) iterator2.next();
                cr = result.getVerifierData().getRulesByFieldPath(f.getPath());
                System.err.println("Field rules: " + cr);
            }
        }
    // System.err.println(a.getResultAsPlainText());
    // System.out.println(result.toString());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierRangeCheckMessage(org.drools.verifier.report.components.VerifierRangeCheckMessage) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierRule(org.drools.verifier.components.VerifierRule) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) VerifierReport(org.drools.verifier.data.VerifierReport) FileOutputStream(java.io.FileOutputStream) Cause(org.drools.verifier.report.components.Cause) VerifierReportWriter(org.drools.verifier.report.VerifierReportWriter) Iterator(java.util.Iterator)

Example 5 with VerifierMessage

use of org.drools.verifier.report.components.VerifierMessage in project drools by kiegroup.

the class NamedConsequencesTest method testMissingConsequence.

@Test
public void testMissingConsequence() throws Exception {
    InputStream in = getClass().getResourceAsStream("NamedConsequences.drl");
    KieSession session = getStatelessKieSession(in);
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(this.getClass().getResourceAsStream("ConsequenceTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("No action - possibly commented out"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.NOTE).iterator();
    Set<String> rulesThatHadErrors = new HashSet<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            VerifierMessage message = (VerifierMessage) o;
            rulesThatHadErrors.addAll(message.getImpactedRules().values());
        }
    }
    assertFalse(rulesThatHadErrors.contains("This one is has an unused named consequence"));
    if (!rulesThatHadErrors.isEmpty()) {
        for (String string : rulesThatHadErrors) {
            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) InputStream(java.io.InputStream) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) KieSession(org.kie.api.runtime.KieSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

VerifierReport (org.drools.verifier.data.VerifierReport)26 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)26 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)26 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)25 Test (org.junit.Test)25 KieSession (org.kie.api.runtime.KieSession)25 HashSet (java.util.HashSet)17 VerifierRule (org.drools.verifier.components.VerifierRule)10 Pattern (org.drools.verifier.components.Pattern)7 ArrayList (java.util.ArrayList)5 Cause (org.drools.verifier.report.components.Cause)5 VariableRestriction (org.drools.verifier.components.VariableRestriction)3 InputStream (java.io.InputStream)2 FileOutputStream (java.io.FileOutputStream)1 Iterator (java.util.Iterator)1 Field (org.drools.verifier.components.Field)1 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)1 ObjectType (org.drools.verifier.components.ObjectType)1 SubRule (org.drools.verifier.components.SubRule)1 VerifierReportWriter (org.drools.verifier.report.VerifierReportWriter)1