Search in sources :

Example 41 with KieBaseModel

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

the class DeclarativeAgendaTest method testFiredRuleDoNotRefireAfterUnblock.

@Test
public void testFiredRuleDoNotRefireAfterUnblock() {
    // BZ-1038076
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.kie.api.runtime.rule.Match\n" + "import java.util.List\n" + "\n" + "global List list\n" + "\n" + "rule startAgenda\n" + "salience 100\n" + "when\n" + "    String( this == 'startAgenda' )\n" + "then\n" + "    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup(\"agenda\").setFocus();\n" + "    list.add(kcontext.getRule().getName());\n" + "end\n" + "\n" + "rule sales @department('sales')\n" + "agenda-group \"agenda\"\n" + "when\n" + "    $s : String( this == 'fireRules' )\n" + "then\n" + "    list.add(kcontext.getRule().getName());\n" + "end\n" + "\n" + "rule salesBlocker salience 10\n" + "when\n" + "    $s : String( this == 'fireBlockerRule' )\n" + "    $i : Match( department == 'sales' )\n" + "then\n" + "    kcontext.blockMatch( $i );\n" + "    list.add(kcontext.getRule().getName());\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setDeclarativeAgenda(DeclarativeAgendaOption.ENABLED);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true);
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write("src/main/resources/block_rule.drl", drl);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    List<String> list = new DebugList<String>();
    ksession.setGlobal("list", list);
    // first run
    ksession.insert("startAgenda");
    ksession.insert("fireRules");
    FactHandle fireBlockerRule = ksession.insert("fireBlockerRule");
    ksession.fireAllRules();
    String[] expected = { "startAgenda", "sales", "salesBlocker" };
    assertEquals(expected.length, list.size());
    for (int i = 0; i < list.size(); i++) {
        assertEquals(expected[i], list.get(i));
    }
    // second run
    ksession.delete(fireBlockerRule);
    list.clear();
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.dispose();
    list.clear();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 42 with KieBaseModel

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

the class CepJavaTypeTest method testJavaTypeAnnotatedWithRole_WindowTime.

@Test
public void testJavaTypeAnnotatedWithRole_WindowTime() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.CepJavaTypeTest.Event;\n" + "\n" + "rule \"CEP Window Time\"\n" + "when\n" + "    Event() over window:time (1d)\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel defaultBase = module.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*");
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(module.toXML());
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(builder.getResults().getMessages().isEmpty());
}
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) Test(org.junit.Test)

Example 43 with KieBaseModel

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

the class CepFireUntilHaltTimerTest method init.

public void init() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.CepFireUntilHaltTimerTest.MetadataEvent;\n" + "import java.util.List;\n" + "\n" + "global List countResult;\n" + "\n" + "declare MetadataEvent\n" + "    @role( event )\n" + "    @timestamp( metadataTimestamp )\n" + "    @duration( metadataDuration )\n" + "    @expires (24h)\n" + "end\n" + "\n" + "rule \"Number of metadata events in the last 10 seconds\"\n" + "timer (int: 1s 10s)\n" + "//timer (int: 0s 10s) // this works\n" + "when\n" + "    String( this == \"events_inserted\" )\n" + "    $count: Number() from accumulate( $event: MetadataEvent() over window:time(10s),  count( $event ) )\n" + "then\n" + "    System.out.println(\"Events count: \" + $count);\n" + "    countResult.add($count);\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel defaultBase = module.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(module.toXML());
    ks.newKieBuilder(kfs).buildAll();
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    result = new ArrayList<Long>();
    ksession.setGlobal("countResult", result);
    clock = ksession.getSessionClock();
    ksession.insert(clock);
}
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)

Example 44 with KieBaseModel

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

the class BenchmarkUtil method getDefaultKieModuleModel.

public static KieModuleModel getDefaultKieModuleModel(final KieServices ks) {
    final KieModuleModel kieModuleModel = ks.newKieModuleModel();
    final KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("kbase").setDefault(true);
    kieBaseModel1.newKieSessionModel("ksession").setDefault(true);
    return kieModuleModel;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel)

Example 45 with KieBaseModel

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

the class KieUtil method getDefaultKieModuleModel.

public static KieModuleModel getDefaultKieModuleModel(KieServices ks) {
    final KieModuleModel kproj = KieServices.get().newKieModuleModel();
    final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("kbase").setDefault(true);
    final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("ksession").setDefault(true);
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

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