Hello,
We are upgrading our project from Drools 5.3 to Drools 5.5.0 and got
some problems. We tried to locate the problem and it seems there is a
problem with the WorkingMemoryEntryPoints?
In our project everything is in WorkingMemoryEntryPoints. In this
example all facts are in "test_me". We removed the dependencies to the
memoryentrypoint and then it works.
The problem is with the WorkingMemoryEntryPoints that the Rule "TestRule
- set state_2" is triggered more then two times.
The output is:
SETTING STATE 2
STATE 2
STATE 1
STATE 1
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
[...]
If we remove the entrypoints the output is:
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
[And that's it]
Is there something with the usage from the WorkingMemoryEntryPoints or
is this a bug?
Regards,
Per Sterner
TestObject.java
---------------------------------------------------------------------------
public class TestObject {
private String stateAsString;
public void setStateAsString(final String stateAsString) {
this.stateAsString = stateAsString;
}
public String getStateAsString() {
return stateAsString;
}
}
---------------------------------------------------------------------------
MyTicker.java
---------------------------------------------------------------------------
public class MyTicker {
private final long timeStart;
private long time;
public MyTicker() {
timeStart = System.currentTimeMillis();
}
public long getTime() {
return time;
}
public void setTime(final long time) {
this.time = time;
}
public long getTimeStart() {
return timeStart;
}
}
---------------------------------------------------------------------------
DroolsTest.java
---------------------------------------------------------------------------
public class DroolsTest {
public static void main(final String[] args) {
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("TestDrools.drl",
Init.class), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
throw new RuntimeException("Unable to compile
\"HelloWorld.drl\".");
}
final Collection<KnowledgePackage> pkgs =
kbuilder.getKnowledgePackages();
final KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(pkgs);
final StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
TestObject testObject1 = new TestObject();
testObject1.setStateAsString("STATE_1");
TestObject testObject2 = new TestObject();
testObject2.setStateAsString("STATE_1");
TestCache testCache = new TestCache();
MyTicker ticker = new MyTicker();
ticker.setTime(System.currentTimeMillis());
WorkingMemoryEntryPoint e =
ksession.getWorkingMemoryEntryPoint("test_me");
e.insert(testObject1);
e.insert(testObject2);
e.insert(testCache);
FactHandle tickerHandle = e.insert(ticker);
DroolsTickerThread droolsTickerThread = new
DroolsTickerThread("droolstickerthread", ticker, ksession, tickerHandle);
droolsTickerThread.start();
}
public static class DroolsTickerThread extends Thread {
private final DroolsTicker ticker;
private final StatefulKnowledgeSession ksession;
private final FactHandle tickerHandle;
public DroolsTickerThread(final String name, final DroolsTicker
ticker, final StatefulKnowledgeSession ksession,
final FactHandle tickerHandle) {
super(name);
this.ticker = ticker;
this.ksession = ksession;
this.tickerHandle = tickerHandle;
}
@Override
public void run() {
WorkingMemoryEntryPoint entry =
ksession.getWorkingMemoryEntryPoint("test_me");
while (!isInterrupted()) {
try {
ticker.setTime(System.currentTimeMillis());
entry.update(tickerHandle, ticker);
ksession.fireAllRules();
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
};
}
}
---------------------------------------------------------------------------
TestCache.java
---------------------------------------------------------------------------
public class TestCache {
private long lastCheckTimestamp = 0;
public void setLastCheckTimestamp(final long lastCheckTimestamp) {
this.lastCheckTimestamp = lastCheckTimestamp;
}
public long getLastCheckTimestamp() {
return lastCheckTimestamp;
}
}
---------------------------------------------------------------------------
TestDrools.drl
---------------------------------------------------------------------------
package de.drools.test
import de.drools.test.*;
rule "TestRule - set state_2"
dialect "mvel"
when
$obj: TestObject (
stateAsString == "STATE_1"
) from entry-point "test_me"
$cache: TestCache ( ) from entry-point "test_me"
DroolsTicker(
$time : time,
(time - $cache.lastCheckTimestamp) > 10000
) from entry-point "test_me"
then
System.out.println("SETTING STATE 2");
modify ($obj) {stateAsString = "STATE_2"};
modify ($cache) {lastCheckTimestamp = $time };
end
rule "TestRule - sysout state_2"
when
TestObject (
stateAsString == "STATE_2"
) from entry-point "test_me"
then
System.out.println("STATE 2");
end
rule "TestRule - sysout state_1"
when
TestObject (
stateAsString == "STATE_1"
) from entry-point "test_me"
then
System.out.println("STATE 1");
end
---------------------------------------------------------------------------