Search in sources :

Example 11 with Pattern

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

the class IncoherentRestrictionsTest method testIncoherentLiteralRestrictionsInSubPattern.

@Test
public void testIncoherentLiteralRestrictionsInSubPattern() throws Exception {
    KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Restrictions.drl"));
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(getClass().getResourceAsStream("RestrictionsTest.drl"), result.getVerifierData());
    session.setGlobal("result", result);
    for (Object o : testData) {
        session.insert(o);
    }
    session.fireAllRules(new RuleNameMatchesAgendaFilter("Incoherent LiteralRestrictions in pattern possibility"));
    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.ERROR).iterator();
    Set<String> rulesThatHadErrors = new HashSet<String>();
    while (iter.hasNext()) {
        Object o = (Object) iter.next();
        if (o instanceof VerifierMessage) {
            Pattern pattern = (Pattern) ((VerifierMessage) o).getFaulty();
            rulesThatHadErrors.add(pattern.getRuleName());
        }
    }
    assertTrue(rulesThatHadErrors.remove("Incoherent restrictions 1"));
    assertTrue(rulesThatHadErrors.remove("Incoherent restrictions 2"));
    if (!rulesThatHadErrors.isEmpty()) {
        for (String string : rulesThatHadErrors) {
            fail("Rule " + string + " caused an error.");
        }
    }
}
Also used : RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Pattern(org.drools.verifier.components.Pattern) VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierReport(org.drools.verifier.data.VerifierReport) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) KieSession(org.kie.api.runtime.KieSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with Pattern

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

the class PatternSolverTest method testAddBasicOr.

/**
 * <pre>
 *       or
 *      /  \
 *  descr descr2
 * </pre>
 *
 * result:<br>
 * descr<br>
 * or<br>
 * descr2
 */
@Test
public void testAddBasicOr() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(2, list.size());
    assertEquals(1, list.get(0).size());
    assertEquals(1, list.get(1).size());
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 13 with Pattern

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

the class PatternSolverTest method testAddOrAndOrDescr.

/**
 * <pre>
 *                   and
 *          /         |      \
 *       and         or       descr5
 *      /  \         / \
 * descr descr2 descr3 descr4
 * </pre>
 *
 * result:<br>
 * descr && descr2 && descr3 && descr5<br>
 * or<br>
 * descr && descr2 && descr4 && descr5<br>
 */
@Test
public void testAddOrAndOrDescr() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction4 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction5 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.AND);
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction3);
    solver.add(literalRestriction4);
    solver.end();
    solver.add(literalRestriction5);
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(2, list.size());
    assertEquals(4, list.get(0).size());
    assertTrue(list.get(0).contains(literalRestriction));
    assertTrue(list.get(0).contains(literalRestriction2));
    assertTrue(list.get(0).contains(literalRestriction3));
    assertTrue(list.get(0).contains(literalRestriction5));
    assertEquals(4, list.get(1).size());
    assertTrue(list.get(1).contains(literalRestriction));
    assertTrue(list.get(1).contains(literalRestriction2));
    assertTrue(list.get(1).contains(literalRestriction4));
    assertTrue(list.get(1).contains(literalRestriction4));
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 14 with Pattern

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

the class PatternSolverTest method testAddBasicAnd.

/**
 * <pre>
 *      and
 *     /   \
 *  descr  descr2
 * </pre>
 *
 * result:<br>
 * descr && descr2
 */
@Test
public void testAddBasicAnd() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).size());
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Example 15 with Pattern

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

the class PatternSolverTest method testAddOrAndOr.

/**
 * <pre>
 *             or
 *         /        \
 *       and         or
 *      /  \         / \
 * descr descr2 descr3 descr4
 * </pre>
 *
 * result:<br>
 * descr && descr2<br>
 * or<br>
 * descr3<br>
 * or<br>
 * descr4
 */
@Test
public void testAddOrAndOr() {
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
    LiteralRestriction literalRestriction4 = LiteralRestriction.createRestriction(pattern, "");
    PatternSolver solver = new PatternSolver(pattern);
    solver.addOperator(OperatorDescrType.OR);
    solver.addOperator(OperatorDescrType.AND);
    solver.add(literalRestriction);
    solver.add(literalRestriction2);
    solver.end();
    solver.addOperator(OperatorDescrType.OR);
    solver.add(literalRestriction3);
    solver.add(literalRestriction4);
    solver.end();
    solver.end();
    List<Set<VerifierComponent>> list = solver.getPossibilityLists();
    assertEquals(3, list.size());
    assertEquals(2, list.get(0).size());
    assertTrue(list.get(0).contains(literalRestriction));
    assertTrue(list.get(0).contains(literalRestriction2));
    assertEquals(1, list.get(1).size());
    assertTrue(list.get(1).contains(literalRestriction3));
    assertEquals(1, list.get(2).size());
    assertTrue(list.get(2).contains(literalRestriction4));
}
Also used : LiteralRestriction(org.drools.verifier.components.LiteralRestriction) Pattern(org.drools.verifier.components.Pattern) Set(java.util.Set) Test(org.junit.Test)

Aggregations

Pattern (org.drools.verifier.components.Pattern)25 Test (org.junit.Test)22 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)10 Set (java.util.Set)8 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)8 VerifierReport (org.drools.verifier.data.VerifierReport)8 KieSession (org.kie.api.runtime.KieSession)8 HashSet (java.util.HashSet)7 VerifierMessage (org.drools.verifier.report.components.VerifierMessage)7 VerifierMessageBase (org.drools.verifier.report.components.VerifierMessageBase)7 SubPattern (org.drools.verifier.components.SubPattern)6 VerifierRule (org.drools.verifier.components.VerifierRule)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 SubRule (org.drools.verifier.components.SubRule)4 ArrayList (java.util.ArrayList)3 Restriction (org.drools.verifier.components.Restriction)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2 ObjectType (org.drools.verifier.components.ObjectType)2 PatternVariable (org.drools.verifier.components.PatternVariable)2 RulePackage (org.drools.verifier.components.RulePackage)2