use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testRuleParseLhsWithStringQuotes2.
@Test
public void testRuleParseLhsWithStringQuotes2() throws Exception {
final String text = "rule X when Cheese( $x: type, type == \"s\\tti\\\"lto\\nn\" ) then end\n";
RuleDescr rule = (RuleDescr) parse("rule", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
AndDescr lhs = rule.getLhs();
ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get(0)).getDescrs().get(1);
assertEquals("type == \"s\\tti\\\"lto\\nn\"", constr.getText());
}
use of org.drools.drl.ast.descr.ExprConstraintDescr 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.ExprConstraintDescr 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;
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class ExprConstraintHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
emptyContentCheck(localName, expression, parser);
PatternDescr p = (PatternDescr) parser.getParent();
ExprConstraintDescr descr = new ExprConstraintDescr(expression);
p.getConstraint().addDescr(descr);
return descr;
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class PatternBuilder method collectProps.
private static Collection<String> collectProps(PatternDescr pattern) {
Collection<String> props = new HashSet<>();
for (BaseDescr expr : pattern.getDescrs()) {
if (expr instanceof ExprConstraintDescr) {
String text = expr.getText();
String prop = StringUtils.extractFirstIdentifier(text, 0);
String propFromGetter = ClassUtils.getter2property(prop);
props.add(propFromGetter != null ? propFromGetter : StringUtils.lcFirst(prop));
}
}
return props;
}
Aggregations