Search in sources :

Example 1 with SubRule

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

use of org.drools.verifier.components.SubRule 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.drl.ast.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.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 3 with SubRule

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

the class Solvers method createRulePossibilities.

private void createRulePossibilities() {
    for (Set<VerifierComponent> list : ruleSolver.getPossibilityLists()) {
        SubRule possibility = new SubRule(ruleSolver.getRule(), subRuleIndex++);
        for (VerifierComponent descr : list) {
            possibility.add((RuleComponent) descr);
        }
        rulePossibilities.add(possibility);
    }
}
Also used : SubRule(org.drools.verifier.components.SubRule) VerifierComponent(org.drools.verifier.data.VerifierComponent)

Example 4 with SubRule

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

the class VerifierDataMapsTest method testSaveVerifierComponentAndGetForAllComponentTypes.

@Test
public void testSaveVerifierComponentAndGetForAllComponentTypes() {
    RulePackage rulePackage = VerifierComponentMockFactory.createPackage1();
    saveVerifierComponentAndGet(rulePackage);
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    saveVerifierComponentAndGet(rule);
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    saveVerifierComponentAndGet(pattern);
    saveVerifierComponentAndGet(new InlineEvalDescr(pattern));
    saveVerifierComponentAndGet(new ObjectType(new PackageDescr("testPackage1")));
    saveVerifierComponentAndGet(new RuleOperatorDescr(new AndDescr(), rule, OperatorDescrType.AND));
    saveVerifierComponentAndGet(new PatternOperatorDescr(pattern, OperatorDescrType.AND));
    saveVerifierComponentAndGet(new SubPattern(pattern, 0));
    saveVerifierComponentAndGet(new ReturnValueFieldDescr(pattern));
    saveVerifierComponentAndGet(new SubRule(rule, 0));
    saveVerifierComponentAndGet(new TextConsequence(rule));
    saveVerifierComponentAndGet(new PatternVariable(rule));
    saveVerifierComponentAndGet(new VerifierAccessorDescr(rule));
    saveVerifierComponentAndGet(new VerifierAccumulateDescr(pattern));
    saveVerifierComponentAndGet(new VerifierCollectDescr(pattern));
    saveVerifierComponentAndGet(new RuleEval(rule));
    saveVerifierComponentAndGet(new VerifierFieldAccessDescr(rule));
    saveVerifierComponentAndGet(new VerifierFromDescr(pattern));
    saveVerifierComponentAndGet(new VerifierMethodAccessDescr(rule));
    saveVerifierComponentAndGet(new PatternEval(pattern));
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) TextConsequence(org.drools.verifier.components.TextConsequence) PatternVariable(org.drools.verifier.components.PatternVariable) VerifierMethodAccessDescr(org.drools.verifier.components.VerifierMethodAccessDescr) ReturnValueFieldDescr(org.drools.verifier.components.ReturnValueFieldDescr) SubRule(org.drools.verifier.components.SubRule) VerifierFieldAccessDescr(org.drools.verifier.components.VerifierFieldAccessDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) VerifierAccumulateDescr(org.drools.verifier.components.VerifierAccumulateDescr) VerifierRule(org.drools.verifier.components.VerifierRule) VerifierFromDescr(org.drools.verifier.components.VerifierFromDescr) SubPattern(org.drools.verifier.components.SubPattern) RuleEval(org.drools.verifier.components.RuleEval) RulePackage(org.drools.verifier.components.RulePackage) PatternEval(org.drools.verifier.components.PatternEval) ObjectType(org.drools.verifier.components.ObjectType) PatternOperatorDescr(org.drools.verifier.components.PatternOperatorDescr) RuleOperatorDescr(org.drools.verifier.components.RuleOperatorDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) VerifierCollectDescr(org.drools.verifier.components.VerifierCollectDescr) VerifierAccessorDescr(org.drools.verifier.components.VerifierAccessorDescr) InlineEvalDescr(org.drools.verifier.components.InlineEvalDescr) Test(org.junit.Test)

Example 5 with SubRule

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

the class SolversTest method testBasicAnd.

/**
 * <pre>
 * when
 *     Foo( descr &amp;&amp; descr2 )
 * </pre>
 *
 * result:<br>
 * descr && descr2
 */
@Test
public void testBasicAnd() {
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    Restriction r = LiteralRestriction.createRestriction(pattern, "");
    Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
    Solvers solvers = new Solvers();
    solvers.startRuleSolver(rule);
    solvers.startPatternSolver(pattern);
    solvers.startOperator(OperatorDescrType.AND);
    solvers.addPatternComponent(r);
    solvers.addPatternComponent(r2);
    solvers.endOperator();
    solvers.endPatternSolver();
    solvers.endRuleSolver();
    List<SubRule> list = solvers.getRulePossibilities();
    assertEquals(1, list.size());
    assertEquals(1, list.get(0).getItems().size());
    List<Restriction> result = new ArrayList<Restriction>();
    result.add(r);
    result.add(r2);
    Set<RuleComponent> set = list.get(0).getItems();
    for (RuleComponent component : set) {
        SubPattern possibility = (SubPattern) component;
        assertTrue(possibility.getItems().containsAll(result));
    }
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) Restriction(org.drools.verifier.components.Restriction) LiteralRestriction(org.drools.verifier.components.LiteralRestriction) SubRule(org.drools.verifier.components.SubRule) Solvers(org.drools.verifier.solver.Solvers) ArrayList(java.util.ArrayList) VerifierRule(org.drools.verifier.components.VerifierRule) SubPattern(org.drools.verifier.components.SubPattern) RuleComponent(org.drools.verifier.components.RuleComponent) Test(org.junit.Test)

Aggregations

SubRule (org.drools.verifier.components.SubRule)7 Test (org.junit.Test)6 SubPattern (org.drools.verifier.components.SubPattern)5 VerifierRule (org.drools.verifier.components.VerifierRule)5 ArrayList (java.util.ArrayList)4 Pattern (org.drools.verifier.components.Pattern)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)2 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)2 Restriction (org.drools.verifier.components.Restriction)2 RulePackage (org.drools.verifier.components.RulePackage)2 VerifierComponent (org.drools.verifier.data.VerifierComponent)2 VerifierReport (org.drools.verifier.data.VerifierReport)2 AlwaysTrue (org.drools.verifier.report.components.AlwaysTrue)2 Solvers (org.drools.verifier.solver.Solvers)2 KieSession (org.kie.api.runtime.KieSession)2 AndDescr (org.drools.drl.ast.descr.AndDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 InlineEvalDescr (org.drools.verifier.components.InlineEvalDescr)1 ObjectType (org.drools.verifier.components.ObjectType)1