Search in sources :

Example 41 with TypeDeclaration

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

the class PatternBuilder method getExpirationForType.

private static ExpirationSpec getExpirationForType(BuildContext context, ObjectType objectType) {
    long offset = NEVER_EXPIRES;
    boolean hard = false;
    for (TypeDeclaration type : context.getKnowledgeBase().getTypeDeclarations()) {
        if (type.getObjectType().isAssignableFrom(objectType)) {
            if (hard) {
                if (type.getExpirationPolicy() == Policy.TIME_HARD && type.getExpirationOffset() > offset) {
                    offset = type.getExpirationOffset();
                }
            } else {
                if (type.getExpirationPolicy() == Policy.TIME_HARD) {
                    offset = type.getExpirationOffset();
                    hard = true;
                } else if (type.getExpirationOffset() > offset) {
                    offset = type.getExpirationOffset();
                }
            }
        }
    }
    // the same timestamp
    return new ExpirationSpec(offset == NEVER_EXPIRES ? NEVER_EXPIRES : offset + 1, hard);
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 42 with TypeDeclaration

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

the class ObjectSource method initDeclaredMask.

public void initDeclaredMask(BuildContext context) {
    if (context == null || context.getLastBuiltPatterns() == null) {
        // only happens during unit tests
        declaredMask = AllSetBitMask.get();
        return;
    }
    Pattern pattern = context.getLastBuiltPatterns()[0];
    ObjectType objectType = pattern.getObjectType();
    if (!(objectType instanceof ClassObjectType)) {
        // Only ClassObjectType can use property specific
        declaredMask = 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
        declaredMask = AllSetBitMask.get();
    } else {
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        declaredMask = calculateDeclaredMask(objectClass, 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 43 with TypeDeclaration

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

the class ReteooFactHandleFactory method newFactHandle.

/* (non-Javadoc)
     * @see org.kie.reteoo.FactHandleFactory#newFactHandle(long)
     */
public InternalFactHandle newFactHandle(final int id, final Object object, final long recency, final ObjectTypeConf conf, final InternalWorkingMemory workingMemory, final WorkingMemoryEntryPoint wmEntryPoint) {
    if (conf != null && conf.isEvent()) {
        TypeDeclaration type = conf.getTypeDeclaration();
        long timestamp;
        if (type != null && type.getTimestampExtractor() != null) {
            timestamp = type.getTimestampExtractor().getLongValue(workingMemory, object);
        } else {
            timestamp = workingMemory.getTimerService().getCurrentTime();
        }
        long duration = 0;
        if (type != null && type.getDurationExtractor() != null) {
            duration = type.getDurationExtractor().getLongValue(workingMemory, object);
        }
        return new EventFactHandle(id, object, recency, timestamp, duration, wmEntryPoint != null ? wmEntryPoint : workingMemory, conf != null && conf.isTrait());
    } else {
        return new DefaultFactHandle(id, object, recency, wmEntryPoint != null ? wmEntryPoint : workingMemory, conf != null && conf.isTrait());
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 44 with TypeDeclaration

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

the class KiePackagesBuilder method getClassObjectType.

private ObjectType getClassObjectType(Class<?> patternClass) {
    return objectTypeCache.computeIfAbsent(patternClass.getCanonicalName(), name -> {
        boolean isEvent = false;
        if (!name.startsWith("java.") && !patternClass.isPrimitive()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(patternClass.getPackage().getName(), this::createKiePackage);
            TypeDeclaration typeDeclaration = pkg.getTypeDeclaration(patternClass);
            if (typeDeclaration == null) {
                typeDeclaration = createTypeDeclaration(patternClass);
                pkg.addTypeDeclaration(typeDeclaration);
            }
            isEvent = typeDeclaration.getRole() == Role.Type.EVENT;
        }
        return new ClassObjectType(patternClass, isEvent);
    });
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 45 with TypeDeclaration

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

the class ModelBuilderImpl method registerTypeDeclarations.

private void registerTypeDeclarations(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        InternalKnowledgePackage pkg = getOrCreatePackageRegistry(packageDescr).getPackage();
        for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
            normalizeAnnotations(typeDescr, pkg.getTypeResolver(), false);
            TypeDeclaration type = new TypeDeclaration(typeDescr.getTypeName());
            type.setResource(typeDescr.getResource());
            TypeDeclarationFactory.processAnnotations(typeDescr, type);
            pkg.addTypeDeclaration(type);
        }
    }
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

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