bdi4jade
Changes
bdi-jade/src/bdi4jade/annotation/PartCapability.java 103(+46 -57)
bdi-jade/src/bdi4jade/core/Capability.java 119(+96 -23)
bdi-jade-test/APDescription.txt 2(+1 -1)
Details
bdi-jade/src/bdi4jade/core/Capability.java 119(+96 -23)
diff --git a/bdi-jade/src/bdi4jade/core/Capability.java b/bdi-jade/src/bdi4jade/core/Capability.java
index 2ee9629..b7ff5a8 100644
--- a/bdi-jade/src/bdi4jade/core/Capability.java
+++ b/bdi-jade/src/bdi4jade/core/Capability.java
@@ -25,6 +25,7 @@ package bdi4jade.core;
import jade.lang.acl.ACLMessage;
import java.io.Serializable;
+import java.lang.reflect.Field;
import java.security.acl.Owner;
import java.util.Collection;
import java.util.HashMap;
@@ -40,6 +41,8 @@ import org.apache.commons.logging.LogFactory;
import bdi4jade.annotation.GoalOwner;
import bdi4jade.belief.Belief;
import bdi4jade.belief.BeliefBase;
+import bdi4jade.belief.TransientBelief;
+import bdi4jade.belief.TransientBeliefSet;
import bdi4jade.core.GoalUpdateSet.GoalDescription;
import bdi4jade.goal.Goal;
import bdi4jade.plan.Plan;
@@ -145,6 +148,7 @@ public class Capability implements Serializable {
this.log = LogFactory.getLog(getClass());
this.intentions = new LinkedList<>();
this.parentCapabilities = generateParentCapabilities();
+ this.started = false;
// Id initialization
if (id == null) {
@@ -179,9 +183,9 @@ public class Capability implements Serializable {
computeGoalOwnersMap();
- synchronized (this) {
- this.started = false;
- }
+ log.debug("Parent capabilities: " + parentCapabilities);
+ log.debug("Full access owners: " + fullAccessOwnersMap);
+ log.debug("Restricted access owners: " + restrictedAccessOwnersMap);
}
/**
@@ -205,6 +209,72 @@ public class Capability implements Serializable {
}
/**
+ * Adds by reflection capability components, such as beliefs and plans,
+ * according to annotated fields. This method is invoked by for capability
+ * class, and all parent classes.
+ *
+ * @param capabilityClass
+ * the capability class of which fields should me added to this
+ * capability.
+ */
+ protected void addAnnotatedFields(
+ Class<? extends Capability> capabilityClass) {
+ for (Field field : capabilityClass.getDeclaredFields()) {
+ boolean b = field.isAccessible();
+ field.setAccessible(true);
+ try {
+ if (field.isAnnotationPresent(bdi4jade.annotation.Belief.class)) {
+ if (Belief.class.isAssignableFrom(field.getType())) {
+ Belief<?> belief = (Belief<?>) field.get(this);
+ this.getBeliefBase().addBelief(belief);
+ }
+ } else if (field
+ .isAnnotationPresent(bdi4jade.annotation.TransientBelief.class)) {
+ bdi4jade.annotation.TransientBelief annotation = field
+ .getAnnotation(bdi4jade.annotation.TransientBelief.class);
+ String name = "".equals(annotation.name()) ? field
+ .getName() : annotation.name();
+ Object value = field.get(this);
+ this.getBeliefBase().addBelief(
+ new TransientBelief(name, value));
+ } else if (field
+ .isAnnotationPresent(bdi4jade.annotation.TransientBeliefSet.class)) {
+ bdi4jade.annotation.TransientBeliefSet annotation = field
+ .getAnnotation(bdi4jade.annotation.TransientBeliefSet.class);
+ String name = "".equals(annotation.name()) ? field
+ .getName() : annotation.name();
+ Object value = field.get(this);
+ if (Set.class.isAssignableFrom(field.getType())) {
+ this.getBeliefBase().addBelief(
+ new TransientBeliefSet(name, (Set) value));
+ }
+ } else if (field
+ .isAnnotationPresent(bdi4jade.annotation.Plan.class)) {
+ if (Plan.class.isAssignableFrom(field.getType())) {
+ Plan plan = (Plan) field.get(this);
+ this.getPlanLibrary().addPlan(plan);
+ }
+ } else if (field
+ .isAnnotationPresent(bdi4jade.annotation.AssociatedCapability.class)) {
+ if (Capability.class.isAssignableFrom(field.getType())) {
+ Capability capability = (Capability) field.get(this);
+ this.addAssociatedCapability(capability);
+ }
+ } else if (field
+ .isAnnotationPresent(bdi4jade.annotation.PartCapability.class)) {
+ if (Capability.class.isAssignableFrom(field.getType())) {
+ Capability capability = (Capability) field.get(this);
+ this.addPartCapability(capability);
+ }
+ }
+ } catch (Exception exc) {
+ log.warn(exc);
+ }
+ field.setAccessible(b);
+ }
+ }
+
+ /**
* Associates a capability to this capability.
*
* @param capability
@@ -230,7 +300,10 @@ public class Capability implements Serializable {
*/
public void addCandidatePlans(Goal goal,
Map<Capability, Set<Plan>> candidatePlansMap) {
- candidatePlansMap.put(this, planLibrary.getCandidatePlans(goal));
+ Set<Plan> plans = planLibrary.getCandidatePlans(goal);
+ if (!plans.isEmpty()) {
+ candidatePlansMap.put(this, plans);
+ }
for (Capability part : partCapabilities) {
part.addCandidatePlans(goal, candidatePlansMap);
}
@@ -282,11 +355,11 @@ public class Capability implements Serializable {
return false;
}
- private void computeGoalOwnersMap() {
+ private final void computeGoalOwnersMap() {
this.fullAccessOwnersMap = new HashMap<>();
ReflectionUtils.addGoalOwner(fullAccessOwnersMap, this);
if (wholeCapability != null) {
- ReflectionUtils.addGoalOwner(fullAccessOwnersMap, this);
+ ReflectionUtils.addGoalOwner(fullAccessOwnersMap, wholeCapability);
}
this.restrictedAccessOwnersMap = new HashMap<>();
for (Capability capability : associationTargets) {
@@ -348,13 +421,12 @@ public class Capability implements Serializable {
}
@SuppressWarnings("unchecked")
- private List<Class<? extends Capability>> generateParentCapabilities() {
+ private final List<Class<? extends Capability>> generateParentCapabilities() {
List<Class<? extends Capability>> parentCapabilities = new LinkedList<>();
- Class<?> currentClass = this.getClass();
+ Class<?> currentClass = this.getClass().getSuperclass();
while (Capability.class.isAssignableFrom(currentClass)
&& !Capability.class.equals(currentClass)) {
- parentCapabilities.add((Class<Capability>) currentClass
- .getSuperclass());
+ parentCapabilities.add((Class<Capability>) currentClass);
currentClass = currentClass.getSuperclass();
}
return parentCapabilities;
@@ -365,7 +437,7 @@ public class Capability implements Serializable {
*
* @return the associated capabilities.
*/
- public Set<Capability> getAssociatedCapabilities() {
+ public final Set<Capability> getAssociatedCapabilities() {
return associationTargets;
}
@@ -439,7 +511,7 @@ public class Capability implements Serializable {
* capability has no access to goals owned by capability of the
* given class.
*/
- public Set<Capability> getGoalOwner(GoalOwner owner) {
+ public final Set<Capability> getGoalOwner(GoalOwner owner) {
Set<Capability> owners = new HashSet<>();
Set<Capability> fullAccessOwners = fullAccessOwnersMap.get(owner
@@ -493,7 +565,7 @@ public class Capability implements Serializable {
*
* @return the parentCapabilities.
*/
- public List<Class<? extends Capability>> getParentCapabilities() {
+ public final List<Class<? extends Capability>> getParentCapabilities() {
return parentCapabilities;
}
@@ -654,17 +726,18 @@ public class Capability implements Serializable {
* @param myAgent
* the myAgent to set
*/
- final void setMyAgent(AbstractBDIAgent myAgent) {
- synchronized (this) {
- if (this.myAgent != null && myAgent == null) {
- takeDown();
- }
- this.myAgent = myAgent;
- if (this.myAgent != null && !started) {
- // TODO Adds all annotated fields.
- setup();
- this.started = true;
+ final synchronized void setMyAgent(AbstractBDIAgent myAgent) {
+ if (this.myAgent != null && myAgent == null) {
+ takeDown();
+ }
+ this.myAgent = myAgent;
+ if (this.myAgent != null && !started) {
+ addAnnotatedFields(this.getClass());
+ for (Class<? extends Capability> parentCapabilityClass : parentCapabilities) {
+ addAnnotatedFields(parentCapabilityClass);
}
+ setup();
+ this.started = true;
}
}
diff --git a/bdi-jade/src/bdi4jade/util/ReflectionUtils.java b/bdi-jade/src/bdi4jade/util/ReflectionUtils.java
index e883998..94f3bea 100644
--- a/bdi-jade/src/bdi4jade/util/ReflectionUtils.java
+++ b/bdi-jade/src/bdi4jade/util/ReflectionUtils.java
@@ -63,10 +63,14 @@ public abstract class ReflectionUtils {
public static void addGoalOwner(
Map<Class<? extends Capability>, Set<Capability>> goalOwnersMap,
Capability capability) {
- addGoalOwner(goalOwnersMap, capability.getClass(), capability);
- for (Class<? extends Capability> parentCapability : capability
- .getParentCapabilities()) {
- addGoalOwner(goalOwnersMap, parentCapability, capability);
+ if (!Capability.class.equals(capability.getClass())) {
+ addGoalOwner(goalOwnersMap, capability.getClass(), capability);
+ for (Class<? extends Capability> parentCapability : capability
+ .getParentCapabilities()) {
+ addGoalOwner(goalOwnersMap, parentCapability, capability);
+ }
+ } else {
+ assert capability.getParentCapabilities().isEmpty();
}
}
bdi-jade-test/APDescription.txt 2(+1 -1)
diff --git a/bdi-jade-test/APDescription.txt b/bdi-jade-test/APDescription.txt
index 398983c..aa4fc28 100644
--- a/bdi-jade-test/APDescription.txt
+++ b/bdi-jade-test/APDescription.txt
@@ -1 +1 @@
-( ap-description :name "192.168.0.2:1099/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://ingrid-asus:7778/acc))))
+( ap-description :name "localhost:1099/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://localhost:7778/acc))))
diff --git a/bdi-jade-test/MTPs-Main-Container.txt b/bdi-jade-test/MTPs-Main-Container.txt
index 6b6c35d..f5617c1 100644
--- a/bdi-jade-test/MTPs-Main-Container.txt
+++ b/bdi-jade-test/MTPs-Main-Container.txt
@@ -1 +1 @@
-http://ingrid-asus:7778/acc
+http://localhost:7778/acc
diff --git a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
index 784adbd..e923a08 100644
--- a/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
+++ b/bdi-jade-test/src/bdi4jade/examples/BDI4JADEExamplesPanel.java
@@ -9,11 +9,15 @@ import java.util.Set;
import javax.swing.Action;
import javax.swing.JButton;
+import javax.swing.JOptionPane;
import javax.swing.JPanel;
import bdi4jade.core.AbstractBDIAgent;
+import bdi4jade.core.SingleCapabilityAgent;
+import bdi4jade.event.GoalEvent;
+import bdi4jade.event.GoalListener;
import bdi4jade.examples.helloworld.HelloWorldAgent;
-import bdi4jade.examples.helloworld.HelloWorldAgent.HelloWorldGoal;
+import bdi4jade.examples.helloworld.HelloWorldAnnotatedCapability;
/**
* This class is a panel that is used as content pane of the application with
@@ -37,7 +41,9 @@ public class BDI4JADEExamplesPanel extends JPanel {
@Override
public void actionPerformed(ActionEvent e) {
- helloWorldAgent.addGoal(new HelloWorldGoal("reader"));
+ String name = JOptionPane.showInputDialog(
+ BDI4JADEExamplesPanel.this, "Please, inform your name:");
+ helloWorldAgent.addGoal(new HelloWorldAgent.HelloWorldGoal(name));
}
@Override
@@ -48,12 +54,49 @@ public class BDI4JADEExamplesPanel extends JPanel {
}
}
+ private class HelloWorldAnnotatedAction extends BDI4JADEExamplesAction
+ implements GoalListener {
+
+ private static final long serialVersionUID = 2100583035268414082L;
+
+ private final SingleCapabilityAgent helloWorldAnnotatedAgent;
+
+ public HelloWorldAnnotatedAction() {
+ super.putValue(Action.NAME, "Hello World Annotated Capability");
+ this.helloWorldAnnotatedAgent = new SingleCapabilityAgent(
+ new HelloWorldAnnotatedCapability());
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String name = JOptionPane.showInputDialog(
+ BDI4JADEExamplesPanel.this, "Please, inform your name:");
+ helloWorldAnnotatedAgent.addGoal(
+ new HelloWorldAnnotatedCapability.HelloWorldGoal(name),
+ this);
+ }
+
+ @Override
+ public Set<AbstractBDIAgent> getAgents() {
+ Set<AbstractBDIAgent> agents = new HashSet<>();
+ agents.add(helloWorldAnnotatedAgent);
+ return agents;
+ }
+
+ @Override
+ public void goalPerformed(GoalEvent event) {
+ if (event.getStatus().isFinished()) {
+ System.out.println("Hello World Goal Finished! Time: "
+ + event.getGoal());
+ }
+ }
+
+ }
+
private static final long serialVersionUID = -1080267169700651610L;
private final BDI4JADEExamplesAction[] actions;
- // agents.put(HelloWorldParamAgent.class.getSimpleName(),
- // new HelloWorldParamAgent());
// agents.put(BDIAgent1.MY_NAME, new BDIAgent1());
// agents.put(BDIAgent2.MY_NAME, new BDIAgent2());
// agents.put(MyAgent.class.getSimpleName(), new MyAgent());
@@ -61,7 +104,8 @@ public class BDI4JADEExamplesPanel extends JPanel {
// new NestedCapabilitiesAgent());
public BDI4JADEExamplesPanel() {
- this.actions = new BDI4JADEExamplesAction[] { new HelloWorldAction() };
+ this.actions = new BDI4JADEExamplesAction[] { new HelloWorldAction(),
+ new HelloWorldAnnotatedAction() };
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/log4j.properties b/bdi-jade-test/src/bdi4jade/examples/log4j.properties
index 7de22df..ed864b5 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=INFO
+log4j.logger.bdi4jade=TRACE
log4j.logger.bdi4jade.examples=DEBUG