1 | package dk.deepthought.sidious.greenhouse; |
2 | |
3 | import java.util.Collection; |
4 | |
5 | import org.apache.commons.logging.Log; |
6 | import org.apache.commons.logging.LogFactory; |
7 | |
8 | import dk.deepthought.sidious.gui.SidiousOutput; |
9 | import dk.deepthought.sidious.planner.Plan; |
10 | import dk.deepthought.sidious.rules.Rule; |
11 | import dk.deepthought.sidious.services.ServiceEngine; |
12 | import dk.deepthought.sidious.supportsystem.Adjustable; |
13 | import dk.deepthought.sidious.supportsystem.PlanRequester; |
14 | import dk.deepthought.sidious.supportsystem.Repository; |
15 | import dk.deepthought.sidious.supportsystem.State; |
16 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
17 | import dk.deepthought.sidious.supportsystem.SystemSettings; |
18 | |
19 | /** |
20 | * This class represents a greenhouse and its properties. |
21 | * |
22 | * @author Deepthought |
23 | * |
24 | */ |
25 | public class Greenhouse implements PlanRequester, Runnable { |
26 | |
27 | private static final Log logger = LogFactory.getLog(Greenhouse.class); |
28 | |
29 | /** |
30 | * The id of this greenhouse. |
31 | */ |
32 | private final SuperLinkID id; |
33 | |
34 | /** |
35 | * Boolean that holds interrupt status. |
36 | */ |
37 | private volatile boolean interrupted = false; |
38 | |
39 | /** |
40 | * Collection of adjustables |
41 | */ |
42 | private final Collection<Adjustable> adjustables; |
43 | |
44 | /** |
45 | * Collection of rules |
46 | */ |
47 | private final Collection<Rule> rules; |
48 | |
49 | private boolean finished; |
50 | |
51 | private Plan currentPlan; |
52 | |
53 | /** |
54 | * Constructs a new instance of <code>Greenhouse</code> with the specified |
55 | * id, adjustables, and rules. |
56 | * |
57 | * @param id |
58 | * the id of the greenhouse |
59 | * @param adjustables |
60 | * the adjustables associated with the greenhouse |
61 | * @param rules |
62 | * the rules associated with the greenhouse |
63 | */ |
64 | public Greenhouse(final SuperLinkID id, |
65 | final Collection<Adjustable> adjustables, |
66 | final Collection<Rule> rules) { |
67 | if (id == null || adjustables == null || rules == null) { |
68 | throw new IllegalArgumentException("null is not a valid parameter"); |
69 | } |
70 | if (adjustables.isEmpty() || rules.isEmpty()) { |
71 | throw new IllegalArgumentException( |
72 | "An empty list is not a valid parameter"); |
73 | } |
74 | this.id = id; |
75 | this.adjustables = adjustables; |
76 | this.rules = rules; |
77 | } |
78 | |
79 | /* |
80 | * (non-Javadoc) |
81 | * |
82 | * @see dk.deepthought.sidious.supportsystem.PlanRequester#getAdjustables() |
83 | */ |
84 | public Collection<Adjustable> getAdjustables() { |
85 | return adjustables; |
86 | } |
87 | |
88 | /* |
89 | * (non-Javadoc) |
90 | * |
91 | * @see dk.deepthought.sidious.supportsystem.PlanRequester#getRules() |
92 | */ |
93 | public Collection<Rule> getRules() { |
94 | return rules; |
95 | } |
96 | |
97 | /* |
98 | * (non-Javadoc) |
99 | * |
100 | * @see dk.deepthought.sidious.supportsystem.PlanRequester#getState() |
101 | */ |
102 | public State getState() { |
103 | if (logger.isDebugEnabled()) { |
104 | logger.debug("getState() - start"); |
105 | } |
106 | |
107 | State returnState = ServiceEngine.getCurrentState(); |
108 | if (logger.isDebugEnabled()) { |
109 | logger.debug("getState() - end - return value=" + returnState); |
110 | } |
111 | return returnState; |
112 | } |
113 | |
114 | /** |
115 | * Main loop of the Greenhouse. Controls the frequency of which a new plan |
116 | * is requested from the Blackboard. |
117 | * |
118 | * @see java.lang.Runnable#run() |
119 | */ |
120 | public void run() { |
121 | if (logger.isDebugEnabled()) { |
122 | logger.debug("run() - start"); |
123 | } |
124 | //FIXME Dette er bare ikke godt nok |
125 | //Fjern tråd run ting |
126 | while (!interrupted()) { |
127 | Repository.getBlackboard().requestPlan(this); |
128 | try { |
129 | Thread.sleep(SystemSettings.getTimestep()); |
130 | } catch (InterruptedException e) { |
131 | logger.error("Thread could not wait", e); |
132 | } |
133 | } |
134 | |
135 | if (logger.isDebugEnabled()) { |
136 | logger.debug("run() - end"); |
137 | } |
138 | } |
139 | |
140 | /** |
141 | * Returns <code>true</code> if this thread has been interrupted, |
142 | * <code>false</code> otherwise. |
143 | * |
144 | * @return <code>true</code> if interrupted |
145 | */ |
146 | private boolean interrupted() { |
147 | return interrupted; |
148 | } |
149 | |
150 | /** |
151 | * Method interrupts the thread. |
152 | */ |
153 | public void interrupt() { |
154 | if (logger.isDebugEnabled()) { |
155 | logger.debug("interrupt() - start"); //$NON-NLS-1$ |
156 | } |
157 | |
158 | interrupted = true; |
159 | |
160 | if (logger.isDebugEnabled()) { |
161 | logger.debug("interrupt() - end"); //$NON-NLS-1$ |
162 | } |
163 | } |
164 | |
165 | /* |
166 | * (non-Javadoc) |
167 | * |
168 | * @see dk.deepthought.sidious.supportsystem.PlanRequester#getID() |
169 | */ |
170 | public SuperLinkID getID() { |
171 | return id; |
172 | } |
173 | |
174 | /* |
175 | * (non-Javadoc) |
176 | * |
177 | * @see dk.deepthought.sidious.supportsystem.PlanRequester#setPlan(dk.deepthought.sidious.planner.Plan) |
178 | */ |
179 | public void setPlan(Plan plan) { |
180 | finished = true; |
181 | currentPlan = plan; |
182 | SidiousOutput.getInstance().addFinishedPlan(plan); |
183 | } |
184 | |
185 | /** |
186 | * Gets the current plan for this greenhouse. |
187 | * @return the current plan of this |
188 | */ |
189 | public Plan getCurrentPlan() { |
190 | return currentPlan; |
191 | } |
192 | |
193 | /** |
194 | * @return <code>true</code> if a plan has been delivered |
195 | */ |
196 | public boolean isFinished() { |
197 | return finished; |
198 | } |
199 | |
200 | /* |
201 | * (non-Javadoc) |
202 | * |
203 | * @see java.lang.Object#toString() |
204 | */ |
205 | @Override |
206 | public String toString() { |
207 | return getClass().getSimpleName() + "[id=" + id + "]"; |
208 | } |
209 | |
210 | } |