Search in sources :

Example 1 with JavaModifyBlockDescr

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

the class DialectUtil method rewriteModifyDescr.

private static void rewriteModifyDescr(RuleBuildContext context, JavaBlockDescr d, String originalBlock, StringBuilder consequence, Declaration declr, String obj) {
    List<String> settableProperties = null;
    Class<?> typeClass = findModifiedClass(context, d, declr);
    TypeDeclaration typeDeclaration = typeClass == null ? null : context.getKnowledgeBuilder().getTypeDeclaration(typeClass);
    boolean isPropertyReactive = typeDeclaration != null && typeDeclaration.isPropertyReactive();
    if (isPropertyReactive) {
        typeDeclaration.setTypeClass(typeClass);
        settableProperties = typeDeclaration.getAccessibleProperties();
    }
    ConsequenceMetaData.Statement statement = null;
    if (typeDeclaration != null) {
        statement = new ConsequenceMetaData.Statement(ConsequenceMetaData.Statement.Type.MODIFY, typeClass);
        context.getRule().getConsequenceMetaData().addStatement(statement);
    }
    BitMask modificationMask = isPropertyReactive ? getEmptyPropertyReactiveMask(settableProperties.size()) : allSetButTraitBitMask();
    int end = originalBlock.indexOf("{");
    if (end == -1) {
        // no block
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Block missing after modify" + d.getTargetExpression() + " ?\n"));
        return;
    }
    addLineBreaks(consequence, originalBlock.substring(0, end));
    int start = end + 1;
    // adding each of the expressions:
    for (String exprStr : ((JavaModifyBlockDescr) d).getExpressions()) {
        end = originalBlock.indexOf(exprStr, start);
        addLineBreaks(consequence, originalBlock.substring(start, end));
        consequence.append(obj).append(".");
        consequence.append(exprStr);
        consequence.append("; ");
        start = end + exprStr.length();
        if (typeDeclaration != null) {
            modificationMask = parseModifiedProperties(statement, settableProperties, typeDeclaration, isPropertyReactive, modificationMask, exprStr);
        }
    }
    addLineBreaks(consequence, originalBlock.substring(end));
    appendUpdateStatement(consequence, declr, obj, modificationMask, typeClass);
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) JavaModifyBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaModifyBlockDescr) PropertySpecificUtil.allSetButTraitBitMask(org.drools.core.reteoo.PropertySpecificUtil.allSetButTraitBitMask) BitMask(org.drools.core.util.bitmask.BitMask) AllSetBitMask(org.drools.core.util.bitmask.AllSetBitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 JavaModifyBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaModifyBlockDescr)1 PropertySpecificUtil.allSetButTraitBitMask (org.drools.core.reteoo.PropertySpecificUtil.allSetButTraitBitMask)1 ConsequenceMetaData (org.drools.core.rule.ConsequenceMetaData)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 AllSetBitMask (org.drools.core.util.bitmask.AllSetBitMask)1 BitMask (org.drools.core.util.bitmask.BitMask)1