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);
}
}
}
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);
}
use of org.drools.verifier.components.SubPattern in project drools by kiegroup.
the class SolversTest method testNotAnd.
/**
* <pre>
* when
* Foo( r && r2 )
* and
* not Foo( r3 && 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.");
}
}
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());
}
use of org.drools.verifier.components.SubPattern in project drools by kiegroup.
the class SolversTest method testBasicAnd.
/**
* <pre>
* when
* Foo( descr && 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));
}
}
Aggregations