[jboss-jira] [JBoss JIRA] (DROOLS-651) Duplicate method call inside patterns

Davide Sottara (JIRA) issues at jboss.org
Sun Nov 16 20:34:39 EST 2014


    [ https://issues.jboss.org/browse/DROOLS-651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13020353#comment-13020353 ] 

Davide Sottara commented on DROOLS-651:
---------------------------------------

Drools "variables" are not variables at all, but "bindings": no value is cached internally.
In your example, you see two invocations: once during the evaluation of the LHS, once when the RHS is executed.

The decision is not so much based on performance, but correctness. 
It is good practice to ensure that methods used in constraints are idempotent (i.e. they cause no state change inside
the object and always return the same value), at least during the lifecycle of a rule - from evaluation to firing, otherwise
the engine needs to be notified using "modify/update".
If you can't or desire not to guarantee this property, Drools won't enforce it caching the first value returned.

I'm inclined to reject this ticket, at least as a bug. We may consider a feature request, but this kind of internal variables 
are not exactly in the spirit of declarative programming :)

Open for further discussion
Davide

> Duplicate method call inside patterns
> -------------------------------------
>
>                 Key: DROOLS-651
>                 URL: https://issues.jboss.org/browse/DROOLS-651
>             Project: Drools
>          Issue Type: Bug
>    Affects Versions: 6.2.0.Beta3
>         Environment: Win7, Drools 6.2.0 Beta3 (probably all Drools versions)
>            Reporter: Marc Dzaebel
>            Assignee: Mark Proctor
>            Priority: Minor
>
> Create sample Drools project, add following method to the Message class:
> {noformat}   public List<String> list() { 
>       System.out.println("Call!");
>       return Arrays.asList("one", "two"); 
>    } {noformat}
> sample.drl:
> {noformat}
> package com.sample
> import com.sample.DroolsTest.Message;
> import java.util.List;
> import java.util.ArrayList;
>  
> rule ""
>     when m : Message(L : list(), L.size>0)
>     then System.out.println("L:"+L);
> end
> {noformat}
> Running the project will result in the following output:
> {noformat}
> Call!
> Call!
> L:[one, two]
> {noformat}
> So _list()_ is called *two* times (duplicate). This may be due to the replacement of "L" with _list()_ in the second constraint. This is ok for simple +getters+ as here HotSpot optimizes this away. However, long running methods will produce unnecessary overhead, especially if further constraints on "L" are added (which I verified produce additional calls to list()). So at least methods with one or more arguments should be saved in temporary variables that should be used in further constraints. A fix of this problem will certainly accelerate Drools performance on similar patterns. If this is not feasible, at least a comment should be added to the documentation.



--
This message was sent by Atlassian JIRA
(v6.3.8#6338)


More information about the jboss-jira mailing list