Search in sources :

Example 6 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class PropertySpecificTest method testClassReactive.

@Test
public void testClassReactive() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare B\n" + "    @classReactive\n" + "    on : boolean\n" + "    s : String\n" + "end\n" + "rule R1\n" + "when\n" + "    $b : B(s == \"test\")\n" + "then\n" + "    modify($b) { setOn(true) }\n" + "    if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" + "end\n";
    KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    config.setOption(PropertySpecificOption.ALWAYS);
    KieBase kbase = loadKnowledgeBaseFromString(config, rule);
    KieSession ksession = kbase.newKieSession();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
    Object factB = factTypeB.newInstance();
    factTypeB.set(factB, "s", "test");
    factTypeB.set(factB, "on", false);
    ksession.insert(factB);
    Assertions.assertThatThrownBy(() -> ksession.fireAllRules()).isInstanceOf(RuntimeException.class).hasMessageContaining("Exception executing consequence for rule \"R1\"");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 7 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class PropertySpecificTest method testConfig.

@Test(timeout = 5000)
public void testConfig() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "declare A\n" + "    i : int\n" + "    j : int\n" + "end\n" + "rule R1\n" + "when\n" + "    $a : A(i == 1)\n" + "then\n" + "    modify($a) { setJ(2) };\n" + "end\n";
    KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    config.setOption(PropertySpecificOption.ALWAYS);
    KieBase kbase = loadKnowledgeBaseFromString(config, rule);
    KieSession ksession = kbase.newKieSession();
    FactType typeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
    Object a = typeA.newInstance();
    typeA.set(a, "i", 1);
    ksession.insert(a);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 8 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class FunctionsTest method testFunctionCallingFunctionWithJanino.

@Test
public void testFunctionCallingFunctionWithJanino() throws Exception {
    KnowledgeBuilderConfiguration kbconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    kbconf.setProperty("drools.dialect.java.compiler", "JANINO");
    KieBase kbase = loadKnowledgeBase(kbconf, "test_functionCallingFunction.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("results", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(12, list.get(0).intValue());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 9 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class FunctionsTest method testFunctionCallingFunctionWithEclipse.

@Test
public void testFunctionCallingFunctionWithEclipse() throws Exception {
    KnowledgeBuilderConfiguration kbconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    kbconf.setProperty("drools.dialect.java.compiler", "ECLIPSE");
    KieBase kbase = loadKnowledgeBase(kbconf, "test_functionCallingFunction.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("results", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(12, list.get(0).intValue());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 10 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class Misc2Test method testBindingComplexExpressionWithDRL5.

@Test
public void testBindingComplexExpressionWithDRL5() {
    // DROOLS-43
    String drl = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "declare Foo \n" + "  a : int \n" + "  b : int \n" + "end \n" + "" + "rule Init when then insert( new Foo( 3, 4 ) ); end \n" + "" + "rule \"Expr\"\n" + "when\n" + "  $c := Integer() from new Integer( 4 ) \n" + "  Foo(  $a : a + b == 7 && a == 3 && $b : b > 0, $c := b - a == 1 ) \n" + "then\n" + "  list.add( $a );\n" + "  list.add( $b );\n" + "  list.add( $c );\n" + "end\n" + "\n";
    KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    kbConf.setOption(LanguageLevelOption.DRL5);
    KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertTrue(!list.isEmpty());
    assertEquals(3, list.size());
    assertEquals(3, list.get(0));
    assertEquals(4, list.get(1));
    assertEquals(4, list.get(2));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)25 Test (org.junit.Test)21 KieSession (org.kie.api.runtime.KieSession)16 KieBase (org.kie.api.KieBase)15 ArrayList (java.util.ArrayList)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)7 List (java.util.List)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Arrays.asList (java.util.Arrays.asList)4 URLClassLoader (java.net.URLClassLoader)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JarEntry (java.util.jar.JarEntry)2 JarInputStream (java.util.jar.JarInputStream)2 MapBackedClassLoader (org.drools.core.rule.MapBackedClassLoader)2 FactType (org.kie.api.definition.type.FactType)2 Resource (org.kie.api.io.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1