HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& W; U- T7 ^7 l- Y
2 j0 D ?, D" H2 t" H( I public Object buildActions () {
0 X. ]+ m6 d- ^5 X super.buildActions();' c% s2 z. j+ d1 [- \' \
: S1 ^6 f4 E, z9 T, q
// Create the list of simulation actions. We put these in
7 z7 K7 V. q# |) m% |5 ? // an action group, because we want these actions to be+ Z& R- L. Q8 {% i3 c! h4 f# B- J
// executed in a specific order, but these steps should
+ k: f* t* Z& j // take no (simulated) time. The M(foo) means "The message
Z5 X& S3 v2 ` P. X" u // called <foo>". You can send a message To a particular+ y# s0 o; B0 K% q: u
// object, or ForEach object in a collection.9 N0 P) Q; |9 |* [0 ~3 ~: h
& c, a9 C- l5 [( k7 e+ u; {4 W // Note we update the heatspace in two phases: first run4 b, ^5 c) G$ b- W
// diffusion, then run "updateWorld" to actually enact the
$ N" Z+ l. b3 f$ N // changes the heatbugs have made. The ordering here is, D* G2 B$ k( y2 d$ c' i6 `
// significant!
- L: V% ~+ Z( T9 C e, w
2 R) |, K! N% f6 F+ t+ _ // Note also, that with the additional, p: E1 l) H4 X9 H0 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- g y; f4 l/ R' C4 r // randomize the order in which the bugs actually run' p) n2 Q5 c, I
// their step rule. This has the effect of removing any* G1 h0 t/ x8 [: j9 b: q q
// systematic bias in the iteration throught the heatbug
4 u# r. b C+ i. {! F, Y" u // list from timestep to timestep
% e, ^+ d L2 C; o9 q 1 T; w8 q$ z1 w; n/ Y; u1 `4 R+ E
// By default, all `createActionForEach' modelActions have3 X5 n# n* |" Q" q
// a default order of `Sequential', which means that the
1 J) B! h0 [' ~5 A; G // order of iteration through the `heatbugList' will be- `! T% `1 d/ P$ f% A8 q4 m
// identical (assuming the list order is not changed
" r$ {1 ~8 D8 ]; S+ Z7 }: J // indirectly by some other process).
6 D @7 N0 D: v+ ?, i5 b) j2 ?0 b8 ? - U. Y! O7 b8 U
modelActions = new ActionGroupImpl (getZone ());4 V% W y9 Y+ S, [9 B: a- f& D) R
. O1 B( g# k. m$ ^8 L
try {4 F; F1 S/ ~0 H! O, W: `- g+ n5 T
modelActions.createActionTo$message
. N5 I7 r) e3 u4 ? (heat, new Selector (heat.getClass (), "stepRule", false));
1 j# S7 c' `, ~6 [ F! p* [: b; C } catch (Exception e) {$ T; d- E* H/ E( j( O
System.err.println ("Exception stepRule: " + e.getMessage ());
. O9 @" @+ v9 \% o. |0 H$ {2 L7 x }
4 W% l/ {4 [0 C) c/ ?/ e* ~# v2 x# U2 [) V: B
try {- m) P, z2 R6 M# R. {+ R* H5 D
Heatbug proto = (Heatbug) heatbugList.get (0);
8 a" ~" Z& ~( S* J+ ?4 Z! c* Y, T: G* n Selector sel = 5 t7 ^0 W, m, T) Z
new Selector (proto.getClass (), "heatbugStep", false);+ j6 C4 k e' Z$ r* n
actionForEach =
9 z" V7 J/ X* w. l modelActions.createFActionForEachHomogeneous$call. J& s! m+ r2 j/ v( M6 V H, e$ P
(heatbugList,
2 t) t" D c# o new FCallImpl (this, proto, sel,
3 q# |( K6 B, L" \2 | new FArgumentsImpl (this, sel)));/ s) V, {7 l4 Q
} catch (Exception e) {
" {8 T+ L' K4 N# K& ~( r- D e.printStackTrace (System.err);
7 o0 o6 n) Y( H; s. X$ P0 V: c }
8 L6 O/ o- S& V M" E 8 t3 o4 G: ]/ b! L* X# V' x
syncUpdateOrder ();
0 p- q! ?- [1 O! k
9 b+ o* C3 K/ w& M1 V0 H try {5 k- S& Y* o- C! F4 X6 `8 m
modelActions.createActionTo$message 6 Y2 o2 U8 `8 t% z
(heat, new Selector (heat.getClass (), "updateLattice", false));
" s& E3 U' x- d9 V8 m2 q } catch (Exception e) {. ~- @' ^) s3 g. s
System.err.println("Exception updateLattice: " + e.getMessage ());! R- ~5 X* j; f7 p( x
}( O" B4 U5 H% G V+ j; R+ l
4 t/ T4 G B( E7 P$ z a1 ?" I6 R // Then we create a schedule that executes the
2 l: f& U0 t/ P5 v0 R( _; [ // modelActions. modelActions is an ActionGroup, by itself it( z& q g9 ~; O9 H$ j+ }
// has no notion of time. In order to have it executed in3 h- E) O% t. l$ x4 x4 P
// time, we create a Schedule that says to use the
8 y# U2 F9 j% x) ]5 C3 k1 P // modelActions ActionGroup at particular times. This
t H9 G# E; Z4 ~8 p // schedule has a repeat interval of 1, it will loop every
8 ]9 ^1 }' O. m // time step. The action is executed at time 0 relative to: I) k- |. {* C- p+ w" W
// the beginning of the loop.
. W9 \- j, L k4 I+ X
/ Q5 Y) V+ z3 n5 r // This is a simple schedule, with only one action that is
" m# F8 I" a, {; [! L- M // just repeated every time. See jmousetrap for more
' Y5 d7 s- U, X7 H // complicated schedules.
+ e* p( }$ L$ c
1 X& }& C: X, E! a modelSchedule = new ScheduleImpl (getZone (), 1);1 n8 P# _ x( c' ]
modelSchedule.at$createAction (0, modelActions);% _( B* P: H9 ^8 k
- G' Q+ Q% r, k return this;
$ `% S9 Z6 O* j" r } |