Search in sources :

Example 6 with MVELAnalysisResult

use of org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult 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 7 with MVELAnalysisResult

use of org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult in project drools by kiegroup.

the class TypeDeclarationConfigurator method wireDurationAccessor.

private static void wireDurationAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
    Duration duration = annotated.getTypedAnnotation(Duration.class);
    if (duration != null) {
        BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
        String durationField;
        try {
            durationField = duration.value();
        } catch (Exception e) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
            return;
        }
        type.setDurationAttribute(durationField);
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, durationField, pkg);
        if (results != null) {
            type.setDurationExtractor(getFieldExtractor(type, durationField, pkg, results));
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error processing @duration for TypeDeclaration '" + type.getFullName() + "': cannot access the field '" + durationField + "'"));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) Duration(org.kie.api.definition.type.Duration) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)7 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)4 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Declaration (org.drools.core.rule.Declaration)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)2 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 Dialect (org.drools.compiler.compiler.Dialect)1 DroolsErrorWrapper (org.drools.compiler.compiler.DroolsErrorWrapper)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 DroolsWarningWrapper (org.drools.compiler.compiler.DroolsWarningWrapper)1 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)1 PackageBuildContext (org.drools.compiler.rule.builder.PackageBuildContext)1 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)1