Permalink
Browse files

Merge pull request #11 from sotty/master

Improve message management & test fixes
  • Loading branch information...
Salaboy committed Mar 31, 2012
2 parents 0615a7c + 281e488 commit 2cdcf9d999480a7c972dcac85a52ab1cf9a49919
@@ -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
@@ -55,32 +55,42 @@ public GridTests() {
@BeforeClass
public static void setUpClass() throws Exception {
+ DeleteDbFiles.execute("~", "mydb", false);
+
+ System.out.println("Staring DB for white pages ...");
+
+ try {
+
+ server = Server.createTcpServer(new String[] {"-tcp","-tcpAllowOthers","-tcpDaemon","-trace"}).start();
+ } catch (SQLException ex) {
+ System.out.println("ERROR: "+ex.getMessage());
+
+ }
+ System.out.println("DB for white pages started! ");
+
+ GridHelper.reset();
}
@AfterClass
- public static void tearDownClass() throws Exception {
+ public static void tearDownClass() {
+ try {
+ Server.shutdownTcpServer(server.getURL(), "", false, false);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ fail ( e.getMessage() );
+ }
}
- private Map<String, GridServiceDescription> coreServicesMap;
+ private Map<String, GridServiceDescription> coreServicesMap;
protected Grid grid1;
protected GridNode remoteN1;
- private Server server;
-
+ private static Server server;
+
+
+
@Before
public void setUp() {
- DeleteDbFiles.execute("~", "mydb", false);
-
- System.out.println("Staring DB for white pages ...");
-
- try {
-
- server = Server.createTcpServer(new String[] {"-tcp","-tcpAllowOthers","-tcpDaemon","-trace"}).start();
- } catch (SQLException ex) {
- System.out.println("ERROR: "+ex.getMessage());
-
- }
- System.out.println("DB for white pages started! ");
this.coreServicesMap = new HashMap();
createRemoteNode();
@@ -90,8 +100,6 @@ public void setUp() {
public void tearDown() {
remoteN1.dispose();
grid1.get(SocketService.class).close();
- server.stop();
-
}
private void createRemoteNode(){
@@ -112,8 +120,7 @@ private void createRemoteNode(){
GridConnection<GridNode> conn = grid2.get( ConnectionFactoryService.class ).createConnection( n1Gsd );
remoteN1 = conn.connect();
-
-
+
}
private void configureGrid1(Grid grid,
@@ -1,121 +0,0 @@
-///*
-// * To change this template, choose Tools | Templates
-// * and open the template in the editor.
-// */
-//package org.drools.mas.core.tests;
-//
-//import mock.MockFact;
-//import org.drools.mas.ACLMessage;
-//import org.drools.mas.Act;
-//import org.drools.mas.Encodings;
-//import org.drools.mas.body.acts.InformIf;
-//import org.drools.mas.core.DroolsAgent;
-//import org.drools.mas.core.DroolsAgentConfiguration;
-//import org.drools.mas.core.DroolsAgentFactory;
-//import org.drools.mas.mock.MockResponseInformer;
-//import org.drools.mas.util.ACLMessageFactory;
-//import org.drools.mas.util.MessageContentEncoder;
-//import org.drools.runtime.StatefulKnowledgeSession;
-//import org.junit.*;
-//import static org.junit.Assert.*;
-//
-///**
-// *
-// * @author salaboy
-// */
-//public class SemanticACLTest {
-//
-// private static DroolsAgent mainAgent;
-// private static DroolsAgent clientAgent;
-// private static MockResponseInformer mainResponseInformer;
-// private static MockResponseInformer clientResponseInformer;
-//
-// public SemanticACLTest() {
-// }
-//
-// @BeforeClass
-// public static void setUpClass() throws Exception {
-// }
-//
-// @AfterClass
-// public static void tearDownClass() throws Exception {
-// }
-//
-// @Before
-// public void setUp() {
-// mainResponseInformer = new MockResponseInformer();
-// clientResponseInformer = new MockResponseInformer();
-//
-// DroolsAgentConfiguration mainConfig = new DroolsAgentConfiguration();
-// mainConfig.setAgentId("Mock Test Agent");
-// mainConfig.setChangeset("mainTestAgent_changeset.xml");
-// mainConfig.setResponseInformer(mainResponseInformer);
-// DroolsAgentConfiguration.SubSessionDescriptor subDescr1 = new DroolsAgentConfiguration.SubSessionDescriptor("session1", "sub1.xml", "NOT_USED_YET");
-// mainConfig.addSubSession(subDescr1);
-// DroolsAgentConfiguration.SubSessionDescriptor subDescr2 = new DroolsAgentConfiguration.SubSessionDescriptor("session2", "sub2.xml", "NOT_USED_YET");
-// mainConfig.addSubSession(subDescr2);
-// mainAgent = DroolsAgentFactory.getInstance().spawn(mainConfig);
-// assertNotNull(mainAgent);
-// assertNotNull(mainAgent.getInnerSession("session1"));
-// assertNotNull(mainAgent.getInnerSession("session2"));
-//
-// DroolsAgentConfiguration clientConfig = new DroolsAgentConfiguration();
-// clientConfig.setAgentId("Humble Test Client");
-// clientConfig.setChangeset("clientTestAgent_changeset.xml");
-// clientConfig.setResponseInformer(clientResponseInformer);
-// clientAgent = DroolsAgentFactory.getInstance().spawn(clientConfig);
-// }
-//
-// @After
-// public void tearDown() {
-// }
-//
-// /*
-// * The sender informs the receiver that a given proposition is true.
-// */
-// @Test
-// public void informACLMessageTest() {
-// // In this case the MockFact represent the proposition
-// MockFact fact = new MockFact("patient1", 18);
-//
-// ACLMessageFactory factory = new ACLMessageFactory(Encodings.XML);
-//
-// ACLMessage info = factory.newInformMessage("me", "you", fact);
-//
-// mainAgent.tell(info);
-//
-// assertNull(mainResponseInformer.getResponses(info));
-//
-// StatefulKnowledgeSession target = mainAgent.getInnerSession("session1");
-//
-// assertTrue(target.getObjects().contains(fact));
-//
-// }
-//
-// /*
-// * The action of asking another agent whether or not a given proposition is true.
-// */
-// @Test
-// public void testQueryIf() {
-// MockFact fact = new MockFact("patient1", 18);
-// ACLMessageFactory factory = new ACLMessageFactory(Encodings.XML);
-//
-// ACLMessage info = factory.newInformMessage("me", "you", fact);
-// // First we inform a new proposition to the main agent
-// mainAgent.tell(info);
-//
-// ACLMessage qryif = factory.newQueryIfMessage("me", "you", fact);
-// assertNull(mainResponseInformer.getResponses(qryif));
-// // Now we query for that proposition
-// mainAgent.tell(qryif);
-//
-//
-// assertNotNull(mainResponseInformer.getResponses(qryif));
-// assertEquals(1, mainResponseInformer.getResponses(qryif).size());
-//
-// ACLMessage answer = mainResponseInformer.getResponses(qryif).get(0);
-// MessageContentEncoder.decodeBody(answer.getBody(), answer.getEncoding());
-// assertEquals(Act.INFORM_IF, answer.getPerformative());
-// assertEquals(((InformIf) answer.getBody()).getProposition().getData(), fact);
-// }
-//}
@@ -14,6 +14,7 @@
import org.drools.grid.SocketService;
import org.drools.grid.conf.GridPeerServiceConfiguration;
import org.drools.grid.conf.impl.GridPeerConfiguration;
+import org.drools.grid.helper.GridHelper;
import org.drools.grid.impl.GridImpl;
import org.drools.grid.impl.MultiplexSocketServerImpl;
import org.drools.grid.io.impl.MultiplexSocketServiceCongifuration;
@@ -43,20 +44,15 @@
private static int port1 = 8000;
private static int port2 = 8010;
private static Logger logger = LoggerFactory.getLogger(SpringAgentTest.class);
- private Server server;
+ private static Server server;
+ private DroolsAgent agent;
+
public SpringAgentTest() {
+
}
@BeforeClass
public static void setUpClass() throws Exception {
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
-
- @Before
- public void setUp() {
DeleteDbFiles.execute("~", "mydb", false);
logger.info("Staring DB for white pages ...");
@@ -66,25 +62,40 @@ public void setUp() {
logger.error(ex.getMessage());
}
logger.info("DB for white pages started! ");
+
+ GridHelper.reset();
}
- @After
- public void tearDown() {
-
+ @AfterClass
+ public static void tearDownClass() {
logger.info("Stopping DB ...");
- server.stop();
+ try {
+ Server.shutdownTcpServer( server.getURL(), "", false, false);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ fail ( e.getMessage() );
+ }
logger.info("DB Stopped!");
+
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ agent.dispose();
}
@Test
public void helloAgentSmith() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- DroolsAgent agent = (DroolsAgent) context.getBean("agent");
+ agent = (DroolsAgent) context.getBean("agent");
+
- assertNotNull(agent);
- agent.dispose();
}
@@ -103,14 +114,12 @@ public void helloAgentSmithGrid() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextGrid.xml");
- DroolsAgent agent = (DroolsAgent) context.getBean("agent");
+ agent = (DroolsAgent) context.getBean("agent");
assertNotNull(agent);
agent.tell(ACLMessageFactory.getInstance().newInformMessage("", "", new mock.MockFact("asdasd", 12)));
- agent.dispose();
-
n1.dispose();
grid1.get(SocketService.class).close();
Oops, something went wrong.

0 comments on commit 2cdcf9d

Please sign in to comment.