Edson Tirelli <tirelli <at> post.com> writes:

> 
> 
>    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
> 
> 
> [cut]


Many thanks to both of you for your replies

Here below I attach the full Java code. The classes Node and Relation have all
the accessors, are derived simply from Object and don't override equals() nor
hashcode().

Using the getMetric() function actually works. (even if I would prefer a total
indipendence from Java :-)

Last question: $person1:Person( $age1 : age )
What the $ symbol stand for? I don't see it in the user manual.

Alberto


//////////////////////////////

package tropostest;

public class Node
{
        public String name = "";
        public float value = 0;
        
        public Node() {}
        public Node( String a_name )
        {
                name = a_name;
        }

        public float getValue()
        {
                return value;
        }
        public void setValue( float f )
        {
                value = f;
        }
        
        public String getName()
        {
                return name;
        }
        public void setName( String n )
        {
                name = n;
        }
}

//////////////////////////////////////////

package tropostest;

public class Relation
{
        public Node startnode;
        public Node endnode;
        
        public float metric = 1.0f;
        
        public Node getStartnode()
        {
                return startnode;
        }
        public void setStartnode( Node n )
        {
                startnode = n;
        }
        public Node getEndnode()
        {
                return endnode;
        }
        public void setEndnode( Node n )
        {
                endnode = n;
        }
        
        public float getMetric()
        {
                return metric;
        }
        public void setMetric( float m )
        {
                metric = m;
        }
}

/////////////////////////////////////

public class Test
{
    public static final void main(String[] args) {
        try {
                
                //load up the rulebase
            RuleBase ruleBase = readRule();
            WorkingMemory workingMemory = ruleBase.newWorkingMemory();
            
            //go !
            Node n1 = new Node( "N1" );
            Node n2 = new Node( "N2" );
            Node n3 = new Node( "N3" );
            Node n4 = new Node( "N4" );
            
            Relation r1 = new Relation();
            r1.setStartnode( n1 );
            r1.setEndnode( n2 );
            
            Relation r2 = new Relation();
            r2.setStartnode( n2 );
            r2.setEndnode( n3 );
            
            Relation r3 = new Relation();
            r3.setStartnode( n4 );
            r3.setEndnode( n3 );
            
            n1.setValue( 0.5f );
            n4.setValue( 0.7f );
            
            workingMemory.assertObject( n1 );
            workingMemory.assertObject( n2 );
            workingMemory.assertObject( n3 );
            workingMemory.assertObject( n4 );
            workingMemory.assertObject( r1 );
            //workingMemory.assertObject( r2 );
            //workingMemory.assertObject( r3 );
            
                System.out.println( "Initial state ( " + 
                                n1.name + ":" + n1.value + ", " + 
                                n2.name + ":" + n2.value + ", " + 
                                n3.name + ":" + n3.value + ")" );
            
            workingMemory.fireAllRules();
            
                System.out.println( "Final state ( " + 
                                n1.name + ":" + n1.value + ", " + 
                                n2.name + ":" + n2.value + ", " + 
                                n3.name + ":" + n3.value + ")" );
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

    /**
     * Please note that this is the "low level" rule assembly API.
     */
        private static RuleBase readRule() throws Exception {
                //read in the source
                Reader source = new InputStreamReader( 
Test.class.getResourceAsStream(
"/BasicTropos.drl" ) );
                
                //optionally read in the DSL (if you are using it).
                //Reader dsl = new InputStreamReader( 
DroolsTest.class.getResourceAsStream(
"/mylang.dsl" ) );

                //Use package builder to build up a rule package.
                //An alternative lower level class called "DrlParser" can also 
be used...
                
                PackageBuilder builder = new PackageBuilder();

                //this wil parse and compile in one step
                //NOTE: There are 2 methods here, the one argument one is for 
normal DRL.
                builder.addPackageFromDrl( source );

                //Use the following instead of above if you are using a DSL:
                //builder.addPackageFromDrl( source, dsl );
                
                //get the compiled package (which is serializable)
                Package pkg = builder.getPackage();
                
                //add the package to a rulebase (deploy the rule package).
                RuleBase ruleBase = RuleBaseFactory.newRuleBase();
                ruleBase.addPackage( pkg );
                return ruleBase;
        }
}


Reply via email to