Search in sources :

Example 71 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.

the class AbstractTerminalNode method initDeclaredMask.

public void initDeclaredMask(BuildContext context) {
    if (!(unwrapTupleSource() instanceof LeftInputAdapterNode)) {
        // RTN's not after LIANode are not relevant for property specific, so don't block anything.
        setDeclaredMask(AllSetBitMask.get());
        return;
    }
    Pattern pattern = context.getLastBuiltPatterns()[0];
    ObjectType objectType = pattern.getObjectType();
    if (!(objectType instanceof ClassObjectType)) {
        // InitialFact has no type declaration and cannot be property specific
        // Only ClassObjectType can use property specific
        setDeclaredMask(AllSetBitMask.get());
        return;
    }
    Class objectClass = ((ClassObjectType) objectType).getClassType();
    TypeDeclaration typeDeclaration = context.getKnowledgeBase().getTypeDeclaration(objectClass);
    if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
        // if property specific is not on, then accept all modification propagations
        setDeclaredMask(AllSetBitMask.get());
    } else {
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        Class modifiedClass = ((ClassObjectType) pattern.getObjectType()).getClassType();
        setDeclaredMask(calculatePositiveMask(modifiedClass, pattern.getListenedProperties(), settableProperties));
        setNegativeMask(calculateNegativeMask(modifiedClass, pattern.getListenedProperties(), settableProperties));
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 72 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.

the class ModifyInterceptor method calculateModificationMask.

private void calculateModificationMask(KnowledgeHelper knowledgeHelper, WithNode node) {
    Class<?> nodeClass = node.getEgressType();
    TypeDeclaration typeDeclaration = knowledgeHelper.getWorkingMemory().getKnowledgeBase().getTypeDeclaration(nodeClass);
    if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
        modificationMask = allSetButTraitBitMask();
        return;
    }
    List<String> settableProperties = typeDeclaration.getAccessibleProperties();
    modificationMask = getEmptyPropertyReactiveMask(settableProperties.size());
    // TODO: access parmValuePairs without reflection
    WithNode.ParmValuePair[] parmValuePairs = getFieldValue(WithNode.class, "withExpressions", node);
    for (WithNode.ParmValuePair parmValuePair : parmValuePairs) {
        Method method = extractMethod(parmValuePair);
        if (method == null) {
            modificationMask = allSetButTraitBitMask();
            return;
        }
        String propertyName = setter2property(method.getName());
        if (propertyName != null) {
            int index = settableProperties.indexOf(propertyName);
            if (index >= 0) {
                modificationMask = setPropertyOnMask(modificationMask, index);
            }
        }
        List<String> modifiedProps = typeDeclaration.getTypeClassDef().getModifiedPropsByMethod(method);
        if (modifiedProps != null) {
            for (String modifiedProp : modifiedProps) {
                int index = settableProperties.indexOf(modifiedProp);
                if (index >= 0) {
                    modificationMask = setPropertyOnMask(modificationMask, index);
                }
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) TypeDeclaration(org.drools.core.rule.TypeDeclaration) WithNode(org.mvel2.ast.WithNode)

Aggregations

TypeDeclaration (org.drools.core.rule.TypeDeclaration)72 Test (org.junit.Test)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)10 ClassDefinition (org.drools.core.factmodel.ClassDefinition)9 ClassObjectType (org.drools.core.base.ClassObjectType)7 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 ObjectType (org.drools.core.spi.ObjectType)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3