Search in sources :

Example 36 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class DMNWeaverService method merge.

@Override
public void merge(KieBase kieBase, KiePackage kiePkg, DMNPackage dmnpkg) {
    Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
    DMNPackageImpl existing = (DMNPackageImpl) map.get(ResourceType.DMN);
    if (existing == null) {
        existing = new DMNPackageImpl(dmnpkg.getNamespace());
        map.put(ResourceType.DMN, existing);
    }
    for (Map.Entry<String, DMNModel> entry : dmnpkg.getAllModels().entrySet()) {
        existing.addModel(entry.getKey(), entry.getValue());
    }
    existing.addProfiles(((DMNPackageImpl) dmnpkg).getProfiles());
}
Also used : DMNPackageImpl(org.kie.dmn.core.impl.DMNPackageImpl) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) DMNModel(org.kie.dmn.api.core.DMNModel) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 37 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class CanonicalKieBaseUpdater method run.

@Override
public void run() {
    CanonicalKieModule oldKM = (CanonicalKieModule) ctx.currentKM;
    CanonicalKieModule newKM = (CanonicalKieModule) ctx.newKM;
    List<RuleImpl> rulesToBeRemoved;
    List<RuleImpl> rulesToBeAdded;
    if (ctx.modifyingUsedClass) {
        // remove all ObjectTypeNodes for the modified classes
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
        rulesToBeRemoved = getAllRulesInKieBase(oldKM, ctx.currentKieBaseModel);
        rulesToBeAdded = getAllRulesInKieBase(newKM, ctx.newKieBaseModel);
    } else {
        newKM.setModuleClassLoader(((CanonicalKieModule) ctx.currentKM).getModuleClassLoader());
        CanonicalKiePackages newPkgs = newKM.getKiePackages(ctx.newKieBaseModel);
        rulesToBeRemoved = new ArrayList<>();
        rulesToBeAdded = new ArrayList<>();
        for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
            if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getResourceName())) {
                continue;
            }
            InternalKnowledgePackage oldKpkg = ctx.kBase.getPackage(changeSet.getResourceName());
            InternalKnowledgePackage kpkg = (InternalKnowledgePackage) newPkgs.getKiePackage(changeSet.getResourceName());
            for (ResourceChange change : changeSet.getChanges()) {
                String changedItemName = change.getName();
                if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.REMOVED) {
                    if (change.getType() == ResourceChange.Type.GLOBAL) {
                        ctx.kBase.removeGlobal(changedItemName);
                    } else {
                        rulesToBeRemoved.add(oldKpkg.getRule(changedItemName));
                    }
                }
                if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.ADDED) {
                    if (change.getType() == ResourceChange.Type.GLOBAL) {
                        try {
                            ctx.kBase.addGlobal(changedItemName, kpkg.getTypeResolver().resolveType(kpkg.getGlobals().get(changedItemName)));
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        rulesToBeAdded.add(kpkg.getRule(changedItemName));
                    }
                }
            }
        }
    }
    ctx.kBase.removeRules(rulesToBeRemoved);
    ctx.kBase.addRules(rulesToBeAdded);
}
Also used : CanonicalKiePackages(org.drools.modelcompiler.CanonicalKiePackages) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 38 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage 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)

Example 39 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class ClassHierarchyManager method mergeFields.

