HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 a0 I: k5 s* J1 d. R. l; [( v4 ^
# _' ~8 ^7 m9 f- U public Object buildActions () {
! |' ^) R* L9 K6 A- A" d0 | super.buildActions();2 i4 Q, U' o5 a# f" r' @8 \# ~
" J: b' Z/ w% j; ^; Z
// Create the list of simulation actions. We put these in
& Q3 Z$ s+ l+ c6 h0 E // an action group, because we want these actions to be/ `1 d D# X# B6 `& i! N/ j
// executed in a specific order, but these steps should5 `* I+ q! A. v# I: A
// take no (simulated) time. The M(foo) means "The message7 ~1 [! d6 g% m# \5 w
// called <foo>". You can send a message To a particular
$ ^+ ^. x/ v# _ // object, or ForEach object in a collection.' f1 B; N& X% F, d6 P& f# ^8 _9 k @+ e8 M
, ]$ u. N4 n* w
// Note we update the heatspace in two phases: first run# F+ p! K, C6 I6 d2 D* y
// diffusion, then run "updateWorld" to actually enact the6 }3 ~% t8 Z: L
// changes the heatbugs have made. The ordering here is# s- d3 H" U( E) U8 X
// significant!& n; i& ]: o2 ~" P2 Z% W1 @
& l+ B# r+ v6 C9 s2 J7 {
// Note also, that with the additional; S3 \- h8 q% D1 e5 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 f5 V! c: l& Q% `3 ?6 V // randomize the order in which the bugs actually run& q2 x# f8 V0 f$ ?6 F
// their step rule. This has the effect of removing any5 @9 p, \2 l% z, d
// systematic bias in the iteration throught the heatbug0 R+ u/ ~$ L9 }; }0 h6 i& J+ U" R
// list from timestep to timestep
2 G$ b8 Y: `) L+ l3 P* N* _
- f9 L# r7 ~' Z ^& A" ^ // By default, all `createActionForEach' modelActions have
( v' k: ^5 i$ f2 m // a default order of `Sequential', which means that the$ ~" Q" Z+ r$ q; [9 ^& i8 M
// order of iteration through the `heatbugList' will be3 Y. h5 T) G/ H/ t/ a: ]$ H
// identical (assuming the list order is not changed, w/ V' c1 `' Y3 R/ _' D! u" o
// indirectly by some other process).* N" S) s$ M+ h0 b) T1 D' k% X; @
/ C t1 ~. v; h7 t6 y" G8 } modelActions = new ActionGroupImpl (getZone ());. [* [1 L7 U$ p% U; J
8 m* K) N, v2 z$ w# d. [4 { try {! ]( O1 s! `: q; c. R
modelActions.createActionTo$message( K1 J# b. ?9 s
(heat, new Selector (heat.getClass (), "stepRule", false));9 j: O9 r# J/ o8 n& x
} catch (Exception e) {5 X, h% F9 }* t+ R) U S, y
System.err.println ("Exception stepRule: " + e.getMessage ());
: h" M" R' j: m. @8 i1 O' \9 E6 \ }
7 Y. a6 ^# c+ ?4 D1 C9 ^& I$ B& E; z, L( D5 E
try {) U/ V8 ?7 l% d1 |1 b
Heatbug proto = (Heatbug) heatbugList.get (0);
4 @) E7 L$ ], y" s& i% J Selector sel = - ~/ m# }/ d0 x: ^1 v
new Selector (proto.getClass (), "heatbugStep", false);! S1 r0 }$ z: {% B) X
actionForEach =
# k7 T/ L8 B | ^* O7 U/ Q3 I modelActions.createFActionForEachHomogeneous$call
q7 ~, L- B9 c% [. ~" B (heatbugList,
8 k) i. {8 x f, @$ | new FCallImpl (this, proto, sel,
; O, j# p) e" D) O0 w3 t2 k new FArgumentsImpl (this, sel)));$ h3 ?$ ]. U$ k9 G; U
} catch (Exception e) {
" [6 A. N# T4 g4 o' c0 M e.printStackTrace (System.err);
# U: g* A5 r0 o8 e }: _: C( c& s9 R- V2 q' Y0 [# i0 P9 D
+ P7 J- k3 f$ M; {# I& m syncUpdateOrder ();
1 T: w! b9 D l3 h2 P
+ F3 w; x/ P0 [6 [# q# V try {
s% C' V# \6 _5 |& l+ |' ?- S& x modelActions.createActionTo$message ; z- p' g. y4 U0 o c; `% E
(heat, new Selector (heat.getClass (), "updateLattice", false));
& M: ~# C9 U! T; X& A } catch (Exception e) { u0 R& H: @$ a; X" o$ e
System.err.println("Exception updateLattice: " + e.getMessage ());" O" g. @ n% t1 i" S
}
3 v* U) f; y0 @/ F' b! ^/ r; v / S0 f6 `' K. Z2 \- k. _" P
// Then we create a schedule that executes the A" L5 V+ z3 ~/ U/ E
// modelActions. modelActions is an ActionGroup, by itself it
9 ]" |% L! t+ P) @5 _ // has no notion of time. In order to have it executed in0 ]( P( T" s P8 U; ?/ h0 R
// time, we create a Schedule that says to use the" I k: u( i1 X2 b
// modelActions ActionGroup at particular times. This) {8 l4 z7 g$ Z1 @+ Z: j S
// schedule has a repeat interval of 1, it will loop every
0 X. z `' _ A% E# p // time step. The action is executed at time 0 relative to1 l/ V, X7 ^ H& y
// the beginning of the loop.2 s. y1 z- Z3 z8 p5 N: x2 p
) Z" k- [+ C7 S
// This is a simple schedule, with only one action that is
) F# f; P$ F' |* D Z // just repeated every time. See jmousetrap for more
( `$ a$ J! G/ W4 e/ s( R // complicated schedules.# L/ G0 v+ w& x# M* H/ Z! G! O
% t' e$ b1 [* K' O
modelSchedule = new ScheduleImpl (getZone (), 1); }* Z2 G% `' _! u" D
modelSchedule.at$createAction (0, modelActions);
; [+ Y: O1 g0 l0 Q8 F% ^7 A
3 C/ C# E+ q# b$ V# m1 ~ @ return this;
1 c+ v& d: I/ t8 `5 b } |