Search in sources :

Example 1 with DrlExprParser

use of org.drools.drl.parser.DrlExprParser in project drools by kiegroup.

the class DRLExprParserTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    new EvaluatorRegistry();
    this.parser = new DrlExprParser(LanguageLevelOption.DRL6);
}
Also used : EvaluatorRegistry(org.drools.compiler.builder.impl.EvaluatorRegistry) DrlExprParser(org.drools.drl.parser.DrlExprParser)

Example 2 with DrlExprParser

use of org.drools.drl.parser.DrlExprParser in project drools by kiegroup.

the class DescrDumperTest method parse.

public ConstraintConnectiveDescr parse(final String constraint) {
    DrlExprParser parser = new DrlExprParser(LanguageLevelOption.DRL6);
    ConstraintConnectiveDescr result = parser.parse(constraint);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    return result;
}
Also used : ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DrlExprParser(org.drools.drl.parser.DrlExprParser)

Example 3 with DrlExprParser

use of org.drools.drl.parser.DrlExprParser in project drools by kiegroup.

the class QueryElementBuilder method parseExpression.

private ConstraintConnectiveDescr parseExpression(final RuleBuildContext context, final PatternDescr patternDescr, final String expression) {
    DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
    ConstraintConnectiveDescr result = parser.parse(expression);
    if (result == null || parser.hasErrors()) {
        for (DroolsParserException error : parser.getErrors()) {
            context.addError(new DescrBuildError(context.getParentDescr(), patternDescr, null, "Unable to parser pattern expression:\n" + error.getMessage()));
        }
        return null;
    }
    return result;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DrlExprParser(org.drools.drl.parser.DrlExprParser) DroolsParserException(org.drools.drl.parser.DroolsParserException)

Example 4 with DrlExprParser

use of org.drools.drl.parser.DrlExprParser in project drools by kiegroup.

the class QueryElementBuilder method processBinding.

private void processBinding(RuleBuildContext context, BaseDescr descr, Declaration[] params, QueryArgument[] arguments, List<Declaration> requiredDeclarations, InternalReadAccessor arrayReader, Pattern pattern, BindingDescr bind) {
    Declaration declr = context.getDeclarationResolver().getDeclaration(bind.getVariable());
    if (declr != null) {
        // check right maps to a slot, otherwise we can't reverse this and should error
        int pos = getPos(bind.getExpression(), params);
        if (pos >= 0) {
            // slot exist, reverse and continue
            String slot = bind.getExpression();
            String var = bind.getVariable();
            bind.setVariable(slot);
            bind.setExpression(var);
        } else {
        // else error, we cannot find the slot to unify against
        }
    }
    // left does not already exist, is it a slot?
    int pos = getPos(bind.getVariable(), params);
    if (pos >= 0) {
        // it's an input on a slot, is the input using bindings?
        declr = context.getDeclarationResolver().getDeclaration(bind.getExpression());
        if (declr != null) {
            requiredDeclarations.add(declr);
            arguments[pos] = new QueryArgument.Declr(declr);
        } else {
            // it must be a literal/expression
            // it's an expression and thus an input
            DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
            ConstraintConnectiveDescr bresult = parser.parse(bind.getExpression());
            if (parser.hasErrors()) {
                for (DroolsParserException error : parser.getErrors()) {
                    context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to parser pattern expression:\n" + error.getMessage()));
                }
                return;
            }
            arguments[pos] = getLiteralQueryArgument(context, descr, bresult);
        }
    } else {
        // this is creating a new output binding
        // we know it doesn't exist, as we already checked for left == var
        pos = getPos(bind.getExpression(), params);
        if (pos < 0) {
            // error this must be a binding on a slot
            context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "named argument does not exist:\n" + bind.getExpression()));
            return;
        }
        arguments[pos] = getVariableQueryArgument(arrayReader, params, pos, pattern, bind.getVariable());
    }
}
Also used : QueryArgument(org.drools.core.rule.QueryArgument) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) Declaration(org.drools.core.rule.Declaration) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DrlExprParser(org.drools.drl.parser.DrlExprParser) DroolsParserException(org.drools.drl.parser.DroolsParserException)

Example 5 with DrlExprParser

use of org.drools.drl.parser.DrlExprParser in project drools by kiegroup.

the class PatternBuilder method parseExpression.

protected ConstraintConnectiveDescr parseExpression(final RuleBuildContext context, final PatternDescr patternDescr, final BaseDescr original, final String expression) {
    DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
    ConstraintConnectiveDescr result = parser.parse(expression);
    result.setResource(patternDescr.getResource());
    result.copyLocation(original);
    if (parser.hasErrors()) {
        for (DroolsParserException error : parser.getErrors()) {
            registerDescrBuildError(context, patternDescr, "Unable to parse pattern expression:\n" + error.getMessage());
        }
        return null;
    }
    return result;
}
Also used : ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) DrlExprParser(org.drools.drl.parser.DrlExprParser) DroolsParserException(org.drools.drl.parser.DroolsParserException)

Aggregations

DrlExprParser (org.drools.drl.parser.DrlExprParser)7 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)6 DroolsParserException (org.drools.drl.parser.DroolsParserException)3 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 EvaluatorRegistry (org.drools.compiler.builder.impl.EvaluatorRegistry)1 Declaration (org.drools.core.rule.Declaration)1 QueryArgument (org.drools.core.rule.QueryArgument)1