Permalink
Browse files

Improve response collection

  • Loading branch information...
1 parent 0615a7c commit 8b7836bf452a98f345df076ca7c91ba0d3ed7d3a @sotty sotty committed Mar 31, 2012
@@ -16,6 +16,7 @@
package org.drools.mas.core;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.drools.grid.*;
@@ -28,6 +29,7 @@
import org.drools.mas.util.helper.SessionLocator;
import org.drools.runtime.rule.QueryResults;
import org.drools.runtime.rule.QueryResultsRow;
+import org.drools.runtime.rule.Variable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -170,13 +172,15 @@ public StatefulKnowledgeSession getMind() {
}
public List<ACLMessage> getAgentAnswers(String msgId){
- List<ACLMessage> answers = new ArrayList<ACLMessage>();
- QueryResults results = mind.getQueryResults("getAnswers",new Object[]{msgId});
+
+ QueryResults results = mind.getQueryResults( "getAnswers", new Object[] { msgId, Variable.v } );
Iterator<QueryResultsRow> iterator = results.iterator();
- while(iterator.hasNext()){
- QueryResultsRow row = iterator.next();
- answers.add((ACLMessage)row.get("$ans"));
+
+ if ( iterator.hasNext() ) {
+ return ( (List<ACLMessage>) iterator.next().get( "$list" ) );
+ } else {
+ return Collections.emptyList();
}
- return answers;
+
}
}
@@ -82,6 +82,7 @@ declare MessageAnswer
end
-query getAnswers(String msgId)
- MessageAnswer(messageId == msgId, $ans: answer)
+query getAnswers( String $msgId, java.util.List $list )
+ accumulate( MessageAnswer( messageId == $msgId, $ans: answer ),
+ $list : collectList( $ans ) )
end
@@ -138,6 +138,10 @@ private boolean validateRequestResponses( List<ACLMessage> answers, String metho
if ( answers.size() != 2 ) {
return false;
}
+
+ if ( Act.AGREE.equals( answers.get( 1 ).getPerformative() ) ) {
+ throw new IllegalStateException( " TODO : Agree was collected after its response, check rules" );
+ }
ACLMessage answer1 = answers.get( 0 );
if ( ! Act.AGREE.equals( answer1.getPerformative() ) ) {

0 comments on commit 8b7836b

Please sign in to comment.