Search in sources :

Example 56 with KieBaseConfiguration

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

the class PatternTest method testDeclaringAndUsingBindsInSamePattern.

@Test
public void testDeclaringAndUsingBindsInSamePattern() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Sensor.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List sensors;\n" + "\n" + "rule \"BindsTest1_returnValue\"\n" + "    when\n" + "        $sensor1 : Sensor( $temp1 : temperature, pressure < $temp1 )\n" + "        $sensor2 : Sensor( $temp2 : temperature, pressure < ( $temp1 + $temp2 ) )\n" + "    then\n" + "        sensors.add( $sensor1 );\n" + "end\n" + "\n" + "rule \"BindsTest2_predicate\"\n" + "    when\n" + "        $sensor1 : Sensor( $temp1 : temperature, pressure < $temp1 )\n" + "        $sensor2 : Sensor( $temp2 : temperature, $p : pressure,  eval ( $p < ($temp1 + $temp2 ) ) )\n" + "    then\n" + "        sensors.add( $sensor1 );\n" + "end\n" + "\n" + "rule \"BindsTest3_eval\"\n" + "    when\n" + "        $sensor1 : Sensor( $temp1 : temperature, pressure < $temp1 )\n" + "        $sensor2 : Sensor( $temp2 : temperature, $p : pressure )\n" + "        eval( $p < $temp1 + $temp2 )\n" + "    then\n" + "        sensors.add( $sensor1 );\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("pattern-test", kieBaseTestConfiguration, drl);
    final KieContainer kieContainer = KieServices.get().newKieContainer(kieModule.getReleaseId());
    final KieBaseConfiguration kieBaseConfiguration = kieBaseTestConfiguration.getKieBaseConfiguration();
    kieBaseConfiguration.setOption(RemoveIdentitiesOption.YES);
    final KieBase kbase = kieContainer.newKieBase(kieBaseConfiguration);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List sensors = new ArrayList();
        ksession.setGlobal("sensors", sensors);
        final Sensor sensor1 = new Sensor(100, 150);
        ksession.insert(sensor1);
        ksession.fireAllRules();
        assertEquals(0, sensors.size());
        final Sensor sensor2 = new Sensor(200, 150);
        ksession.insert(sensor2);
        ksession.fireAllRules();
        assertEquals(3, sensors.size());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieModule(org.kie.api.builder.KieModule) Sensor(org.drools.testcoverage.common.model.Sensor) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 57 with KieBaseConfiguration

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

the class ObjectHashMapTest method testIdentityWithResize.

@Test
public void testIdentityWithResize() {
    KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kconf));
    KieSession ksession = kbase.newKieSession();
    int length = 1 * 300;
    List<FactHandle> handles = new ArrayList<FactHandle>(1000);
    List<String> objects = new ArrayList<String>(1000);
    for (int i = 0; i < length; i++) {
        String s = getPropertyName(i);
        FactHandle handle = ksession.insert(s);
        objects.add(s);
        handles.add(handle);
    }
    for (int i = 0; i < length; i++) {
        String s = objects.get(i);
        FactHandle handle = handles.get(i);
        assertEquals(s, ksession.getObject(handle));
        assertSame(handle, ksession.getFactHandle(s));
        // now check with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        assertEquals(s, ksession.getObject(handle));
    }
    for (int i = 0; i < length; i++) {
        FactHandle handle = handles.get(i);
        // now retract with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        ksession.retract(handle);
        assertEquals(length - i - 1, ksession.getObjects().size());
        assertEquals(length - i - 1, ksession.getFactHandles().size());
    }
    assertEquals(0, ksession.getObjects().size());
    assertEquals(0, ksession.getFactHandles().size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 58 with KieBaseConfiguration

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

the class ObjectHashMapTest method testEqualityWithResize.

@Test
public void testEqualityWithResize() {
    KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EqualityBehaviorOption.EQUALITY);
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kconf));
    KieSession ksession = kbase.newKieSession();
    int length = 1 * 300 * 1000;
    List<FactHandle> handles = new ArrayList<FactHandle>(1000);
    List<String> objects = new ArrayList<String>(1000);
    for (int i = 0; i < length; i++) {
        String s = getPropertyName(i);
        FactHandle handle = ksession.insert(s);
        objects.add(s);
        handles.add(handle);
    }
    for (int i = 0; i < length; i++) {
        String s = objects.get(i);
        FactHandle handle = handles.get(i);
        assertEquals(s, ksession.getObject(handle));
        assertSame(handle, ksession.getFactHandle(s));
        // now check with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        assertEquals(s, ksession.getObject(handle));
    }
    for (int i = 0; i < length; i++) {
        FactHandle handle = handles.get(i);
        // now retract with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        ksession.retract(handle);
        assertEquals(length - i - 1, ksession.getObjects().size());
        assertEquals(length - i - 1, ksession.getFactHandles().size());
    }
    assertEquals(0, ksession.getObjects().size());
    assertEquals(0, ksession.getFactHandles().size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 59 with KieBaseConfiguration

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

the class DynamicRulesTest method testJBRULES_2206.

@Test(timeout = 10000)
public void testJBRULES_2206() {
    KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
    ((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
    final KieModule kieModule = KieUtil.getKieModuleFromResources(KieUtil.generateReleaseId("test"), kieBaseTestConfiguration);
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromReleaseId(kieModule.getReleaseId(), config);
    KieSession session = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    for (int i = 0; i < 5; i++) {
        session.insert(new Cheese());
    }
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_1.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // two matching rules were added, so 2 activations should have been created
    verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
    int fireCount = session.fireAllRules();
    // both should have fired
    assertEquals(2, fireCount);
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // one rule was overridden and should activate
    verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
    fireCount = session.fireAllRules();
    // that rule should fire again
    assertEquals(1, fireCount);
    session.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 60 with KieBaseConfiguration

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

the class FailureOnRemovalTest method createKnowledgeBase.

private KieBase createKnowledgeBase(boolean shareBetaNodes) {
    KieBaseConfiguration ruleBaseConfiguration = createKnowledgeBaseConfiguration(shareBetaNodes);
    final KieModule kieModule = KieUtil.getKieModuleFromResources("test", kieBaseTestConfiguration);
    return KieBaseUtil.newKieBaseFromReleaseId(kieModule.getReleaseId(), ruleBaseConfiguration);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieModule(org.kie.api.builder.KieModule)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7