[rules-users] Unification with logical or question

bdolbeare bdolbeare at yahoo.com
Fri Jun 8 15:23:45 EDT 2012


Can someone explain to me why these two rules behave differently?  I expected
that the second rule would produce the same output as the first but it seems
to ignore the first half of the logical or expression.  Is this because the
unification happens before the null checks occur?



declare Foo
	name : String
	item : Item
end
declare Item
	id : long
end

rule "insert stuff"
	when
	then
		Item item1 = new Item(1);
		
		Foo foo1 = new Foo("foo1", item1);
		insert(foo1);
		
		Foo foo3 = new Foo("foo3", null);
		insert(foo3);
end
rule "Unification Test Rule"
	when
		f : Foo ( item != null, $item := item )		
		f2 : (
			Foo ( item == null ) 
			or Foo ( $item := item ) 
			)
	then
		System.out.println(String.format("%s Fired:\n\tFoo: %s\n\t%s",
kcontext.getRule().getName(), f, f2));
end
rule "Unification Test Rule 2"
salience -10
	when
		f : Foo ( item != null, $item := item )		
		f2 : Foo ( ( item == null ) || ( item != null && $item := item ) )
	then
		System.out.println(String.format("%s Fired:\n\tFoo: %s\n\t%s",
kcontext.getRule().getName(), f, f2));
end


Unification Test Rule Fired:
	Foo: Foo( name=foo1, item=Item( id=1 ) )
	Foo( name=foo3, item=null )
Unification Test Rule Fired:
	Foo: Foo( name=foo1, item=Item( id=1 ) )
	Foo( name=foo1, item=Item( id=1 ) )

Unification Test Rule 2 Fired:
	Foo: Foo( name=foo1, item=Item( id=1 ) )
	Foo( name=foo1, item=Item( id=1 ) )

--
View this message in context: http://drools.46999.n3.nabble.com/Unification-with-logical-or-question-tp4017826.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list