protected void mergeFields(String simpleSuperTypeName, String superTypePackageName, String fullSuper, TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver resolver) {
    Map<String, TypeFieldDescr> fieldMap = new LinkedHashMap<String, TypeFieldDescr>();
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, kbuilder.getPackageRegistry(typeDescr.getNamespace()));
    PackageRegistry registry = kbuilder.getPackageRegistry(superTypePackageName);
    InternalKnowledgePackage pack = null;
    if (registry != null) {
        pack = registry.getPackage();
    }
    if (unprocessableDescrs.containsKey(fullSuper)) {
        unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        return;
    }
    // if a class is declared in DRL, its package can't be null? The default package is replaced by "defaultpkg"
    boolean isSuperClassTagged = false;
    // in the same package, or in a previous one
    boolean isSuperClassDeclared = true;
    if (pack != null) {
        // look for the supertype declaration in available packages
        TypeDeclaration superTypeDeclaration = pack.getTypeDeclaration(simpleSuperTypeName);
        if (superTypeDeclaration != null && superTypeDeclaration.getTypeClassDef() != null) {
            ClassDefinition classDef = superTypeDeclaration.getTypeClassDef();
            // inherit fields
            for (org.kie.api.definition.type.FactField fld : classDef.getFields()) {
                TypeFieldDescr inheritedFlDescr = buildInheritedFieldDescrFromDefinition(fld, typeDescr);
                fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
            // new classes are already distinguished from tagged external classes
            isSuperClassTagged = !superTypeDeclaration.isNovel();
        }
    } else {
        isSuperClassDeclared = false;
    }
    // look for the class externally
    if (!isSuperClassDeclared || isSuperClassTagged) {
        try {
            Class superKlass;
            if (registry != null) {
                superKlass = registry.getTypeResolver().resolveType(fullSuper);
            } else {
                // if the supertype has not been declared, and we have got so far, it means that this class is not novel
                superKlass = resolver.resolveType(fullSuper);
            }
            buildDescrsFromFields(superKlass, typeDescr, registry, fieldMap);
        } catch (ClassNotFoundException cnfe) {
            unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            return;
        }
    }
    // notice that it is not possible to override a field changing its type
    for (String fieldName : typeDescr.getFields().keySet()) {
        if (fieldMap.containsKey(fieldName)) {
            String type1 = fieldMap.get(fieldName).getPattern().getObjectType();
            String type2 = typeDescr.getFields().get(fieldName).getPattern().getObjectType();
            if (type2.lastIndexOf(".") < 0) {
                try {
                    TypeResolver typeResolver = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getTypeResolver();
                    type1 = typeResolver.resolveType(type1).getName();
                    type2 = typeResolver.resolveType(type2).getName();
                    // now that we are at it... this will be needed later anyway
                    fieldMap.get(fieldName).getPattern().setObjectType(type1);
                    typeDescr.getFields().get(fieldName).getPattern().setObjectType(type2);
                } catch (ClassNotFoundException cnfe) {
                // will fail later
                }
            }
            boolean clash = !type1.equals(type2);
            TypeFieldDescr overriding = null;
            if (clash) {
                // this may still be an override using a subclass of the original type
                try {
                    Class<?> sup = resolver.resolveType(type1);
                    Class<?> loc = resolver.resolveType(type2);
                    if (sup.isAssignableFrom(loc)) {
                        clash = false;
                        // mark as non inherited so that a new field is actually built
                        overriding = fieldMap.get(fieldName);
                    }
                } catch (ClassNotFoundException cnfe) {
                // not much to do
                }
            }
            if (clash) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot redeclare field '" + fieldName + " from " + type1 + " to " + type2));
                typeDescr.setType(null, null);
                return;
            } else {
                String initVal = fieldMap.get(fieldName).getInitExpr();
                TypeFieldDescr fd = typeDescr.getFields().get(fieldName);
                if (fd.getInitExpr() == null) {
                    fd.setInitExpr(initVal);
                }
                fd.setInherited(fieldMap.get(fieldName).isInherited());
                fd.setOverriding(overriding);
                for (String key : fieldMap.get(fieldName).getAnnotationNames()) {
                    if (fd.getAnnotation(key) == null) {
                        fd.addAnnotation(fieldMap.get(fieldName).getAnnotation(key));
                    }
                }
                if (fd.getIndex() < 0) {
                    fd.setIndex(fieldMap.get(fieldName).getIndex());
                }
            }
        }
        fieldMap.put(fieldName, typeDescr.getFields().get(fieldName));
    }
    typeDescr.setFields(fieldMap);
}
Also used : TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassDefinition(org.drools.core.factmodel.ClassDefinition) LinkedHashMap(java.util.LinkedHashMap) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 40 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBuilderImpl method processKieBaseTypes.

protected void processKieBaseTypes() {
    if (!hasErrors() && this.kBase != null) {
        List<InternalKnowledgePackage> pkgs = new ArrayList<>();
        for (PackageRegistry pkgReg : pkgRegistryMap.values()) {
            pkgs.add(pkgReg.getPackage());
        }
        this.kBase.processAllTypesDeclaration(pkgs);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ArrayList(java.util.ArrayList) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8