[rules-users] Instantiate fact class declared only in drl
Hi, I have declared a fact in a drl file as follows: package a.b.c declare MyFact vehicle : Vehicle end The corresponding class file doesn't exists: it is created on the fly by drools at runtime. The class name should be a.b.c.MyFact, but the classloader cannot find it by using: Class c = Class.forName(a.b.c.MyFact); Is it possible to do that and how? I don't want to create the class because it is used only by rules inside the drl file, but I would like to write a test case for them. Thank you -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Instantiate-fact-class-declared-only-in-drl-tp3644780p3644780.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Instantiate fact class declared only in drl
Ok laune, but I would like to create this objects inside a junit test (so in a method defined in a *.java file), not from the drl itself. Were you suggesting a workaround? -- MM laune wrote I've found that creating objects and facts from DRL-declared classes is easily done in DRL, in RHS code. -W -- View this message in context: http://drools.46999.n3.nabble.com/Instantiate-fact-class-declared-only-in-drl-tp3644780p3644822.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Instantiate fact class declared only in drl
Thanks Esteban: this is exactly what I was looking for! -- MM Esteban wrote There is a full api that let you instantiate and manipulate declared types in Java code: http://docs.jboss.org/drools/release/5.4.0.Beta1/drools-expert-docs/html_single/index.html#d0e3750 -- View this message in context: http://drools.46999.n3.nabble.com/Instantiate-fact-class-declared-only-in-drl-tp3644780p3644872.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Rule evaluation logic
I have reduced my problem to a minimal example: - I use a pseudo clock and stream mode processing - The events are inserted using a succession of advanceTime(), insert(), fireAllRules() - I have three events named: Thief, SwitchOn and SwitchOff - I have three rules similar to: rule detect theft when $t : Thief() from entry-point thieves // Line A $lastSwitchOn : SwitchOn(this before $t) from entry-point lightsControl // Line B SwitchOff(this before $t, this after $lastSwitchOn) from entry-point lightsControl // Line C not( SwitchOff(this before $t, this after $lastSwitchOn) from entry-point lightsControl ) // Line D then System.out.println($t); end The first rule is composed of lines A, B, the second of lines A, B and C and the third of lines A, B and D. Please note that line D is the negation of line C. - Inserting the same sequence of events, only the first rule triggers. This seems strange to me, because: 1) if the first rule triggers, then Line A and Line B = true 2) if the second rule doesn't, then Line A and Line B and Line C = false. 3) from 1) and 2), it follows Line C = false 4) from 1) and 3), it follows that Line A and Line B and not Line C = true, so the third rule should be triggered, but it is not. Why? -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Rule-evaluation-logic-tp3620772p3620772.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Empty rule and event retention
Hello everybody, I'm using drools 5.3.0-final to process events in stream mode with a pseudo clock. I have created an do-nothing rule as follows: rule Do nothing when $m : Message( ) from entry-point fromDb then end Then I add a big number of events: Calendar timestamp = new GregorianCalendar(2011, Calendar.DECEMBER, 10, 12, 0, 0); for(int i = 0;i 10;i++) { Message message = new Message(test, timestamp.getTime()); advanceSessionClock(pseudoClock, message.getTimestamp()); fromDb.insert(message); // --- ksession.fireAllRules(); // --- timestamp.add(Calendar.HOUR_OF_DAY, 1); } At the loop exit the memory usage is about 600MB. Increasing the number of events increases also the ram usage up to about 1.4GB, then it remains constant. If I don't insert the messages (lines with the comments), but I still instantiate them, at the exit of the loop, the program requires only 90MB. With the given rule, it's not necessary to store the events, so in theory the insert() should do nothing... however, when the events are inserted, it seems that drools performs a lot of useless work and allocates a lot of memory. The fact that the consumption tops at 1.4GB (a quantity much higher than the 90MB used by the messages alone) tells us that at that point the gc is triggered and that drools correctly releases the references. It seems pointless to optimize the engine for this case, but this happens also when the rules are used as filters between streams: rule Filter test messages when $m : Message( message == test ) from entry-point fromDb then entryPoints[testMessages].insert($m); end rule Only test messages when $m : Message( ) from entry-point testMessages then end How can I decrease the memory footprint for these simple rules? -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Empty-rule-and-event-retention-tp3616771p3616771.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
Thank you for your precious help laune. With your explanations everything works perfectly. Using the correct sequence of operations and considering that a 1h window extends from 1h in the past to the future, I have been able to adapt my rules to make them work. -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3585074.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
laune wrote A virtual (pseudo) clock is indicated. The timestamp of the facts should control the advancement of the clock. When the window is moved, does Drools consider all the intermediate positions? (Since my rule considers events depending only if they are included or not in the window, obviously it's not needed to consider all the infinite positions, but only those that affect the rule activation) What about my third point? -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3566708.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
I have set the session clock type to pseudo, the processing mode is still stream and I have added the following rule: rule SingleEvent dialect mvel when $e0 : TriggerEvent() over window:time(1h) from entry-point EventStream then System.err.println(SingleEvent + $e0); end And I have changed the code in the following way: eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 0, 0).getTime())); eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 13, 0, 0).getTime())); eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 0, 0).getTime())); eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 10, 0).getTime())); EventFactHandle lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 20, 0).getTime())); System.err.println(The session clock is set to: + new Date(pseudoClock.getCurrentTime())); ksession.fireAllRules(); System.err.println(The session clock is set to: + new Date(pseudoClock.getCurrentTime())); It prints: The session clock is set to: Thu Jan 01 01:00:00 CET 1970 SingleEvent TriggerEvent [timestamp=Tue Dec 06 14:10:00 CET 2011] SingleEvent TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011] The session clock is set to: Thu Jan 01 01:00:00 CET 1970 This means that, with a pseudo clock, even if you don't update it, drools will consider also the events happening after it (The events are inserted in the entry-point, not as facts). In my opinion this doesn't make any sense. Adding others fireAllRules() and clock updates doesn't change the output. What am I doing wrong? Now I would like to trigger also the EventNotInhibited rule. I have defined a method that updates the session clock in one big step: private static void advanceSessionClock(StatefulKnowledgeSession ksession, long time) { final SessionPseudoClock pseudoClock = ksession.getSessionClock(); final long advance = time - pseudoClock.getCurrentTime(); if(advance 0) pseudoClock.advanceTime(advance, TimeUnit.MILLISECONDS); ksession.fireAllRules(); } The event insertion is done like this: lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 0, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 13, 0, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 0, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 10, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 20, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); This works as expected, but the rule is triggered after inserting the event at 13:00:00, so it could also be that drools has already removed the event at 12:00:00 (the 1h window is strict) and it triggers the event, so I have modified the timestamps like this: lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 10, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 13, 0, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 13, 50, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 0, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); lastEventHandle = (EventFactHandle) eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 10, 0).getTime())); advanceSessionClock(ksession, lastEventHandle.getStartTimestamp()); The rule should still be triggered, for example considering a 1h window betweeen 12:30:00 and 13:30:00. It is not, even changing the clock update method as follows: private static void advanceSessionClock(StatefulKnowledgeSession ksession,
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
laune wrote Do you really set the pseudo clock to the values in the timestamps? set the clock insert the event fire all rules Now, if I do: set the clock fire all rules insert the event i.e., something like this: nextEventTime = new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 10, 0).getTime(); advanceSessionClock(ksession, nextEventTime.getTime()); System.err.println(Firing rules); ksession.fireAllRules(); System.err.println(Inserting event); eventStream.insert(new InhibitEvent(nextEventTime)); everything works unexpectedly correctly. It works even when the clock is updated in one single step (instead of once per second). Doing the three steps in the sequence you suggested, i.e. nextEventTime = new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 10, 0).getTime(); advanceSessionClock(ksession, nextEventTime.getTime()); System.err.println(Inserting event); eventStream.insert(new InhibitEvent(nextEventTime)); System.err.println(Firing rules); ksession.fireAllRules(); never triggers the rule. Have you an idea why this happens? Does it exists a configuration option that implicitly updates the session clock with that of the last inserted event if the latter is in the future? -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3567049.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
laune wrote With the rules you gave, in STREAM mode and with a pseudo clock and running 5.2.0: Output: SingleEvent TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011] Found TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011] SingleEvent TriggerEvent [timestamp=Tue Dec 06 14:10:00 CET 2011] I cannot reproduce your output with 5.3.0 final. A possible explanation is that if you fireAllRules when the inhibit event @ 13:50:00 is already in the entry-point, the sliding window moves from 13:00:00, but it already sees the event in the future (if I have correctly understood, a 1h window sees the past 1h and all the future). The event @ 13:50:00 is of type Inhibit, so it blocks the rule. -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3567750.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Sliding window behavior in stream mode and realtime clock
laune wrote I didn't use an event with time stamp 1350. See the times in my previous mail; data was taken from your original post. With the data taken from the original post I can reproduce your output. Setting the timestamps to: 12:10 - 13:00 - 13:50 - 14:00 - 14:10 It still doesn't work. -- MM -- View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3567891.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Sliding window behavior in stream mode and realtime clock
Hi, The situation is the following: we need to process a stream of delayed events. This means that the event timestamp is provided for each one of them and that it doesn't generally correspond to the system time when we insert them in the entry-point. The events delay may vary, but they are always inserted in chronological order. My questions refer to the enclosed implementation and are: 1) Given my source code, would the timestamps correctly read and used? 2) I would like that the time window behaves as if it is initially placed at t = -inf and, when a new event is inserted in the entry-point, it would slide to the time of the last event timestamp. In my code I have used the stream mode and a realtime clock: is it correct? 3) When I run the given application, I expect it to print only: Found TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011] because it is the only event of type TriggerEvent for which there exists a window of size 1h that contains no InhibitEvent. Instead, sometimes it prints nothing and sometimes: Found TriggerEvent [timestamp=Tue Dec 06 14:10:00 CET 2011] Found TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011] How is this possible? How can I achieve my goal? (I'm currently using Drools 5.3.0 final.) Cheers Marco Mojana package com.sample; import java.sql.Date; import java.util.Calendar; import java.util.GregorianCalendar; 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.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.WorkingMemoryEntryPoint; public class DroolsTest { public static final void main(String[] args) { try { // load up the knowledge base KnowledgeBase kbase = readKnowledgeBase(); final KnowledgeSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption(ClockTypeOption.get(realtime)); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(sessionConfig, null); WorkingMemoryEntryPoint eventStream = ksession.getWorkingMemoryEntryPoint(EventStream); KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, test); // Insert events eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 12, 0, 0).getTime())); eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 13, 0, 0).getTime())); eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 0, 0).getTime())); eventStream.insert(new TriggerEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 10, 0).getTime())); eventStream.insert(new InhibitEvent(new GregorianCalendar(2011, Calendar.DECEMBER, 6, 14, 20, 0).getTime())); ksession.fireAllRules(); ksession.dispose(); logger.close(); } catch (Throwable t) { t.printStackTrace(); } } private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource(Rules.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.); } final KnowledgeBaseConfiguration kbConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kbConfig.setOption(EventProcessingOption.STREAM); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConfig);