Search in sources :

Example 91 with KieModuleModel

use of org.kie.api.builder.model.KieModuleModel 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 92 with KieModuleModel

use of org.kie.api.builder.model.KieModuleModel 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 93 with KieModuleModel

use of org.kie.api.builder.model.KieModuleModel 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 94 with KieModuleModel

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

the class ClasspathKieProject method fetchKModule.

private static InternalKieModule fetchKModule(URL url, String fixedURL) {
    if (url.getPath().endsWith("-spring.xml")) {
        // the entire kmodule creation is happening in the kie-spring module,
        // hence we force a null return
        fetchKModuleFromSpring(url);
        return null;
    }
    KieModuleModel kieProject = KieModuleModelImpl.fromXML(url);
    setDefaultsforEmptyKieModule(kieProject);
    String pomProperties = getPomProperties(fixedURL);
    if (pomProperties == null) {
        log.warn("Cannot find maven pom properties for this project. Using the container's default ReleaseId");
    }
    ReleaseId releaseId = pomProperties != null ? ReleaseIdImpl.fromPropertiesString(pomProperties) : KieServices.Factory.get().getRepository().getDefaultReleaseId();
    String rootPath = fixedURL;
    if (rootPath.lastIndexOf(':') > 0) {
        rootPath = IoUtils.asSystemSpecificPath(rootPath, rootPath.lastIndexOf(':'));
    }
    return createInternalKieModule(kieProject, releaseId, rootPath);
}
Also used : KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId)

Example 95 with KieModuleModel

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

the class KieRepositoryImpl method getKieModule.

public KieModule getKieModule(Resource resource) {
    InternalResource res = (InternalResource) resource;
    try {
        KieModule kModule;
        // find kmodule.xml
        if (res.hasURL()) {
            String urlPath = res.getURL().toExternalForm();
            if (res.isDirectory()) {
                if (!urlPath.endsWith("/")) {
                    urlPath = urlPath + "/";
                }
                urlPath = urlPath + KieModuleModelImpl.KMODULE_JAR_PATH;
            } else {
                urlPath = "jar:" + urlPath + "!/" + KieModuleModelImpl.KMODULE_JAR_PATH;
            }
            kModule = ClasspathKieProject.fetchKModule(new URL(urlPath));
            log.debug("Fetched KieModule from resource: " + resource);
        } else {
            // might be a byte[] resource
            MemoryFileSystem mfs = MemoryFileSystem.readFromJar(res.getInputStream());
            byte[] bytes = mfs.getBytes(KieModuleModelImpl.KMODULE_JAR_PATH);
            KieModuleModel kieProject = KieModuleModelImpl.fromXML(new ByteArrayInputStream(bytes));
            setDefaultsforEmptyKieModule(kieProject);
            String pomProperties = mfs.findPomProperties();
            ReleaseId releaseId = ReleaseIdImpl.fromPropertiesString(pomProperties);
            kModule = new MemoryKieModule(releaseId, kieProject, mfs);
        }
        return kModule;
    } catch (Exception e) {
        throw new RuntimeException("Unable to fetch module from resource: " + res, e);
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) KieBuilderImpl.setDefaultsforEmptyKieModule(org.drools.compiler.kie.builder.impl.KieBuilderImpl.setDefaultsforEmptyKieModule) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

KieModuleModel (org.kie.api.builder.model.KieModuleModel)126 KieFileSystem (org.kie.api.builder.KieFileSystem)78 KieServices (org.kie.api.KieServices)76 Test (org.junit.Test)67 KieBuilder (org.kie.api.builder.KieBuilder)56 KieBaseModel (org.kie.api.builder.model.KieBaseModel)55 KieContainer (org.kie.api.runtime.KieContainer)50 ReleaseId (org.kie.api.builder.ReleaseId)38 KieSession (org.kie.api.runtime.KieSession)27 KieSessionModel (org.kie.api.builder.model.KieSessionModel)25 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)18 InternalKieContainer (org.drools.core.impl.InternalKieContainer)14 KieModule (org.kie.api.builder.KieModule)14 KieBase (org.kie.api.KieBase)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)10 Results (org.kie.api.builder.Results)10 Resource (org.kie.api.io.Resource)9 ArrayList (java.util.ArrayList)8 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)8 Before (org.junit.Before)7