HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% d5 B- A+ x! @
9 @/ r$ N- ]3 P
public Object buildActions () {2 ^9 |+ Y5 o6 P4 B
super.buildActions();
# Y' [, U) e# m+ I5 @) `$ b5 a
* F& W" \1 y" s8 A( Q // Create the list of simulation actions. We put these in
( ^, Z+ t8 h) G! g // an action group, because we want these actions to be7 }, q" B3 m6 ^9 b2 s; q
// executed in a specific order, but these steps should
$ _4 ?# m: R! v0 P5 i( l // take no (simulated) time. The M(foo) means "The message
3 w7 Y3 i Z! L // called <foo>". You can send a message To a particular0 G- `2 c- I* R2 S
// object, or ForEach object in a collection.
' ?" p( ?% K: a% R2 E; l
0 n* N9 L8 q; n- n0 T9 L // Note we update the heatspace in two phases: first run7 q3 E5 W; W. E0 l# r, N O
// diffusion, then run "updateWorld" to actually enact the
4 x$ s( y, N/ Y // changes the heatbugs have made. The ordering here is( B1 a2 t3 W7 Z+ J- u
// significant!
7 E. j' q v& f7 K $ K' Z1 |4 K. Y
// Note also, that with the additional
6 [- ^# V4 @: H: _1 F! @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
. s# N7 G& B; V4 D // randomize the order in which the bugs actually run6 x7 X3 [5 N( y$ i( ?
// their step rule. This has the effect of removing any
& U1 T( @7 J0 b/ w# ]) I4 D: U // systematic bias in the iteration throught the heatbug: F& l' [- L6 Z' u W1 E
// list from timestep to timestep7 f- P+ z0 x) K- N# [) h5 z
x6 d, x; r- m1 H* p // By default, all `createActionForEach' modelActions have
( _& G4 A" w! \7 E% X8 ?6 } // a default order of `Sequential', which means that the
% `; s5 c; E% _ F+ a# W9 F // order of iteration through the `heatbugList' will be
- A6 g: k) \% {7 s, h; i // identical (assuming the list order is not changed' t: c4 [- F6 s4 m2 m8 J$ S; c0 P
// indirectly by some other process).7 G$ Y+ U6 B; S
/ g7 E" ?+ B9 _4 J modelActions = new ActionGroupImpl (getZone ());
. u# b) b9 a/ w+ z0 O8 a1 G
9 E! J2 t4 n% \' B H" z try {
3 X8 q/ O1 V, y" f& d) f modelActions.createActionTo$message3 _; |0 y, L8 T( l- H L, G+ o
(heat, new Selector (heat.getClass (), "stepRule", false));
2 I. \" A6 [% ]- b/ j2 c } catch (Exception e) {0 d* f; i0 O2 u4 I
System.err.println ("Exception stepRule: " + e.getMessage ());
8 k( R2 T9 A6 N& E }
' B3 o$ D2 G1 F6 e8 m! c S$ W8 r$ ~( k% N5 r& ]1 p( P
try {+ T8 ?3 C& M+ A2 E! [
Heatbug proto = (Heatbug) heatbugList.get (0);- N; t+ `! O4 s* A# p) B, I
Selector sel = ' k |) v7 N& t- ]* `# T5 N6 v
new Selector (proto.getClass (), "heatbugStep", false);
. h( U) K! S! x, N0 K( {2 j actionForEach =% F1 U7 {% P$ L' A( {2 q ~$ U
modelActions.createFActionForEachHomogeneous$call
" E5 `$ m* a, X7 w7 R (heatbugList,
7 C4 \6 B$ ^( _. m+ i# K new FCallImpl (this, proto, sel,
: ~& E2 g+ n, \% L5 G5 k new FArgumentsImpl (this, sel)));. _2 \" u0 F5 A* x. {
} catch (Exception e) {
! |, q& i1 @; G% y: g8 G* a- _ e.printStackTrace (System.err);
' T+ z- n6 S" f/ p/ y: M0 m# g' _ }, P/ P5 V$ e- Z8 L
* b# ], P: ?1 }; \, z8 | syncUpdateOrder ();, ?" b }, P; m5 s( m' Z L; _
# S# V; Z# Z2 Y- A9 l) m( {2 l, l, H
try {
9 v3 V0 X9 }! e% g& y2 S9 M modelActions.createActionTo$message
3 w! L4 o8 O: |0 k, k (heat, new Selector (heat.getClass (), "updateLattice", false));0 ^+ q9 V$ l# Y0 D% L( s
} catch (Exception e) {3 J0 Z/ B- x! P/ q$ y5 _
System.err.println("Exception updateLattice: " + e.getMessage ());
) r$ b! u8 K5 H/ D" `" a9 f ~* o }
9 P6 i5 C7 r5 Y$ s, e
* m2 p% Z) ~& n9 I; r( D/ ?" H // Then we create a schedule that executes the9 h+ U N# G% p
// modelActions. modelActions is an ActionGroup, by itself it7 F' O! H F' l" A- S G4 o
// has no notion of time. In order to have it executed in% S) n7 o0 |5 \" t: F3 {, q( r! M
// time, we create a Schedule that says to use the
6 W P5 W) R# f8 O) A // modelActions ActionGroup at particular times. This
9 @. O8 r5 f" V) W+ O" ` // schedule has a repeat interval of 1, it will loop every. {* D% T8 q( N$ U/ f! v
// time step. The action is executed at time 0 relative to1 u6 y- [& t4 g7 o4 i, K
// the beginning of the loop.
' A/ b P6 k, y8 M; {
/ G- X, z) ]) P; g // This is a simple schedule, with only one action that is
4 K9 W4 P( w' N- R# ^6 Y& F // just repeated every time. See jmousetrap for more
) e# |4 H& }' ~3 g* f% ~ // complicated schedules.; H4 j/ o5 e$ w. a6 x
( Y" Q) X1 z) ~* B modelSchedule = new ScheduleImpl (getZone (), 1);* s% n$ P6 R. ~! V. w4 {6 D
modelSchedule.at$createAction (0, modelActions);' J: F0 R P6 F. `4 h
0 H4 _% K. ~ ]5 U3 Z return this;
( D& {( v/ q! n" n" C! A7 t } |