Search in sources :

Example 6 with DumperContext

use of org.drools.compiler.lang.DumperContext 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 7 with DumperContext

use of org.drools.compiler.lang.DumperContext 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 8 with DumperContext

use of org.drools.compiler.lang.DumperContext in project drools by kiegroup.

the class QueryElementBuilder method getLiteralQueryArgument.

private QueryArgument getLiteralQueryArgument(RuleBuildContext context, BaseDescr descr, ConstraintConnectiveDescr result) {
    DumperContext mvelCtx = new DumperContext();
    String expr = DescrDumper.getInstance().dump(result, mvelCtx);
    try {
        Object value = CoreComponentsBuilder.get().evaluateMvelExpression(context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"), context.getKnowledgeBuilder().getRootClassLoader(), expr);
        return new QueryArgument.Literal(value);
    } catch (Exception e) {
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to compile expression: " + expr));
    }
    return null;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) DumperContext(org.drools.compiler.lang.DumperContext) DroolsParserException(org.drools.drl.parser.DroolsParserException)

Aggregations

DumperContext (org.drools.compiler.lang.DumperContext)8 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)7 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Test (org.junit.Test)5 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)2 Constraint (org.drools.core.spi.Constraint)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1