1 | package dk.deepthought.sidious.goalhandler; |
2 | |
3 | import net.jcip.annotations.Immutable; |
4 | |
5 | import org.apache.commons.logging.Log; |
6 | import org.apache.commons.logging.LogFactory; |
7 | |
8 | import dk.deepthought.sidious.supportsystem.State; |
9 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
10 | |
11 | /** |
12 | * This class represents a goal. |
13 | * |
14 | * @author Deepthought |
15 | * |
16 | */ |
17 | @Immutable |
18 | public class Goal implements Comparable<Goal> { |
19 | /** |
20 | * Logger for this class |
21 | */ |
22 | private static final Log logger = LogFactory.getLog(Goal.class); |
23 | |
24 | /** |
25 | * The parent of this goal. I.e. the requester to whom it belongs. |
26 | */ |
27 | private final SuperLinkID origin; |
28 | |
29 | /** |
30 | * The goal state. |
31 | */ |
32 | private final State goalState; |
33 | |
34 | /** |
35 | * The desire of this goal. |
36 | */ |
37 | private final double desire; |
38 | |
39 | /** |
40 | * Constructor; returns a new instance of Goal. |
41 | * |
42 | * @param goalState |
43 | * the desired goal state |
44 | * @param desire |
45 | * the desire of the goal |
46 | * @param origin |
47 | * id of the originating plan requester of this goal |
48 | */ |
49 | public Goal(State goalState, double desire, SuperLinkID origin) { |
50 | if (goalState == null) { |
51 | logger.error("Goal(State goalState=null, double desire=" + desire |
52 | + ", SuperLinkID origin=" + origin |
53 | + ") - goalState invalid!"); |
54 | throw new IllegalArgumentException("Invalid goalState!"); |
55 | } |
56 | if (origin == null) { |
57 | logger.error("Goal(State goalState=" + goalState |
58 | + ", double desire=" + desire + ", SuperLinkID origin=null) - origin invalid!"); |
59 | throw new IllegalArgumentException("Invalid origin!"); |
60 | } |
61 | |
62 | this.goalState = goalState; |
63 | this.desire = desire; |
64 | this.origin = origin; |
65 | } |
66 | |
67 | /** |
68 | * Returns the desire of this goal. |
69 | * |
70 | * @return the desire of this goal |
71 | */ |
72 | public double getDesire() { |
73 | return desire; |
74 | } |
75 | |
76 | /** |
77 | * Returns the goal state of this goal. |
78 | * |
79 | * @return the goal state of this goal |
80 | */ |
81 | public State getGoalState() { |
82 | return goalState; |
83 | } |
84 | |
85 | /** |
86 | * Returns the id of the originating <code>PlanRequester</code> of this |
87 | * goal. |
88 | * |
89 | * @return id of the origin of this goal |
90 | */ |
91 | public SuperLinkID getOrigin() { |
92 | return origin; |
93 | } |
94 | |
95 | @Override |
96 | public int hashCode() { |
97 | final int PRIME = 31; |
98 | int result = 1; |
99 | result = PRIME * result |
100 | + ((goalState == null) ? 0 : goalState.hashCode()); |
101 | result = PRIME * result + ((origin == null) ? 0 : 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 = (Goal) obj; |
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 | } |