ok thanks, I reproduced the problem with the following below. If you
change the String insertion order it works. I'll get this fixed over
the weekend:
@Test
public void testQueryWithObject() throws Exception {
String str = "" +
"package org.drools.test \n" +
"import java.util.List\n" +
"import java.util.ArrayList\n" +
"global List list\n" +
"dialect \"mvel\"\n" +
"\n" +
"import
org.drools.integrationtests.BackwardChainingTest.Q\n" +
"import
org.drools.integrationtests.BackwardChainingTest.R\n" +
"import
org.drools.integrationtests.BackwardChainingTest.S\n"
+
"query object(Object o)\n" +
" o := Object() \n" +
"end\n" +
"rule collectObjects when\n" +
" String( this == 'go1' )\n" +
" object( o; )\n" +
"then\n" +
" list.add( o );\n" +
"end\n" +
"rule init when\n" +
" String( this == 'init' )\n" +
"then\n" +
" insert( new Q(1) );\n " +
" insert( new Q(5) );\n " +
" insert( new Q(6) );\n " +
" insert( new R(1) );\n " +
" insert( new R(4) );\n " +
" insert( new R(6) );\n " +
" insert( new R(2) );\n " +
" insert( new S(2) );\n " +
" insert( new S(3) );\n " +
" insert( new S(6) );\n " +
"end\n" +
"";
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource(
str.getBytes() ),
ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
fail( kbuilder.getErrors().toString() );
}
KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages()
);
kbase = SerializationHelper.serializeObject( kbase );
StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal( "list", list );
ksession.insert( "go1" );
ksession.fireAllRules();
ksession.insert( "init" );
ksession.fireAllRules();
System.out.println( list );
}
Mark
On 25/06/2011 11:22, Wolfgang Laun wrote:
// This rule prints all objects in WM.
rule findObject
when
$o: Object()
then
System.out.println( "Object: " + $o );
end
Happily I get
Object: 42
Object: a String object
Object: org.drools.reteoo.InitialFactImpl@4dde85f0
Now the query way:
query object( Object o )
o := Object()
end
rule findObjectByQuery
when
object( $a ; )
then
System.out.println( "Object by query: " + $a );
end
Surprisingly, I get just one:
Object by query: org.drools.reteoo.InitialFactImpl@4dde85f0
A marvellous effect can be observed when the condition in rule
findObjectByQuery is modified to
when
Object()
object( $a ; )
then
I'd have guessed that the above result would appear repeatedly,
once for each Object in WM,
but that's not so. For N-1 facts the rule fires N*(N+1)/2 times,
showing the "initial object"
N times, the first inserted object N-1 times, the second one N-2
times and, finally, the last one
once.
Object by query: 42
Object by query: org.drools.reteoo.InitialFactImpl@4dde85f0
Object by query: a String object
Object by query: 100
Object by query: 42
Object by query: org.drools.reteoo.InitialFactImpl@4dde85f0
Object by query: a String object
Object by query: org.drools.reteoo.InitialFactImpl@4dde85f0
Object by query: a String object
Object by query: org.drools.reteoo.InitialFactImpl@4dde85f0
-W
_______________________________________________
rules-dev mailing list
rules-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-dev