Search in sources :

Example 56 with KieFileSystem

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

the class Misc2Test method testMultilineStatement.

@Test
public void testMultilineStatement() {
    // BZ-1092502
    String str = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "System  \n" + "  .out  \n" + "  .println(\"hello\");\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)

Example 57 with KieFileSystem

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

the class Misc2Test method testNestedNots1.

@Test
public void testNestedNots1() {
    // DROOLS-444
    String str = "package org.test; " + "rule negation_over_nested " + "when " + " not ( (String() and Integer()) " + " or " + " (String() and Integer())) " + "then " + " System.out.println(\"negation_over_nested\"); " + "end " + " " + "rule negation_distributed_partially_no_sharing " + "when " + " (not (String() and Long())) " + " and " + " (not (String() and Long())) " + "then " + " System.out.println(\"negation_distributed_partially_no_sharing\"); " + "end " + " " + "rule negation_distributed_partially_sharing " + "when " + " (not (String() and Integer())) " + " and " + " (not (String() and Integer())) " + "then " + " System.out.println(\"negation_distributed_partially_sharing\"); " + "end " + " " + "rule negation_distributed_fully " + "when " + " ((not String()) or (not Integer())) " + " and " + " ((not String()) or (not Integer())) " + "then " + " System.out.println(\"negation_distributed_fully\"); " + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    int n = ksession.fireAllRules();
    assertEquals(7, n);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 58 with KieFileSystem

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

the class Misc2Test method testNPEOnMutableGlobal.

@Test
public void testNPEOnMutableGlobal() throws Exception {
    // BZ-1019473
    String str = "package org.drools.compiler\n" + "global java.util.List context\n" + "rule B\n" + "  when\n" + "    Message( message == \"b\" )\n" + "    $s : String() from context\n" + "  then\n" + "    System.out.println($s);\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());
    ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    assertNotNull(ksession);
    List<String> context = new ArrayList<String>();
    ksession.setGlobal("context", context);
    FactHandle b = ksession.insert(new Message("b"));
    ksession.delete(b);
    int fired = ksession.fireAllRules(1);
    assertEquals(0, fired);
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) 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 59 with KieFileSystem

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

the class Misc2Test method testTwoTimers.

@Test
public void testTwoTimers() {
    // BZ-980385
    String str = "import java.util.Date\n" + "import java.util.List\n" + "\n" + "global List dates\n" + "\n" + "rule \"intervalRule\"\n" + "  timer(int: 200ms 100ms)\n" + "when\n" + "  String(this == \"intervalRule\")\n" + "then\n" + "  Date date = new Date();\n" + "  dates.add(date);\n" + "end\n" + "\n" + "\n" + "// this rule stops timer\n" + "rule \"stopIntervalRule\"\n" + "  timer(int: 320ms)\n" + "when\n" + "  $s : String(this == \"intervalRule\")\n" + "then\n" + "  delete($s);\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    assertEquals(0, ks.newKieBuilder(kfs).buildAll().getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 60 with KieFileSystem

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

the class ListenersTest method prepareKieModule.

/**
 * Inserts a new KieModule containing single KieBase and a stateful and stateless KieSessions with listeners
 * into KieRepository.
 *
 * @return created KIE module ReleaseId
 */
private ReleaseId prepareKieModule() {
    final KieServices ks = KieServices.Factory.get();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
    baseModel.setDefault(true);
    baseModel.addPackage("*");
    KieSessionModel sessionModel = baseModel.newKieSessionModel("defaultKSession");
    sessionModel.setDefault(true);
    sessionModel.setType(KieSessionModel.KieSessionType.STATEFUL);
    sessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    sessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
    KieSessionModel statelessSessionModel = baseModel.newKieSessionModel("defaultStatelessKSession");
    statelessSessionModel.setDefault(true);
    statelessSessionModel.setType(KieSessionModel.KieSessionType.STATELESS);
    statelessSessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    statelessSessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(module.toXML());
    kfs.generateAndWritePomXML(RELEASE_ID);
    kfs.write("src/main/resources/" + PACKAGE_PATH + "/test.drl", ResourceFactory.newByteArrayResource(DRL.getBytes()));
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals("Unexpected compilation errors", 0, builder.getResults().getMessages().size());
    ks.getRepository().addKieModule(builder.getKieModule());
    return RELEASE_ID;
}
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) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

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