Permalink
Browse files

MAS : Improving Graceful Agent (Un)Deployment and Error Recovery

Fixing Example Agent
  • Loading branch information...
1 parent bb114d3 commit 4aaa18364ed0221011565a5700a1653c6a2c0737 @sotty sotty committed Jun 29, 2012
Showing with 1,042 additions and 1,325 deletions.
  1. +0 −2 ...ype/src/main/resources/archetype-resources/src/main/resources/META-INF/drools.packagebuilder.conf
  2. +0 −2 ...ype/src/main/resources/archetype-resources/src/main/resources/META-INF/drools.packagebuilder.conf
  3. +13 −6 drools-mas-core/pom.xml
  4. +47 −36 drools-mas-core/src/main/java/org/drools/mas/core/DroolsAgent.java
  5. +65 −56 drools-mas-core/src/main/java/org/drools/mas/core/DroolsAgentFactory.java
  6. +137 −102 drools-mas-core/src/main/java/org/drools/mas/core/SessionManager.java
  7. +0 −2 drools-mas-core/src/main/resources/META-INF/drools.packagebuilder.conf
  8. +2 −1 drools-mas-core/src/main/resources/META-INF/orm.xml
  9. +16 −14 drools-mas-core/src/main/resources/org/drools/mas/acl_common.drl
  10. +13 −11 drools-mas-core/src/main/resources/org/drools/mas/acl_content_based_routing.drl
  11. +44 −46 drools-mas-core/src/main/resources/org/drools/mas/acl_internal.drl
  12. +13 −13 drools-mas-core/src/main/resources/org/drools/mas/acl_protocol.drl
  13. +3 −4 drools-mas-core/src/main/resources/org/drools/mas/sessions_handling.drl
  14. +21 −11 drools-mas-core/src/test/java/org/drools/mas/core/tests/GridTests.java
  15. +127 −52 drools-mas-core/src/test/java/org/drools/mas/core/tests/SpringAgentTest.java
  16. +43 −44 drools-mas-core/src/test/java/org/drools/mas/core/tests/TestAgent.java
  17. +0 −2 drools-mas-core/src/test/resources/META-INF/drools.packagebuilder.conf
  18. +1 −1 drools-mas-core/src/test/resources/META-INF/persistence.xml
  19. +1 −3 drools-mas-core/src/test/resources/applicationContextGrid.xml
  20. +10 −11 drools-mas-core/src/test/resources/{applicationContext.xml → applicationContextOneNode.xml}
  21. +40 −0 drools-mas-core/src/test/resources/applicationContextSharing.xml
  22. +9 −9 drools-mas-core/src/test/resources/session2.drl
  23. +4 −4 drools-mas-generic-client/src/main/java/org/drools/mas/helpers/DialogueHelper.java
  24. +15 −7 drools-mas-util/src/main/java/org/drools/mas/util/helper/ResponseContent.java
  25. +10 −4 examples/drools-mas-emergency-agent-client/pom.xml
  26. +0 −145 ...ols-mas-emergency-agent-client/src/main/java/org/drools/mas/helpers/SynchronousRequestHelper.java
  27. +144 −0 ...ools-mas-emergency-agent-client/src/test/java/org/drools/mas/EmergencyAgentServiceRemoteTest.java
  28. +0 −169 ...emergency-agent-client/src/test/java/org/drools/mas/SynchronousDroolsAgentServiceServiceTest.java
  29. +0 −282 examples/drools-mas-emergency-agent-client/src/wsdl/localhost_8080/new-action-agent/services/.wsdl
  30. +1 −1 examples/drools-mas-emergency-agent-model/pom.xml
  31. +0 −38 ...les/drools-mas-emergency-agent-model/src/test/java/org/drools/mas/examples/emergency/AppTest.java
  32. +6 −6 examples/drools-mas-emergency-agent/pom.xml
  33. +6 −1 examples/drools-mas-emergency-agent/src/main/java/org/drools/mas/AsyncDroolsAgentService.java
  34. +11 −17 examples/drools-mas-emergency-agent/src/main/java/org/drools/mas/AsyncDroolsAgentServiceImpl.java
  35. +5 −3 examples/drools-mas-emergency-agent/src/main/java/org/drools/mas/SynchronousDroolsAgentService.java
  36. +11 −17 ...es/drools-mas-emergency-agent/src/main/java/org/drools/mas/SynchronousDroolsAgentServiceImpl.java
  37. +8 −8 examples/drools-mas-emergency-agent/src/main/resources/META-INF/applicationContext.xml
  38. +0 −2 examples/drools-mas-emergency-agent/src/main/resources/META-INF/drools.packagebuilder.conf
  39. +2 −1 examples/drools-mas-emergency-agent/src/main/resources/META-INF/orm.xml
  40. +18 −23 examples/drools-mas-emergency-agent/src/main/resources/META-INF/persistence.xml
  41. +17 −19 examples/drools-mas-emergency-agent/src/main/resources/knowledge/agent-brain_hello-module.drl
  42. +4 −5 examples/drools-mas-emergency-agent/src/main/resources/knowledge/agent_cbr.drl
  43. +172 −0 examples/drools-mas-emergency-agent/src/test/java/org/drools/mas/AgentTest.java
  44. +0 −142 ...es/drools-mas-emergency-agent/src/test/java/org/drools/mas/KnowledgeResourcesCompilationTest.java
  45. +2 −2 examples/drools-mas-emergency-agent/src/test/resources/test-applicationContext.xml
  46. +1 −1 pom.xml
