Search in sources :

Example 1 with Solvers

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

use of org.drools.verifier.solver.Solvers 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

ArrayList (java.util.ArrayList)2 LiteralRestriction (org.drools.verifier.components.LiteralRestriction)2 Pattern (org.drools.verifier.components.Pattern)2 Restriction (org.drools.verifier.components.Restriction)2 SubPattern (org.drools.verifier.components.SubPattern)2 SubRule (org.drools.verifier.components.SubRule)2 VerifierRule (org.drools.verifier.components.VerifierRule)2 Solvers (org.drools.verifier.solver.Solvers)2 Test (org.junit.Test)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 RuleComponent (org.drools.verifier.components.RuleComponent)1 RulePackage (org.drools.verifier.components.RulePackage)1