use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class MVELConstraintBuilder method buildMvelFieldReadAccessor.
@Override
public InternalReadAccessor buildMvelFieldReadAccessor(RuleBuildContext context, BaseDescr descr, Pattern pattern, ObjectType objectType, String fieldName, boolean reportError) {
InternalReadAccessor reader;
Dialect dialect = context.getDialect();
try {
MVELDialect mvelDialect = (MVELDialect) context.getDialect("mvel");
context.setDialect(mvelDialect);
final AnalysisResult analysis = context.getDialect().analyzeExpression(context, descr, fieldName, new BoundIdentifiers(pattern, context, Collections.EMPTY_MAP, objectType));
if (analysis == null) {
// something bad happened
if (reportError) {
registerDescrBuildError(context, descr, "Unable to analyze expression '" + fieldName + "'");
}
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
if (!usedIdentifiers.getGlobals().isEmpty()) {
// cannot create a read accessors here when using globals
return null;
}
if (!usedIdentifiers.getDeclrClasses().isEmpty()) {
if (reportError && descr instanceof BindingDescr) {
registerDescrBuildError(context, descr, "Variables can not be used inside bindings. Variable " + usedIdentifiers.getDeclrClasses().keySet() + " is being used in binding '" + fieldName + "'");
}
return null;
}
reader = ((MVELKnowledgePackageImpl) context.getPkg()).getClassFieldAccessorStore().getMVELReader(context.getPkg().getName(), objectType.getClassName(), fieldName, context.isTypesafe(), ((MVELAnalysisResult) analysis).getReturnType());
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
((MVELCompileable) reader).compile(data, context.getRule());
data.addCompileable((MVELCompileable) reader);
} catch (final Exception e) {
if (reportError) {
AsmUtil.copyErrorLocation(e, descr);
registerDescrBuildError(context, descr, e, "Unable to create reader for '" + fieldName + "':" + e.getMessage());
}
// if there was an error, set the reader back to null
reader = null;
} finally {
context.setDialect(dialect);
}
return reader;
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DescrDumperTest method testDumpBindingsComplexOp2.
@Test
public void testDumpBindingsComplexOp2() throws Exception {
String input = "$x : age not in (10, 20, $someVal)";
String expected = "age != 10 && age != 20 && age != $someVal";
ConstraintConnectiveDescr descr = parse(input);
DumperContext ctx = new DumperContext();
String result = dumper.dump(descr, ctx);
assertEquals(expected, result);
assertEquals(1, ctx.getBindings().size());
BindingDescr bind = ctx.getBindings().get(0);
assertEquals("$x", bind.getVariable());
assertEquals("age", bind.getExpression());
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DescrDumperTest method testDumpBindingsComplexOp.
@Test
public void testDumpBindingsComplexOp() throws Exception {
String input = "$x : age in (10, 20, $someVal)";
String expected = "( age == 10 || age == 20 || age == $someVal )";
ConstraintConnectiveDescr descr = parse(input);
DumperContext ctx = new DumperContext();
String result = dumper.dump(descr, ctx);
assertEquals(expected, result);
assertEquals(1, ctx.getBindings().size());
BindingDescr bind = ctx.getBindings().get(0);
assertEquals("$x", bind.getVariable());
assertEquals("age", bind.getExpression());
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DescrDumperTest method testDumpBindings.
@Test
public void testDumpBindings() throws Exception {
String input = "$x : property > value";
String expected = "property > value";
ConstraintConnectiveDescr descr = parse(input);
DumperContext ctx = new DumperContext();
String result = dumper.dump(descr, ctx);
assertEquals(expected, result);
assertEquals(1, ctx.getBindings().size());
BindingDescr bind = ctx.getBindings().get(0);
assertEquals("$x", bind.getVariable());
assertEquals("property", bind.getExpression());
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DRLExprParserTest method testDoubleBinding.
@Test
public void testDoubleBinding() throws Exception {
String source = "$x : x.m( 1, a ) && $y : y[z].foo";
ConstraintConnectiveDescr result = parser.parse(source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(ConnectiveType.AND, result.getConnective());
assertEquals(2, result.getDescrs().size());
BindingDescr bind = (BindingDescr) result.getDescrs().get(0);
assertEquals("$x", bind.getVariable());
assertEquals("x.m( 1, a )", bind.getExpression());
bind = (BindingDescr) result.getDescrs().get(1);
assertEquals("$y", bind.getVariable());
assertEquals("y[z].foo", bind.getExpression());
}
Aggregations