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