Search in sources :

Example 31 with BindingDescr

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;
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) MVELCompileable(org.drools.mvel.expr.MVELCompileable) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MVELDialect(org.drools.mvel.builder.MVELDialect) Dialect(org.drools.compiler.compiler.Dialect) MVELDialect(org.drools.mvel.builder.MVELDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult) IOException(java.io.IOException) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 32 with BindingDescr

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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Example 33 with BindingDescr

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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Example 34 with BindingDescr

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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DumperContext(org.drools.compiler.lang.DumperContext) Test(org.junit.Test)

Example 35 with BindingDescr

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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) Test(org.junit.Test)

Aggregations

BindingDescr (org.drools.drl.ast.descr.BindingDescr)42 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)17 Test (org.junit.Test)17 BaseDescr (org.drools.drl.ast.descr.BaseDescr)13 PatternDescr (org.drools.drl.ast.descr.PatternDescr)11 RuleDescr (org.drools.drl.ast.descr.RuleDescr)11 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)10 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)10 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 AndDescr (org.drools.drl.ast.descr.AndDescr)8 Cheese (org.drools.mvel.compiler.Cheese)8 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)7 DumperContext (org.drools.compiler.lang.DumperContext)6 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)5 Declaration (org.drools.core.rule.Declaration)5 TypeDeclaration (org.drools.core.rule.TypeDeclaration)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 PredicateConstraint (org.drools.core.rule.PredicateConstraint)4