Search in sources :

Example 1 with VerifierRule

use of org.drools.verifier.components.VerifierRule in project drools by kiegroup.

the class VerifierComponentMockFactory method createRule.

public static VerifierRule createRule(int i) {
    VerifierRule rule = new VerifierRule(new PackageDescr("testPackage1"), createPackage1(), new HashMap<String, Object>());
    rule.setName("testRule" + i);
    return rule;
}
Also used : VerifierRule(org.drools.verifier.components.VerifierRule) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 2 with VerifierRule

use of org.drools.verifier.components.VerifierRule 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 3 with VerifierRule

use of org.drools.verifier.components.VerifierRule 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 4 with VerifierRule

use of org.drools.verifier.components.VerifierRule 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 5 with VerifierRule

use of org.drools.verifier.components.VerifierRule in project drools by kiegroup.

the class VerifierDataMapsTest method testSaveVerifierComponentAndGet.

@Test
public void testSaveVerifierComponentAndGet() {
    VerifierData data = VerifierReportFactory.newVerifierData();
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    rule.setName("0");
    String rulePath = rule.getPath();
    data.add(rule);
    Collection<VerifierComponent> all = data.getAll();
    assertEquals(1, all.size());
    assertEquals(rule, all.toArray()[0]);
    Collection<VerifierRule> rules = data.getAll(VerifierComponentType.RULE);
    assertEquals(1, rules.size());
    assertEquals(rule, rules.toArray()[0]);
    VerifierRule rule2 = data.getVerifierObject(VerifierComponentType.RULE, rulePath);
    assertNotNull(rule2);
    assertEquals(rule, rule2);
}
Also used : VerifierRule(org.drools.verifier.components.VerifierRule) Test(org.junit.Test)

Aggregations

VerifierRule (org.drools.verifier.components.VerifierRule)20 Test (org.junit.Test)15 VerifierReport (org.drools.verifier.data.VerifierReport)11 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)10 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)10 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)10 KieSession (org.kie.api.runtime.KieSession)10 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)5 ObjectType (org.drools.verifier.components.ObjectType)5 Pattern (org.drools.verifier.components.Pattern)5 SubPattern (org.drools.verifier.components.SubPattern)5 SubRule (org.drools.verifier.components.SubRule)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 Restriction (org.drools.verifier.components.Restriction)3 Field (org.drools.verifier.components.Field)2 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)2 RulePackage (org.drools.verifier.components.RulePackage)2 VerifierData (org.drools.verifier.data.VerifierData)2 AlwaysTrue (org.drools.verifier.report.components.AlwaysTrue)2