Search in sources :

Example 1 with SubPattern

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

the class Solvers method createPatternPossibilities.

private void createPatternPossibilities() {
    List<Set<VerifierComponent>> lists = patternSolver.getPossibilityLists();
    if (lists.size() == 0) {
        SubPattern subPattern = newSubPattern();
        ruleSolver.add(subPattern);
        subPatterns.add(subPattern);
    } else {
        for (Set<VerifierComponent> list : lists) {
            SubPattern subPattern = newSubPattern();
            for (VerifierComponent descr : list) {
                subPattern.add((PatternComponent) descr);
            }
            ruleSolver.add(subPattern);
            subPatterns.add(subPattern);
        }
    }
}
Also used : Set(java.util.Set) SubPattern(org.drools.verifier.components.SubPattern) VerifierComponent(org.drools.verifier.data.VerifierComponent)

Example 2 with SubPattern

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

the class PatternSolverDRLTest method testOrInsidePattern.

@Test
public void testOrInsidePattern() throws Exception {
    StringBuffer rule = new StringBuffer();
    rule.append("rule \"Test rule\" ");
    rule.append("   when ");
    rule.append("       customer : Customer( status > 30 && < 50 ) ");
    rule.append("       order : OrderHeader( customer == customer , orderPriority == 3 || == 4 ) ");
    rule.append("   then ");
    rule.append("       order.setOrderDiscount( 6.0 ); ");
    rule.append("end");
    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<? extends Object> testData = getTestData(new ByteArrayInputStream(rule.toString().getBytes()), result.getVerifierData());
    int patternCount = 0;
    // the right amount of items.
    for (Object o : testData) {
        if (o instanceof SubPattern) {
            SubPattern pp = (SubPattern) o;
            if (pp.getItems().size() == 2) {
                patternCount++;
            }
        }
    }
    assertEquals(3, patternCount);
}
Also used : VerifierReport(org.drools.verifier.data.VerifierReport) ByteArrayInputStream(java.io.ByteArrayInputStream) SubPattern(org.drools.verifier.components.SubPattern) Test(org.junit.Test)

Example 3 with SubPattern

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

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

the class PackageDescrVisitorTest method testSubPatterns.

@Test
public void testSubPatterns() throws Exception {
    PackageDescr packageDescr = getPackageDescr(getClass().getResourceAsStream("SubPattern.drl"));
    assertNotNull(packageDescr);
    packageDescrVisitor.visitPackageDescr(packageDescr);
    Collection<VerifierComponent> all = verifierData.getAll();
    assertNotNull(all);
    SubPattern test1SubPattern = null;
    SubPattern test2SubPattern = null;
    SubRule test1SubRule = null;
    SubRule test2SubRule = null;
    for (VerifierComponent verifierComponent : all) {
        // System.out.println( verifierComponent );
        System.out.println("-" + verifierComponent);
        if (verifierComponent.getDescr() != null) {
            System.out.println(" \n\t\t => " + verifierComponent.getDescr().getLine() + ":" + +verifierComponent.getDescr().getEndLine() + " " + verifierComponent.getDescr().getText());
        } else {
            System.out.println(" \n\t\t => null for " + verifierComponent.getClass().getSimpleName());
        }
        if (verifierComponent.getVerifierComponentType().equals(VerifierComponentType.SUB_PATTERN)) {
            SubPattern subPattern = (SubPattern) verifierComponent;
            if ("Test 1".equals(subPattern.getRuleName())) {
                assertNull(test1SubPattern);
                test1SubPattern = subPattern;
            } else if ("Test 2".equals(subPattern.getRuleName())) {
                assertNull(test2SubPattern);
                test2SubPattern = subPattern;
            }
        }
        if (verifierComponent.getVerifierComponentType().equals(VerifierComponentType.SUB_RULE)) {
            SubRule subRule = (SubRule) verifierComponent;
            if ("Test 1".equals(subRule.getRuleName())) {
                assertNull(test1SubRule);
                test1SubRule = subRule;
            } else if ("Test 2".equals(subRule.getRuleName())) {
                assertNull(test2SubRule);
                test2SubRule = subRule;
            }
        }
    }
    assertNotNull(test1SubPattern);
    assertEquals(3, test1SubPattern.getItems().size());
    assertNotNull(test2SubPattern);
    assertEquals(3, test2SubPattern.getItems().size());
    assertNotNull(test1SubRule);
    assertEquals(1, test1SubRule.getItems().size());
    assertNotNull(test2SubRule);
    assertEquals(1, test2SubRule.getItems().size());
}
Also used : SubRule(org.drools.verifier.components.SubRule) SubPattern(org.drools.verifier.components.SubPattern) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) VerifierComponent(org.drools.verifier.data.VerifierComponent) Test(org.junit.Test)

Example 5 with SubPattern

use of org.drools.verifier.components.SubPattern 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

SubPattern (org.drools.verifier.components.SubPattern)7 Test (org.junit.Test)6 SubRule (org.drools.verifier.components.SubRule)5 Pattern (org.drools.verifier.components.Pattern)4 VerifierRule (org.drools.verifier.components.VerifierRule)4 ArrayList (java.util.ArrayList)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 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 Solvers (org.drools.verifier.solver.Solvers)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Set (java.util.Set)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)1 InlineEvalDescr (org.drools.verifier.components.InlineEvalDescr)1 ObjectType (org.drools.verifier.components.ObjectType)1