Search in sources :

Example 6 with JavaLocalDeclarationDescr

use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.

the class JavaAccumulateBuilder method buildInlineAccumulate.

private Accumulate buildInlineAccumulate(final RuleBuildContext context, final AccumulateDescr accumDescr, final RuleConditionElement source, Map<String, Declaration> decls, Map<String, Class<?>> declCls, final boolean readLocalsFromTuple) {
    // ELSE, if it is not an external function, build it using the regular java builder
    final String className = "Accumulate" + context.getNextId();
    accumDescr.setClassName(className);
    BoundIdentifiers available = new BoundIdentifiers(declCls, context);
    final JavaAnalysisResult initCodeAnalysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getInitCode(), available);
    final AnalysisResult actionCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
    final AnalysisResult resultCodeAnalysis = context.getDialect().analyzeExpression(context, accumDescr, accumDescr.getResultCode(), available);
    if (initCodeAnalysis == null || actionCodeAnalysis == null || resultCodeAnalysis == null) {
        // not possible to get the analysis results - compilation error has been already logged
        return null;
    }
    final Set<String> requiredDeclarations = new HashSet<String>(initCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    requiredDeclarations.addAll(actionCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    requiredDeclarations.addAll(resultCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    final Map<String, Class<?>> requiredGlobals = new HashMap<String, Class<?>>(initCodeAnalysis.getBoundIdentifiers().getGlobals());
    requiredGlobals.putAll(actionCodeAnalysis.getBoundIdentifiers().getGlobals());
    requiredGlobals.putAll(resultCodeAnalysis.getBoundIdentifiers().getGlobals());
    if (accumDescr.getReverseCode() != null) {
        final AnalysisResult reverseCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
        requiredDeclarations.addAll(reverseCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
        requiredGlobals.putAll(reverseCodeAnalysis.getBoundIdentifiers().getGlobals());
    }
    final Declaration[] declarations = new Declaration[requiredDeclarations.size()];
    int i = 0;
    for (Iterator<String> it = requiredDeclarations.iterator(); it.hasNext(); i++) {
        declarations[i] = decls.get(it.next());
    }
    final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
    Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
    final Map<String, Object> map = createVariableContext(className, null, context, declarations, null, requiredGlobals);
    map.put("className", accumDescr.getClassName());
    map.put("innerDeclarations", sourceDeclArr);
    map.put("isMultiPattern", readLocalsFromTuple ? Boolean.TRUE : Boolean.FALSE);
    final String initCode = this.fixInitCode(initCodeAnalysis, accumDescr.getInitCode());
    final String actionCode = accumDescr.getActionCode();
    final String resultCode = accumDescr.getResultCode();
    String[] attributesTypes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
    String[] attributes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
    int index = 0;
    for (Map.Entry<String, JavaLocalDeclarationDescr> entry : initCodeAnalysis.getLocalVariablesMap().entrySet()) {
        attributes[index] = entry.getKey();
        attributesTypes[index] = entry.getValue().getType();
        index++;
    }
    map.put("attributes", attributes);
    map.put("attributesTypes", attributesTypes);
    map.put("initCode", initCode);
    map.put("actionCode", actionCode);
    map.put("resultCode", resultCode);
    if (accumDescr.getReverseCode() == null) {
        map.put("reverseCode", "");
        map.put("supportsReverse", "false");
    } else {
        map.put("reverseCode", accumDescr.getReverseCode());
        map.put("supportsReverse", "true");
    }
    map.put("hashCode", actionCode.hashCode());
    SingleAccumulate accumulate = new SingleAccumulate(source, declarations);
    generateTemplates("accumulateInnerClass", "accumulateInvoker", context, className, map, accumulate.new Wirer(), accumDescr);
    return accumulate;
}
Also used : HashMap(java.util.HashMap) SingleAccumulate(org.drools.core.rule.SingleAccumulate) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr) Declaration(org.drools.core.rule.Declaration) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 7 with JavaLocalDeclarationDescr

use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.

the class JavaExprAnalyzer method analyzeBlock.

@SuppressWarnings("unchecked")
public JavaAnalysisResult analyzeBlock(final String expr, final BoundIdentifiers availableIdentifiers) throws RecognitionException {
    final JavaParser parser = parse("{" + expr + "}");
    parser.block();
    JavaAnalysisResult result = new JavaAnalysisResult();
    result.setAnalyzedExpr(expr);
    result.setIdentifiers(new HashSet<String>(parser.getIdentifiers()));
    result.setLocalVariables(new HashMap<String, JavaLocalDeclarationDescr>());
    if (parser.getRootBlockDescr().getInScopeLocalVars() != null) {
        for (JavaLocalDeclarationDescr descr : parser.getRootBlockDescr().getInScopeLocalVars()) {
            for (JavaLocalDeclarationDescr.IdentifierDescr ident : descr.getIdentifiers()) {
                result.addLocalVariable(ident.getIdentifier(), descr);
            }
        }
    }
    result.setBlockDescrs(parser.getRootBlockDescr());
    return analyze(result, availableIdentifiers);
}
Also used : JavaParser(org.drools.compiler.rule.builder.dialect.java.parser.JavaParser) JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr)

Example 8 with JavaLocalDeclarationDescr

use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.

the class JavaParser method localVariableDeclaration.

// $ANTLR end "blockStatement"
// $ANTLR start "localVariableDeclaration"
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:702:1: localVariableDeclaration : ( variableModifier )* type variableDeclarators ';' ;
public final void localVariableDeclaration() throws RecognitionException {
    VarDecl_stack.push(new VarDecl_scope());
    int localVariableDeclaration_StartIndex = input.index();
    ParserRuleReturnScope variableModifier1 = null;
    ParserRuleReturnScope type2 = null;
    VarDecl_stack.peek().descr = new JavaLocalDeclarationDescr();
    try {
        if (state.backtracking > 0 && alreadyParsedRule(input, 82)) {
            return;
        }
        // src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:710:5: ( ( variableModifier )* type variableDeclarators ';' )
        // src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:711:5: ( variableModifier )* type variableDeclarators ';'
        {
            // src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:711:5: ( variableModifier )*
            loop106: while (true) {
                int alt106 = 2;
                int LA106_0 = input.LA(1);
                if ((LA106_0 == 58 || LA106_0 == 83)) {
                    alt106 = 1;
                }
                switch(alt106) {
                    case 1:
                        // src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:711:7: variableModifier
                        {
                            pushFollow(FOLLOW_variableModifier_in_localVariableDeclaration2865);
                            variableModifier1 = variableModifier();
                            state._fsp--;
                            if (state.failed)
                                return;
                            if (state.backtracking == 0) {
                                VarDecl_stack.peek().descr.updateStart(((CommonToken) (variableModifier1 != null ? (variableModifier1.start) : null)).getStartIndex() - 1);
                                VarDecl_stack.peek().descr.addModifier((variableModifier1 != null ? input.toString(variableModifier1.start, variableModifier1.stop) : null));
                            }
                        }
                        break;
                    default:
                        break loop106;
                }
            }
            pushFollow(FOLLOW_type_in_localVariableDeclaration2888);
            type2 = type();
            state._fsp--;
            if (state.failed)
                return;
            if (state.backtracking == 0) {
                VarDecl_stack.peek().descr.updateStart(((CommonToken) (type2 != null ? (type2.start) : null)).getStartIndex() - 1);
                VarDecl_stack.peek().descr.setType((type2 != null ? input.toString(type2.start, type2.stop) : null));
                VarDecl_stack.peek().descr.setEnd(((CommonToken) (type2 != null ? (type2.stop) : null)).getStopIndex());
            }
            pushFollow(FOLLOW_variableDeclarators_in_localVariableDeclaration2904);
            variableDeclarators();
            state._fsp--;
            if (state.failed)
                return;
            match(input, 52, FOLLOW_52_in_localVariableDeclaration2906);
            if (state.failed)
                return;
        }
        if (state.backtracking == 0) {
            addLocalDeclaration(VarDecl_stack.peek().descr);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
    } finally {
        // do for sure before leaving
        if (state.backtracking > 0) {
            memoize(input, 82, localVariableDeclaration_StartIndex);
        }
        VarDecl_stack.pop();
    }
}
Also used : JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr)

Aggregations

JavaLocalDeclarationDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr)8 IdentifierDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr.IdentifierDescr)2 Declaration (org.drools.core.rule.Declaration)2 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)2 Constraint (org.drools.core.spi.Constraint)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 JavaParser (org.drools.compiler.rule.builder.dialect.java.parser.JavaParser)1 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)1 SingleAccumulate (org.drools.core.rule.SingleAccumulate)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)1