Search in sources :

Example 1 with TypeDeclaration

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

the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.

private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
    BitMask leftMask = getLeftInferredMask();
    ObjectType leftObjectType = leftInput.getObjectType();
    if (leftObjectType instanceof ClassObjectType) {
        TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
        if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
            List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
            for (Declaration decl : accumulate.getRequiredDeclarations()) {
                if (leftObjectType.equals(decl.getPattern().getObjectType())) {
                    leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
                }
            }
        }
    }
    setLeftInferredMask(leftMask);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) BitMask(org.drools.core.util.bitmask.BitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with TypeDeclaration

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

the class KnowledgeBaseImpl method processTypeDeclaration.

protected void processTypeDeclaration(TypeDeclaration newDecl, InternalKnowledgePackage newPkg) throws ClassNotFoundException {
    JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
    TypeDeclaration typeDeclaration = this.classTypeDeclaration.get(newDecl.getTypeClassName());
    if (typeDeclaration == null) {
        String className = newDecl.getTypeClassName();
        byte[] def = runtime != null ? runtime.getClassDefinition(convertClassToResourcePath(className)) : null;
        Class<?> definedKlass = registerAndLoadTypeDefinition(className, def);
        if (definedKlass == null && newDecl.isNovel()) {
            throw new RuntimeException("Registering null bytes for class " + className);
        }
        if (newDecl.getTypeClassDef() == null) {
            newDecl.setTypeClassDef(new ClassDefinition());
        }
        newDecl.setTypeClass(definedKlass);
        this.classTypeDeclaration.put(className, newDecl);
        typeDeclaration = newDecl;
    } else {
        Class<?> definedKlass = typeDeclaration.getTypeClass();
        newDecl.getTypeClassDef().setDefinedClass(definedKlass);
        newDecl.setTypeClass(definedKlass);
        mergeTypeDeclarations(typeDeclaration, newDecl);
    }
    // update existing OTNs
    updateDependentTypes(typeDeclaration);
}
Also used : JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with TypeDeclaration

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

the class KnowledgeBaseImpl method processAllTypesDeclaration.

public void processAllTypesDeclaration(List<InternalKnowledgePackage> pkgs) {
    List<TypeDeclaration> allTypeDeclarations = new ArrayList<TypeDeclaration>();
    // Add all Type Declarations, this has to be done first incase packages cross reference each other during build process.
    for (InternalKnowledgePackage newPkg : pkgs) {
        // we have to do this before the merging, as it does some classloader resolving
        if (newPkg.getTypeDeclarations() != null) {
            allTypeDeclarations.addAll(newPkg.getTypeDeclarations().values());
        }
    }
    Collections.sort(allTypeDeclarations);
    String lastType = null;
    try {
        // add type declarations according to the global order
        for (TypeDeclaration newDecl : allTypeDeclarations) {
            lastType = newDecl.getTypeClassName();
            InternalKnowledgePackage newPkg = null;
            for (InternalKnowledgePackage kpkg : pkgs) {
                if (kpkg.getTypeDeclarations().containsKey(newDecl.getTypeName())) {
                    newPkg = kpkg;
                    break;
                }
            }
            processTypeDeclaration(newDecl, newPkg);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("unable to resolve Type Declaration class '" + lastType + "'", e);
    }
}
Also used : ArrayList(java.util.ArrayList) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 4 with TypeDeclaration

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

the class KnowledgeBaseImpl method getTypeDeclaration.

public TypeDeclaration getTypeDeclaration(Class<?> clazz) {
    TypeDeclaration typeDeclaration = getExactTypeDeclaration(clazz);
    if (typeDeclaration == null) {
        // check super classes and keep a score of how up in the hierarchy is there a declaration
        TypeDeclarationCandidate candidate = checkSuperClasses(clazz);
        // now check interfaces
        candidate = checkInterfaces(clazz, candidate, 1);
        if (candidate != null) {
            typeDeclaration = candidate.candidate;
        }
    }
    return typeDeclaration;
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 5 with TypeDeclaration

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

the class KnowledgeBaseImpl method mergePackage.

/**
 * Merge a new package with an existing package.
 * Most of the work is done by the concrete implementations,
 * but this class does some work (including combining imports, compilation data, globals,
 * and the actual Rule objects into the package).
 */
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg) {
    // Merge imports
    final Map<String, ImportDeclaration> imports = pkg.getImports();
    imports.putAll(newPkg.getImports());
    // Merge static imports
    for (String staticImport : newPkg.getStaticImports()) {
        pkg.addStaticImport(staticImport);
    }
    String lastIdent = null;
    String lastType = null;
    try {
        // merge globals
        if (newPkg.getGlobals() != null && newPkg.getGlobals() != Collections.EMPTY_MAP) {
            Map<String, String> globals = pkg.getGlobals();
            // Add globals
            for (final Map.Entry<String, String> entry : newPkg.getGlobals().entrySet()) {
                final String identifier = entry.getKey();
                final String type = entry.getValue();
                lastIdent = identifier;
                lastType = type;
                if (globals.containsKey(identifier) && !globals.get(identifier).equals(type)) {
                    throw new RuntimeException(pkg.getName() + " cannot be integrated");
                } else {
                    pkg.addGlobal(identifier, this.rootClassLoader.loadClass(type));
                    // this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
                    addGlobal(identifier, this.rootClassLoader.loadClass(type));
                }
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to resolve class '" + lastType + "' for global '" + lastIdent + "'");
    }
    // merge entry point declarations
    if (newPkg.getEntryPointIds() != null) {
        for (String ep : newPkg.getEntryPointIds()) {
            pkg.addEntryPointId(ep);
        }
    }
    // merge the type declarations
    if (newPkg.getTypeDeclarations() != null) {
        // add type declarations
        for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
            // @TODO should we allow overrides? only if the class is not in use.
            if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
                // add to package list of type declarations
                pkg.addTypeDeclaration(type);
            }
        }
    }
    // merge window declarations
    if (newPkg.getWindowDeclarations() != null) {
        // add window declarations
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
                pkg.addWindowDeclaration(window);
            } else {
                throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
            }
        }
    }
    // Merge rules into the RuleBase package
    // as this is needed for individual rule removal later on
    List<RuleImpl> rulesToBeRemoved = new ArrayList<RuleImpl>();
    for (Rule newRule : newPkg.getRules()) {
        // remove the rule if it already exists
        RuleImpl oldRule = pkg.getRule(newRule.getName());
        if (oldRule != null) {
            rulesToBeRemoved.add(oldRule);
        }
    }
    if (!rulesToBeRemoved.isEmpty()) {
        removeRules(rulesToBeRemoved);
    }
    for (Rule newRule : newPkg.getRules()) {
        pkg.addRule((RuleImpl) newRule);
    }
    // Merge The Rule Flows
    if (newPkg.getRuleFlows() != null) {
        for (Process flow : newPkg.getRuleFlows().values()) {
            pkg.addProcess(flow);
        }
    }
    if (!newPkg.getResourceTypePackages().isEmpty()) {
        for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
            ResourceType rt = rtkKpg.getResourceType();
            KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
            KieWeaverService weaver = weavers.getWeavers().get(rt);
            weaver.merge(this, pkg, rtkKpg);
        }
    }
}
Also used : KieWeaverService(org.kie.api.internal.weaver.KieWeaverService) ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) KieWeavers(org.kie.api.internal.weaver.KieWeavers) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Rule(org.kie.api.definition.rule.Rule) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

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