Search in sources :

Example 1 with CanonicalKieModule

use of org.drools.modelcompiler.CanonicalKieModule in project drools by kiegroup.

the class BuildtimeUtil method generateKJarFromKieBuilder.

public static void generateKJarFromKieBuilder(final KieBuilder kieBuilder, final boolean useCanonicalModel) throws IOException {
    final ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId();
    final InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModule();
    final File kjarFile = bytesToTempFile(releaseId, kieModule.getBytes(), ".jar");
    final KieModule zipKieModule;
    if (useCanonicalModel) {
        zipKieModule = new CanonicalKieModule(releaseId, kieModule.getKieModuleModel(), kjarFile);
    } else {
        zipKieModule = new ZipKieModule(releaseId, kieModule.getKieModuleModel(), kjarFile);
    }
    KieServices.get().getRepository().addKieModule(zipKieModule);
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) ReleaseId(org.kie.api.builder.ReleaseId) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 2 with CanonicalKieModule

use of org.drools.modelcompiler.CanonicalKieModule in project drools by kiegroup.

the class BuildFromKJarBenchmark method buildKnowledge.

@Benchmark
public KieBase buildKnowledge(final Blackhole eater) {
    final KieModule zipKieModule = type.useRuleModel ? new CanonicalKieModule(releaseId, kieModuleModel, kjarFiles.getJarFile(), kjarFiles.getKnowledgeFiles()) : new ZipKieModule(releaseId, kieModuleModel, kjarFiles.getJarFile());
    kieRepository.addKieModule(zipKieModule);
    if (eater != null) {
        eater.consume(zipKieModule);
    }
    return kieServices.newKieContainer(releaseId).getKieBase();
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) KieModule(org.kie.api.builder.KieModule) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 3 with CanonicalKieModule

use of org.drools.modelcompiler.CanonicalKieModule 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 4 with CanonicalKieModule

use of org.drools.modelcompiler.CanonicalKieModule in project drools by kiegroup.

the class KieBaseUtil method generateKieModuleForCanonicalModel.

private static void generateKieModuleForCanonicalModel(KieBuilder kieBuilder) {
    KieServices ks = KieServices.get();
    ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId();
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModule();
    File kjarFile = FileUtil.bytesToTempKJARFile(releaseId, kieModule.getBytes(), ".jar");
    KieModule zipKieModule = new CanonicalKieModule(releaseId, KieUtil.getDefaultKieModuleModel(ks), kjarFile);
    ks.getRepository().addKieModule(zipKieModule);
}
Also used : CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 5 with CanonicalKieModule

use of org.drools.modelcompiler.CanonicalKieModule in project drools by kiegroup.

the class KieUtil method buildAndInstallKieModuleIntoRepo.

public static KieModule buildAndInstallKieModuleIntoRepo(final KieBaseTestConfiguration kieBaseTestConfiguration, final ReleaseId releaseId, final KieModuleModel kieModuleModel, final Resource... resources) {
    final KieServices kieServices = KieServices.Factory.get();
    final KieFileSystem fileSystem = getKieFileSystemWithKieModule(kieModuleModel, releaseId, resources);
    final KieBuilder builder = getKieBuilderFromKieFileSystem(kieBaseTestConfiguration, fileSystem, true);
    KieModule kieModule = builder.getKieModule();
    if (kieBaseTestConfiguration.useCanonicalModel()) {
        final File kjarFile = FileUtil.bytesToTempKJARFile(releaseId, ((InternalKieModule) kieModule).getBytes(), ".jar");
        kieModule = new CanonicalKieModule(releaseId, kieModuleModel, kjarFile);
    }
    kieServices.getRepository().addKieModule(kieModule);
    return kieModule;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) KieModule(org.kie.api.builder.KieModule)

Aggregations

CanonicalKieModule (org.drools.modelcompiler.CanonicalKieModule)5 KieModule (org.kie.api.builder.KieModule)4 File (java.io.File)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 ZipKieModule (org.drools.compiler.kie.builder.impl.ZipKieModule)2 KieServices (org.kie.api.KieServices)2 ReleaseId (org.kie.api.builder.ReleaseId)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 CanonicalKiePackages (org.drools.modelcompiler.CanonicalKiePackages)1 KieBuilder (org.kie.api.builder.KieBuilder)1 KieFileSystem (org.kie.api.builder.KieFileSystem)1 ResourceChange (org.kie.internal.builder.ResourceChange)1 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1