[rules-users] Property Navigation

Michael B. michael.bain at mckesson.com
Tue May 6 18:24:10 EDT 2008


Okay, I wrote a test case around the following and it works fine so let me
know if this isnt what you are working with...

Foo Class

public class Foo {
    
    private String m_name;
    
    public String getName()
    {
        return m_name;        
    }
    
    public void setName(String nm)
    {
        m_name = nm;
    }

}


Bar Class

public class Bar {
    
    private Foo m_foo;
    
    public Foo getFoo()
    {
        return m_foo;
    }
    
    public void setFoo(Foo f)
    {
        m_foo = f;
    }
    
    public String toString()
    {
        return "I have a foo with a name of " + m_foo.getName();
    }

}

DRL Rule

rule "FooBar Rule"
	
	when
		$bar : Bar( foo.name != "Wrong" )
	then 
        System.out.println("Test Successful - " + $bar.toString());
		
end


Junit Test Case

    public void testProperties() 
    {
        try
        {
            final Reader source = new InputStreamReader(
FooBarTest.class.getResourceAsStream( "FooBar.drl" ) );
            RuleBase ruleBase = RuleBaseFactory.newRuleBase();
            PackageBuilder builder = new PackageBuilder();
            
            builder.addPackageFromDrl(source);
            
            if ( builder.hasErrors() ) {
                System.out.println( builder.getErrors().toString() );
                throw new RuntimeException( "Unable to compile
\"FooBar.drl\".");
            }
            
            //get the compiled package (which is serializable)
            final Package pkg = builder.getPackage();

            //add the package to a rulebase (deploy the rule package).
            ruleBase.addPackage( pkg );
            final StatefulSession session = ruleBase.newStatefulSession();
            
            Foo foo = new Foo();
            foo.setName("Test");
            Bar bar = new Bar();
            bar.setFoo(foo);
            session.insert(bar);
            
            session.fireAllRules();                       
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
            fail(e.getMessage());
        }

    }




-- 
View this message in context: http://www.nabble.com/Property-Navigation-tp17089209p17093620.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list