bdi4jade

Goals --> equals e hashcode

8/27/2014 7:18:48 PM

Details

diff --git a/bdi-jade/src/bdi4jade/goal/BeliefGoal.java b/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
index 31f3914..75a77e9 100644
--- a/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
+++ b/bdi-jade/src/bdi4jade/goal/BeliefGoal.java
@@ -36,7 +36,7 @@ public class BeliefGoal<K> implements Goal {
 
 	private static final long serialVersionUID = 2493877854717226283L;
 
-	private K beliefName;
+	protected K beliefName;
 
 	/**
 	 * Creates a new BeliefGoal with the provided belief name.
@@ -49,6 +49,26 @@ public class BeliefGoal<K> implements Goal {
 	}
 
 	/**
+	 * @see java.lang.Object#equals(java.lang.Object)
+	 */
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		BeliefGoal<?> other = (BeliefGoal<?>) obj;
+		if (beliefName == null) {
+			if (other.beliefName != null)
+				return false;
+		} else if (!beliefName.equals(other.beliefName))
+			return false;
+		return true;
+	}
+
+	/**
 	 * Returns the name of the belief associated with this goal.
 	 * 
 	 * @return the belief name.
@@ -59,6 +79,20 @@ public class BeliefGoal<K> implements Goal {
 	}
 
 	/**
+	 * @see java.lang.Object#hashCode()
+	 */
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result
+				+ getClass().hashCode();
+		result = prime * result
+				+ ((beliefName == null) ? 0 : beliefName.hashCode());
+		return result;
+	}
+
+	/**
 	 * Checks whether this goal is achieved by verifying if the provided belief
 	 * base contains the belief of this goal.
 	 * 
diff --git a/bdi-jade/src/bdi4jade/goal/BeliefSetValueGoal.java b/bdi-jade/src/bdi4jade/goal/BeliefSetValueGoal.java
index 7ed7ea0..b2204f5 100644
--- a/bdi-jade/src/bdi4jade/goal/BeliefSetValueGoal.java
+++ b/bdi-jade/src/bdi4jade/goal/BeliefSetValueGoal.java
@@ -89,5 +89,7 @@ public class BeliefSetValueGoal<K, V> extends BeliefValueGoal<K, V> {
 				.append(getBeliefName()).append(" should have ")
 				.append(getValue()).toString();
 	}
+	
+	
 
 }
diff --git a/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java b/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
index 89153c3..885d29b 100644
--- a/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
+++ b/bdi-jade/src/bdi4jade/goal/BeliefValueGoal.java
@@ -61,6 +61,29 @@ public class BeliefValueGoal<K, V> extends BeliefGoal<K> {
 	}
 
 	/**
+	 * @see java.lang.Object#equals(java.lang.Object)
+	 */
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (getClass() != obj.getClass())
+			return false;
+		BeliefValueGoal<?, ?> other = (BeliefValueGoal<?, ?>) obj;
+		if (beliefName == null) {
+			if (other.beliefName != null)
+				return false;
+		} else if (!beliefName.equals(other.beliefName))
+			return false;
+		if (value == null) {
+			if (other.value != null)
+				return false;
+		} else if (!value.equals(other.value))
+			return false;
+		return true;
+	}
+
+	/**
 	 * The belief value associated with this goal.
 	 * 
 	 * @return the belief value.
@@ -71,6 +94,22 @@ public class BeliefValueGoal<K, V> extends BeliefGoal<K> {
 	}
 
 	/**
+	 * @see java.lang.Object#hashCode()
+	 */
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + getClass().hashCode();
+		result = prime * result
+				+ ((beliefName == null) ? 0 : beliefName.hashCode());
+		result = prime * result
+				+ ((beliefName == null) ? 0 : beliefName.hashCode());
+		result = prime * result + ((value == null) ? 0 : value.hashCode());
+		return result;
+	}
+
+	/**
 	 * Checks whether this goal is achieved by verifying if the provided belief
 	 * has the value specified in this goal.
 	 * 
diff --git a/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java b/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
index a1ee0cb..0beb4f3 100644
--- a/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
+++ b/bdi-jade/src/bdi4jade/goal/GoalTemplateFactory.java
@@ -152,6 +152,37 @@ public abstract class GoalTemplateFactory {
 
 	/**
 	 * This method creates a goal template that positively matches a goal if it
+	 * is of the type {@link BeliefValueGoal}, is of the class of given belief
+	 * name class, and has the given value.
+	 * 
+	 * @param beliefNameClass
+	 *            the belief name class to be matched.
+	 * @param beliefValue
+	 *            the value to be matched.
+	 * @return the goal template that checks if the goal is a
+	 *         {@link BeliefValueGoal} with the given name and value.
+	 */
+	public static GoalTemplate beliefValueGoal(final Class<?> beliefNameClass,
+			final Object beliefValue) {
+		return new GoalTemplate() {
+			public boolean match(Goal goal) {
+				if (goal instanceof BeliefValueGoal) {
+					BeliefValueGoal<?, ?> bg = (BeliefValueGoal<?, ?>) goal;
+					return beliefNameClass.isInstance(bg.getBeliefName())
+							&& beliefValue.equals(bg.getValue());
+				}
+				return false;
+			}
+
+			public String toString() {
+				return "belief(<" + beliefNameClass.getName() + ">("
+						+ beliefValue + "))";
+			}
+		};
+	}
+
+	/**
+	 * This method creates a goal template that positively matches a goal if it
 	 * is of the type {@link BeliefValueGoal}, has the given belief name, and
 	 * has the given value.
 	 * 
@@ -162,7 +193,7 @@ public abstract class GoalTemplateFactory {
 	 * @return the goal template that checks if the goal is a
 	 *         {@link BeliefValueGoal} with the given name and value.
 	 */
-	public static GoalTemplate beliefValueGoal(final String beliefName,
+	public static GoalTemplate beliefValueGoal(final Object beliefName,
 			final Object beliefValue) {
 		return new GoalTemplate() {
 			public boolean match(Goal goal) {