[rules-users] Call class method from LHS of a rule

Esteban Aliverti esteban.aliverti at gmail.com
Mon Jan 4 08:25:43 EST 2010


Using Wolfgang's inline eval epression, I get the following error: "Cannot
use this in a static context" when compiling the rule. I changed
"this" by "$c" and it works. I don't know why we are in a static context.

The rule that worked for me is:

Rule "Rule1"
 when
   $c:Cheese(type == "chedar", eval($c.existsInShop ("shopName"))
 then
   System.out.println("Rule1 is fired");
end

I attached a working test case. Change $c to "*this" *in rule's condition
and you will see the error I mentioned.

Best,

2010/1/4 Wolfgang Laun <wolfgang.laun at gmail.com>

> What you need is called "inline eval expression" (see the Drools Expert
> manual),
> or an eval conditional element. The first one might be written as:
>
> $c:Cheese(type == "chedar", eval( this.existsInShop("shopName") ) )
>
> -W
>
>
>
> On Mon, Jan 4, 2010 at 12:49 PM, orchid <maya.pollack at gmail.com> wrote:
>
>>
>> Hi All,
>>
>> I'm trying to call a class method from the LHS of a rule in the following
>> way:
>>
>> public class Cheese{
>>  private string type;
>>  //setters and getters
>>  public boolean existsInShop(String shop){
>>    ...
>>    return true;
>>  }
>> }
>>
>> I would like to call the method "existsInShop" from a rule:
>>
>> Rule "Rule1"
>>  when
>>    $c:Cheese(type == "chedar", existsInShop "shopName")
>>  then
>>    System.out.println("Rule1 is fired");
>> end
>>
>> I get the compilation error: "no viable alternative at input "shopName" in
>> rule "Rule1" in pattern document". How can I call the method from the
>> rule?
>>
>> Thanks in advance.
>>
>>
>>
>> --
>> View this message in context:
>> http://n3.nabble.com/Call-class-method-from-LHS-of-a-rule-tp108383p108383.html
>> Sent from the Drools - User mailing list archive at Nabble.com.
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>


-- 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Esteban Aliverti
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20100104/a877d0a9/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MethodInvocationTest.java
Type: text/x-java
Size: 3448 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20100104/a877d0a9/attachment.bin 


More information about the rules-users mailing list