HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# \6 l9 n0 M# @
( s9 Y. W' `( h+ z$ d5 t( ` public Object buildActions () {, Y* N0 f8 d X- Y- |. r6 b9 a
super.buildActions();
$ s+ k+ c0 z, B# y , ?, R7 s: k. E/ ^: X% W0 b* i: a- f
// Create the list of simulation actions. We put these in! K7 ^5 [: p+ I
// an action group, because we want these actions to be
( P, f/ F/ X8 X9 D. Q // executed in a specific order, but these steps should7 g5 T C- V0 Y0 q+ \& ^) M( u! t
// take no (simulated) time. The M(foo) means "The message
; u9 a; y# c; a // called <foo>". You can send a message To a particular
, C# A% z. l B! U' Y" N // object, or ForEach object in a collection.
) `& w# N- ^! t" b
) z8 m' e; z8 y3 @ // Note we update the heatspace in two phases: first run0 ~! I r) Q2 q* W9 ?2 m
// diffusion, then run "updateWorld" to actually enact the
; p- t% G9 w! ~" `. I // changes the heatbugs have made. The ordering here is1 b5 c" x& | b. }% @
// significant!
: C1 | _/ h$ P7 ]/ t ; E5 }6 ^& ^* {, l
// Note also, that with the additional( X6 t1 s0 t! i7 T E4 ^; y
// `randomizeHeatbugUpdateOrder' Boolean flag we can; j! ]) H: |' A- \
// randomize the order in which the bugs actually run _% x6 S7 h2 h' C9 |
// their step rule. This has the effect of removing any
+ b4 c0 W; C2 [$ T // systematic bias in the iteration throught the heatbug2 O0 c; C: o1 N1 R
// list from timestep to timestep5 `/ e8 N3 [( Y4 F* E% A
( H4 u1 L b. Y( l5 m6 q4 ^7 }. x4 u // By default, all `createActionForEach' modelActions have
6 s4 R" q! z7 I; w8 f // a default order of `Sequential', which means that the, W+ o; p! c. J! j: B+ H
// order of iteration through the `heatbugList' will be" f& A, z# ?( N4 W
// identical (assuming the list order is not changed6 i/ z& F& W- K& X0 a2 m; [
// indirectly by some other process).9 B, @5 E1 D, e7 S8 O: b
- P% n Z7 T- R- H z4 j modelActions = new ActionGroupImpl (getZone ());/ |1 ]5 h$ Y" l" V3 @
6 @, Z G% P6 H2 j1 \2 S
try {
' e- D- u4 }# @ modelActions.createActionTo$message
) c3 y/ X/ Y V; v (heat, new Selector (heat.getClass (), "stepRule", false));$ J' @" |1 v2 V" a- B
} catch (Exception e) {
b2 A4 M n) O7 Y System.err.println ("Exception stepRule: " + e.getMessage ());
" d+ S% ?; j' g }
8 F; O( h) ^1 ?6 N# ], C3 A- A3 P8 L& t/ G3 [8 J ?; f
try {
8 d. Y: B5 z$ R Heatbug proto = (Heatbug) heatbugList.get (0);
y/ o3 x6 c! G2 p6 w3 R1 }( h2 ^ Selector sel =
5 h0 }/ b6 ~8 d# } new Selector (proto.getClass (), "heatbugStep", false);
* z; U* L+ Q7 x' m, j" b2 x" I actionForEach =
* q5 A1 L8 O0 W0 @" u1 \* s! H1 ~ modelActions.createFActionForEachHomogeneous$call$ M4 A0 E' m5 I( \4 r% R @
(heatbugList,
+ O, Y1 Y: F7 z$ k( z1 E; f Q new FCallImpl (this, proto, sel,
6 E0 ^0 }- C2 l V% P new FArgumentsImpl (this, sel)));
7 d/ i/ [+ A& v7 j/ F' K% n } catch (Exception e) {
: ?, H( O, T* D* n/ e# L: [ e.printStackTrace (System.err);9 v1 f9 Q" o+ F; F; ^9 s9 L
}
* `) |$ l3 F5 G9 _
' t3 x+ ^0 B, z1 i! _( l7 `, n" Q+ _ syncUpdateOrder ();' Q8 {& ^$ X" Q' o& V% ^6 q( ~
( @4 V: N( u9 e7 m( _& F, } try {
, K4 F+ d; ?( _, d* P+ L. h1 `; l modelActions.createActionTo$message " O' {7 f' I$ ^: X- }1 p s; P7 X5 r
(heat, new Selector (heat.getClass (), "updateLattice", false));
" [3 K" N6 h+ ^3 S- |, U } catch (Exception e) {
$ b8 c' A* O5 X8 V+ t" b( t: ~ System.err.println("Exception updateLattice: " + e.getMessage ());
) u/ p% c; i) |. M; W* g }) k% \ X$ k4 u0 w# ~
2 ]7 r6 O, y3 u9 `! Q
// Then we create a schedule that executes the
3 K+ p( v, K2 p; L; M5 [- X, n // modelActions. modelActions is an ActionGroup, by itself it
6 W+ y/ J- c5 c0 r4 e // has no notion of time. In order to have it executed in
9 ^* Q7 P7 q% Z& w( _ // time, we create a Schedule that says to use the
, p. I7 W2 W# j+ O& ^$ x3 v // modelActions ActionGroup at particular times. This
, ^) U' \5 l3 I0 D, P // schedule has a repeat interval of 1, it will loop every
0 R# k. ~/ q7 b9 }/ ?* K9 ? // time step. The action is executed at time 0 relative to
+ X8 v4 r3 b& m2 [5 c, v // the beginning of the loop.
+ A- O. O: h7 g! F
* m* E6 g" L4 v5 R \ // This is a simple schedule, with only one action that is' j& r, T5 u: z' ^2 }8 t6 @
// just repeated every time. See jmousetrap for more- h+ @, v& {* U( ~3 g
// complicated schedules.
: ]* Z" P! a, K 5 l) ], Q2 F: @8 a& k/ N
modelSchedule = new ScheduleImpl (getZone (), 1);/ [" g2 I3 ?' K+ I( E6 q/ \% I+ Z
modelSchedule.at$createAction (0, modelActions);% `5 A% ]9 z" P' G/ |$ X' p
# {! G5 J# K) e4 c' D2 i return this;, H5 l" F5 f/ _% N4 a
} |