1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <!-- Generated by javadoc (build 1.5.0_09) on Tue Jan 08 20:46:01 GMT 2008 -->
7 Activity (leJOS NXT API documentation)
10 <META NAME="keywords" CONTENT="lejos.subsumption.Activity class">
12 <LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
14 <SCRIPT type="text/javascript">
15 function windowTitle()
17 parent.document.title="Activity (leJOS NXT API documentation)";
25 <BODY BGCOLOR="white" onload="windowTitle();">
28 <!-- ========= START OF TOP NAVBAR ======= -->
29 <A NAME="navbar_top"><!-- --></A>
30 <A HREF="#skip-navbar_top" title="Skip navigation links"></A>
31 <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
33 <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
34 <A NAME="navbar_top_firstrow"><!-- --></A>
35 <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
36 <TR ALIGN="center" VALIGN="top">
37 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
38 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
39 <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
40 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
41 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
42 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
43 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
47 <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
53 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
54 PREV CLASS
55 <A HREF="../../lejos/subsumption/ActivityBase.html" title="class in lejos.subsumption"><B>NEXT CLASS</B></A></FONT></TD>
56 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
57 <A HREF="../../index.html?lejos/subsumption/Activity.html" target="_top"><B>FRAMES</B></A>
58 <A HREF="Activity.html" target="_top"><B>NO FRAMES</B></A>
59 <SCRIPT type="text/javascript">
62 document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
67 <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
74 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
75 SUMMARY: NESTED | <A HREF="#fields_inherited_from_class_lejos.subsumption.ActivityBase">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD>
76 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
77 DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD>
80 <A NAME="skip-navbar_top"></A>
81 <!-- ========= END OF TOP NAVBAR ========= -->
84 <!-- ======== START OF CLASS DATA ======== -->
87 lejos.subsumption</FONT>
91 <A HREF="../../java/lang/Object.html" title="class in java.lang">java.lang.Object</A>
92 <IMG SRC="../../resources/inherit.gif" ALT="extended by "><A HREF="../../java/lang/Thread.html" title="class in java.lang">java.lang.Thread</A>
93 <IMG SRC="../../resources/inherit.gif" ALT="extended by "><A HREF="../../lejos/subsumption/ActivityBase.html" title="class in lejos.subsumption">lejos.subsumption.ActivityBase</A>
94 <IMG SRC="../../resources/inherit.gif" ALT="extended by "><B>lejos.subsumption.Activity</B>
98 <DT><PRE>public abstract class <B>Activity</B><DT>extends <A HREF="../../lejos/subsumption/ActivityBase.html" title="class in lejos.subsumption">ActivityBase</A></DL>
102 An activity that operates in coordination with other activities.
103 Only one activity can run at a time.
104 When an activity of a higher priority wants to run any activities
105 of a lower priority will be suppressed. Furthermore, if there is
106 already an activity of the same or lower priority running, that
107 activity will be stopped.
109 This is essentially an alternative way of implementing a Behavior or
110 Subsumption architecture. It is totally distinct from the Behavior
111 and Arbitrator classes and does not use either.
115 <LI>This class should be sub-classed and the action() method should
116 be implemented to perform the desired action. That may be to respond
117 to a change in a sensor, or it may be to cause the robot to wander
118 around (or anything else).
119 This method should call one of the pause() methods occasionally. In
120 particular if it wants to sleep or yield control it should call pause()
121 instead of sleep() or yield() or wait().
122 <LI>An application should create instances of the sub-classes as needed,
123 set their priorities by calling setPriority() and then start them
125 <LI>If an activity wants to run it should call activity.iWantToRun(), or
126 another class could call it.
127 <LI>If there is no higher priority activity
128 running when an activity become runnable, any activities of the same or
129 lower priority will be suppressed. In addition, if an activity was not
130 already running the action() method will be called in its own thread.
131 <LI>If an activity is already running when it is run again, pause() throws
132 an InterruptedException. It can do what it wants with that. An activity that
133 is run when a sensor is activated might want to restart action() from the
134 beginning for example. A background activity might just ignore it.
135 <LI>If a higher priority activity gets to run, pause() on this activity will
136 throw a StopException. This should not be caught so that the action()
137 method exits with that exception.
139 Note. A background activity should ensure that something makes it runnable
140 if it is stopped. The easiest way to do that is to override resetRunnable()
141 to call iWantToRun().
146 * When sensor one is pressed, run the motors in some pattern.
148 class ControlMotors extends Activity implements PortListener
150 public ControlMotors()
152 Port.S1.addPortListener(this);
156 * Called when the sensor state changes (in some thread other than this one).
158 public void stateChanged(Port s, int old, int nu)
167 * Encapsulates the actual activity we want to perform.
169 * @exception StopException if we are forcibly stopped.
171 protected void action() throws StopException
173 boolean finished = false;
175 // Sit here until we are finished or we are forcibly halted.
184 // Wait for 0.25 secs, may throw InterruptedException
190 // We are finished
192 } catch (InterruptedException ie)
194 // pause() was interrupted. Re-start from the beginning
205 <DT><B>Author:</B></DT>
206 <DD>Paul Andrews</DD>
211 <!-- =========== FIELD SUMMARY =========== -->
213 <A NAME="field_summary"><!-- --></A>
214 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
215 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
216 <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
217 <B>Field Summary</B></FONT></TH>
220 <A NAME="fields_inherited_from_class_lejos.subsumption.ActivityBase"><!-- --></A>
221 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
222 <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
223 <TH ALIGN="left"><B>Fields inherited from class lejos.subsumption.<A HREF="../../lejos/subsumption/ActivityBase.html" title="class in lejos.subsumption">ActivityBase</A></B></TH>
225 <TR BGCOLOR="white" CLASS="TableRowColor">
226 <TD><CODE><A HREF="../../lejos/subsumption/ActivityBase.html#monitor">monitor</A></CODE></TD>
229 <A NAME="fields_inherited_from_class_java.lang.Thread"><!-- --></A>
230 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
231 <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
232 <TH ALIGN="left"><B>Fields inherited from class java.lang.<A HREF="../../java/lang/Thread.html" title="class in java.lang">Thread</A></B></TH>
234 <TR BGCOLOR="white" CLASS="TableRowColor">
235 <TD><CODE><A HREF="../../java/lang/Thread.html#MAX_PRIORITY">MAX_PRIORITY</A>, <A HREF="../../java/lang/Thread.html#MIN_PRIORITY">MIN_PRIORITY</A>, <A HREF="../../java/lang/Thread.html#NORM_PRIORITY">NORM_PRIORITY</A></CODE></TD>
239 <!-- ======== CONSTRUCTOR SUMMARY ======== -->
241 <A NAME="constructor_summary"><!-- --></A>
242 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
243 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
244 <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
245 <B>Constructor Summary</B></FONT></TH>
247 <TR BGCOLOR="white" CLASS="TableRowColor">
248 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#Activity()">Activity</A></B>()</CODE>
251 </TD>
255 <!-- ========== METHOD SUMMARY =========== -->
257 <A NAME="method_summary"><!-- --></A>
258 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
259 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
260 <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
261 <B>Method Summary</B></FONT></TH>
263 <TR BGCOLOR="white" CLASS="TableRowColor">
264 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
265 <CODE>protected abstract void</CODE></FONT></TD>
266 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#action()">action</A></B>()</CODE>
269 Encapsulates the actual activity we want to perform.</TD>
271 <TR BGCOLOR="white" CLASS="TableRowColor">
272 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
273 <CODE>protected void</CODE></FONT></TD>
274 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#iWantToRun()">iWantToRun</A></B>()</CODE>
277 Call this if you want this activity to run.</TD>
279 <TR BGCOLOR="white" CLASS="TableRowColor">
280 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
281 <CODE>protected void</CODE></FONT></TD>
282 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#pause()">pause</A></B>()</CODE>
285 Wait until we've either been made runnable or someone else has.</TD>
287 <TR BGCOLOR="white" CLASS="TableRowColor">
288 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
289 <CODE>protected void</CODE></FONT></TD>
290 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#pause(long)">pause</A></B>(long time)</CODE>
293 Wait at most 'time' milliseconds.</TD>
295 <TR BGCOLOR="white" CLASS="TableRowColor">
296 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
297 <CODE>protected void</CODE></FONT></TD>
298 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#resetRunnable()">resetRunnable</A></B>()</CODE>
301 Reset the runnable activity.</TD>
303 <TR BGCOLOR="white" CLASS="TableRowColor">
304 <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
305 <CODE> void</CODE></FONT></TD>
306 <TD><CODE><B><A HREF="../../lejos/subsumption/Activity.html#run()">run</A></B>()</CODE>
309 Thread entry point.</TD>
312 <A NAME="methods_inherited_from_class_java.lang.Thread"><!-- --></A>
313 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
314 <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
315 <TH ALIGN="left"><B>Methods inherited from class java.lang.<A HREF="../../java/lang/Thread.html" title="class in java.lang">Thread</A></B></TH>
317 <TR BGCOLOR="white" CLASS="TableRowColor">
318 <TD><CODE><A HREF="../../java/lang/Thread.html#currentThread()">currentThread</A>, <A HREF="../../java/lang/Thread.html#getPriority()">getPriority</A>, <A HREF="../../java/lang/Thread.html#interrupt()">interrupt</A>, <A HREF="../../java/lang/Thread.html#interrupted()">interrupted</A>, <A HREF="../../java/lang/Thread.html#isAlive()">isAlive</A>, <A HREF="../../java/lang/Thread.html#isDaemon()">isDaemon</A>, <A HREF="../../java/lang/Thread.html#isInterrupted()">isInterrupted</A>, <A HREF="../../java/lang/Thread.html#join()">join</A>, <A HREF="../../java/lang/Thread.html#join(long)">join</A>, <A HREF="../../java/lang/Thread.html#setDaemon(boolean)">setDaemon</A>, <A HREF="../../java/lang/Thread.html#setPriority(int)">setPriority</A>, <A HREF="../../java/lang/Thread.html#sleep(long)">sleep</A>, <A HREF="../../java/lang/Thread.html#start()">start</A>, <A HREF="../../java/lang/Thread.html#yield()">yield</A></CODE></TD>
321 <A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
322 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
323 <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
324 <TH ALIGN="left"><B>Methods inherited from class java.lang.<A HREF="../../java/lang/Object.html" title="class in java.lang">Object</A></B></TH>
326 <TR BGCOLOR="white" CLASS="TableRowColor">
327 <TD><CODE><A HREF="../../java/lang/Object.html#equals(java.lang.Object)">equals</A>, <A HREF="../../java/lang/Object.html#getClass()">getClass</A>, <A HREF="../../java/lang/Object.html#hashCode()">hashCode</A>, <A HREF="../../java/lang/Object.html#notify()">notify</A>, <A HREF="../../java/lang/Object.html#notifyAll()">notifyAll</A>, <A HREF="../../java/lang/Object.html#toString()">toString</A>, <A HREF="../../java/lang/Object.html#wait()">wait</A>, <A HREF="../../java/lang/Object.html#wait(long)">wait</A></CODE></TD>
333 <!-- ========= CONSTRUCTOR DETAIL ======== -->
335 <A NAME="constructor_detail"><!-- --></A>
336 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
337 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
338 <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
339 <B>Constructor Detail</B></FONT></TH>
343 <A NAME="Activity()"><!-- --></A><H3>
346 public <B>Activity</B>()</PRE>
350 <!-- ============ METHOD DETAIL ========== -->
352 <A NAME="method_detail"><!-- --></A>
353 <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
354 <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
355 <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
356 <B>Method Detail</B></FONT></TH>
360 <A NAME="action()"><!-- --></A><H3>
363 protected abstract void <B>action</B>()
364 throws <A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></PRE>
366 <DD>Encapsulates the actual activity we want to perform.
367 Returns when complete or when stopped.
372 <DD><CODE><A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></CODE> - when some other activity has stopped this one.</DL>
377 <A NAME="resetRunnable()"><!-- --></A><H3>
380 protected void <B>resetRunnable</B>()</PRE>
382 <DD>Reset the runnable activity. Normal activities should not
383 override this. Background activities can in order to request that
384 they become runnable.
392 <A NAME="iWantToRun()"><!-- --></A><H3>
395 protected final void <B>iWantToRun</B>()</PRE>
397 <DD>Call this if you want this activity to run.
405 <A NAME="run()"><!-- --></A><H3>
408 public final void <B>run</B>()</PRE>
410 <DD>Thread entry point. Never returns.
413 <DT><B>Specified by:</B><DD><CODE><A HREF="../../java/lang/Thread.html#run()">run</A></CODE> in class <CODE><A HREF="../../java/lang/Thread.html" title="class in java.lang">Thread</A></CODE></DL>
421 <A NAME="pause(long)"><!-- --></A><H3>
424 protected final void <B>pause</B>(long time)
425 throws <A HREF="../../java/lang/InterruptedException.html" title="class in java.lang">InterruptedException</A>,
426 <A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></PRE>
428 <DD>Wait at most 'time' milliseconds. Returning
429 without an exception means continue the activity where it left off.
434 <DD><CODE><A HREF="../../java/lang/InterruptedException.html" title="class in java.lang">InterruptedException</A></CODE> - if we were made runnable whilst
435 we were running. This might indicate, for example, that a sensor
436 was pressed while we were still reacting to an earlier press.
437 Typically, an activity would want to restart execution of the
438 action() method from the beginning if that happened.
439 <DD><CODE><A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></CODE> - if we should stop executing altogether.</DL>
444 <A NAME="pause()"><!-- --></A><H3>
447 protected final void <B>pause</B>()
448 throws <A HREF="../../java/lang/InterruptedException.html" title="class in java.lang">InterruptedException</A>,
449 <A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></PRE>
451 <DD>Wait until we've either been made runnable or someone else has.
456 <DD><CODE><A HREF="../../java/lang/InterruptedException.html" title="class in java.lang">InterruptedException</A></CODE> - if we should restart execution of the
457 action() method from the beginning.
458 <DD><CODE><A HREF="../../lejos/subsumption/StopException.html" title="class in lejos.subsumption">StopException</A></CODE> - if we should stop executing altogether.</DL>
461 <!-- ========= END OF CLASS DATA ========= -->
465 <!-- ======= START OF BOTTOM NAVBAR ====== -->
466 <A NAME="navbar_bottom"><!-- --></A>
467 <A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
468 <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
470 <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
471 <A NAME="navbar_bottom_firstrow"><!-- --></A>
472 <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
473 <TR ALIGN="center" VALIGN="top">
474 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
475 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
476 <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
477 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
478 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
479 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
480 <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
484 <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
490 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
491 PREV CLASS
492 <A HREF="../../lejos/subsumption/ActivityBase.html" title="class in lejos.subsumption"><B>NEXT CLASS</B></A></FONT></TD>
493 <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
494 <A HREF="../../index.html?lejos/subsumption/Activity.html" target="_top"><B>FRAMES</B></A>
495 <A HREF="Activity.html" target="_top"><B>NO FRAMES</B></A>
496 <SCRIPT type="text/javascript">
499 document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
504 <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
511 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
512 SUMMARY: NESTED | <A HREF="#fields_inherited_from_class_lejos.subsumption.ActivityBase">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD>
513 <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
514 DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD>
517 <A NAME="skip-navbar_bottom"></A>
518 <!-- ======== END OF BOTTOM NAVBAR ======= -->