Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

drools - Retrieving facts of a specific type from working memory

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...