bdi4jade

Default goal owner

8/21/2014 7:14:09 AM

Details

diff --git a/bdi-jade/src/bdi4jade/core/AbstractBDIAgent.java b/bdi-jade/src/bdi4jade/core/AbstractBDIAgent.java
index 1ed6af3..eacc9cc 100644
--- a/bdi-jade/src/bdi4jade/core/AbstractBDIAgent.java
+++ b/bdi-jade/src/bdi4jade/core/AbstractBDIAgent.java
@@ -39,7 +39,6 @@ import java.util.Set;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import bdi4jade.annotation.GoalOwner;
 import bdi4jade.belief.Belief;
 import bdi4jade.core.GoalUpdateSet.GoalDescription;
 import bdi4jade.event.GoalEvent;
@@ -566,15 +565,20 @@ public abstract class AbstractBDIAgent extends Agent implements BDIAgent {
 	 * a goal without the scope of a dispatcher that has access to it.
 	 * 
 	 * @param owner
-	 *            the annotation with the goal owner.
+	 *            the capability class that is the goal owner.
 	 * @return the capability instances related to this capability that owns the
 	 *         goal, or an empty set if the agent cannot add this goal.
 	 */
-	protected final Set<Capability> getGoalOwner(GoalOwner owner) {
-		Set<Capability> restrictedAccessOwners = restrictedAccessOwnersMap
-				.get(owner.capability());
-		return restrictedAccessOwners == null ? new HashSet<Capability>()
-				: restrictedAccessOwners;
+	protected final Set<Capability> getGoalOwner(
+			Class<? extends Capability> owner, boolean internal) {
+		if (internal) {
+			return new HashSet<Capability>();
+		} else {
+			Set<Capability> restrictedAccessOwners = restrictedAccessOwnersMap
+					.get(owner);
+			return restrictedAccessOwners == null ? new HashSet<Capability>()
+					: restrictedAccessOwners;
+		}
 	}
 
 	/**
diff --git a/bdi-jade/src/bdi4jade/core/Capability.java b/bdi-jade/src/bdi4jade/core/Capability.java
index 75b38bd..1855a58 100644
--- a/bdi-jade/src/bdi4jade/core/Capability.java
+++ b/bdi-jade/src/bdi4jade/core/Capability.java
@@ -38,7 +38,6 @@ import java.util.Set;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import bdi4jade.annotation.GoalOwner;
 import bdi4jade.belief.Belief;
 import bdi4jade.belief.BeliefBase;
 import bdi4jade.belief.TransientBelief;
@@ -534,23 +533,26 @@ public class Capability implements Serializable {
 	 * access to the goal owned by capabilities of the given class.
 	 * 
 	 * @param owner
-	 *            the annotation with the goal owner.
+	 *            the capability class that is the goal owner.
+	 * @param internal
+	 *            the boolean that indicates whether the goal is internal or
+	 *            external.
 	 * @return the capability instances related to this capability (or the
 	 *         capability itself) that owns the goal, or an empty set if the
 	 *         capability has no access to goals owned by capability of the
 	 *         given class.
 	 */
-	public final Set<Capability> getGoalOwner(GoalOwner owner) {
+	public final Set<Capability> getGoalOwner(
+			Class<? extends Capability> owner, boolean internal) {
 		Set<Capability> owners = new HashSet<>();
 
-		Set<Capability> fullAccessOwners = fullAccessOwnersMap.get(owner
-				.capability());
+		Set<Capability> fullAccessOwners = fullAccessOwnersMap.get(owner);
 		if (fullAccessOwners != null)
 			owners.addAll(fullAccessOwners);
 
-		if (!owner.internal()) {
+		if (!internal) {
 			Set<Capability> restrictedAccessOwners = restrictedAccessOwnersMap
-					.get(owner.capability());
+					.get(owner);
 			if (restrictedAccessOwners != null)
 				owners.addAll(restrictedAccessOwners);
 		}
diff --git a/bdi-jade/src/bdi4jade/core/Intention.java b/bdi-jade/src/bdi4jade/core/Intention.java
index 63ef69e..0d93139 100644
--- a/bdi-jade/src/bdi4jade/core/Intention.java
+++ b/bdi-jade/src/bdi4jade/core/Intention.java
@@ -107,22 +107,35 @@ public class Intention {
 		this.goalListeners = new LinkedList<>();
 		this.dispatcher = dispatcher;
 
-		GoalOwner owner = null;
+		Class<? extends Capability> owner = null;
+		boolean internal = false;
+
 		if (goal.getClass().isAnnotationPresent(GoalOwner.class)) {
-			owner = goal.getClass().getAnnotation(GoalOwner.class);
+			GoalOwner ownerAnnotation = goal.getClass().getAnnotation(
+					GoalOwner.class);
+			owner = ownerAnnotation.capability();
+			internal = ownerAnnotation.internal();
+		} else {
+			Class<?> enclosingClass = goal.getClass().getEnclosingClass();
+			if (enclosingClass != null
+					&& Capability.class.isAssignableFrom(goal.getClass()
+							.getEnclosingClass())) {
+				owner = (Class<Capability>) enclosingClass;
+			}
 		}
+
 		if (owner == null) {
 			this.owners = new HashSet<>();
 		} else {
 			if (dispatcher == null) {
-				this.owners = myAgent.getGoalOwner(owner);
+				this.owners = myAgent.getGoalOwner(owner, internal);
 			} else {
-				this.owners = dispatcher.getGoalOwner(owner);
+				this.owners = dispatcher.getGoalOwner(owner, internal);
 				if (owners.isEmpty()) {
 					throw new IllegalAccessException("Capability " + dispatcher
 							+ " has no access to goal "
 							+ goal.getClass().getName() + " of capability "
-							+ owner.capability().getName());
+							+ owner.getName());
 				}
 			}
 		}