use of org.drools.verifier.components.Pattern in project drools by kiegroup.
the class PatternSolverTest method testAddOrAnd.
/**
* <pre>
* or
* / \
* descr and
* / \
* descr2 descr3
* </pre>
*
* result:<br>
* descr <br>
* or<br>
* descr2 && descr3
*/
@Test
public void testAddOrAnd() {
Pattern pattern = VerifierComponentMockFactory.createPattern1();
LiteralRestriction literalRestriction = LiteralRestriction.createRestriction(pattern, "");
LiteralRestriction literalRestriction2 = LiteralRestriction.createRestriction(pattern, "");
LiteralRestriction literalRestriction3 = LiteralRestriction.createRestriction(pattern, "");
PatternSolver solver = new PatternSolver(pattern);
solver.addOperator(OperatorDescrType.OR);
solver.add(literalRestriction);
solver.addOperator(OperatorDescrType.AND);
solver.add(literalRestriction2);
solver.add(literalRestriction3);
solver.end();
solver.end();
List<Set<VerifierComponent>> list = solver.getPossibilityLists();
assertEquals(2, list.size());
assertEquals(1, list.get(0).size());
assertTrue(list.get(0).contains(literalRestriction));
assertEquals(2, list.get(1).size());
assertTrue(list.get(1).contains(literalRestriction2));
assertTrue(list.get(1).contains(literalRestriction3));
}
use of org.drools.verifier.components.Pattern in project drools by kiegroup.
the class PatternDescrVisitor method visitAccumulateDescr.
private VerifierAccumulateDescr visitAccumulateDescr(AccumulateDescr descr) throws UnknownDescriptionException {
VerifierAccumulateDescr accumulate = new VerifierAccumulateDescr(pattern);
PatternDescrVisitor visitor = new PatternDescrVisitor(data, rule, solvers);
Pattern parentPattern = visitor.visitPatternDescr(descr.getInputPattern(), null, 0);
accumulate.setInitCode(descr.getInitCode());
accumulate.setActionCode(descr.getActionCode());
accumulate.setReverseCode(descr.getReverseCode());
accumulate.setResultCode(descr.getResultCode());
// XXX: Array seems to be always null.
// accumulate.setDeclarations(descr.getDeclarations());
accumulate.setClassName(descr.getClassName());
accumulate.setExternalFunction(descr.isExternalFunction());
accumulate.setFunctionIdentifier(descr.getFunctions().get(0).getFunction());
accumulate.setExpression(descr.getFunctions().get(0).getParams()[0]);
accumulate.setParentPath(parentPattern.getPath());
accumulate.setParentType(parentPattern.getVerifierComponentType());
data.add(accumulate);
return accumulate;
}
use of org.drools.verifier.components.Pattern in project drools by kiegroup.
the class VerifierComponentMockFactory method createPattern.
public static Pattern createPattern(int i) {
Pattern pattern = new Pattern(new PatternDescr("objectType" + i, "testPattern" + i), createRule(i));
pattern.setObjectTypePath("objectType" + i);
pattern.setName("testPattern" + i);
return pattern;
}
use of org.drools.verifier.components.Pattern 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));
}
}
use of org.drools.verifier.components.Pattern in project drools by kiegroup.
the class AlwaysTrueRuleTest method testPatternPossibilities.
@Test
public void testPatternPossibilities() 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();
Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
SubRule rp1 = new SubRule(rule1, 0);
SubPattern pp1 = new SubPattern(pattern1, 0);
AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
SubPattern pp2 = new SubPattern(pattern1, 1);
AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);
rp1.add(pp1);
rp1.add(pp2);
// This rule is okay.
VerifierRule rule2 = VerifierComponentMockFactory.createRule2();
Pattern pattern2 = VerifierComponentMockFactory.createPattern2();
SubRule rp2 = new SubRule(rule2, 0);
SubPattern pp3 = new SubPattern(pattern2, 0);
SubPattern pp4 = new SubPattern(pattern2, 1);
AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);
rp2.add(pp3);
rp2.add(pp4);
data.add(rule1);
data.add(rp1);
data.add(pp1);
data.add(pp2);
data.add(alwaysTrue1);
data.add(alwaysTrue2);
data.add(rule2);
data.add(rp2);
data.add(pp3);
data.add(pp4);
data.add(alwaysTrue4);
for (Object o : data) {
session.insert(o);
}
session.fireAllRules(new RuleNameMatchesAgendaFilter("Rule possibility that is always true"));
boolean rp1true = false;
boolean rp2true = false;
boolean rp3true = false;
boolean rp4true = false;
for (Object o : session.getObjects()) {
if (o instanceof AlwaysTrue) {
AlwaysTrue alwaysTrue = (AlwaysTrue) o;
if (!rp1true) {
rp1true = alwaysTrue.getCause().equals(pp1);
}
if (!rp2true) {
rp2true = alwaysTrue.getCause().equals(pp2);
}
if (!rp3true) {
rp3true = alwaysTrue.getCause().equals(pp3);
}
if (!rp4true) {
rp4true = alwaysTrue.getCause().equals(pp4);
}
}
}
assertTrue(rp1true);
assertTrue(rp2true);
assertFalse(rp3true);
assertTrue(rp4true);
}
Aggregations