use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DescrDumperTest method testProcessNullSafeDereferencing.
@Test
public void testProcessNullSafeDereferencing() throws Exception {
String expr = "field1!.field2";
String expectedNullCheck = "field1 != null";
String expectedExpr = "field1.field2";
AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
ConstraintConnectiveDescr ccd = new ConstraintConnectiveDescr();
String[] nullCheckAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
assertEquals(expectedNullCheck, ccd.getDescrs().get(0).toString());
assertEquals(expectedExpr, nullCheckAndExpr[1]);
assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
expr = "field1!.field2!.field3";
String expectedNullCheck1 = "field1 != null";
String expectedNullCheck2 = "field1.field2 != null";
expectedExpr = "field1.field2.field3";
atomicExpr = new AtomicExprDescr(expr);
ccd = new ConstraintConnectiveDescr();
nullCheckAndExpr = dumper.processImplicitConstraints(expr, atomicExpr, ccd, ccd.getDescrs().indexOf(atomicExpr), null);
assertEquals(expectedNullCheck1, ccd.getDescrs().get(0).toString());
assertEquals(expectedNullCheck2, ccd.getDescrs().get(1).toString());
assertEquals(expectedExpr, nullCheckAndExpr[1]);
assertEquals(expectedExpr, atomicExpr.getRewrittenExpression());
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class PatternBuilder method processAtomicExpression.
protected Constraint processAtomicExpression(RuleBuildContext context, Pattern pattern, BaseDescr d, String expr, Map<String, OperatorDescr> aliases) {
if (d instanceof AtomicExprDescr) {
Matcher m = evalRegexp.matcher(((AtomicExprDescr) d).getExpression());
if (m.find()) {
// MVELDumper already stripped the eval
// this will build the eval using the specified dialect
PredicateDescr pdescr = new PredicateDescr(context.getRuleDescr().getResource(), expr);
pdescr.copyLocation(d);
return buildEval(context, pattern, pdescr, aliases, expr, true);
}
}
return null;
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class PatternBuilder method rewriteCompositeExpressions.
private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
int i = 0;
StringBuilder sb = new StringBuilder();
for (BaseDescr subDescr : d.getDescrs()) {
if (subDescr instanceof BindingDescr) {
continue;
}
if (i++ > 0) {
sb.append(" ").append(d.getConnective().getConnective()).append(" ");
}
String normalizedExpr;
if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
if (relDescr.getExpression() != null) {
normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
} else {
i--;
normalizedExpr = "";
}
} else if (subDescr instanceof ConstraintConnectiveDescr) {
String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
if (rewrittenExpr == null) {
return null;
}
normalizedExpr = "(" + rewrittenExpr + ")";
} else if (subDescr instanceof AtomicExprDescr) {
normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
} else {
return null;
}
sb.append(normalizedExpr);
}
return sb.toString();
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DescrDumper method processBinding.
private void processBinding(StringBuilder sbuilder, BindingDescr bind, ConstraintConnectiveDescr parent, boolean isInsideRelCons, DumperContext context) {
String expr = bind.getExpression().trim();
AtomicExprDescr atomicExpr = new AtomicExprDescr(expr);
atomicExpr.setResource(parent.getResource());
String[] constrAndExpr = processImplicitConstraints(expr, atomicExpr, parent, parent.getDescrs().indexOf(bind), context);
if (isInsideRelCons) {
sbuilder.append(constrAndExpr[0]).append(constrAndExpr[1]);
} else if (constrAndExpr[0].length() > 4) {
sbuilder.append(constrAndExpr[0], 0, constrAndExpr[0].length() - 4);
}
if (bind.getExpression().equals(bind.getBindingField())) {
bind.setExpressionAndBindingField(constrAndExpr[1]);
} else {
bind.setExpression(constrAndExpr[1]);
}
context.addBinding(bind);
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DescrDumper method processNullSafeDereferencing.
private String[] processNullSafeDereferencing(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, int nullSafePos, int parentIdx, int childIdx) {
// convert "field1!.field2" in ["field1 != null && ", "field1.field2"]
String field1 = expr.substring(0, nullSafePos).trim();
expr = field1 + "." + expr.substring(nullSafePos + 2).trim();
RelationalExprDescr check = new RelationalExprDescr("!=", false, null, new AtomicExprDescr(getPreconditionsToAppend(field1)), new AtomicExprDescr("null"));
if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
ccd.getDescrs().add(childIdx, check);
} else {
BaseDescr desc = ccd.getDescrs().get(parentIdx);
if (desc instanceof ConstraintConnectiveDescr) {
((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
} else {
ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
BaseDescr original = ccd.getDescrs().remove(parentIdx);
localAnd.getDescrs().add(check);
localAnd.getDescrs().add(original);
ccd.getDescrs().add(parentIdx, localAnd);
}
}
String innerCheck = check + " && ";
String[] nullCheckAndExpr = new String[] { innerCheck, expr };
atomicExpr.setRewrittenExpression(expr);
return nullCheckAndExpr;
}
Aggregations