[rules-users] newbie: update and shallowCache

Edson Tirelli tirelli at post.com
Tue Apr 15 10:28:04 EDT 2008


   Regis,

   It is well known that from a performance perspective, rules engines work
more efficiently with flat models than with deeply nested models. That is
because rules engines usually implement relational-like algorithms (you can
make an analogy to relational database technology).
   It does not mean that you can't use pure OO models in Drools. In fact,
drools is one of the engines that better handles OO models (if it is not the
best). The example I gave you using "from" instead of the nested accessors
should work for your case just fine.

   Now, the immutability of the predicates is a different matter. To keep
working memory consistency, drools 4 makes use of shadow facts, that are
lazy cache objects implemented as transparent proxies for your facts. Of
course, it would not be feasible to cache everything in an object graph, and
so, drools caches only the root objects you assert as facts into the working
memory. When you use nested accessors, you are obviously accessing
non-cached data and for that reason they must be immutable or you will have
problems.
   Drools 5 is making the use of shadow proxies not required by implementing
a different strategy of constraint evaluation. But that is only in trunk,
not in 4.0.x branch.

   []s
   Edson


2008/4/15 Piccand, Regis <rpiccand at verisign.com>:

>  Hi Edson,
>
> Thanks for your help. I restructured my biz objects to avoid nested
> structures, which makes it work now (I have new objects to hold
> allocatedHour per subject).
>
> However, I have the feeling that this constraint has a certain impact on
> o-o design (more flat objects vs composite object). The documentation says
> that nested values should be considered immutable, unless using the modify
> block (as you suggested) and retract/re-assert the objects in the
> workingMemory ; it also has a much greater performance cost.
>
> Am I getting it right when saying that the object design should really be
> like 'many flat-type objects avoiding composite approach' to have Drools
> perform well or is it more sensible to have a proper o-o design and tweak
> Drools (using modify block, disabling cache, etc.)
>
> Thanks in advance for sharing your experience
>
> Regis
>
>
>
> *
>
> when
> *
>
> $lesson : Lesson(allocated ==
> *false*
> , session.type == Session.DISTINCT)
>
> $subjectAllocation : SubjectAllocation( subject == $lesson.subject,
> $allocatedDays : allocatedDays )
>
> $availableHour : Hour(allocated ==
> *false*, day *not* *memberOf* $allocatedDays)
>
> *then*
>
> $lesson.setHour($availableHour ) ;
>
> $lesson.setAllocated(
> *true* ) ;
>
> *update*($lesson) ;
>
> $availableHour.setSubject($lesson.getSubject() ) ;
>
> $availableHour.setAllocated(
> *true* ) ;
>
> *update*
> ($availableHour) ;
>
> $subjectAllocation.addAllocation($availableHour ) ;
>
> *update*($subjectAllocation) ;
>
>  ------------------------------
> *From:* rules-users-bounces at lists.jboss.org [mailto:
> rules-users-bounces at lists.jboss.org] *On Behalf Of *Edson Tirelli
> *Sent:* Monday, April 14, 2008 4:55 PM
> *To:* Rules Users List
> *Subject:* Re: [rules-users] newbie: update and shallowCache
>
>
>    Regis,
>
>    You are probably having problems with mutable predicates, when using "
> subject.allocatedDays". As a first step to understand your problem, I
> strongly encourage you to make constraints flat, i.e., avoid using nested
> accessors. After you get used to what can and what can't be done, you can go
> back to use nested accessors.
>    Also, I suggest you use modify() block instead of update():
>
> *
>
> rule
> *"allocate"
>
> *when*
>
> $lesson : Lesson(allocated == *false*, session.type == Session.DISTINCT )
>
> $subject : Subject( $allocatedDays : allocatedDays ) from $lesson.subject
>
> $availableHour : Hour(allocated == *false*, day *not* *memberOf*$allocatedDays)
>
> *then*
>
> modify( $lesson) {
>
>     setHour($availableHour ),
>
>     setAllocated(*true*)
>
> }
>
> modify( $availableHour )
>
>     setSubject($lesson.getSubject()),
>
>     setAllocated(*true* )
>
> }
> **
>
> *end*
>
>     Hope it helps.
>
>     []s
>     Edson
>
> 2008/4/14 Piccand, Regis <rpiccand at verisign.com>:
>
> >  Hi all,
> >
> > I am evaluating Drools and am stuck with a simple rule that selects
> > elements which do not correspond to the condition... Since I am updating
> > objects in the RHD, I guess it has to do with my wrong understanding (and
> > the cache...?)
> >
> >
> > I am trying to allocate lessons to periods (hour). My object model can
> > be summarize is as follows :
> >
> > Lesson
> >     boolean : allocated    // true if the lesson has been allocated to
> > an hour
> >     int : session             // a session is a bunch of lessons
> >     Subject : subject    // an allocated lesson is assigned a subject
> >
> > Subject
> >     Set<Day> : allocatedDays // days in which this subject is taught -
> > checked before allocation, as a subject can only be taught once a day
> >
> > Hour
> >     boolean : allocated // true is this hour has been allocated with a
> > lesson
> >     Day : day // the day this hour belongs to - required to check with
> > subject.allocatedDays
> >
> >
> > I put in the workingMemory a bunch of Lesson objects, which have been
> > set with Subject and Session id. I also put a bunch of Hour object, set with
> > a Day.
> >
> >
> > My rule is as follows:
> >
> > *
> >
> > rule
> > *"allocate"
> >
> > *when*
> >
> > $lesson : Lesson(allocated == *false*, session.type == Session.DISTINCT,
> > $allocatedDays : subject.allocatedDays)
> >
> > $availableHour : Hour(allocated == *false*, day *not* *memberOf*$allocatedDays)
> >
> > *then*
> >
> > $lesson.setHour($availableHour ) ;
> >
> > $lesson.setAllocated(*true*) ;
> >
> > *update*($lesson);
> >
> > $availableHour.setSubject($lesson.getSubject()) ;
> >
> > $availableHour.setAllocated(*true* );
> >
> > *update*($availableHour);
> >
> > *end*
> > When I set a debug on the first line of the THEN section, I can see that
> > sometimes $availableHour.allocated == true ... sometimes $lesson.allocated
> > == true (although it's an implicit And statement ...)
> >
> > Therefore, I can see that the update() works, but it seems that the rule
> > does not use the updated object (due to shallow copy ??)
> >
> > Any help more than welcome
> >
> > Regis
> >
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
>
> --
> Edson Tirelli
> JBoss Drools Core Development
> Office: +55 11 3529-6000
> Mobile: +55 11 9287-5646
> JBoss, a division of Red Hat @ www.jboss.com
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>


-- 
Edson Tirelli
JBoss Drools Core Development
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080415/c16ba7d0/attachment.html 


More information about the rules-users mailing list