Search in sources :

Example 56 with KieServices

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

the class SecurityPolicyTest method testCustomAccumulate.

@Test
public void testCustomAccumulate() throws Exception {
    String drl = "package org.foo.bar\n" + "rule testRule\n" + "    when\n" + "        Number() from accumulate(Object(), " + "               init(System.exit(-1);), " + "               action(System.exit(-1);), " + "               reverse(System.exit(-1);), " + "               result(0))\n" + "    then\n" + "end";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (ShouldHavePrevented e) {
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (Exception e) {
    // test succeeded. the policy in place prevented the rule from executing the System.exit().
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) PropertyAccessException(org.mvel2.PropertyAccessException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 57 with KieServices

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

the class StrictAnnotationTest method testImportedAnnotation.

@Test
public void testImportedAnnotation() throws Exception {
    String str = "package org.simple \n" + "import " + Xyz.class.getCanonicalName() + " \n" + "@Xyz rule yyy \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str).writeKModuleXML(ks.newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()).toXML());
    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) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 58 with KieServices

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

the class SequentialTest method testSequentialWithNoLoop.

@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
    // BZ-1228098
    String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + "    $s : String( )" + "    $m : Message( )\n" + "then\n" + "    modify($m) { setMessage($s) };\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/r0.drl", str);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results results = kieBuilder.getResults();
    if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        throw new RuntimeException(results.getMessages().toString());
    }
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
    kieBaseConf.setOption(SequentialOption.YES);
    StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
    List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
    StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
    result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Results(org.kie.api.builder.Results) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 59 with KieServices

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

the class DslExpansionTest method testDSLExpansion_MessageImplNPE.

@Test
public void testDSLExpansion_MessageImplNPE() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "dsl-test", "1.0-SNAPSHOT");
    final KieModuleModel kproj = ks.newKieModuleModel();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML()).writePomXML(generatePomXml(releaseId)).write("src/main/resources/KBase1/test-dsl.dsl", createDSL()).write("src/main/resources/KBase1/test-rule.dslr", createDRL());
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    if (!messages.isEmpty()) {
        for (final Message m : messages) {
            System.out.println(m.getText());
        }
    }
    assertTrue(messages.isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 60 with KieServices

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

the class WireChannelTest method testWireChannel.

@Test
public void testWireChannel() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "listener-test", "1.0-SNAPSHOT");
    build(ks, releaseId);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieSession ksession = kieContainer.newKieSession();
    ksession.fireAllRules();
    assertEquals(1, channelMessages.size());
    assertEquals("Test Message", channelMessages.get(0));
}
Also used : KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) 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