Search in sources :

Example 16 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class PropertySpecificTest method testPropSpecOnPatternWithThis.

@Test(timeout = 5000)
public void testPropSpecOnPatternWithThis() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "declare A\n" + "    @propertyReactive\n" + "    i : int\n" + "end\n" + "declare B\n" + "    @propertyReactive\n" + "    a : A\n" + "end\n" + "rule R1\n" + "when\n" + "    $b : B();\n" + "    $a : A(this == $b.a);\n" + "then\n" + "    modify($b) { setA(null) };\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    KieSession ksession = kbase.newKieSession();
    FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
    Object factA = factTypeA.newInstance();
    factTypeA.set(factA, "i", 1);
    ksession.insert(factA);
    FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
    Object factB = factTypeB.newInstance();
    factTypeB.set(factB, "a", factA);
    ksession.insert(factB);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 17 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class PropertySpecificTest method testWatchNothing.

@Test
public void testWatchNothing() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "dialect \"mvel\"\n" + "declare A\n" + "    s : String\n" + "end\n" + "declare B\n" + "    @propertyReactive\n" + "    on : boolean\n" + "    s : String\n" + "end\n" + "rule R1\n" + "when\n" + "    A($s : s)\n" + "    $b : B(s != $s) @watch( !* )\n" + "then\n" + "    modify($b) { setS($s) }\n" + "end\n" + "rule R2\n" + "when\n" + "    $b : B(on == false)\n" + "then\n" + "    modify($b) { setOn(true) }\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    KieSession ksession = kbase.newKieSession();
    FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
    Object factA = factTypeA.newInstance();
    factTypeA.set(factA, "s", "y");
    ksession.insert(factA);
    FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
    Object factB = factTypeB.newInstance();
    factTypeB.set(factB, "on", false);
    factTypeB.set(factB, "s", "x");
    ksession.insert(factB);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
    assertEquals(true, factTypeB.get(factB, "on"));
    assertEquals("y", factTypeB.get(factB, "s"));
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 18 with FactType

use of org.kie.api.definition.type.FactType 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 19 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class PropertySpecificTest method testBetaWith2RTNSinksExec.

private void testBetaWith2RTNSinksExec(boolean addInfiniteLoopWatch) throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare A\n" + "   @propertyReactive\n" + "   x : int\n" + "end\n" + "declare B\n" + "   @propertyReactive\n" + "end\n" + "declare C\n" + "   @propertyReactive\n" + "   y : int\n" + "end\n" + "rule R1 when\n" + "   A ( x == 1 )\n" + "   B ( )\n" + (addInfiniteLoopWatch ? "   $c : C ( ) @watch(y)\n" : "   $c : C ( )\n") + "then " + "   modify( $c ) { setY( 2 ) };\n" + "   if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" + "end;\n" + "rule R2 when\n" + "   A ( x == 1 )\n" + "   B ( )\n" + "   C ( ) @watch(y)\n" + "then end;\n" + "rule InitA when\n" + "   $a : A ( x == 0 )\n" + "then\n" + "   modify( $a ) { setX( 1 ) };\n" + "end;\t\n" + "rule InitC salience 1 when\n" + "   $c : C ( y == 0 )\n" + "then\n" + "   modify( $c ) { setY( 1 ) };\n" + "end;\n";
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    KieSession ksession = kbase.newKieSession();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
    Object factA = factTypeA.newInstance();
    factTypeA.set(factA, "x", 0);
    ksession.insert(factA);
    FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
    Object factB = factTypeB.newInstance();
    ksession.insert(factB);
    FactType factTypeC = kbase.getFactType("org.drools.compiler.integrationtests", "C");
    Object factC = factTypeC.newInstance();
    factTypeC.set(factC, "y", 0);
    ksession.insert(factC);
    try {
        ksession.fireAllRules();
    } finally {
        assertEquals(2, factTypeC.get(factC, "y"));
        ksession.dispose();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType)

Example 20 with FactType

use of org.kie.api.definition.type.FactType 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)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9