Skip to content
Browse files

Merge pull request #11 from sotty/master

Improve message management & test fixes
  • Loading branch information...
2 parents 0615a7c + 281e488 commit 2cdcf9d999480a7c972dcac85a52ab1cf9a49919 @Salaboy Salaboy committed
View
16 drools-mas-core/src/main/java/org/drools/mas/core/DroolsAgent.java
@@ -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;
+
}
}
View
5 drools-mas-core/src/main/resources/org/drools/mas/acl_declares.drl
@@ -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
View
47 drools-mas-core/src/test/java/org/drools/mas/core/tests/GridTests.java
@@ -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,
View
121 drools-mas-core/src/test/java/org/drools/mas/core/tests/SemanticACLTest.java
@@ -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);
-// }
-//}
View
47 drools-mas-core/src/test/java/org/drools/mas/core/tests/SpringAgentTest.java
@@ -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();
View
81 drools-mas-core/src/test/java/org/drools/mas/core/tests/TestAgent.java
@@ -16,6 +16,8 @@
*/
import java.sql.SQLException;
+
+import org.drools.grid.helper.GridHelper;
import org.drools.mas.body.acts.Failure;
import org.drools.mas.body.content.Query;
import org.drools.mas.body.acts.InformIf;
@@ -49,22 +51,28 @@
private static DroolsAgent mainAgent;
private static Logger logger = LoggerFactory.getLogger(TestAgent.class);
- private Server server;
+ private static Server server;
- @Before
- public void createAgents() {
+ @BeforeClass
+ public static void setupDB() {
DeleteDbFiles.execute("~", "mydb", false);
logger.info("Staring DB for white pages ...");
try {
-
- server = Server.createTcpServer(new String[] {"-tcp","-tcpAllowOthers","-tcpDaemon","-trace"}).start();
+
+ server = Server.createTcpServer(new String[] {"-tcp","-tcpAllowOthers","-tcpDaemon","-trace"}).start();
} catch (SQLException ex) {
logger.error(ex.getMessage());
}
logger.info("DB for white pages started! ");
+ // force DB initialization
+ GridHelper.reset();
+ }
+
+ @Before
+ public void createAgents() {
DroolsAgentConfiguration mainConfig = new DroolsAgentConfiguration();
mainConfig.setAgentId("Mock Test Agent");
@@ -86,17 +94,26 @@ public void createAgents() {
@After
public void cleanUp() {
-
+
if (mainAgent != null) {
mainAgent.dispose();
}
+ }
+
+ @AfterClass
+ public static void cleanDB() {
logger.info("Stopping DB ...");
- server.stop();
+ try {
+ Server.shutdownTcpServer(server.getURL(), "", false, false);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
logger.info("DB Stopped!");
+
}
-
+
private void waitForAnswers( String id, int expectedSize, long sleep, int maxIters ) {
int counter = 0;
do {
@@ -113,9 +130,9 @@ private void waitForAnswers( String id, int expectedSize, long sleep, int maxIte
}
}
-
-
-
+
+
+
@Test
public void testSimpleInform() throws InterruptedException {
MockFact fact = new MockFact( "patient1", 18 );
@@ -123,10 +140,10 @@ public void testSimpleInform() throws InterruptedException {
ACLMessage info = factory.newInformMessage( "me", "you", fact );
mainAgent.tell( info );
-
+
//Now this is also async
waitForAnswers( info.getId(), 0, 250, 50 );
-
+
assertNotNull( mainAgent.getAgentAnswers( info.getId() ) );
StatefulKnowledgeSession target = mainAgent.getInnerSession( "session1" );
assertTrue( target.getObjects().contains( fact ) );
@@ -174,22 +191,22 @@ public void testInformAsTrigger() throws InterruptedException {
ACLMessage info = factory.newInformMessage("me", "you", fact);
mainAgent.tell(info);
-
+
waitForAnswers( info.getId(), 0, 250, 50 );
-
-
+
+
assertNotNull( mainAgent.getAgentAnswers(info.getId() ) );
StatefulKnowledgeSession target = mainAgent.getInnerSession("session2");
-
+
for (Object o : target.getObjects()) {
System.err.println("\t Inform-Trigger test : " + o);
}
-
+
assertTrue(target.getObjects().contains(new Double(22.0)));
assertTrue(target.getObjects().contains(new Integer(484)));
}
-
+
@Test
public void testQueryIf() throws InterruptedException {
@@ -259,7 +276,7 @@ public void testRequest() throws InterruptedException {
mainAgent.tell(req);
waitForAnswers( req.getId(), 2, 250, 50 );
-
+
assertNotNull( mainAgent.getAgentAnswers(req.getId() ) );
assertEquals( 2, mainAgent.getAgentAnswers( req.getId() ).size() );
@@ -369,15 +386,15 @@ public void testRequestWithMultipleOutputs() throws InterruptedException {
Action action = MessageContentFactory.newActionContent("randomSum", args);
-
+
ACLMessage req = factory.newRequestMessage("me", "you", action);
mainAgent.tell(req);
-
+
Thread.sleep(5000);
-
+
assertNotNull(mainAgent.getAgentAnswers(req.getId()));
assertEquals(2, mainAgent.getAgentAnswers(req.getId()).size());
@@ -425,7 +442,7 @@ public void testSimpleInformInNewSession() throws InterruptedException {
ACLMessageFactory factory = new ACLMessageFactory( Encodings.XML );
ACLMessage info = factory.newInformMessage( "me", "you", fact );
-
+
mainAgent.tell(info);
waitForAnswers( info.getId(), 0, 2000, 10 );
@@ -438,7 +455,7 @@ public void testSimpleInformInNewSession() throws InterruptedException {
assertTrue( target.getObjects().contains( fact ) );
ACLMessage info2 = factory.newInformMessage( "me", "you", fact2 );
-
+
mainAgent.tell( info2 );
//Now this is also async
waitForAnswers( info.getId(), 0, 250, 50 );
@@ -461,7 +478,7 @@ public void testNotUnderstood() throws InterruptedException {
mainAgent.tell(notUnd);
waitForAnswers( notUnd.getId(), 0, 250, 50 );
-
+
assertEquals(Act.NOT_UNDERSTOOD, mainAgent.getAgentAnswers(notUnd.getId()).get(0).getPerformative());
}
@@ -481,7 +498,7 @@ public void testImplicitRequestFailure() throws InterruptedException {
mainAgent.tell( req );
waitForAnswers( req.getId(), 2, 250, 50 );
-
+
assertEquals( 2, mainAgent.getAgentAnswers(req.getId()).size() );
assertEquals( Act.AGREE, mainAgent.getAgentAnswers(req.getId()).get(0).getPerformative() );
assertEquals( Act.FAILURE, mainAgent.getAgentAnswers(req.getId()).get(1).getPerformative() );
@@ -489,11 +506,11 @@ public void testImplicitRequestFailure() throws InterruptedException {
Failure fail = (Failure) mainAgent.getAgentAnswers(req.getId()).get(1).getBody();
String msg = fail.getCause().getData().toString();
-
+
assertTrue( msg.contains( "can't extract the square root of -9" ) );
}
-
+
@Test
public void testExplicitRequestFailure() throws InterruptedException {
@@ -502,14 +519,14 @@ public void testExplicitRequestFailure() throws InterruptedException {
ACLMessageFactory factory = new ACLMessageFactory(Encodings.XML);
Map<String, Object> args = new LinkedHashMap<String, Object>();
- args.put( "x", in );
+ args.put( "x", in );
Action action = MessageContentFactory.newActionContent( "squareRoot", args );
ACLMessage req = factory.newRequestMessage( "me", "you", action );
mainAgent.tell( req );
waitForAnswers( req.getId(), 2, 250, 50 );
-
+
assertEquals( Act.AGREE, mainAgent.getAgentAnswers(req.getId()).get(0).getPerformative() );
assertEquals( Act.FAILURE, mainAgent.getAgentAnswers(req.getId()).get(1).getPerformative() );
@@ -546,7 +563,7 @@ public void testQueryRefFailure() throws InterruptedException {
mainAgent.tell( qryref );
//Now this is also async
waitForAnswers( qryref.getId(), 0, 250, 50 );
-
+
assertEquals( 2, mainAgent.getAgentAnswers(qryref.getId()).size() );
assertEquals( Act.AGREE, mainAgent.getAgentAnswers(qryref.getId()).get(0).getPerformative() );
assertEquals( Act.FAILURE, mainAgent.getAgentAnswers(qryref.getId()).get(1).getPerformative() );
View
4 drools-mas-core/src/test/resources/META-INF/persistence.xml
@@ -9,7 +9,7 @@
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
- <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/mydb"/>
+ <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/mydb;DB_CLOSE_ON_EXIT=FALSE"/>
<!--property name="hibernate.connection.url" value="jdbc:h2:file:/h2/test;AUTO_SERVER=TRUE" /-->
<!-- property name="hibernate.connection.url" value="jdbc:h2:file:\dev\drools\trunk7\jbpm-process\jbpm-human-task\mydb"/ -->
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>-->
@@ -19,7 +19,7 @@
<property name="hibernate.connection.password" value="sasa"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.max_fetch_depth" value="3"/>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
+ <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
View
4 drools-mas-generic-client/src/main/java/org/drools/mas/helpers/SyncDialogueHelper.java
@@ -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 2cdcf9d

Please sign in to comment.
Something went wrong with that request. Please try again.