Search in sources :

Example 31 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 32 with KieFileSystem

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

the class OOPathCastTest method testInvalidCast.

@Test
public void testInvalidCast() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children#Man/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    final Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertTrue(results.hasMessages(Message.Level.ERROR));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 33 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 34 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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)

Example 35 with KieFileSystem

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

the class Misc2Test method testExtendingDate.

@Test
public void testExtendingDate() {
    // BZ-1072629
    String str = "import " + MyDate.class.getCanonicalName() + " \n" + "rule 'sample rule' \n" + "when \n" + "  $date: MyDate() \n" + "then \n" + "$date.setDescription(\"test\"); \n" + "end \n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertEquals(0, results.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18