use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testSimpleQuery.
@Test
public void testSimpleQuery() throws Exception {
final QueryDescr query = (QueryDescr) parseResource("query", "simple_query.drl");
assertNotNull(query);
assertEquals("simple_query", query.getName());
final AndDescr lhs = query.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// Check first pattern
final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("foo3", first.getIdentifier());
assertEquals("Bar", first.getObjectType());
assertEquals(1, first.getConstraint().getDescrs().size());
AndDescr and = (AndDescr) first.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertNotNull(fld);
assertEquals("a==3", fld.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
assertEquals(1, second.getDescrs().size());
// check it has field bindings.
final ExprConstraintDescr bindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
assertEquals("a4:a==4", bindingDescr.getExpression());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMultiPattern.
@Test
public void testAccumulateMultiPattern() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_multi_pattern.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accum = (AccumulateDescr) outPattern.getSource();
assertEqualsIgnoreWhitespace("$counter", outPattern.getIdentifier());
assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
final AndDescr and = (AndDescr) accum.getInput();
assertEquals(2, and.getDescrs().size());
final PatternDescr person = (PatternDescr) and.getDescrs().get(0);
final PatternDescr cheese = (PatternDescr) and.getDescrs().get(1);
assertEquals("Person", person.getObjectType());
assertEquals("Cheese", cheese.getObjectType());
}
use of org.drools.drl.ast.descr.PatternDescr 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.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class AlwaysTruePatternTest method testPatterns.
@Test
@Ignore
public void testPatterns() throws Exception {
KieSession session = getStatelessKieSession(this.getClass().getResourceAsStream("Patterns.drl"));
VerifierReport result = VerifierReportFactory.newVerifierReport();
Collection<Object> data = new ArrayList<Object>();
session.setGlobal("result", result);
VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
// This pattern is always true.
Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
SubPattern pp1 = new SubPattern(pattern1, 0);
AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
SubPattern pp2 = new SubPattern(pattern1, 1);
AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);
// This pattern is okay.
Pattern pattern2 = new Pattern(new PatternDescr("objectType2", "testPattern2"), rule1);
pattern2.setName("testPattern2");
SubPattern pp3 = new SubPattern(pattern2, 0);
SubPattern pp4 = new SubPattern(pattern2, 1);
AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);
data.add(rule1);
data.add(pattern1);
data.add(pp1);
data.add(pp2);
data.add(alwaysTrue1);
data.add(alwaysTrue2);
data.add(pattern2);
data.add(pp3);
data.add(pp4);
data.add(alwaysTrue4);
for (Object o : data) {
session.insert(o);
}
session.fireAllRules(new RuleNameMatchesAgendaFilter("Pattern that is always true"));
Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.NOTE).iterator();
boolean works = false;
while (iter.hasNext()) {
Object o = (Object) iter.next();
if (o instanceof VerifierMessage) {
VerifierMessage message = (VerifierMessage) o;
if (message.getFaulty().equals(pattern1)) {
works = true;
} else {
fail("There can be only one. (And this is not the one)");
}
}
}
assertEquals(0, result.getBySeverity(Severity.ERROR).size());
assertEquals(0, result.getBySeverity(Severity.WARNING).size());
assertEquals(1, result.getBySeverity(Severity.NOTE).size());
assertTrue(works);
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class FieldConstraintHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final ConnectiveDescr c = (ConnectiveDescr) parser.getCurrent();
Object p = parser.getParent();
if (p instanceof PatternDescr) {
StringBuilder sb = new StringBuilder();
c.buildExpression(sb);
ExprConstraintDescr expr = new ExprConstraintDescr();
expr.setExpression(sb.toString());
final PatternDescr patternDescr = (PatternDescr) parser.getParent();
patternDescr.addConstraint(expr);
} else if (p instanceof ConnectiveDescr) {
((ConnectiveDescr) p).add(c);
}
return c;
}
Aggregations