Search in sources :

Example 1 with DeclarationScopeResolver

use of org.drools.core.spi.DeclarationScopeResolver in project drools by kiegroup.

the class PatternBuilder method lookupObjectType.

private void lookupObjectType(RuleBuildContext context, PatternDescr patternDescr) {
    List<? extends BaseDescr> descrs = patternDescr.getConstraint().getDescrs();
    if (descrs.size() != 1 || !(descrs.get(0) instanceof ExprConstraintDescr)) {
        return;
    }
    ExprConstraintDescr descr = (ExprConstraintDescr) descrs.get(0);
    String expr = descr.getExpression();
    if (expr.charAt(0) != '/') {
        return;
    }
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expr);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expr + "': " + xpathAnalysis.getError());
        return;
    }
    XpathPart firstXpathChunk = xpathAnalysis.getPart(0);
    String identifier = firstXpathChunk.getField();
    DeclarationScopeResolver resolver = context.getDeclarationResolver();
    if (resolver.hasDataSource(identifier)) {
        patternDescr.setObjectType(findObjectType(context, firstXpathChunk, identifier));
        FromDescr fromDescr = new FromDescr();
        fromDescr.setDataSource(new MVELExprDescr(identifier));
        patternDescr.setSource(fromDescr);
        patternDescr.removeAllConstraint();
        firstXpathChunk.getConstraints().forEach(s -> patternDescr.addConstraint(new ExprConstraintDescr(s)));
        if (!xpathAnalysis.isSinglePart()) {
            patternDescr.addConstraint(new ExprConstraintDescr(patternDescr.getIdentifier() + " : " + expr.substring(xpathAnalysis.getPart(1).getStart())));
            patternDescr.setIdentifier("$void$");
        }
    } else {
        Declaration declr = resolver.getDeclaration(identifier);
        patternDescr.setXpathStartDeclaration(declr);
        patternDescr.setObjectType(declr.getExtractor().getExtractToClassName());
        expr = patternDescr.getIdentifier() + (patternDescr.isUnification() ? " := " : " : ") + expr.substring(identifier.length() + 1);
        descr.setExpression(expr);
    }
}
Also used : XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 2 with DeclarationScopeResolver

use of org.drools.core.spi.DeclarationScopeResolver in project drools by kiegroup.

the class QueryElementBuilder method processPositional.

private void processPositional(RuleBuildContext context, QueryImpl query, Declaration[] params, QueryArgument[] arguments, List<Declaration> requiredDeclarations, InternalReadAccessor arrayReader, Pattern pattern, BaseDescr base, String expression, ConstraintConnectiveDescr result) {
    int pos = ((ExprConstraintDescr) base).getPosition();
    if (pos >= arguments.length) {
        context.addError(new DescrBuildError(context.getParentDescr(), base, null, "Unable to parse query '" + query.getName() + "', as postion " + pos + " for expression '" + expression + "' does not exist on query size " + arguments.length));
        return;
    }
    boolean isVariable = isVariable(expression);
    DeclarationScopeResolver declarationResolver = context.getDeclarationResolver();
    Declaration declr = isVariable ? declarationResolver.getDeclaration(expression) : null;
    if (declr != null) {
        // it exists, so it's an input
        requiredDeclarations.add(declr);
        arguments[pos] = new QueryArgument.Declr(declr);
    } else if (isVariable && expression.indexOf('.') < 0) {
        arguments[pos] = getVariableQueryArgument(arrayReader, params, pos, pattern, expression);
    } else {
        // it's an expression and thus an input
        AnalysisResult analysisResult = analyzeExpression(context, base, expression);
        if (analysisResult == null || analysisResult.getIdentifiers().isEmpty()) {
            arguments[pos] = getLiteralQueryArgument(context, base, result);
        } else {
            List<Declaration> declarations = new ArrayList<Declaration>();
            for (String identifier : analysisResult.getIdentifiers()) {
                Declaration declaration = declarationResolver.getDeclaration(identifier);
                if (declaration != null) {
                    declarations.add(declaration);
                }
            }
            if (declarations.size() == analysisResult.getIdentifiers().size()) {
                arguments[pos] = new QueryArgument.Expression(declarations, expression, getParserContext(context));
            } else {
                arguments[pos] = getLiteralQueryArgument(context, base, result);
            }
        }
    }
}
Also used : QueryArgument(org.drools.core.rule.QueryArgument) DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) ArrayList(java.util.ArrayList) List(java.util.List) Declaration(org.drools.core.rule.Declaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 3 with DeclarationScopeResolver

use of org.drools.core.spi.DeclarationScopeResolver in project drools by kiegroup.

the class LogicTransformer method fixClonedDeclarations.

/**
 * During the logic transformation, we eventually clone CEs,
 * specially patterns and corresponding declarations. So now
 * we need to fix any references to cloned declarations.
 */
protected void fixClonedDeclarations(GroupElement and, Map<String, Class<?>> globals) {
    Stack<RuleConditionElement> contextStack = new Stack<RuleConditionElement>();
    DeclarationScopeResolver resolver = new DeclarationScopeResolver(globals, contextStack);
    contextStack.push(and);
    processElement(resolver, contextStack, and);
    contextStack.pop();
}
Also used : DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) Stack(java.util.Stack)

Aggregations

DeclarationScopeResolver (org.drools.core.spi.DeclarationScopeResolver)3 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)2 Declaration (org.drools.core.rule.Declaration)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Stack (java.util.Stack)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 FromDescr (org.drools.compiler.lang.descr.FromDescr)1 MVELExprDescr (org.drools.compiler.lang.descr.MVELExprDescr)1 XpathPart (org.drools.compiler.rule.builder.XpathAnalysis.XpathPart)1 QueryArgument (org.drools.core.rule.QueryArgument)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1