Retrieving facts of a specific type from working memory

╄→гoц情女王★ 提交于 2019-12-18 04:54:16

问题


Instead of retrieving all facts i need to retrieve specific type of facts from working memory.

i learnt that i can retrieve all the facts from working memory as below.

drools.getWorkingMemory().getWorkingMemoryEntryPoint("Stream").getObjects();

Please provide some pointers to retrieve specific type of objects from working memory.


回答1:


Instead of using the getObjects() method you could use a query. Queries are like rules without RHS:

query "getObjectsOfClassA"
    $result: ClassA()
end

You can use all the power of DRL language inside your queries to create really complex matching patterns. You can even pass arguments to queries too: http://docs.jboss.org/drools/release/5.5.0.Final/drools-expert-docs/html_single/#d0e7632

Then, in your java code, you can invoke your query using:

QueryResults results = ksession.getQueryResults( "getObjectsOfClassA" ); 
for ( QueryResultsRow row : results ) {
    ClassA classA = ( ClassA ) row.get( "$result" ); //you can retrieve all the bounded variables here
    //do whatever you want with classA
}

If you need the set of all ClassA you can use an accumulate function in your query.

Hope it helps,




回答2:


You could use an ObjectFilter

Collection<Object> myfacts = session.getObjects( new ClassObjectFilter(MyFact.class) );

Unfortunately in Drools 5.5.0 Final, the resulting collection's contains method doesn't work as expected. Usually a contains method returns true, if the searched for object equals something in the collection, but Drool's collection only finds objects, which have the same reference (it uses == to compare objects).



来源:https://stackoverflow.com/questions/15035209/retrieving-facts-of-a-specific-type-from-working-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!