Details
diff --git a/bdi-jade/src/bdi4jade/goal/BeliefGoal.java b/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
index e97b8fc..19b6a5d 100644
--- a/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
+++ b/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
@@ -22,6 +22,8 @@
package bdi4jade.goal;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
import bdi4jade.belief.BeliefBase;
/**
@@ -51,6 +53,7 @@ public class BeliefGoal implements Goal {
*
* @return the belief name.
*/
+ @Parameter(direction = Direction.IN)
public String getBeliefName() {
return beliefName;
}
diff --git a/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java b/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
index bff3e39..0bba114 100644
--- a/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
+++ b/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
@@ -22,6 +22,8 @@
package bdi4jade.goal;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
import bdi4jade.belief.Belief;
import bdi4jade.belief.BeliefBase;
@@ -61,6 +63,7 @@ public class BeliefValueGoal<T> extends BeliefGoal {
*
* @return the belief value.
*/
+ @Parameter(direction = Direction.IN)
public T getValue() {
return value;
}
@@ -95,7 +98,8 @@ public class BeliefValueGoal<T> extends BeliefGoal {
@Override
public String toString() {
return new StringBuffer(getClass().getName()).append(": ")
- .append(getBeliefName()).append(" should be ").append(value).toString();
+ .append(getBeliefName()).append(" should be ").append(value)
+ .toString();
}
}
\ No newline at end of file
diff --git a/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java b/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
index b7ef629..f9bc84f 100644
--- a/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
+++ b/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
@@ -73,7 +73,7 @@ public abstract class GoalTemplateFactory {
final Class<?> beliefValueClass) {
return new GoalTemplate() {
public boolean match(Goal goal) {
- if (goal instanceof BeliefValueGoal) {
+ if (goal instanceof BeliefSetValueGoal) {
BeliefSetValueGoal<?> bg = (BeliefSetValueGoal<?>) goal;
return bg.getBeliefName().equals(beliefName)
&& beliefValueClass.isInstance(bg.getValue());
@@ -104,7 +104,7 @@ public abstract class GoalTemplateFactory {
final Object beliefValue) {
return new GoalTemplate() {
public boolean match(Goal goal) {
- if (goal instanceof BeliefValueGoal) {
+ if (goal instanceof BeliefSetValueGoal) {
BeliefSetValueGoal<?> bg = (BeliefSetValueGoal<?>) goal;
return bg.getBeliefName().equals(beliefName)
&& beliefValue.equals(bg.getValue());
diff --git a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
index 6dfe1d4..8fa7605 100644
--- a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
+++ b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
@@ -9,8 +9,10 @@ import java.util.Set;
import javax.swing.Action;
import javax.swing.JButton;
+import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
import bdi4jade.core.AbstractBDIAgent;
import bdi4jade.core.MultipleCapabilityAgent;
@@ -24,6 +26,10 @@ import bdi4jade.examples.bdicycle.CompositeGoalCapability.MyGoal3;
import bdi4jade.examples.bdicycle.PlanFailureCapability;
import bdi4jade.examples.bdicycle.PlanFailureCapability.MyGoal;
import bdi4jade.examples.bdicycle.SubgoalCapability;
+import bdi4jade.examples.blocksworld.BlocksWorldCapability;
+import bdi4jade.examples.blocksworld.BlocksWorldView;
+import bdi4jade.examples.blocksworld.domain.On;
+import bdi4jade.examples.blocksworld.domain.Thing;
import bdi4jade.examples.capabilities.Middle1Capability;
import bdi4jade.examples.capabilities.TopCapability;
import bdi4jade.examples.helloworld.HelloWorldAgent;
@@ -43,6 +49,52 @@ import bdi4jade.goal.SequentialGoal;
*/
public class BDI4JADEExamplesPanel extends JPanel {
+ private class BlocksWorldAction extends BDI4JADEExamplesAction implements
+ GoalListener {
+
+ private static final long serialVersionUID = 2100583035268414082L;
+
+ private final SingleCapabilityAgent blocksWorldAgent;
+
+ public BlocksWorldAction() {
+ super.putValue(Action.NAME, "Blocks World");
+ this.blocksWorldAgent = new SingleCapabilityAgent(
+ new BlocksWorldCapability());
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ final JFrame frame = new JFrame();
+ frame.setTitle((String) this.getValue(Action.NAME));
+ frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+ frame.setVisible(false);
+ frame.setContentPane(new BlocksWorldView(blocksWorldAgent
+ .getCapability().getBeliefBase()));
+
+ frame.pack();
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ frame.setVisible(true);
+ }
+ });
+ blocksWorldAgent.addGoal(
+ new BlocksWorldCapability.AchieveBlocksStacked(target),
+ this);
+ }
+
+ @Override
+ public Set<AbstractBDIAgent> getAgents() {
+ Set<AbstractBDIAgent> agents = new HashSet<>();
+ agents.add(blocksWorldAgent);
+ return agents;
+ }
+
+ @Override
+ public void goalPerformed(GoalEvent event) {
+ log.info("Goal achieved!!");
+ }
+ }
+
private class CompositeGoalAction extends BDI4JADEExamplesAction implements
GoalListener {
private static final long serialVersionUID = 2100583035268414082L;
@@ -291,13 +343,20 @@ public class BDI4JADEExamplesPanel extends JPanel {
private static final long serialVersionUID = -1080267169700651610L;
+ private static final On[] target = { new On(Thing.BLOCK_5, Thing.TABLE),
+ new On(Thing.BLOCK_4, Thing.BLOCK_5),
+ new On(Thing.BLOCK_3, Thing.BLOCK_4),
+ new On(Thing.BLOCK_2, Thing.BLOCK_3),
+ new On(Thing.BLOCK_1, Thing.BLOCK_2) };
+
private final BDI4JADEExamplesAction[] actions;
public BDI4JADEExamplesPanel() {
this.actions = new BDI4JADEExamplesAction[] { new HelloWorldAction(),
new HelloWorldAnnotatedAction(), new PingPongAction(),
new CompositeGoalAction(), new PlanFailureAction(),
- new SubgoalCapabilityAction(), new MultiCapabilityAgentAction() };
+ new SubgoalCapabilityAction(),
+ new MultiCapabilityAgentAction(), new BlocksWorldAction() };
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/blocksworld/BlocksWorldCapability.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldCapability.java
index f4ed91e..8eff90d 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldCapability.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldCapability.java
@@ -23,6 +23,9 @@
package bdi4jade.examples.blocksworld;
import bdi4jade.annotation.Belief;
+import bdi4jade.annotation.GoalOwner;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
import bdi4jade.annotation.Plan;
import bdi4jade.belief.BeliefSet;
import bdi4jade.belief.TransientBeliefSet;
@@ -30,9 +33,8 @@ import bdi4jade.core.Capability;
import bdi4jade.examples.blocksworld.domain.Clear;
import bdi4jade.examples.blocksworld.domain.On;
import bdi4jade.examples.blocksworld.domain.Thing;
-import bdi4jade.examples.blocksworld.goal.AchieveBlocksStacked;
-import bdi4jade.examples.blocksworld.goal.PerformMove;
import bdi4jade.examples.blocksworld.plan.AchieveOnPlanBody;
+import bdi4jade.examples.blocksworld.plan.ClearPlanBody;
import bdi4jade.examples.blocksworld.plan.PerformMovePlanBody;
import bdi4jade.examples.blocksworld.plan.TopLevelPlanBody;
import bdi4jade.goal.Goal;
@@ -40,70 +42,117 @@ import bdi4jade.goal.GoalTemplateFactory;
import bdi4jade.plan.DefaultPlan;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class BlocksWorldCapability extends Capability {
- public static final String BELIEF_CLEAR = "clear";
+ @GoalOwner(capability = BlocksWorldCapability.class, internal = false)
+ public static class AchieveBlocksStacked implements Goal {
+ private static final long serialVersionUID = -8126833927953226126L;
+
+ private On[] target;
+
+ public AchieveBlocksStacked(On[] target) {
+ this.target = target;
+ }
+
+ @Parameter(direction = Direction.IN)
+ public On[] getTarget() {
+ return target;
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer sb = new StringBuffer("AchieveBlocksStacked: ");
+ for (On on : target) {
+ sb.append(on).append(" ");
+ }
+ return sb.toString();
+ }
+
+ }
+
+ @GoalOwner(capability = BlocksWorldCapability.class, internal = true)
+ public static class PerformMove implements Goal {
+ private static final long serialVersionUID = 8286023371969088149L;
+
+ private Thing thing1;
+ private Thing thing2;
+ public PerformMove(Thing thing1, Thing thing2) {
+ this.thing1 = thing1;
+ this.thing2 = thing2;
+ }
+
+ @Parameter(direction = Direction.IN)
+ public Thing getThing1() {
+ return thing1;
+ }
+
+ @Parameter(direction = Direction.IN)
+ public Thing getThing2() {
+ return thing2;
+ }
+
+ @Override
+ public String toString() {
+ return "PerformMove: " + thing1 + " to " + thing2;
+ }
+
+ }
+
+ public static final String BELIEF_CLEAR = "clear";
public static final String BELIEF_ON = "on";
private static final long serialVersionUID = 2298178213927064463L;
@Plan
- private bdi4jade.plan.Plan achieveBlocksStackedPlan;
+ private bdi4jade.plan.Plan achieveBlocksStackedPlan = new DefaultPlan(
+ AchieveBlocksStacked.class, TopLevelPlanBody.class);
@Plan
- private bdi4jade.plan.Plan achieveOnPlan;
+ private bdi4jade.plan.Plan achieveOnPlan = new DefaultPlan(
+ GoalTemplateFactory.beliefSetTypeGoal(BELIEF_ON, On.class),
+ AchieveOnPlanBody.class);
@Belief
- private BeliefSet<Clear> clear;
+ private BeliefSet<Clear> clear = new TransientBeliefSet<Clear>(BELIEF_CLEAR);
@Plan
- private bdi4jade.plan.Plan clearPlan;
+ private bdi4jade.plan.Plan clearPlan = new DefaultPlan(
+ GoalTemplateFactory.beliefSetTypeGoal(BELIEF_CLEAR, Clear.class),
+ ClearPlanBody.class);
@Belief
- private BeliefSet<On> on;
+ private BeliefSet<On> on = new TransientBeliefSet<On>(BELIEF_ON);
@Plan
private bdi4jade.plan.Plan performMovePlan;
public BlocksWorldCapability() {
- this.on = new TransientBeliefSet<On>(BELIEF_ON);
- on.addValue(new On(Thing.BLOCK_1, Thing.TABLE));
- on.addValue(new On(Thing.BLOCK_3, Thing.BLOCK_1));
- on.addValue(new On(Thing.BLOCK_2, Thing.BLOCK_3));
- on.addValue(new On(Thing.BLOCK_5, Thing.BLOCK_2));
- on.addValue(new On(Thing.BLOCK_4, Thing.BLOCK_5));
-
- this.clear = new TransientBeliefSet<Clear>(BELIEF_CLEAR);
- clear.addValue(new Clear(Thing.BLOCK_4));
- clear.addValue(new Clear(Thing.TABLE));
-
- this.achieveOnPlan = new DefaultPlan(
- GoalTemplateFactory.beliefSetTypeGoal(
- BlocksWorldAgent.BELIEF_ON, On.class),
- AchieveOnPlanBody.class);
- this.clearPlan = new DefaultPlan(GoalTemplateFactory.beliefSetTypeGoal(
- BlocksWorldAgent.BELIEF_CLEAR, Clear.class),
- AchieveOnPlanBody.class);
this.performMovePlan = new DefaultPlan(PerformMove.class,
PerformMovePlanBody.class) {
@Override
- @SuppressWarnings("unchecked")
public boolean isContextApplicable(Goal goal) {
if (goal instanceof PerformMove) {
PerformMove performMove = (PerformMove) goal;
- BeliefSet<Clear> set = (BeliefSet<Clear>) getPlanLibrary()
- .getCapability().getBeliefBase()
- .getBelief(BlocksWorldAgent.BELIEF_CLEAR);
- return set.hasValue(new Clear(performMove.getThing1()))
- && set.hasValue(new Clear(performMove.getThing2()));
+ return clear.hasValue(new Clear(performMove.getThing1()))
+ && clear.hasValue(new Clear(performMove.getThing2()));
}
return false;
}
};
- this.achieveBlocksStackedPlan = new DefaultPlan(
- AchieveBlocksStacked.class, TopLevelPlanBody.class);
}
+
+ @Override
+ protected void setup() {
+ clear.addValue(new Clear(Thing.BLOCK_4));
+ clear.addValue(new Clear(Thing.TABLE));
+
+ on.addValue(new On(Thing.BLOCK_1, Thing.TABLE));
+ on.addValue(new On(Thing.BLOCK_3, Thing.BLOCK_1));
+ on.addValue(new On(Thing.BLOCK_2, Thing.BLOCK_3));
+ on.addValue(new On(Thing.BLOCK_5, Thing.BLOCK_2));
+ on.addValue(new On(Thing.BLOCK_4, Thing.BLOCK_5));
+ }
+
}
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldView.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldView.java
index 3383d66..e4ad86a 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldView.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/BlocksWorldView.java
@@ -25,8 +25,7 @@ import bdi4jade.examples.blocksworld.domain.On;
import bdi4jade.examples.blocksworld.domain.Thing;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class BlocksWorldView extends JPanel implements BeliefListener {
@@ -73,9 +72,9 @@ public class BlocksWorldView extends JPanel implements BeliefListener {
State state = new State();
BeliefSet<On> onBelief = (BeliefSet<On>) beliefBase
- .getBelief(BlocksWorldAgent.BELIEF_ON);
+ .getBelief(BlocksWorldCapability.BELIEF_ON);
BeliefSet<Clear> clearBelief = (BeliefSet<Clear>) beliefBase
- .getBelief(BlocksWorldAgent.BELIEF_CLEAR);
+ .getBelief(BlocksWorldCapability.BELIEF_CLEAR);
List<Thing> tops = new ArrayList<>(2);
for (On on : onBelief.getValue()) {
@@ -113,7 +112,7 @@ public class BlocksWorldView extends JPanel implements BeliefListener {
private Thing getNext(Thing thing) {
BeliefSet<On> onBelief = (BeliefSet<On>) beliefBase
- .getBelief(BlocksWorldAgent.BELIEF_ON);
+ .getBelief(BlocksWorldCapability.BELIEF_ON);
for (On on : onBelief.getValue()) {
if (on.getThing1().equals(thing))
return on.getThing2();
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Block.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Block.java
index 4134113..2464d68 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Block.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Block.java
@@ -23,8 +23,7 @@
package bdi4jade.examples.blocksworld.domain;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class Block implements Thing {
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Clear.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Clear.java
index b6ed096..7470fbc 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Clear.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Clear.java
@@ -22,8 +22,7 @@
package bdi4jade.examples.blocksworld.domain;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class Clear {
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/On.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/On.java
index e08bab3..a23b4f5 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/On.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/On.java
@@ -23,8 +23,7 @@
package bdi4jade.examples.blocksworld.domain;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class On {
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Table.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Table.java
index 633938b..52019d7 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Table.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Table.java
@@ -23,8 +23,7 @@
package bdi4jade.examples.blocksworld.domain;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class Table implements Thing {
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Thing.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Thing.java
index 4592e6d..b97d80a 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Thing.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/domain/Thing.java
@@ -22,8 +22,7 @@
package bdi4jade.examples.blocksworld.domain;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public interface Thing {
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/AchieveOnPlanBody.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/AchieveOnPlanBody.java
index 17ec940..0e99d81 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/AchieveOnPlanBody.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/AchieveOnPlanBody.java
@@ -22,22 +22,26 @@
package bdi4jade.examples.blocksworld.plan;
-import bdi4jade.examples.blocksworld.BlocksWorldAgent;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
+import bdi4jade.event.GoalEvent;
+import bdi4jade.examples.blocksworld.BlocksWorldCapability;
+import bdi4jade.examples.blocksworld.BlocksWorldCapability.PerformMove;
import bdi4jade.examples.blocksworld.domain.Clear;
import bdi4jade.examples.blocksworld.domain.On;
import bdi4jade.examples.blocksworld.domain.Thing;
-import bdi4jade.examples.blocksworld.goal.PerformMove;
import bdi4jade.goal.BeliefSetValueGoal;
+import bdi4jade.goal.GoalStatus;
+import bdi4jade.plan.Plan.EndState;
import bdi4jade.plan.planbody.BeliefGoalPlanBody;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class AchieveOnPlanBody extends BeliefGoalPlanBody {
enum Step {
- CLEAR_1, CLEAR_2, PERFORM_MOVE, WAIT_CLEAR_1, WAIT_CLEAR_2, WAIT_DONE;
+ CLEAR_1, CLEAR_2, DONE, PERFORM_MOVE, WAIT_DONE;
}
private static final long serialVersionUID = -5919677537834351951L;
@@ -51,40 +55,52 @@ public class AchieveOnPlanBody extends BeliefGoalPlanBody {
switch (step) {
case CLEAR_1:
dispatchSubgoalAndListen(new BeliefSetValueGoal<Clear>(
- BlocksWorldAgent.BELIEF_CLEAR, new Clear(thing1)));
- step = Step.WAIT_CLEAR_1;
- case WAIT_CLEAR_1:
- if (getGoalEvent() != null) {
- step = Step.CLEAR_2;
- }
- break;
+ BlocksWorldCapability.BELIEF_CLEAR, new Clear(thing1)));
+ step = Step.CLEAR_2;
case CLEAR_2:
- dispatchSubgoalAndListen(new BeliefSetValueGoal<Clear>(
- BlocksWorldAgent.BELIEF_CLEAR, new Clear(thing2)));
- step = Step.WAIT_CLEAR_2;
- case WAIT_CLEAR_2:
- if (getGoalEvent() != null) {
+ if (isSubgoalAchieved()) {
+ dispatchSubgoalAndListen(new BeliefSetValueGoal<Clear>(
+ BlocksWorldCapability.BELIEF_CLEAR, new Clear(thing2)));
step = Step.PERFORM_MOVE;
}
break;
case PERFORM_MOVE:
- dispatchSubgoalAndListen(new PerformMove(thing1, thing2));
- step = Step.WAIT_DONE;
+ if (isSubgoalAchieved()) {
+ dispatchSubgoalAndListen(new PerformMove(thing1, thing2));
+ step = Step.WAIT_DONE;
+ }
break;
case WAIT_DONE:
- getGoalEvent();
+ if (isSubgoalAchieved()) {
+ step = Step.DONE;
+ }
+ break;
+ default:
break;
}
}
+ private boolean isSubgoalAchieved() {
+ GoalEvent goalEvent = getGoalEvent();
+ if (goalEvent == null) {
+ return false;
+ } else if (!GoalStatus.ACHIEVED.equals(goalEvent.getStatus())) {
+ setEndState(EndState.FAILED);
+ return false;
+ } else {
+ return true;
+ }
+ }
+
@Override
- @SuppressWarnings("unchecked")
public void onStart() {
- super.onStart();
- BeliefSetValueGoal<On> achieveOn = (BeliefSetValueGoal<On>) getGoal();
- this.thing1 = achieveOn.getValue().getThing1();
- this.thing2 = achieveOn.getValue().getThing2();
this.step = Step.CLEAR_1;
}
+ @Parameter(direction = Direction.IN, mandatory = true)
+ public void setValue(On on) {
+ this.thing1 = on.getThing1();
+ this.thing2 = on.getThing2();
+ }
+
}
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/ClearPlanBody.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/ClearPlanBody.java
index b5dee01..e7cc927 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/ClearPlanBody.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/ClearPlanBody.java
@@ -22,47 +22,64 @@
package bdi4jade.examples.blocksworld.plan;
+import bdi4jade.annotation.Belief;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
import bdi4jade.belief.BeliefSet;
-import bdi4jade.examples.blocksworld.BlocksWorldAgent;
+import bdi4jade.event.GoalEvent;
+import bdi4jade.examples.blocksworld.BlocksWorldCapability;
import bdi4jade.examples.blocksworld.domain.Clear;
import bdi4jade.examples.blocksworld.domain.On;
import bdi4jade.examples.blocksworld.domain.Thing;
import bdi4jade.goal.BeliefSetValueGoal;
+import bdi4jade.goal.GoalStatus;
+import bdi4jade.plan.Plan.EndState;
import bdi4jade.plan.planbody.BeliefGoalPlanBody;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class ClearPlanBody extends BeliefGoalPlanBody {
private static final long serialVersionUID = -5919677537834351951L;
- private BeliefSet<On> onSet;
+ private boolean goalDispatched;
+ @Belief
+ private BeliefSet<On> on;
private Thing thing;
@Override
public void execute() {
- for (int i = 0; i < Thing.THINGS.length; i++) {
- Thing t = Thing.THINGS[i];
- On on = new On(t, thing);
- if (onSet.hasValue(on)) {
- dispatchSubgoalAndListen(new BeliefSetValueGoal<On>(
- BlocksWorldAgent.BELIEF_ON, new On(t, Thing.TABLE)));
- getGoalEvent();
- break;
+ if (!goalDispatched) {
+ for (int i = 0; i < Thing.THINGS.length; i++) {
+ Thing t = Thing.THINGS[i];
+ if (on.hasValue(new On(t, thing))) {
+ dispatchSubgoalAndListen(new BeliefSetValueGoal<On>(
+ BlocksWorldCapability.BELIEF_ON, new On(t,
+ Thing.TABLE)));
+ this.goalDispatched = true;
+ break;
+ }
+ }
+ } else {
+ GoalEvent goalEvent = getGoalEvent();
+ if (goalEvent == null) {
+ return;
+ } else if (!GoalStatus.ACHIEVED.equals(goalEvent.getStatus())) {
+ setEndState(EndState.FAILED);
+ return;
}
}
}
@Override
- @SuppressWarnings("unchecked")
public void onStart() {
- super.onStart();
- this.onSet = (BeliefSet<On>) getBeliefBase().getBelief(
- BlocksWorldAgent.BELIEF_ON);
- BeliefSetValueGoal<Clear> achieveClear = (BeliefSetValueGoal<Clear>) getGoal();
- this.thing = achieveClear.getValue().getThing();
+ this.goalDispatched = false;
+ }
+
+ @Parameter(direction = Direction.IN, mandatory = true)
+ public void setValue(Clear clear) {
+ this.thing = clear.getThing();
}
}
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/PerformMovePlanBody.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/PerformMovePlanBody.java
index c9ba161..56dae54 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/PerformMovePlanBody.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/PerformMovePlanBody.java
@@ -22,58 +22,59 @@
package bdi4jade.examples.blocksworld.plan;
+import bdi4jade.annotation.Belief;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
import bdi4jade.belief.BeliefSet;
-import bdi4jade.examples.blocksworld.BlocksWorldAgent;
import bdi4jade.examples.blocksworld.domain.Clear;
import bdi4jade.examples.blocksworld.domain.On;
import bdi4jade.examples.blocksworld.domain.Thing;
-import bdi4jade.examples.blocksworld.goal.PerformMove;
import bdi4jade.plan.Plan.EndState;
import bdi4jade.plan.planbody.AbstractPlanBody;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class PerformMovePlanBody extends AbstractPlanBody {
private static final long serialVersionUID = -5919677537834351951L;
- private BeliefSet<Clear> clearSet;
- private BeliefSet<On> onSet;
+ @Belief
+ private BeliefSet<Clear> clear;
+ @Belief
+ private BeliefSet<On> on;
private Thing thing1;
private Thing thing2;
@Override
public void action() {
+ // If thing1 was over something, this something will now be clear
for (Thing thing : Thing.THINGS) {
- On on = new On(thing1, thing);
- if (onSet.hasValue(on)) {
- onSet.removeValue(on);
+ On onVal = new On(thing1, thing);
+ if (on.hasValue(onVal)) {
+ on.removeValue(onVal);
if (!Thing.TABLE.equals(thing)) {
- clearSet.addValue(new Clear(thing));
+ clear.addValue(new Clear(thing));
}
}
}
if (!thing2.equals(Thing.TABLE)) {
- clearSet.removeValue(new Clear(thing2));
+ clear.removeValue(new Clear(thing2));
}
- onSet.addValue(new On(thing1, thing2));
+ on.addValue(new On(thing1, thing2));
setEndState(EndState.SUCCESSFULL);
}
- @Override
- @SuppressWarnings("unchecked")
- public void onStart() {
- this.onSet = (BeliefSet<On>) getBeliefBase().getBelief(
- BlocksWorldAgent.BELIEF_ON);
- this.clearSet = (BeliefSet<Clear>) getBeliefBase().getBelief(
- BlocksWorldAgent.BELIEF_CLEAR);
- PerformMove goal = (PerformMove) getGoal();
- this.thing1 = goal.getThing1();
- this.thing2 = goal.getThing2();
+ @Parameter(direction = Direction.IN, mandatory = true)
+ public void setThing1(Thing thing1) {
+ this.thing1 = thing1;
+ }
+
+ @Parameter(direction = Direction.IN, mandatory = true)
+ public void setThing2(Thing thing2) {
+ this.thing2 = thing2;
}
}
diff --git a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/TopLevelPlanBody.java b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/TopLevelPlanBody.java
index e3b5a19..f3448bd 100644
--- a/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/TopLevelPlanBody.java
+++ b/bdi-jade-test/src/bdi4jade/examples/blocksworld/plan/TopLevelPlanBody.java
@@ -22,50 +22,48 @@
package bdi4jade.examples.blocksworld.plan;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import bdi4jade.examples.blocksworld.BlocksWorldAgent;
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
+import bdi4jade.event.GoalEvent;
+import bdi4jade.examples.blocksworld.BlocksWorldCapability;
import bdi4jade.examples.blocksworld.domain.On;
-import bdi4jade.examples.blocksworld.goal.AchieveBlocksStacked;
import bdi4jade.goal.BeliefSetValueGoal;
+import bdi4jade.goal.GoalStatus;
import bdi4jade.plan.Plan.EndState;
import bdi4jade.plan.planbody.AbstractPlanBody;
/**
- * @author ingrid
- *
+ * @author Ingrid Nunes
*/
public class TopLevelPlanBody extends AbstractPlanBody {
private static final long serialVersionUID = -5919677537834351951L;
private int counter;
- private Log log;
private On[] target;
- public TopLevelPlanBody() {
- this.counter = 0;
- this.log = LogFactory.getLog(this.getClass());
- }
-
@Override
public void action() {
// If a subgoal has been dispatched, wait for its completion
if (counter != 0) {
- if ((getGoalEvent() == null)) {
+ GoalEvent goalEvent = getGoalEvent();
+ if (goalEvent == null) {
+ return;
+ } else if (!GoalStatus.ACHIEVED.equals(goalEvent.getStatus())) {
+ setEndState(EndState.FAILED);
return;
}
}
// Dispatch the next subgoal, if there are subgoals left
- if (counter != target.length) {
+ if (counter < target.length) {
dispatchSubgoalAndListen(new BeliefSetValueGoal<On>(
- BlocksWorldAgent.BELIEF_ON, target[counter]));
+ BlocksWorldCapability.BELIEF_ON, target[counter]));
}
counter++;
- if (counter > target.length)
+ if (counter > target.length) {
setEndState(EndState.SUCCESSFULL);
+ }
}
@Override
@@ -77,9 +75,15 @@ public class TopLevelPlanBody extends AbstractPlanBody {
@Override
public void onStart() {
+ this.counter = 0;
+
log.info("World Model at start is:");
- this.target = ((AchieveBlocksStacked) getGoal()).getTarget();
log.info(getBeliefBase());
}
+ @Parameter(direction = Direction.IN, mandatory = true)
+ public void setTarget(On[] target) {
+ this.target = target;
+ }
+
}