@@ -27,8 +27,6 @@ drools.evaluator.starts = org.drools.base.evaluators.StartsEvaluatorDefinition
drools.evaluator.startedby = org.drools.base.evaluators.StartedByEvaluatorDefinition
drools.evaluator.finishes = org.drools.base.evaluators.FinishesEvaluatorDefinition
drools.evaluator.finishedby = org.drools.base.evaluators.FinishedByEvaluatorDefinition
-drools.evaluator.equality = org.drools.base.evaluators.EqualityEvaluatorsDefinition
-drools.evaluator.comparable = org.drools.base.evaluators.ComparableEvaluatorsDefinition
drools.evaluator.set = org.drools.base.evaluators.SetEvaluatorsDefinition
drools.evaluator.matches = org.drools.base.evaluators.MatchesEvaluatorsDefinition
drools.evaluator.soundslike = org.drools.base.evaluators.SoundslikeEvaluatorsDefinition
@@ -27,8 +27,6 @@ drools.evaluator.starts = org.drools.base.evaluators.StartsEvaluatorDefinition
drools.evaluator.startedby = org.drools.base.evaluators.StartedByEvaluatorDefinition
drools.evaluator.finishes = org.drools.base.evaluators.FinishesEvaluatorDefinition
drools.evaluator.finishedby = org.drools.base.evaluators.FinishedByEvaluatorDefinition
-drools.evaluator.equality = org.drools.base.evaluators.EqualityEvaluatorsDefinition
-drools.evaluator.comparable = org.drools.base.evaluators.ComparableEvaluatorsDefinition
drools.evaluator.set = org.drools.base.evaluators.SetEvaluatorsDefinition
drools.evaluator.matches = org.drools.base.evaluators.MatchesEvaluatorsDefinition
drools.evaluator.soundslike = org.drools.base.evaluators.SoundslikeEvaluatorsDefinition
View
@@ -26,20 +26,27 @@
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>drools-mas-core</artifactId>
-
+
<name>Drools :: MAS Core</name>
-
+
<dependencies>
<dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>5.1.18</version>
+ </dependency>
+
+
+ <dependency>
<groupId>org.drools.mas</groupId>
<artifactId>drools-mas-acl</artifactId>
- <version>${project.version}</version>
+ <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.drools.mas</groupId>
<artifactId>drools-mas-util</artifactId>
- <version>${project.version}</version>
+ <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
@@ -61,7 +68,7 @@
<artifactId>drools-grid-impl</artifactId>
<version>${drools.version}</version>
</dependency>
-
+
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -142,7 +149,7 @@
<manifestLocation>META-INF</manifestLocation>
<instructions>
<_removeheaders>Ignore-Package</_removeheaders>
- <Require-Bundle>org.drools.api;org.drools.core;org.drools.compiler;visibility:=reexport;bundle-version="${drools.osgi.version}"</Require-Bundle>
+ <Require-Bundle>org.drools.api;org.drools.core;org.drools.compiler;visibility:=reexport;bundle-version="5.5.0-SNAPSHOT"</Require-Bundle>
<Import-Package>!org.drools.*, *,org.osgi.util.tracker</Import-Package>
<Export-Package>org.drools.mas.core.*</Export-Package>
<DynamicImport-Package>*</DynamicImport-Package>
@@ -15,10 +15,11 @@
*/
package org.drools.mas.core;
-import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import org.drools.grid.*;
import org.drools.mas.util.MessageContentEncoder;
import org.drools.runtime.StatefulKnowledgeSession;
@@ -81,13 +82,19 @@ public DroolsAgent(Grid grid, AgentID id, StatefulKnowledgeSession session) {
*
* @param msg
*/
- public void tell(ACLMessage msg) {
- if (logger.isTraceEnabled()) {
- logger.trace(" +++ Message Inside Tell -> " + msg);
+ public void tell( ACLMessage msg ) {
+ try {
+ if ( logger.isTraceEnabled() ) {
+ logger.trace( " +++ Message Inside Tell -> " + msg );
+ }
+ MessageContentEncoder.decodeBody( msg.getBody(), msg.getEncoding() );
+ this.mind.insert( msg );
+ this.mind.fireAllRules();
+ } catch ( Exception e ) {
+ // Should not happen, but in case...
+ logger.error( " FATAL -> Agent " + getAgentId() + " can not survive ", e );
+ dispose();
}
- MessageContentEncoder.decodeBody(msg.getBody(), msg.getEncoding());
- this.mind.insert(msg);
- this.mind.fireAllRules();
}
@@ -96,40 +103,44 @@ public void tell(ACLMessage msg) {
* Destructor
*/
public void dispose() {
- if (logger.isInfoEnabled()) {
- logger.info(" >>> Disposing Agent " + agentId.getName());
- }
- QueryResults queryResults = mind.getQueryResults("getSessions", new Object[]{});
- Iterator<QueryResultsRow> iterator = queryResults.iterator();
- while (iterator.hasNext()) {
-
- SessionLocator sessionLoc = (SessionLocator) iterator.next().get("$sessionLocator");
- if (logger.isDebugEnabled()) {
- logger.debug(" ### \t Disposing Agent Session :" + sessionLoc);
- }
- GridNode node = grid.getGridNode(sessionLoc.getNodeId());
- GridServiceDescription<GridNode> nGsd = null;
- if (node == null) {
- nGsd = grid.get(WhitePages.class).lookup(sessionLoc.getNodeId());
- GridConnection<GridNode> conn = grid.get(ConnectionFactoryService.class).createConnection(nGsd);
- node = conn.connect();
- }
- StatefulKnowledgeSession ksession = node.get(sessionLoc.getSessionId(), StatefulKnowledgeSession.class);
- ksession.dispose();
- //@TODO: We need to check if there are no more sessions in the remote node to dispose it completely
- if( nGsd != null ){
- grid.removeGridNode( nGsd.getId() );
+ //TODO : what if another agent is using this grid's nodes?
+ try {
+ if ( logger.isInfoEnabled() ) {
+ logger.info( " >>> Disposing Agent " + agentId.getName() );
}
- if( node != null){
- node.dispose();
+ QueryResults queryResults = mind.getQueryResults( "getSessions", new Object[] {} );
+ Iterator<QueryResultsRow> iterator = queryResults.iterator();
+ while ( iterator.hasNext() ) {
+
+ SessionLocator sessionLoc = (SessionLocator) iterator.next().get( "$sessionLocator" );
+ if ( logger.isDebugEnabled() ) {
+ logger.debug(" ### \t Disposing Agent Session :" + sessionLoc );
+ }
+ GridNode node = grid.getGridNode( sessionLoc.getNodeId() );
+ GridServiceDescription<GridNode> nGsd = null;
+ if ( node == null ) {
+ nGsd = grid.get( WhitePages.class ).lookup( sessionLoc.getNodeId() );
+ GridConnection<GridNode> conn = grid.get( ConnectionFactoryService.class ).createConnection( nGsd );
+ node = conn.connect();
+
+ StatefulKnowledgeSession ksession = node.get( sessionLoc.getSessionId(), StatefulKnowledgeSession.class );
+ ksession.dispose();
+ node.dispose();
+
+ } else {
+ // it's a local node ( triple check! ), so we just shut the KS down. The node will be disposed later
+ StatefulKnowledgeSession ksession = node.get( sessionLoc.getSessionId(), StatefulKnowledgeSession.class );
+ ksession.dispose();
+ }
}
-
+
+ } finally {
+ grid.dispose();
}
-
- grid.get(SocketService.class).close();
- mind.dispose();
}
+
+
public StatefulKnowledgeSession getInnerSession(String sessionId) {
if (logger.isDebugEnabled()) {
logger.debug("Looking for the inner session: " + sessionId);
@@ -15,24 +15,20 @@
*/
package org.drools.mas.core;
-import org.drools.mas.util.ACLMessageFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
import javax.persistence.Persistence;
import org.drools.SystemEventListenerFactory;
import org.drools.grid.Grid;
import org.drools.grid.GridServiceDescription;
-import org.drools.grid.SocketService;
import org.drools.grid.conf.GridPeerServiceConfiguration;
import org.drools.grid.conf.impl.GridPeerConfiguration;
import org.drools.grid.impl.GridImpl;
import org.drools.grid.impl.MultiplexSocketServerImpl;
-import org.drools.grid.io.impl.MultiplexSocketServiceCongifuration;
+import org.drools.grid.io.impl.MultiplexSocketServiceConfiguration;
import org.drools.grid.remote.mina.MinaAcceptorFactoryService;
import org.drools.grid.service.directory.WhitePages;
import org.drools.grid.service.directory.impl.CoreServicesLookupConfiguration;
@@ -41,7 +37,6 @@
import org.drools.impl.StatefulKnowledgeSessionImpl;
import org.drools.management.DroolsManagementAgent;
import org.drools.mas.AgentID;
-import org.drools.mas.Encodings;
import org.drools.mas.util.helper.SessionLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -62,7 +57,7 @@
private static Logger logger = LoggerFactory.getLogger(DroolsAgentFactory.class);
private static DroolsAgentFactory singleton;
-
+
public static DroolsAgentFactory getInstance() {
if (singleton == null) {
singleton = new DroolsAgentFactory();
@@ -73,89 +68,103 @@ public static DroolsAgentFactory getInstance() {
private DroolsAgentFactory() {
}
- public DroolsAgent spawn(DroolsAgentConfiguration config) {
- Grid grid = new GridImpl("peer-"+config.getAgentId(),new HashMap<String, Object>());
- configureGrid(grid, config.getPort());
-
+ public DroolsAgent spawn( DroolsAgentConfiguration config ) {
+ Grid grid = new GridImpl( "peer-" + config.getAgentId(), new HashMap<String, Object>() );
+ configureGrid( grid, config.getPort() );
+
AgentID aid = new AgentID();
- aid.setName(config.getAgentId());
- aid.setLocalName(config.getAgentId());
- if (logger.isInfoEnabled()) {
- logger.info(" >>> Spawning Agent => Name: " + aid.getName());
+ aid.setName( config.getAgentId() );
+ aid.setLocalName( config.getAgentId() );
+ if ( logger.isInfoEnabled() ) {
+ logger.info( " >>> Spawning Agent => Name: " + aid.getName() );
}
try {
- if (logger.isDebugEnabled()) {
- logger.debug(" ### Creating Agent Mind: " + config.getAgentId() + "- CS: " + config.getChangeset() +" - mind location: " +config.getMindNodeLocation());
+ if ( logger.isDebugEnabled() ) {
+ logger.debug(" ### Creating Agent Mind: " + config.getAgentId() + "- CS: " + config.getChangeset() +" - mind location: " +config.getMindNodeLocation() );
}
- StatefulKnowledgeSession mind = SessionManager.create(config, null, grid).getStatefulKnowledgeSession();
-
+ SessionManager manager = SessionManager.create( config, null, grid, false );
+ if ( manager == null ) {
+ logger.error( "SOMETHING BAD HAPPENED WHILE TRYING TO CREATE AN AGENT, could not create sessionManager" );
+ grid.dispose();
+ return null;
+ }
+ StatefulKnowledgeSession mind = manager.getStatefulKnowledgeSession();
+
DroolsManagementAgent kmanagement = DroolsManagementAgent.getInstance();
//kmanagement.registerKnowledgeBase((ReteooRuleBase) ((KnowledgeBaseImpl) mind.get).getRuleBase());
- kmanagement.registerKnowledgeSession(((StatefulKnowledgeSessionImpl) mind).getInternalWorkingMemory());
-
- if (logger.isDebugEnabled()) {
- logger.debug(" ### Mind Created: " + config.getAgentId() + "- CS: " + config.getChangeset() + " in "+ config.getMindNodeLocation());
- logger.debug(" ### Creating Agent Sub-Sessions ");
+ kmanagement.registerKnowledgeSession( ( (StatefulKnowledgeSessionImpl) mind ).getInternalWorkingMemory() );
+
+ if ( logger.isDebugEnabled() ) {
+ logger.debug( " ### Mind Created: " + config.getAgentId() + "- CS: " + config.getChangeset() + " in "+ config.getMindNodeLocation() );
+ logger.debug( " ### Creating Agent Sub-Sessions " );
}
-
-
- for (DroolsAgentConfiguration.SubSessionDescriptor descr : config.getSubSessions()) {
- if (logger.isDebugEnabled()) {
- logger.debug(" ### Creating Agent Sub-Session: " + descr.getSessionId() + "- CS: " + descr.getChangeset() + " - on node: " + descr.getNodeId());
+
+
+ for ( DroolsAgentConfiguration.SubSessionDescriptor descr : config.getSubSessions() ) {
+ if ( logger.isDebugEnabled() ) {
+ logger.debug( " ### Creating Agent Sub-Session: " + descr.getSessionId() + "- CS: " + descr.getChangeset() + " - on node: " + descr.getNodeId() );
}
- SessionManager sm = SessionManager.create(config, descr, grid);
+ SessionManager sm = SessionManager.create( config, descr, grid, true );
StatefulKnowledgeSession mindSet = sm.getStatefulKnowledgeSession();
-
+
mindSet.fireAllRules();
-
- mindSet.insert(new SessionLocator(config.getMindNodeLocation(),config.getAgentId(),true, false));
- mindSet.insert(new SessionLocator(descr.getNodeId(),descr.getSessionId(), false, true));
- mind.insert(new SessionLocator(descr.getNodeId(),descr.getSessionId(), false, true));
+
+ mind.setGlobal( "grid", grid );
+ mindSet.insert( new SessionLocator( config.getMindNodeLocation(), config.getAgentId(), true, false ) );
+ mindSet.insert( new SessionLocator( descr.getNodeId(), descr.getSessionId(), false, true ) );
+ mind.insert( new SessionLocator( descr.getNodeId(), descr.getSessionId(), false, true ) );
+
}
- mind.insert(aid);
+ mind.insert( aid );
//Insert configuration as a fact inside the mind session
- mind.insert(config);
- mind.insert(new SessionLocator(config.getMindNodeLocation(),config.getAgentId(),true, false));
+ mind.insert( config );
+ mind.insert( new SessionLocator( config.getMindNodeLocation(), config.getAgentId(), true, false ) );
mind.fireAllRules();
- return new DroolsAgent(grid, aid, mind);
- } catch (Exception ioe) {
- ioe.printStackTrace();
+ return new DroolsAgent( grid, aid, mind );
+ } catch ( Throwable t ) {
+ logger.error( "SOMETHING BAD HAPPENED WHILE TRYING TO CREATE AN AGENT " + t.getMessage() + ", due to " + t.getCause().getMessage() );
+ if ( logger.isDebugEnabled() ) {
+ t.printStackTrace();
+ }
+ grid.dispose();
}
return null;
}
+
+
- private void configureGrid(Grid grid, int port) {
+ private void configureGrid( Grid grid, int port ) {
//Local Grid Configuration, for our client
GridPeerConfiguration conf = new GridPeerConfiguration();
//Configuring the Core Services White Pages
- GridPeerServiceConfiguration coreSeviceWPConf = new CoreServicesLookupConfiguration(new HashMap<String, GridServiceDescription>());
- conf.addConfiguration(coreSeviceWPConf);
+ GridPeerServiceConfiguration coreSeviceWPConf = new CoreServicesLookupConfiguration( new HashMap<String, GridServiceDescription>() );
+ conf.addConfiguration( coreSeviceWPConf );
//Configuring the a local WhitePages service that is being shared with all the grid peers
WhitePagesLocalConfiguration wplConf = new WhitePagesLocalConfiguration();
//wplConf.setWhitePages(new WhitePagesImpl());
- wplConf.setWhitePages(new JpaWhitePages(Persistence.createEntityManagerFactory("org.drools.grid")));
- conf.addConfiguration(wplConf);
-
-
- if ( port >= 0 ) {
+ wplConf.setWhitePages( new JpaWhitePages( Persistence.createEntityManagerFactory( "org.drools.grid" ) ) );
+ conf.addConfiguration( wplConf );
+
+
+ if ( port >= 0 ) {
//Configuring the SocketService
- MultiplexSocketServiceCongifuration socketConf = new MultiplexSocketServiceCongifuration( new MultiplexSocketServerImpl( "127.0.0.1",
- new MinaAcceptorFactoryService(),
- SystemEventListenerFactory.getSystemEventListener(),
- grid) );
+ MultiplexSocketServiceConfiguration socketConf = new MultiplexSocketServiceConfiguration( new MultiplexSocketServerImpl( "127.0.0.1",
+ new MinaAcceptorFactoryService(),
+ SystemEventListenerFactory.getSystemEventListener(),
+ grid ) );
socketConf.addService( WhitePages.class.getName(), wplConf.getWhitePages(), port );
// socketConf.addService( SchedulerService.class.getName(), schlConf.getSchedulerService(), port );
-
+
conf.addConfiguration( socketConf );
}
- conf.configure(grid);
-
+ conf.configure( grid );
+
}
}
Oops, something went wrong.

0 comments on commit 4aaa183

Please sign in to comment.