Alberto,

  Follow my comments:

1. In case 1, the only difference between the rules is that inside eval() you are using java code, so "==" means identity compare. When you use Relation( startnode == n1), it will effectivelly call equals() method on the startnode attribute value with n1 as a parameter. This way, the first thing would be to check if equals() method and hashcode() method are correctly implemented in Node class. If this does not work, please send me a self contained code and I will run and find out what is happening. For now, if you want to use identity compare in Drools, you can also use predicates as I show bellow. We don't have an identity operator yet, but we will.

r : Relation ( sn : startnode -> ( sn == n1 ), en : endnode -> ( en == n2 ) )

2. For mathematical operators, you can use a "return value" constraint. As Drools boxes primitive types, the code is not that clean in java 1.4, but it is the best option for now. Look at the return value integration test (that I paste bellow) for a sample. Also, you can always use predicates and embed java code there.

------
package org.drools.test;

import org.drools.Person;

global java.util.List list;
global java.lang.Integer two;

rule "returnvalue rule test"
   when
       $person1:Person( $age1 : age )
       // We have no autoboxing of primtives, so have to do by hand
person2:Person( age == ( new Integer( $age1.intValue() + two.intValue() ) ) )
   then
       list.add( $person1 );
list.add( person2 ); end -----------

Hope it helps, and let me know if case 1 does not work after checking the equals() and hashcode() methods.

   []s
   Edson



Alberto Siena wrote:

Hi all,

I'm currently evaluating the use of drools for my research purposes and I have
two questions for you. I omit the java code because I think the problem is not
on that side, but if needed I can post it.

1. I have a directed graph with two nodes (N1 and N2) and (possibily) a relation
(R) between them. N1 has a value, let say, 1.0; what I'm trying to do is a
propagation mechanism that, if N2.value < N1.value, and the relation actually
exists between the nodes, then assigns N1.value to N2.value; otherwise, all
remains unchanged.
I wrote the following rule:

rule "Propagate values"

when

        n1 : Node( v1: value > 0 );
        
        n2 : Node( value < v1 );
        
        r : Relation();
        
        eval( r.startnode == n1 );
        eval( r.endnode == n2 );
        
then
        
        n2.value = n1.value;
        modify( n2 );

end

And it works. However, if I understood correctly, eval() is a second-best
choice. So I wrote this one:

rule "Propagate values"

when

        n1 : Node( v1: value > 0 );
        
        n2 : Node( value < v1 );
        
        r : Relation( startnode == n1, endgoal == n2 );
        
then
        
        n2.value = n1.value;
        modify( n2 );

end

But it won't work (the condition is never satisfied). Could you please explain
me why? (The objects in the workingMemory are the same, only the rule changes)


2. Let suppose the relation has a "metric", let say 0.5. The meaning of the
metric is this: if N2.value is less than (N1.value * R.metric), then N2.value =
(N1.value * R.metric); otherwise, it remains unchanged.
The first problem is: the "*" operator is not supported in rules. So, how could
I produce the same effect without using it? (And btw, will mathematical
operators be available in the future)?

Thank you

Alberto




Reply via email to