Search in sources :

Example 16 with KieBuilder

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

the class WireChannelTest method build.

private void build(KieServices ks, ReleaseId releaseId) throws IOException {
    KieModuleModel kproj = ks.newKieModuleModel();
    KieSessionModel ksession1 = kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setDefault(true);
    ksession1.newChannelModel("testChannel", RecordingChannel.class.getName());
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML()).writePomXML(generatePomXml(releaseId)).write("src/main/resources/KBase1/rules.drl", createDRL());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 17 with KieBuilder

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

the class AbstractKnowledgeTest method createKieModule.

public KieModuleModel createKieModule(String namespace, boolean createJar, String version) throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kproj = new KieModuleModelImpl();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(namespace + ".KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(namespace + ".KBase1").setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".KBase2").setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    KieBaseModel kieBaseModel3 = kproj.newKieBaseModel(namespace + ".KBase3").addInclude(kieBaseModel1.getName()).addInclude(kieBaseModel2.getName()).setEqualsBehavior(EqualityBehaviorOption.IDENTITY).setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel3.newKieSessionModel(namespace + ".KSession4").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("pseudo"));
    KieServices ks = KieServices.Factory.get();
    KieFileSystemImpl kfs = (KieFileSystemImpl) ks.newKieFileSystem();
    kfs.write("src/main/resources/META-INF/beans.xml", generateBeansXML());
    kfs.writeKModuleXML(((KieModuleModelImpl) kproj).toXML());
    ReleaseId releaseId = ks.newReleaseId(namespace, "art1", version);
    kfs.generateAndWritePomXML(releaseId);
    String kBase1R1 = getRule(namespace + ".test1", "rule1", version);
    String kBase1R2 = getRule(namespace + ".test1", "rule2", version);
    String kbase2R1 = getRule(namespace + ".test2", "rule1", version);
    String kbase2R2 = getRule(namespace + ".test2", "rule2", version);
    String fldKB1 = "src/main/resources/" + kieBaseModel1.getName().replace('.', '/');
    String fldKB2 = "src/main/resources/" + kieBaseModel2.getName().replace('.', '/');
    kfs.write(fldKB1 + "/rule1.drl", kBase1R1.getBytes());
    kfs.write(fldKB1 + "/rule2.drl", kBase1R2.getBytes());
    kfs.write(fldKB2 + "/rule1.drl", kbase2R1.getBytes());
    kfs.write(fldKB2 + "/rule2.drl", kbase2R2.getBytes());
    kfs.write("src/main/java/org/drools/compiler/cdi/test/KProjectTestClass" + namespace + ".java", generateKProjectTestClass(kproj, namespace));
    KieBuilder kBuilder = ks.newKieBuilder(kfs);
    kBuilder.buildAll();
    if (kBuilder.getResults().hasMessages(Level.ERROR)) {
        fail("should not have errors" + kBuilder.getResults());
    }
    MemoryKieModule kieModule = (MemoryKieModule) kBuilder.getKieModule();
    MemoryFileSystem trgMfs = kieModule.getMemoryFileSystem();
    if (createJar) {
        trgMfs.writeAsJar(fileManager.getRootDirectory(), namespace + "-" + version);
    } else {
        java.io.File file = fileManager.newFile(namespace + "-" + version);
        trgMfs.writeAsFs(file);
    }
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl)

Example 18 with KieBuilder

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

the class SimulateTestBase method createKJarWithMultipleResources.

public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int i = 0; i < resources.length; i++) {
        String res = resources[i];
        String type = types[i].getDefaultExtension();
        kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
    }
    KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
    KieModule kieModule = kieBuilder.getKieModule();
    return kieModule.getReleaseId();
}
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) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 19 with KieBuilder

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

the class Misc2Test method testNonSerializableInEvaluatorWrapper.

@Test
public void testNonSerializableInEvaluatorWrapper() throws Exception {
    // BZ-1315143
    String str = "package org.drools.compiler\n" + "rule B\n" + "  when\n" + "    $m1 : Message( $message1 : message, $date1 : birthday )\n" + "    $m2 : Message( this != $m1, message != $message1, birthday after $date1 )\n" + "  then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(str.getBytes()).setTargetPath("org/drools/compiler/rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession1 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message1 = new Message();
    message1.setMessage("Hello World");
    message1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message2 = new Message();
    message2.setMessage("Goodbye World");
    message2.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession1.insert(message1);
    ksession1.insert(message2);
    int fired1 = ksession1.fireAllRules();
    assertEquals(1, fired1);
    ksession1.dispose();
    // Force deepClone
    KieSession ksession2 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message3 = new Message();
    message3.setMessage("Hello World");
    message3.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message4 = new Message();
    message4.setMessage("Goodbye World");
    message4.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession2.insert(message3);
    ksession2.insert(message4);
    int fired2 = ksession2.fireAllRules();
    assertEquals(1, fired2);
    ksession2.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 20 with KieBuilder

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

the class Misc2Test method testAutomaticallySwitchFromReteOOToPhreak.

@Test
public void testAutomaticallySwitchFromReteOOToPhreak() {
    String str = "rule R when then end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/rule.drl", str);
    KieBuilder kbuilder = ks.newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieBase kbase = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12