bdi4jade

Composite Goals

8/15/2014 1:11:31 PM

Details

diff --git a/bdi-jade/src/bdi4jade/core/Intention.java b/bdi-jade/src/bdi4jade/core/Intention.java
index 06d5274..fe62bc3 100644
--- a/bdi-jade/src/bdi4jade/core/Intention.java
+++ b/bdi-jade/src/bdi4jade/core/Intention.java
@@ -160,8 +160,12 @@ public class Intention {
 			}
 		}
 
-		for (Set<Plan> plans : options.values()) {
+		for (Capability capability : options.keySet()) {
+			Set<Plan> plans = options.get(capability);
 			plans.removeAll(executedPlans);
+			if (plans.isEmpty()) {
+				options.remove(capability);
+			}
 		}
 
 		while (this.currentPlan == null && !options.isEmpty()) {
diff --git a/bdi-jade/src/bdi4jade/plan/DefaultPlan.java b/bdi-jade/src/bdi4jade/plan/DefaultPlan.java
index 5418e8e..5f8eae4 100644
--- a/bdi-jade/src/bdi4jade/plan/DefaultPlan.java
+++ b/bdi-jade/src/bdi4jade/plan/DefaultPlan.java
@@ -139,6 +139,25 @@ public class DefaultPlan extends AbstractPlan {
 	}
 
 	/**
+	 * Creates a new simple plan. It is a plan whose body is the specified class
+	 * and its id is the given id. Goal templates can be specified by overriding
+	 * the {@link #initGoalTemplates()} method or invoking the
+	 * {@link #addGoalTemplate(GoalTemplate)} method, while more message
+	 * templates can be specified by overriding the
+	 * {@link #initMessageTemplates()} method or invoking the
+	 * {@link #addMessageTemplate(MessageTemplate)} method.
+	 * 
+	 * @param id
+	 *            the plan id.
+	 * @param planBodyClass
+	 *            the class of this plan body.
+	 */
+	public DefaultPlan(String id, Class<? extends PlanBody> planBodyClass) {
+		super(id);
+		this.planBodyClass = planBodyClass;
+	}
+
+	/**
 	 * Creates a new simple plan, which is able to achieve goals that match the
 	 * provided template. It is a plan whose body is the specified class and its
 	 * id is the given id. It sets that this plan can achieve goals of the
diff --git a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
index bf4ee45..adec43a 100644
--- a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
+++ b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
@@ -16,9 +16,17 @@ import bdi4jade.core.AbstractBDIAgent;
 import bdi4jade.core.SingleCapabilityAgent;
 import bdi4jade.event.GoalEvent;
 import bdi4jade.event.GoalListener;
+import bdi4jade.examples.compositegoal.CompositeGoalCapability;
+import bdi4jade.examples.compositegoal.CompositeGoalCapability.MyGoal1;
+import bdi4jade.examples.compositegoal.CompositeGoalCapability.MyGoal2;
+import bdi4jade.examples.compositegoal.CompositeGoalCapability.MyGoal3;
 import bdi4jade.examples.helloworld.HelloWorldAgent;
 import bdi4jade.examples.helloworld.HelloWorldAnnotatedCapability;
 import bdi4jade.examples.ping.PingPongCapability;
+import bdi4jade.goal.CompositeGoal;
+import bdi4jade.goal.Goal;
+import bdi4jade.goal.ParallelGoal;
+import bdi4jade.goal.SequentialGoal;
 
 /**
  * This class is a panel that is used as content pane of the application with
@@ -130,6 +138,52 @@ public class BDI4JADEExamplesPanel extends JPanel {
 		}
 	}
 
+	private class CompositeGoalAction extends BDI4JADEExamplesAction implements
+			GoalListener {
+		private static final long serialVersionUID = 2100583035268414082L;
+
+		private final AbstractBDIAgent compositeGoalAgent;
+
+		public CompositeGoalAction() {
+			super.putValue(Action.NAME, "Composite Goal Agent");
+			this.compositeGoalAgent = new SingleCapabilityAgent(
+					new CompositeGoalCapability());
+		}
+
+		@Override
+		public void actionPerformed(ActionEvent e) {
+			int answer = JOptionPane.showConfirmDialog(
+					BDI4JADEExamplesPanel.this,
+					"Should goals be achieved sequentially?",
+					"Sequential vs. Parallel Goals", JOptionPane.YES_NO_OPTION);
+			Goal[] goals = { new MyGoal1("Hello World!"), new MyGoal2(),
+					new MyGoal3() };
+			CompositeGoal compositeGoal = null;
+			if (JOptionPane.YES_OPTION == answer) {
+				compositeGoal = new SequentialGoal(goals);
+			} else {
+				compositeGoal = new ParallelGoal(goals);
+			}
+			compositeGoalAgent.addGoal(compositeGoal, this);
+		}
+
+		@Override
+		public void goalPerformed(GoalEvent event) {
+			if (event.getStatus().isFinished()
+					&& event.getGoal() instanceof CompositeGoal) {
+				log.info("Goal finished!");
+				log.info(event.getGoal() + " Status: " + event.getStatus());
+			}
+		}
+
+		@Override
+		public Set<AbstractBDIAgent> getAgents() {
+			Set<AbstractBDIAgent> agents = new HashSet<>();
+			agents.add(compositeGoalAgent);
+			return agents;
+		}
+	}
+
 	private static final long serialVersionUID = -1080267169700651610L;
 
 	private final BDI4JADEExamplesAction[] actions;
@@ -144,7 +198,8 @@ public class BDI4JADEExamplesPanel extends JPanel {
 
 	public BDI4JADEExamplesPanel() {
 		this.actions = new BDI4JADEExamplesAction[] { new HelloWorldAction(),
-				new HelloWorldAnnotatedAction(), new PingPongAction() };
+				new HelloWorldAnnotatedAction(), new PingPongAction(),
+				new CompositeGoalAction() };
 		this.setLayout(new GridLayout(actions.length, 1));
 		for (BDI4JADEExamplesAction action : actions) {
 			this.add(new JButton(action));
diff --git a/bdi-jade-test/src/bdi4jade/examples/compositegoal/CompositeGoalCapability.java b/bdi-jade-test/src/bdi4jade/examples/compositegoal/CompositeGoalCapability.java
index 49b55f4..28f0032 100644
--- a/bdi-jade-test/src/bdi4jade/examples/compositegoal/CompositeGoalCapability.java
+++ b/bdi-jade-test/src/bdi4jade/examples/compositegoal/CompositeGoalCapability.java
@@ -22,32 +22,25 @@
 
 package bdi4jade.examples.compositegoal;
 
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import java.util.Random;
 
+import bdi4jade.annotation.GoalOwner;
 import bdi4jade.annotation.Parameter;
 import bdi4jade.annotation.Parameter.Direction;
 import bdi4jade.core.Capability;
-import bdi4jade.event.GoalEvent;
-import bdi4jade.event.GoalListener;
-import bdi4jade.goal.CompositeGoal;
 import bdi4jade.goal.Goal;
 import bdi4jade.goal.GoalTemplateFactory;
-import bdi4jade.goal.ParallelGoal;
-import bdi4jade.goal.SequentialGoal;
 import bdi4jade.plan.DefaultPlan;
-import bdi4jade.plan.Plan;
+import bdi4jade.plan.Plan.EndState;
+import bdi4jade.plan.planbody.AbstractPlanBody;
 
 /**
- * @author ingrid
- * 
+ * @author Ingrid Nunes
  */
-public class CompositeGoalCapability extends Capability implements GoalListener {
+public class CompositeGoalCapability extends Capability {
 
-	public class MyGoal1 implements Goal {
+	@GoalOwner(capability = CompositeGoalCapability.class)
+	public static class MyGoal1 implements Goal {
 		private static final long serialVersionUID = 3405041038738876061L;
 
 		private String msg;
@@ -65,10 +58,10 @@ public class CompositeGoalCapability extends Capability implements GoalListener 
 		public String toString() {
 			return getClass().getSimpleName() + ": " + msg;
 		}
+	}
 
-	};
-
-	public class MyGoal2 implements Goal {
+	@GoalOwner(capability = CompositeGoalCapability.class)
+	public static class MyGoal2 implements Goal {
 		private static final long serialVersionUID = 3405041038738876061L;
 
 		private String message;
@@ -86,10 +79,10 @@ public class CompositeGoalCapability extends Capability implements GoalListener 
 		public String toString() {
 			return getClass().getSimpleName() + ": " + message;
 		}
+	}
 
-	};
-
-	public class MyGoal3 implements Goal {
+	@GoalOwner(capability = CompositeGoalCapability.class)
+	public static class MyGoal3 implements Goal {
 		private static final long serialVersionUID = 3405041038738876061L;
 
 		private String msg;
@@ -107,52 +100,43 @@ public class CompositeGoalCapability extends Capability implements GoalListener 
 		public String toString() {
 			return getClass().getSimpleName() + ": " + msg;
 		}
+	}
 
-	};
-
-	private static final Log log = LogFactory
-			.getLog(CompositeGoalCapability.class);
-	private static final long serialVersionUID = -4800805796961540570L;
-
-	private static Set<Plan> getPlans() {
-		Set<Plan> plans = new HashSet<Plan>();
-		DefaultPlan plan = new DefaultPlan(MyPlan.class);
-		plan.addGoalTemplate(GoalTemplateFactory.goalType(MyGoal1.class));
-		plan.addGoalTemplate(GoalTemplateFactory.goalType(MyGoal2.class));
-		plan.addGoalTemplate(GoalTemplateFactory.goalType(MyGoal3.class));
-		plans.add(plan);
-		return plans;
+	public static class MyPlan extends AbstractPlanBody {
+		private static final long serialVersionUID = -220345270457161508L;
+
+		public void action() {
+			long random = new Random().nextLong();
+			log.info("Random: " + random);
+			if (random % 7 != 0)
+				setEndState(EndState.SUCCESSFULL);
+			else
+				setEndState(EndState.FAILED);
+			log.info(getGoal() + " Plan#" + getPlan().getId() + " EndState: "
+					+ getEndState());
+		}
 	}
 
-	private boolean sequential;
+	private static final long serialVersionUID = -4800805796961540570L;
 
-	public CompositeGoalCapability(boolean sequential) {
-		super(null, getPlans());
-		this.sequential = sequential;
-	}
+	@bdi4jade.annotation.Plan
+	private DefaultPlan multigoalPlan1;
 
-	@Override
-	public void goalPerformed(GoalEvent event) {
-		if (event.getStatus().isFinished()
-				&& event.getGoal() instanceof CompositeGoal) {
-			log.info(event.getGoal() + " Status: " + event.getStatus());
-			log.info("Goal finished!! Removing capability of this agent...");
-			//getMyAgent().removeCapability(this);
+	@bdi4jade.annotation.Plan
+	private DefaultPlan multigoalPlan2;
 
-		}
-	}
+	public CompositeGoalCapability() {
+		this.multigoalPlan1 = new DefaultPlan("multigoalPlan1", MyPlan.class);
+		multigoalPlan1.addGoalTemplate(GoalTemplateFactory
+				.goalType(MyGoal1.class));
+		multigoalPlan1.addGoalTemplate(GoalTemplateFactory
+				.goalType(MyGoal2.class));
 
-	@Override
-	protected void setup() {
-		Goal[] goals = { new MyGoal1("Hello World!"), new MyGoal2(),
-				new MyGoal3() };
-		CompositeGoal compositeGoal = null;
-		if (this.sequential) {
-			compositeGoal = new SequentialGoal(goals);
-		} else {
-			compositeGoal = new ParallelGoal(goals);
-		}
-		this.getMyAgent().addGoal(compositeGoal, this);
+		this.multigoalPlan2 = new DefaultPlan("multigoalPlan2", MyPlan.class);
+		multigoalPlan2.addGoalTemplate(GoalTemplateFactory
+				.goalType(MyGoal2.class));
+		multigoalPlan2.addGoalTemplate(GoalTemplateFactory
+				.goalType(MyGoal3.class));
 	}
 
 }
\ No newline at end of file
diff --git a/bdi-jade-test/src/bdi4jade/examples/log4j.properties b/bdi-jade-test/src/bdi4jade/examples/log4j.properties
index ed864b5..0a27ef3 100644
--- a/bdi-jade-test/src/bdi4jade/examples/log4j.properties
+++ b/bdi-jade-test/src/bdi4jade/examples/log4j.properties
@@ -29,6 +29,6 @@ log4j.logger.org.apache.struts2=FATAL
 # Spring Stuff
 log4j.logger.org.springframework=FATAL
 
-log4j.logger.bdi4jade=TRACE
+log4j.logger.bdi4jade=WARN
 log4j.logger.bdi4jade.examples=DEBUG