use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testAndConnective.
@Test
public void testAndConnective() throws Exception {
String source = "a > b && 10 != 20";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get(0);
assertEquals(">", expr.getOperator());
AtomicExprDescr left = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr right = (AtomicExprDescr) expr.getRight();
assertEquals("a", left.getExpression());
assertEquals("b", right.getExpression());
expr = (RelationalExprDescr) result.getDescrs().get(1);
assertEquals("!=", expr.getOperator());
left = (AtomicExprDescr) expr.getLeft();
right = (AtomicExprDescr) expr.getRight();
assertEquals("10", left.getExpression());
assertEquals("20", right.getExpression());
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testBindingConstraint.
@Test
public void testBindingConstraint() throws Exception {
String source = "$x : property > value";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(1, result.getDescrs().size());
RelationalExprDescr rel = (RelationalExprDescr) result.getDescrs().get(0);
assertEquals(">", rel.getOperator());
BindingDescr bind = (BindingDescr) rel.getLeft();
assertEquals("$x", bind.getVariable());
assertEquals("property", bind.getExpression());
AtomicExprDescr right = (AtomicExprDescr) rel.getRight();
assertEquals("value", right.getExpression());
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DRLExprParserTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
String source = "a > b";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(1, result.getDescrs().size());
RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get(0);
assertEquals(">", expr.getOperator());
AtomicExprDescr left = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr right = (AtomicExprDescr) expr.getRight();
assertEquals("a", left.getExpression());
assertEquals("b", right.getExpression());
}
use of org.drools.drl.ast.descr.AtomicExprDescr in project drools by kiegroup.
the class DescrDumper method processInlineCast.
private String[] processInlineCast(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, DumperContext context, int sharpPos, int parentIdx, int childIdx) {
// convert "field1#Class.field2" in ["field1 instanceof Class && ", "((Class)field1).field2"]
String field1 = expr.substring(0, sharpPos).trim();
int sharpPos2 = expr.indexOf('#', sharpPos + 1);
String part2 = sharpPos2 < 0 ? expr.substring(sharpPos + 1).trim() : expr.substring(sharpPos + 1, sharpPos2).trim();
String[] classAndField = splitInClassAndField(part2, context);
BaseDescr desc = parentIdx >= 0 ? ccd.getDescrs().get(parentIdx) : null;
if (classAndField == null) {
return new String[] { "", expr };
} else if (desc instanceof AtomicExprDescr && classAndField.length == 1) {
return new String[] { "", field1 + " instanceof " + classAndField[0] };
}
String className = classAndField[0];
String castedExpression = classAndField.length == 1 ? "((" + className + ")" + field1 + ")" : "((" + className + ")" + field1 + ")." + classAndField[1] + (sharpPos2 > 0 ? expr.substring(sharpPos2) : "");
RelationalExprDescr check = new RelationalExprDescr("instanceof", false, null, new AtomicExprDescr(field1), new AtomicExprDescr(className));
if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
ccd.getDescrs().add(childIdx, check);
} else {
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);
}
}
atomicExpr.setRewrittenExpression(castedExpression);
String innerCheck = check + " && ";
return new String[] { innerCheck, castedExpression };
}
Aggregations