Search in sources :

Example 56 with KieModuleModel

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

the class ScenarioRunnerServiceTest method initKieSession.

private void initKieSession(String... resourceNames) {
    final KieServices kieServices = KieServices.Factory.get();
    final KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
    final KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("defaultKieBase").setDefault(true).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    kieBaseModel.newKieSessionModel("defaultKieSession").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).newWorkItemHandlerModel("Rest", "org.drools.workbench.screens.testscenario.backend.server.GithubContributorsWIH");
    final KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.writeKModuleXML(kieModuleModel.toXML());
    for (String resource : resourceNames) {
        kfs.write(ResourceFactory.newUrlResource(this.getClass().getResource(resource).toString()));
    }
    kieServices.newKieBuilder(kfs).buildAll();
    kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
    kieSession = kieContainer.newKieSession();
    doReturn(kieSession).when(sessionService).newDefaultKieSessionWithPseudoClock(any(KieModule.class));
}
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) KieModule(org.kie.workbench.common.services.shared.project.KieModule)

Example 57 with KieModuleModel

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

the class KieProjectRuntimeModulesTest method createMultpleJarAndFileResources.

@Test
public void createMultpleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kProjModel1 = createKieModule("jar1", true);
    KieModuleModel kProjModel2 = createKieModule("jar2", true);
    KieModuleModel kProjModel3 = createKieModule("jar3", true);
    KieModuleModel kProjModel4 = createKieModule("fol4", false);
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("jar1", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId("jar2", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId3 = KieServices.Factory.get().newReleaseId("jar3", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId4 = KieServices.Factory.get().newReleaseId("fol4", "art1", "1.0-SNAPSHOT");
    java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
    java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
    java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
    java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
    ZipKieModule mod1 = new ZipKieModule(releaseId1, kProjModel1, file1);
    ZipKieModule mod2 = new ZipKieModule(releaseId2, kProjModel2, file2);
    ZipKieModule mod3 = new ZipKieModule(releaseId3, kProjModel3, file3);
    FileKieModule mod4 = new FileKieModule(releaseId4, kProjModel4, fol4);
    mod1.addKieDependency(mod2);
    mod1.addKieDependency(mod3);
    mod1.addKieDependency(mod4);
    KieModuleKieProject kProject = new KieModuleKieProject(mod1);
    KieContainer kContainer = new KieContainerImpl(kProject, null);
    KieBase kBase = kContainer.getKieBase("jar1.KBase1");
    ClassLoader cl = ((KnowledgeBaseImpl) kBase).getRootClassLoader();
    Class cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
    assertNotNull(cls);
    testEntry(new KProjectTestClassImpl("jar1", kContainer), "jar1");
    testEntry(new KProjectTestClassImpl("jar2", kContainer), "jar2");
    testEntry(new KProjectTestClassImpl("jar3", kContainer), "jar3");
    testEntry(new KProjectTestClassImpl("fol4", kContainer), "fol4");
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 58 with KieModuleModel

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

the class KieBuilderTest method testGetKieBaseAfterKieSessionCreation.

@Test
public void testGetKieBaseAfterKieSessionCreation() {
    final String KBASE_NAME = "kieBase";
    final String KSESSION_NAME = "kieSession";
    final String drl = "declare TestEvent\n" + "    @role( event )\n" + "    name : String\n" + "end\n" + "\n" + "declare window DeclaredTimeWindow\n" + "    TestEvent ( name == \"timeDec\" ) over window:time( 50ms ) from entry-point EventStream\n" + "end";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/window.drl", drl);
    final KieModuleModel kmoduleModel = ks.newKieModuleModel();
    kmoduleModel.newKieBaseModel(KBASE_NAME).addPackage("*").newKieSessionModel(KSESSION_NAME).setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmoduleModel.toXML());
    final KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    ks.getRepository().addKieModule(builder.getKieModule());
    final KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(KSESSION_NAME);
    assertNotNull(kieSession);
    final KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase(KBASE_NAME);
    assertNotNull(kieBase);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 59 with KieModuleModel

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

the class KieBuilderTest method testKieBuilderWithDotFiles.

@Test
public void testKieBuilderWithDotFiles() {
    // BZ-1044409
    final String KBASE_NAME = "kieBase";
    final String KSESSION_NAME = "kieSession";
    // This can be any DRL, or any file recognised by drools-compiler
    final String drl = "declare TestEvent\n" + "  name : String\n" + "end\n";
    final String dotDrl = "Meta-data used by kie-wb";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/drlFile.drl", drl);
    kfs.write("src/main/resources/.drlFile.drl", dotDrl);
    final KieModuleModel kmoduleModel = ks.newKieModuleModel();
    kmoduleModel.newKieBaseModel(KBASE_NAME).addPackage("*").newKieSessionModel(KSESSION_NAME).setDefault(true);
    kfs.writeKModuleXML(kmoduleModel.toXML());
    final KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    for (final org.kie.api.builder.Message m : builder.getResults().getMessages()) {
        System.out.println(m);
    }
    assertEquals(0, builder.getResults().getMessages().size());
    ks.getRepository().addKieModule(builder.getKieModule());
    final KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(KSESSION_NAME);
    assertNotNull(kieSession);
    final KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase(KBASE_NAME);
    assertNotNull(kieBase);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 60 with KieModuleModel

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

the class KieHelloWorldTest method testHelloWorldWithKBaseInclude.

@Test
public void testHelloWorldWithKBaseInclude() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" + "declare CancelFact\n" + " cancel : boolean = true\n" + "end\n" + "rule R1 when\n" + " $m : CancelFact( cancel == true )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    KieModuleModel module = ks.newKieModuleModel();
    // define first kbase
    final String defaultBaseName = "defaultKBase";
    KieBaseModel defaultBase = module.newKieBaseModel(defaultBaseName);
    defaultBase.setDefault(true);
    defaultBase.addPackage("*");
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
    // define second kbase including resources of the first one
    final String includingBaseName = "includingKBase";
    KieBaseModel includingBase = module.newKieBaseModel(includingBaseName);
    includingBase.setDefault(false);
    includingBase.addInclude(defaultBaseName);
    includingBase.newKieSessionModel("includingKSession").setDefault(false);
    kfs.writeKModuleXML(module.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kb.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    FactType factType = ksession.getKieBase().getFactType("org.drools.compiler.integrationtests", "CancelFact");
    assertNotNull(factType);
    ksession.insert(factType.newInstance());
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
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) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

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