HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! _% c) l" Z& C: U0 k; I! F1 X% L- d
! C4 I, g' Y9 T2 ~3 f& |0 u4 ?. o
public Object buildActions () {
H2 a6 J/ t! W- w% [% C super.buildActions();3 n( G3 p ^" m+ A
{2 m; F! c& V6 J: o) I& c" N
// Create the list of simulation actions. We put these in
1 r2 Z) y7 Y9 E // an action group, because we want these actions to be
, k5 \, U1 e& d // executed in a specific order, but these steps should, c0 z: ^( k/ @# |) W
// take no (simulated) time. The M(foo) means "The message8 z) D4 {, i, v& a( H
// called <foo>". You can send a message To a particular
4 Q; g3 U; B- v // object, or ForEach object in a collection.
& z3 A' _6 [9 P$ N. m) D6 q
' q7 A c3 m# \" d7 O% o // Note we update the heatspace in two phases: first run
, i( ^) H7 t$ o& B+ ] // diffusion, then run "updateWorld" to actually enact the7 Q! b1 ~1 I q L/ [
// changes the heatbugs have made. The ordering here is @/ U: X) ]5 C( a
// significant!2 _. I( K8 } O) ~% r! U8 g
: b0 U) \/ Y* k' S( n6 v
// Note also, that with the additional
6 N) V+ N+ K! D8 R! t% | // `randomizeHeatbugUpdateOrder' Boolean flag we can
) ]* A) j, N, `& F1 C# v( l6 U5 }- i // randomize the order in which the bugs actually run
& Z! Z9 t$ r& Y5 u" R6 S // their step rule. This has the effect of removing any0 c- y9 s3 D! I+ F% L7 q9 s2 A/ |3 W/ G
// systematic bias in the iteration throught the heatbug6 L3 c. h$ K* B) d& v K
// list from timestep to timestep
1 W0 n) _$ W; A( c & r" h3 ^$ E. w
// By default, all `createActionForEach' modelActions have2 A% X+ M8 `7 l/ k& h
// a default order of `Sequential', which means that the
% [/ d4 m" T* ^( Y( j* q5 B // order of iteration through the `heatbugList' will be' R9 @) q: D) r0 I7 }9 i1 {3 ~
// identical (assuming the list order is not changed
; F. _# S+ Y! R1 p0 Z // indirectly by some other process).) u' {' [" }3 j* `
( |* p. a' V0 k, `
modelActions = new ActionGroupImpl (getZone ());; e* T( F, F: T% _% j- l
, ^/ c$ p( T. x7 T2 r V try {" U6 h$ T9 Z$ C4 Z4 z
modelActions.createActionTo$message0 m% d R# X% `6 M9 r9 ?- Z u
(heat, new Selector (heat.getClass (), "stepRule", false));
' z$ x& s, e% J$ O6 M$ A } catch (Exception e) {) {" C* m4 {0 a) Q: G
System.err.println ("Exception stepRule: " + e.getMessage ());
1 j2 s" B& _, k) w, L) H }
* A1 V* |9 Y! U0 _6 _
( V k! y/ ]) q! ~, V2 q+ o try {
' y, G. \+ k" P9 D: u! G, W Heatbug proto = (Heatbug) heatbugList.get (0);$ o6 _; ?6 _; B w- r' n ]" U3 o- V
Selector sel = 7 M. }9 d) X8 {! o7 T* t. S" T( O
new Selector (proto.getClass (), "heatbugStep", false);8 y5 t$ @! b+ }/ |' k, x [
actionForEach =: ?+ X3 e6 H! `( A3 I
modelActions.createFActionForEachHomogeneous$call
+ B. s* O: f/ x, b+ L (heatbugList,
# s( k( ~0 n! c. E' h* x new FCallImpl (this, proto, sel,8 L- T3 R5 A5 v* I
new FArgumentsImpl (this, sel)));
! A$ q6 r9 q9 l$ m$ { } catch (Exception e) {/ M8 Q0 l; ^7 ^7 o' p8 d! U
e.printStackTrace (System.err);
+ d: p& M$ }0 T8 _/ ?# c# v }7 ^" u/ e2 n' i. h% ?9 ?9 ]0 a
/ M& V6 y' o4 d7 x& b, ^
syncUpdateOrder ();
' u5 X- Y) ?2 m3 _3 J& W% V4 B7 _9 m5 y9 t
try {
9 J |4 a- i, o( u modelActions.createActionTo$message
3 u$ m& w/ A; y* Y' y/ | (heat, new Selector (heat.getClass (), "updateLattice", false));( W2 m7 A( z0 @/ R$ \* h1 c
} catch (Exception e) {" i; k! d/ j/ L |$ X
System.err.println("Exception updateLattice: " + e.getMessage ());2 M; K* X* M( Z/ L% L( d
}5 o g/ o! P+ J5 | h
% q) z6 ?6 z' z9 I
// Then we create a schedule that executes the# X& T5 l" e" p
// modelActions. modelActions is an ActionGroup, by itself it
: A6 `" F6 g0 n4 U( C/ T // has no notion of time. In order to have it executed in
9 D! h# C9 w2 U$ T" \ s/ r // time, we create a Schedule that says to use the1 z) t5 g8 e* _$ e! W4 v
// modelActions ActionGroup at particular times. This
: k7 y" w) ~& b- ?& w2 u // schedule has a repeat interval of 1, it will loop every" o1 `- e+ r+ L; e7 }, X; ^; U
// time step. The action is executed at time 0 relative to. j+ l; z. [: s# U
// the beginning of the loop." o7 Y# ~ w- u. n
) M h* Q: i! b$ g W2 Q% I
// This is a simple schedule, with only one action that is* M' {/ V1 h& \* H% O; X
// just repeated every time. See jmousetrap for more
7 U3 ^8 Q% C9 G7 e // complicated schedules.; U" B3 G; E3 _6 O$ K
3 N6 i: t8 g; D( e modelSchedule = new ScheduleImpl (getZone (), 1);4 H2 D4 e: x3 W: y* W, k
modelSchedule.at$createAction (0, modelActions);
) n5 B5 I$ ?+ h$ v. F+ f( `
% j6 \) {3 |* X% C. B3 K: j return this;
/ F/ j5 _' b9 }* W& C0 c$ d F } |