001 package dk.deepthought.sidious.goalhandler;
002 
003 import net.jcip.annotations.Immutable;
004 
005 import org.apache.commons.logging.Log;
006 import org.apache.commons.logging.LogFactory;
007 
008 import dk.deepthought.sidious.supportsystem.State;
009 import dk.deepthought.sidious.supportsystem.SuperLinkID;
010 
011 /**
012  * This class represents a goal.
013  
014  @author Deepthought
015  
016  */
017 @Immutable
018 public class Goal implements Comparable<Goal> {
019     /**
020      * Logger for this class
021      */
022     private static final Log logger = LogFactory.getLog(Goal.class);
023 
024     /**
025      * The parent of this goal. I.e. the requester to whom it belongs.
026      */
027     private final SuperLinkID origin;
028 
029     /**
030      * The goal state.
031      */
032     private final State goalState;
033 
034     /**
035      * The desire of this goal. 
036      */
037     private final double desire;
038 
039     /**
040      * Constructor; returns a new instance of Goal.
041      
042      @param goalState
043      *            the desired goal state
044      @param desire
045      *            the desire of the goal
046      @param origin
047      *            id of the originating plan requester of this goal
048      */
049     public Goal(State goalState, double desire, SuperLinkID origin) {
050         if (goalState == null) {
051             logger.error("Goal(State goalState=null, double desire=" + desire
052                     ", SuperLinkID origin=" + origin
053                     ") - goalState invalid!");
054             throw new IllegalArgumentException("Invalid goalState!");
055         }
056         if (origin == null) {
057             logger.error("Goal(State goalState=" + goalState
058                     ", double desire=" + desire + ", SuperLinkID origin=null) - origin invalid!");
059             throw new IllegalArgumentException("Invalid origin!");
060         }
061 
062         this.goalState = goalState;
063         this.desire = desire;
064         this.origin = origin;
065     }
066 
067     /**
068      * Returns the desire of this goal.
069      
070      @return the desire of this goal
071      */
072     public double getDesire() {
073         return desire;
074     }
075 
076     /**
077      * Returns the goal state of this goal.
078      
079      @return the goal state of this goal
080      */
081     public State getGoalState() {
082         return goalState;
083     }
084 
085     /**
086      * Returns the id of the originating <code>PlanRequester</code> of this
087      * goal.
088      
089      @return id of the origin of this goal
090      */
091     public SuperLinkID getOrigin() {
092         return origin;
093     }
094 
095     @Override
096     public int hashCode() {
097         final int PRIME = 31;
098         int result = 1;
099         result = PRIME * result
100                 ((goalState == null: goalState.hashCode());
101         result = PRIME * result + ((origin == null: origin.hashCode());
102         return result;
103     }
104 
105     @Override
106     public boolean equals(Object obj) {
107         if (this == obj)
108             return true;
109         if (obj == null)
110             return false;
111         if (getClass() != obj.getClass())
112             return false;
113         final Goal other = (Goalobj;
114         if (goalState == null) {
115             if (other.goalState != null)
116                 return false;
117         else if (!goalState.equals(other.goalState))
118             return false;
119         if (origin == null) {
120             if (other.origin != null)
121                 return false;
122         else if (!origin.equals(other.origin))
123             return false;
124         return true;
125     }
126 
127     @Override
128     public String toString() {
129         return "Goal[origin=" + origin + ", goalState=" + goalState
130                 ", desire=" + desire + "]";
131     }
132 
133     /* (non-Javadoc)
134      * @see java.lang.Comparable#compareTo(java.lang.Object)
135      */
136     public int compareTo(Goal other) {
137         if (this.getDesire() > other.getDesire()) {
138             return -1;
139         else if (this.getDesire() < other.getDesire()) {
140             return 1;
141         else {
142             return 0;
143         }
144     }
145 
146 }