bdi4jade

Details

diff --git a/bdi-jade/src/bdi4jade/message/BDIAgentMsgReceiver.java b/bdi-jade/src/bdi4jade/message/BDIAgentMsgReceiver.java
index a286361..baee3e0 100644
--- a/bdi-jade/src/bdi4jade/message/BDIAgentMsgReceiver.java
+++ b/bdi-jade/src/bdi4jade/message/BDIAgentMsgReceiver.java
@@ -16,7 +16,7 @@
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 // 
 // To contact the authors:
-// http://inf.ufrgs.br/~ingridnunes/bdi4jade/
+// http://inf.ufrgs.br/prosoft/bdi4jade/
 //
 //----------------------------------------------------------------------------
 
@@ -40,90 +40,113 @@ import bdi4jade.core.BDIAgent;
 import bdi4jade.core.Capability;
 
 /**
- * @author ingrid
+ * This class extends the {@link MsgReceiver} behavior from the JADE platform
+ * and is responsible for receiving agent messages and creating
+ * {@link MessageGoal} so that a BDI agent can process it. Message goals are
+ * creates solely if there is an agent plan of any capability that is able to
+ * process the message.
+ * 
+ * @author Ingrid Nunes
  * 
  */
 public class BDIAgentMsgReceiver extends MsgReceiver {
 
-	public static class BDIAgentMatchExpression implements MatchExpression {
+	/**
+	 * This class implements the {@link MatchExpression} interface from JADE and
+	 * is responsible for verifying if there is at least one agent capability
+	 * with a plan that is able to process a given message.
+	 * 
+	 * @author Ingrid Nunes
+	 * 
+	 */
+	public class BDIAgentMatchExpression implements MatchExpression {
 
 		private static final long serialVersionUID = -1076583615928481034L;
 
-		private BDIAgentMsgReceiver bdiAgentMsgReceiver;
-
-		private void getCanProcessCapabilities(final ACLMessage msg,
-				final Set<Capability> capabilities, Capability capability) {
-			if (capability.canProcess(msg)) {
-				capabilities.add(capability);
-			}
-			for (Capability child : capability.getPartCapabilities()) {
-				getCanProcessCapabilities(msg, capabilities, child);
+		private Set<Capability> getCanProcessCapabilities(final ACLMessage msg) {
+			Set<Capability> capabilities = new HashSet<Capability>();
+			for (Capability capability : getMyAgent().getCapabilities()) {
+				if (capability.canProcess(msg)) {
+					capabilities.add(capability);
+				}
 			}
+			return capabilities;
 		}
 
+		/**
+		 * @see jade.lang.acl.MessageTemplate.MatchExpression#match(jade.lang.acl.ACLMessage)
+		 */
 		@Override
 		public boolean match(ACLMessage msg) {
-			Set<Capability> capabilities = new HashSet<Capability>();
-			getCanProcessCapabilities(msg, capabilities, bdiAgentMsgReceiver
-					.getMyAgent().getRootCapability());
-
+			Set<Capability> capabilities = getCanProcessCapabilities(msg);
 			if (!capabilities.isEmpty()) {
-				bdiAgentMsgReceiver.messageMatched(msg, capabilities);
+				synchronized (msgs) {
+					msgs.put(msg, capabilities);
+				}
 				return true;
 			} else {
 				return false;
 			}
 		}
 
-		public void setBdiAgentMsgReceiver(
-				BDIAgentMsgReceiver bdiAgentMsgReceiver) {
-			this.bdiAgentMsgReceiver = bdiAgentMsgReceiver;
-		}
-
 	}
 
 	public static final Object MSG_KEY = "msgs";
-
 	private static final long serialVersionUID = -4435254708782532901L;
 
 	private final Log log;
 	private final Map<ACLMessage, Set<Capability>> msgs;
 
-	public BDIAgentMsgReceiver(BDIAgent agent,
-			BDIAgentMatchExpression matchExpression) {
-		super(agent, new MessageTemplate(matchExpression), INFINITE,
-				new DataStore(), MSG_KEY);
-		matchExpression.setBdiAgentMsgReceiver(this);
+	/**
+	 * Initializes this message receiver, which is associated with a BDI agent.
+	 * 
+	 * @param agent
+	 *            the BDI agent that this behavior is associated with.
+	 */
+	public BDIAgentMsgReceiver(BDIAgent agent) {
+		super(agent, MessageTemplate.MatchAll(), INFINITE, new DataStore(),
+				MSG_KEY);
+		this.template = new MessageTemplate(new BDIAgentMatchExpression());
 		this.msgs = new HashMap<ACLMessage, Set<Capability>>();
 		this.log = LogFactory.getLog(this.getClass());
 	}
 
+	/**
+	 * Returns always false, as this behavior is responsible for message
+	 * processing while a BDI agent is alive.
+	 * 
+	 * @see jade.proto.states.MsgReceiver#done()
+	 */
 	@Override
 	public boolean done() {
 		return false;
 	}
 
-	public BDIAgent getMyAgent() {
+	private BDIAgent getMyAgent() {
 		return (BDIAgent) this.myAgent;
 	}
 
+	/**
+	 * Creates a {@link MessageGoal} for the received message, when handling the
+	 * message.
+	 * 
+	 * @see jade.proto.states.MsgReceiver#handleMessage(jade.lang.acl.ACLMessage)
+	 */
 	@Override
 	protected void handleMessage(ACLMessage msg) {
 		log.debug("Message received.");
-		Set<Capability> capabilities = msgs.get(msg);
-		if (capabilities != null) {
-			MessageGoal goal = new MessageGoal(msg);
-			log.debug("This capabilities can process the message:");
-			for (Capability capability : capabilities) {
-				log.info("* " + capability);
+		synchronized (msgs) {
+			Set<Capability> capabilities = msgs.get(msg);
+			if (capabilities != null) {
+				MessageGoal goal = new MessageGoal(msg);
+				log.debug("This capabilities can process the message:");
+				for (Capability capability : capabilities) {
+					log.debug("* " + capability);
+				}
+				getMyAgent().addGoal(goal);
+				msgs.remove(msg);
 			}
-			getMyAgent().addGoal(goal);
-			msgs.remove(msg);
 		}
 	}
 
-	public void messageMatched(ACLMessage msg, Set<Capability> capabilities) {
-		this.msgs.put(msg, capabilities);
-	}
-
 }
diff --git a/bdi-jade/src/bdi4jade/message/MessageGoal.java b/bdi-jade/src/bdi4jade/message/MessageGoal.java
index 3da7f99..71d5fc5 100644
--- a/bdi-jade/src/bdi4jade/message/MessageGoal.java
+++ b/bdi-jade/src/bdi4jade/message/MessageGoal.java
@@ -16,7 +16,7 @@
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 // 
 // To contact the authors:
-// http://inf.ufrgs.br/~ingridnunes/bdi4jade/
+// http://inf.ufrgs.br/prosoft/bdi4jade/
 //
 //----------------------------------------------------------------------------
 
@@ -26,9 +26,9 @@ import jade.lang.acl.ACLMessage;
 import bdi4jade.goal.Goal;
 
 /**
- * This class represents the goal of processing a message.
+ * This class represents the goal of processing a message received by the agent.
  * 
- * @author ingrid
+ * @author Ingrid Nunes
  */
 public class MessageGoal implements Goal {
 
@@ -37,7 +37,15 @@ public class MessageGoal implements Goal {
 	private ACLMessage message;
 
 	/**
-	 * Constructor.
+	 * The default constructor. It should be only used if persistence frameworks
+	 * are used.
+	 */
+	protected MessageGoal() {
+
+	}
+
+	/**
+	 * Initializes a message goal with the given message.
 	 * 
 	 * @param message
 	 *            the message to be processed.
@@ -47,10 +55,38 @@ public class MessageGoal implements Goal {
 	}
 
 	/**
+	 * Returns the message associated with this message goal.
+	 * 
 	 * @return the message
 	 */
 	public ACLMessage getMessage() {
 		return message;
 	}
 
+	/**
+	 * Sets the message of this goal. Ideally, the message should be final and
+	 * initialized in the constructor. This method should be only used if
+	 * persistence frameworks are used.
+	 * 
+	 * @param message
+	 *            the message to set
+	 */
+	protected void setMessage(ACLMessage message) {
+		this.message = message;
+	}
+
+	/**
+	 * Returns a string representation of this goal, in the form
+	 * "MessageGoal: message".
+	 * 
+	 * @return the string representation of this message goal.
+	 * 
+	 * @see java.lang.Object#toString()
+	 */
+	@Override
+	public String toString() {
+		return new StringBuffer(getClass().getName()).append(": ")
+				.append(message).toString();
+	}
+
 }
diff --git a/bdi-jade/src/bdi4jade/message/package-info.java b/bdi-jade/src/bdi4jade/message/package-info.java
new file mode 100644
index 0000000..0287305
--- /dev/null
+++ b/bdi-jade/src/bdi4jade/message/package-info.java
@@ -0,0 +1,30 @@
+//----------------------------------------------------------------------------
+// Copyright (C) 2011  Ingrid Nunes
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// 
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+// 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+// 
+// To contact the authors:
+// http://inf.ufrgs.br/prosoft/bdi4jade/
+//
+//----------------------------------------------------------------------------
+
+/**
+ * This package contains interfaces and classes that allows processing 
+ * messages received by BDI agents and creating goals to process them.
+ * 
+ * @author Ingrid Nunes
+ *
+ */
+package bdi4jade.message;
\ No newline at end of file