I am new in Drools applications and above all in Drools fusion. In my office we are working on a Standalone Complex Event Processing application. One of main requirements is the possibility to calculate starting time, frequency, end time, duration of each event. We are testing Drools Fusion to do so and we made this example that doens't work. In this example we try to calculate duration. As side note, we were not able to calculate event timestamp and for now we resolved using System.currentTimeMillis().
Maybe Drools is not a good framework and we have to change our choice?
Sample.drl
import it.ipiu.other.SElDroolsTest.Message;
declare Message
@role(event)
@timestamp(time)
@duration(howMuch)
end
rule "Message"
when
Message() from entry-point "entry"
then
System.out.println("a message!!!");
end
and example class
package it.ipiu.other;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.common.EventFactHandle;
import org.drools.conf.EventProcessingOption;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.KnowledgeSessionConfiguration;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.conf.ClockTypeOption;
import org.drools.runtime.rule.FactHandle;
import org.drools.runtime.rule.WorkingMemoryEntryPoint;
import org.drools.time.SessionClock;
import org.drools.time.impl.PseudoClockScheduler;
/**
* This is a sample class to launch a rule.
*/
public class SElDroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
KnowledgeSessionConfiguration sessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfiguration.setOption(ClockTypeOption.get("pseudo"));
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(sessionConfiguration, null);
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
PseudoClockScheduler sessionClock = ksession.getSessionClock(); // !!!
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
message.setTime(System.currentTimeMillis());
WorkingMemoryEntryPoint entryPoint = ksession.getWorkingMemoryEntryPoint("entry");
sessionClock.advanceTime(1, TimeUnit.HOURS);
EventFactHandle factHandle = (EventFactHandle) entryPoint.insert(message);
sessionClock.advanceTime(1, TimeUnit.HOURS);
int fireAllRules = ksession.fireAllRules();
System.out.println("FACT : startTimeStamp " + factHandle.getStartTimestamp());
System.out.println("FACT : duration " + factHandle.getDuration());
System.out.println("FIRED : " + fireAllRules);
System.out.println("TIME STAMP " + message.getTime());
System.out.println("TIME DURATION " + message.getHowMuch());
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBaseConfiguration configuration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
configuration.setOption(EventProcessingOption.STREAM);
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(/*configuration*/);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private Long time = new Long(0);
private Long howMuch = new Long(0);
public Long getHowMuch() {
return howMuch;
}
public void setHowMuch(Long howMuch) {
this.howMuch = howMuch;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}