Search in sources :

Example 71 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class NegativePatternsTest method prepareKieSession.

@Before
public void prepareKieSession() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.NegativePatternsTest.TestEvent\n" + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "    @expires( 22ms )\n" + "end\n" + "\n" + "rule \"SingleAbsence\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"SingleConstrained\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( name == \"EventA\" ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEvents\"\n" + "    duration( 18ms )\n" + "    when\n" + "        TestEvent ( name == \"EventA\" ) over window:time( 22ms ) from entry-point EventStream\n" + "        not ( TestEvent ( name == \"EventB\" ) over window:time( 22ms )  from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEntryPoints\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not (\n" + "            TestEvent( name == \"EventA\" ) from entry-point EventStream\n" + "        )\n" + "        not (\n" + "            TestEvent( name == \"EventB\" ) from entry-point OtherStream\n" + "        )\n" + "    then\n" + "        // consequence\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kieModule = ks.newKieModuleModel();
    KieBaseModel defaultBase = kieModule.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
    defaultBase.newKieSessionModel(DEFAULT_SESSION_NAME).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(kieModule.toXML());
    KieModule builtModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    ks.getRepository().addKieModule(builtModule);
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(DEFAULT_SESSION_NAME);
    firedRulesListener = new TrackingAgendaEventListener();
    ksession.addEventListener(firedRulesListener);
}
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.api.builder.KieModule) Before(org.junit.Before)

Example 72 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class QueryCepFireUntilHaltTest method prepare.

@Before
public void prepare() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + TestEvent.class.getCanonicalName() + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "end\n" + "query EventsFromStream\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "end\n" + "query ZeroToNineteenSeconds\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "    $result : TestEvent ( this after [0s, 19s] $event) from entry-point SecondStream\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/queries.drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    firstEntryPoint = ksession.getEntryPoint("FirstStream");
    secondEntryPoint = ksession.getEntryPoint("SecondStream");
    clock = ksession.getSessionClock();
    this.startEngine();
}
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) Before(org.junit.Before)

Example 73 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class QueryInRHSCepTest method prepare1.

private void prepare1() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + SolicitFirePojo.class.getCanonicalName() + "\n" + "import " + QueryItemPojo.class.getCanonicalName() + "\n" + "global java.util.List myGlobal \n" + "declare SolicitFirePojo\n" + "    @role( event )\n" + "end\n" + "query \"myQuery\"\n" + "    $r : QueryItemPojo()\n" + "end\n" + "rule \"drools-usage/WLHxG8S\"\n" + " no-loop\n" + " when\n" + " SolicitFirePojo()\n" + " then\n" + " myGlobal.add(drools.getKieRuntime().getQueryResults(\"myQuery\"));\n" + " end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    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();
    clock = ksession.getSessionClock();
    myGlobal = new ArrayList<>();
    ksession.setGlobal("myGlobal", myGlobal);
}
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 74 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class QueryCepTest method prepare.

@Before
public void prepare() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + TestEvent.class.getCanonicalName() + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "end\n" + "query EventsFromStream\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "end\n" + "query ZeroToNineteenSeconds\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "    $result : TestEvent ( this after [0s, 19s] $event) from entry-point SecondStream\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/queries.drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    firstEntryPoint = ksession.getEntryPoint("FirstStream");
    secondEntryPoint = ksession.getEntryPoint("SecondStream");
    clock = ksession.getSessionClock();
}
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) Before(org.junit.Before)

Example 75 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class KieBaseIncludesTest method testKieBaseIncludesCrossKJarDifferentPackageNames.

/**
 * Test the inclusion of a KieBase defined in one KJAR into the KieBase of another KJAR.
 * <p/>
 * The 2 KieBases use different package names for the rules (i.e. "rules" and "rules2").
 */
@Test
public void testKieBaseIncludesCrossKJarDifferentPackageNames() {
    // @formatter:off
    String pomContent1 = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + "<modelVersion>4.0.0</modelVersion>\n" + "<groupId>org.kie</groupId>\n" + "<artifactId>rules-1</artifactId>\n" + "<version>1.0.0</version>\n" + "<packaging>jar</packaging>\n" + "<dependencies>\n" + "<dependency>\n" + "<groupId>org.kie</groupId>\n" + "<artifactId>rules-2</artifactId>\n" + "<version>1.0.0</version>\n" + "</dependency>\n" + "</dependencies>\n" + "</project>\n";
    String kmoduleContent1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "<kbase name=\"kbase1\" equalsBehavior=\"equality\" default=\"true\" packages=\"rules\" includes=\"kbase2\">\n" + "<ksession name=\"ksession1\" default=\"true\" type=\"stateful\"/>\n" + "</kbase>\n" + "</kmodule>";
    String drl1 = "package org.jboss.ddoyle.drools.rules1\n" + "\n" + "rule \"Rule in KieBase 1\"\n" + "when\n" + "then\n" + "System.out.println(\"Rule in KieBase1\");\n" + "end";
    String kmoduleContent2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "<kbase name=\"kbase2\" equalsBehavior=\"equality\" default=\"false\" packages=\"rules2\">\n" + "<ksession name=\"ksession2\" default=\"false\" type=\"stateful\"/>\n" + "</kbase>\n" + "</kmodule>";
    String drl2 = "package org.jboss.ddoyle.drools.rules2\n" + "\n" + "rule \"Rule in KieBase 2\"\n" + "when\n" + "then\n" + "System.out.println(\"Rule in KieBase2\");\n" + "end";
    // @formatter:on
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "rules-1", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "rules-2", "1.0.0");
    // First deploy the second KJAR on which the first one depends.
    KieFileSystem kfs2 = ks.newKieFileSystem().generateAndWritePomXML(releaseId2).write("src/main/resources/rules2/rules.drl", drl2).writeKModuleXML(kmoduleContent2);
    ks.newKieBuilder(kfs2).buildAll();
    KieFileSystem kfs1 = ks.newKieFileSystem().writePomXML(pomContent1).write("src/main/resources/rules/rules.drl", drl1).writeKModuleXML(kmoduleContent1);
    ks.newKieBuilder(kfs1).buildAll();
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kieBase = kc.getKieBase();
    // Assert the number of rules in the KieBase.
    long nrOfRules = getNumberOfRules(kieBase);
    // We should have 2 rules in our KieBase. One from our own DRL and one from the DRL in the KieBase we've included.
    assertEquals(2, nrOfRules);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieServices (org.kie.api.KieServices)479 Test (org.junit.Test)385 KieContainer (org.kie.api.runtime.KieContainer)249 KieFileSystem (org.kie.api.builder.KieFileSystem)242 KieSession (org.kie.api.runtime.KieSession)239 ReleaseId (org.kie.api.builder.ReleaseId)214 KieBuilder (org.kie.api.builder.KieBuilder)146 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)119 KieModule (org.kie.api.builder.KieModule)104 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)99 KieModuleModel (org.kie.api.builder.model.KieModuleModel)78 ArrayList (java.util.ArrayList)76 Results (org.kie.api.builder.Results)73 KieBase (org.kie.api.KieBase)59 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)49 KieBaseModel (org.kie.api.builder.model.KieBaseModel)45 IncrementalResults (org.kie.internal.builder.IncrementalResults)44 Message (org.drools.compiler.Message)43 Resource (org.kie.api.io.Resource)42 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)28