Search in sources :

Example 56 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class QueryInRHSCepTest method withInsertBeforeQueryCloudTest.

@Test
public void withInsertBeforeQueryCloudTest() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + SolicitFirePojo.class.getCanonicalName() + "\n" + "import " + QueryItemPojo.class.getCanonicalName() + "\n" + "global java.util.List myGlobal \n" + "query \"myQuery\"\n" + "    $r : QueryItemPojo()\n" + "end\n" + "rule \"drools-usage/WLHxG8S\"\n" + " no-loop\n" + " when\n" + " SolicitFirePojo()\n" + " then\n" + " insert(new QueryItemPojo());\n" + " myGlobal.add(drools.getKieRuntime().getQueryResults(\"myQuery\"));\n" + " end\n";
    System.out.println(drl);
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.CLOUD);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true);
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/" + this.getClass().getName() + ".drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    myGlobal = new ArrayList<>();
    ksession.setGlobal("myGlobal", myGlobal);
    ksession.insert(new QueryItemPojo());
    ksession.insert(new SolicitFirePojo());
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    assertEquals(1, myGlobal.size());
    // notice 1 is manually inserted, 1 get inserted from rule's RHS, for a total of 2.
    assertEquals(2, ((QueryResults) myGlobal.get(0)).size());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 57 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class RuleUnit method createKieProjectWithPackages.

private KieModuleModel createKieProjectWithPackages(KieServices ks) {
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setEventProcessingMode(EventProcessingOption.STREAM).addPackage("*").setDefault(true);
    kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel)

Example 58 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieModuleDeploymentConfig method getKieProject.

KieModuleModel getKieProject() {
    if (kproj != null) {
        return kproj;
    }
    kproj = getKieServicesInstance().newKieModuleModel();
    KieBaseModel kieBaseModel = kproj.newKieBaseModel(getKbaseName()).setDefault(true);
    kieBaseModel.newKieSessionModel(getKsessionName()).setDefault(true);
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel)

Example 59 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class AbstractKieProject method indexKieModule.

private void indexKieModule(Map<String, InternalKieModule> kJarFromKBaseName, InternalKieModule kJar, boolean isMainModule) {
    boolean defaultKieBaseFromMain = false;
    boolean defaultKieSessionFromMain = false;
    boolean defaultStatelessKieSessionFromMain = false;
    KieModuleModel kieProject = kJar.getKieModuleModel();
    for (KieBaseModel kieBaseModel : kieProject.getKieBaseModels().values()) {
        if (kieBaseModel.isDefault()) {
            if (defaultKieBase == null || (isMainModule && !defaultKieBaseFromMain)) {
                defaultKieBase = kieBaseModel;
                defaultKieBaseFromMain = isMainModule;
            } else {
                defaultKieBase = null;
                log.warn("Found more than one default KieBase: disabling all. KieBases will be accessible only by name");
            }
        }
        kBaseModels.put(kieBaseModel.getName(), kieBaseModel);
        // should already be set, but just in case
        ((KieBaseModelImpl) kieBaseModel).setKModule(kieProject);
        kJarFromKBaseName.put(kieBaseModel.getName(), kJar);
        for (KieSessionModel kieSessionModel : kieBaseModel.getKieSessionModels().values()) {
            if (kieSessionModel.isDefault()) {
                if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
                    if (defaultKieSession == null || (isMainModule && !defaultKieSessionFromMain)) {
                        defaultKieSession = kieSessionModel;
                        defaultKieSessionFromMain = isMainModule;
                    } else {
                        defaultKieSession = null;
                        log.warn("Found more than one default KieSession: disabling all. KieSessions will be accessible only by name");
                    }
                } else {
                    if (defaultStatelessKieSession == null || (isMainModule && !defaultStatelessKieSessionFromMain)) {
                        defaultStatelessKieSession = kieSessionModel;
                        defaultStatelessKieSessionFromMain = isMainModule;
                    } else {
                        defaultStatelessKieSession = null;
                        log.warn("Found more than one default StatelessKieSession: disabling all. StatelessKieSessions will be accessible only by name");
                    }
                }
            }
            // should already be set, but just in case
            ((KieSessionModelImpl) kieSessionModel).setKBase(kieBaseModel);
            kSessionModels.put(kieSessionModel.getName(), kieSessionModel);
        }
    }
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 60 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieBuilderImpl method setDefaultsforEmptyKieModule.

public static boolean setDefaultsforEmptyKieModule(KieModuleModel kModuleModel) {
    if (kModuleModel != null && kModuleModel.getKieBaseModels().isEmpty()) {
        // would be null if they pass a corrupted kModuleModel
        KieBaseModel kieBaseModel = kModuleModel.newKieBaseModel("defaultKieBase").addPackage("*").setDefault(true);
        kieBaseModel.newKieSessionModel("defaultKieSession").setDefault(true);
        kieBaseModel.newKieSessionModel("defaultStatelessKieSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(true);
        return true;
    }
    return false;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel)

Aggregations

KieBaseModel (org.kie.api.builder.model.KieBaseModel)74 KieModuleModel (org.kie.api.builder.model.KieModuleModel)55 KieServices (org.kie.api.KieServices)45 KieFileSystem (org.kie.api.builder.KieFileSystem)31 Test (org.junit.Test)26 KieSessionModel (org.kie.api.builder.model.KieSessionModel)24 KieBuilder (org.kie.api.builder.KieBuilder)22 KieContainer (org.kie.api.runtime.KieContainer)22 ReleaseId (org.kie.api.builder.ReleaseId)20 KieSession (org.kie.api.runtime.KieSession)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 ArrayList (java.util.ArrayList)7 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 KieBase (org.kie.api.KieBase)7 KieModule (org.kie.api.builder.KieModule)7 KieRepository (org.kie.api.builder.KieRepository)6 Before (org.junit.Before)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4