[rules-users] Property Navigation
Alessandro Lazarotti
alessandro at siq.com.br
Tue May 6 20:04:13 EDT 2008
Thanks Michael, I simulate.. your rule fire with successful.
I discovered the problem. The object in property (like Foo in your
example), when null, it's not throws NullPointerException, but
"org.mvel.PropertyAccessException: unable to resolve property".
With that, I never imagined that the problem was nullPointer, but I
thought about any access visibility (duh!).
[]'s
Alessandro
Michael B. escreveu:
> 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());
> }
>
> }
>
>
>
>
>
More information about the rules-users
mailing list