Search in sources :

Example 1 with IdentifierDescr

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

the class DialectUtil method rewriteDescr.

private static boolean rewriteDescr(final RuleBuildContext context, final String originalCode, final MVELDialect mvel, final StringBuilder consequence, final JavaBlockDescr d, final BoundIdentifiers bindings, final Map<String, Declaration> decls) {
    if (d.getEnd() == 0) {
        // do nothing, it was incorrectly parsed, but this error should be picked up else where
        return false;
    }
    boolean typeSafety = context.isTypesafe();
    // we have to analyse in dynamic mode for now, as we cannot safely determine all input vars
    context.setTypesafe(false);
    Map<String, Class<?>> localTypes = d.getInputs();
    if (d.getInScopeLocalVars() != null && !d.getInScopeLocalVars().isEmpty()) {
        localTypes = new HashMap<String, Class<?>>(d.getInputs() != null ? d.getInputs() : Collections.EMPTY_MAP);
        for (JavaLocalDeclarationDescr local : d.getInScopeLocalVars()) {
            // these are variables declared in the code itself that are in the scope for this expression
            try {
                Class<?> type = context.getDialect("java").getPackageRegistry().getTypeResolver().resolveType(local.getRawType());
                for (IdentifierDescr id : local.getIdentifiers()) {
                    localTypes.put(id.getIdentifier(), type);
                }
            } catch (ClassNotFoundException e) {
                context.addError(new DescrBuildError(context.getRuleDescr(), context.getParentDescr(), null, "Unable to resolve type " + local.getRawType() + ":\n" + e.getMessage()));
            }
        }
    }
    MVELAnalysisResult mvelAnalysis = (MVELAnalysisResult) mvel.analyzeBlock(context, d.getTargetExpression(), bindings, localTypes, "drools", KnowledgeHelper.class);
    context.setTypesafe(typeSafety);
    if (mvelAnalysis == null) {
        // something bad happened, issue already logged in errors
        return false;
    }
    Class ret = mvelAnalysis.getReturnType();
    if (ret == null) {
        // not possible to evaluate expression return value
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), originalCode, "Unable to determine the resulting type of the expression: " + d.getTargetExpression() + "\n"));
        return false;
    }
    // adding modify expression
    String retString = ClassUtils.canonicalName(ret);
    String declrString;
    if (d.getTargetExpression().charAt(0) == '(') {
        declrString = d.getTargetExpression().substring(1, d.getTargetExpression().length() - 1).trim();
    } else {
        declrString = d.getTargetExpression();
    }
    String obj = declrString;
    Declaration declr = decls.get(declrString);
    consequence.append("{ ");
    if (declr == null) {
        obj = "__obj__";
        consequence.append(retString);
        consequence.append(" ");
        consequence.append(obj);
        consequence.append(" = ");
        consequence.append(d.getTargetExpression());
        consequence.append("; ");
    }
    if (declr == null || declr.isInternalFact()) {
        consequence.append("org.kie.api.runtime.rule.FactHandle ");
        consequence.append(obj);
        consequence.append("__Handle2__ = drools.getFactHandle(");
        consequence.append(obj);
        consequence.append(");");
    }
    // the following is a hack to preserve line breaks.
    String originalBlock = originalCode.substring(d.getStart() - 1, d.getEnd());
    switch(d.getType()) {
        case MODIFY:
            rewriteModifyDescr(context, d, originalBlock, consequence, declr, obj);
            break;
        case UPDATE:
            rewriteUpdateDescr(context, d, consequence, declr, obj);
            break;
        case DELETE:
            rewriteDeleteDescr(context, d, consequence, declr, obj);
            break;
    }
    return declr != null;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) IdentifierDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr.IdentifierDescr)

Example 2 with IdentifierDescr

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

the class DialectUtil method findDeclarationClass.

private static Class<?> findDeclarationClass(RuleBuildContext context, JavaBlockDescr d, String statement) {
    Class<?> inputClass = d.getInputs() == null ? null : d.getInputs().get(statement);
    if (inputClass != null) {
        return inputClass;
    }
    List<JavaLocalDeclarationDescr> localDeclarationDescrs = d.getInScopeLocalVars();
    if (localDeclarationDescrs == null) {
        return null;
    }
    String className = null;
    for (JavaLocalDeclarationDescr localDeclr : localDeclarationDescrs) {
        for (IdentifierDescr idDescr : localDeclr.getIdentifiers()) {
            if (statement.equals(idDescr.getIdentifier())) {
                className = localDeclr.getType();
                break;
            }
        }
        if (className != null) {
            break;
        }
    }
    return findClassByName(context, className);
}
Also used : JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr) IdentifierDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr.IdentifierDescr)

Aggregations

JavaLocalDeclarationDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr)2 IdentifierDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr.IdentifierDescr)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)1 Declaration (org.drools.core.rule.Declaration)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)1