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\"");
}
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);
}
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());
}
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());
}
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));
}
Aggregations