HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! Y$ W8 Z# H6 D% y2 y. ~
1 y+ R% \6 m: P/ Q- J1 k( T public Object buildActions () {
/ U2 ^. x# }) c, \% r super.buildActions();
6 n6 d. E9 ^9 J/ I+ S3 Q
! l3 h9 V* u2 ~6 C // Create the list of simulation actions. We put these in
' Q0 L; ~% w" b. K5 n // an action group, because we want these actions to be2 q. o. F' Z: Y' }' ^0 i P# d( m w
// executed in a specific order, but these steps should! q6 A7 D( f( z: |" f1 c4 `
// take no (simulated) time. The M(foo) means "The message
. s4 {6 z5 c. x. ^1 {' ^- \+ Q // called <foo>". You can send a message To a particular
2 t% W* J1 {+ Y // object, or ForEach object in a collection.- I, I2 T; `' h S8 n6 D
! o, S8 U% I$ M7 T! |/ D0 o; l
// Note we update the heatspace in two phases: first run: S {8 k. O) \% [4 O6 H1 k
// diffusion, then run "updateWorld" to actually enact the
/ H {8 g9 T% m5 m# B( o' h // changes the heatbugs have made. The ordering here is3 Y+ |- L7 m1 P/ p# h9 B
// significant!* R8 g( i0 k% j
1 q4 C; z5 I1 ]
// Note also, that with the additional3 h% Z4 |/ d4 H9 | c! b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, x6 _# M* O* U$ d // randomize the order in which the bugs actually run' B: o3 q3 @& a2 b
// their step rule. This has the effect of removing any
( O" `" \+ R/ R) d+ w, k // systematic bias in the iteration throught the heatbug" h- c n3 w# q# I6 B' S1 ?
// list from timestep to timestep! G/ l7 T$ A$ D5 F7 C" B+ `
8 G8 m' x) c" X
// By default, all `createActionForEach' modelActions have
2 v3 x; _% z0 p+ M+ ]; l; l // a default order of `Sequential', which means that the
) x: e/ W5 f2 ~% ~ ~ // order of iteration through the `heatbugList' will be
' {& ]5 d2 M3 P; t+ k // identical (assuming the list order is not changed* f! G( I6 b1 |5 s
// indirectly by some other process).# B5 ?: n; x ~2 I4 ?" a9 g) i
% q; U$ \1 j9 o* V4 r$ y modelActions = new ActionGroupImpl (getZone ());5 M2 U2 ^ ^- k" P
8 a5 m& C9 b8 X% K% W" u. p7 m8 O3 q try {
% n& W# R' t- a8 h* B8 k2 p% ?# f modelActions.createActionTo$message: V a, \; W0 x& q, c# D9 K/ s7 k
(heat, new Selector (heat.getClass (), "stepRule", false));7 \& @, B% @# D
} catch (Exception e) {5 ]! Y# K$ T+ [: o+ E' P4 V
System.err.println ("Exception stepRule: " + e.getMessage ());
. k6 K7 O% M2 E5 n }
5 G7 |9 U6 T: e, g0 R4 \* O% u5 o
3 H0 N# R+ Z, h9 L try {& c0 Y8 G* F) a5 ~( u3 A
Heatbug proto = (Heatbug) heatbugList.get (0);$ ]3 {. c& I1 Y+ B: Z3 g' ^
Selector sel = w/ T9 W# K! J
new Selector (proto.getClass (), "heatbugStep", false);1 }2 ^$ W& i7 D7 T: Q4 ^% R
actionForEach =
7 B6 c% A5 Q' J; B modelActions.createFActionForEachHomogeneous$call* [' G3 Y! @# l7 g
(heatbugList,2 }# B3 F5 B8 q5 b3 z0 D' x
new FCallImpl (this, proto, sel,
: o0 l/ V8 p1 L! _6 \ new FArgumentsImpl (this, sel)));
% @. o6 V ~) j3 G1 p* z, j } catch (Exception e) {
& M: a0 u, }. w3 B e.printStackTrace (System.err);+ w0 t! G6 n' @
}. r" @' b5 n5 y
" H+ n( b: ]$ n syncUpdateOrder ();0 V5 Y/ j- f/ M$ t9 A2 G
. E7 p0 r6 ]- Y+ E1 ` try {! @& i1 f9 J2 Q/ O
modelActions.createActionTo$message 3 X+ z _+ Y# q" [$ _' ~3 M6 ]6 j
(heat, new Selector (heat.getClass (), "updateLattice", false));# c$ O7 P6 r7 p% Z
} catch (Exception e) {2 w2 i# x4 i1 D5 y5 w# I. K2 H: h' y
System.err.println("Exception updateLattice: " + e.getMessage ());
9 \5 T0 [( A1 w) t }
8 ^; m" P4 {( i T ) J* V; v1 K1 m
// Then we create a schedule that executes the& J' A3 A U/ M% p# W. E: F8 j
// modelActions. modelActions is an ActionGroup, by itself it/ m; W; j0 S4 V! g3 L) x; }0 t- C
// has no notion of time. In order to have it executed in" V) I& q! k2 F2 `% G
// time, we create a Schedule that says to use the
9 {* }0 V7 ?/ O' @ // modelActions ActionGroup at particular times. This0 f* ?- L' ?1 @6 D: E* v) `
// schedule has a repeat interval of 1, it will loop every
& ?2 G5 Y+ }0 Y, z# Z1 V // time step. The action is executed at time 0 relative to2 A0 e7 M! X# Z* x0 z2 ~( P0 W! v
// the beginning of the loop. _ B+ ?9 y4 e0 w p
* K& ~- ^; s4 f: B // This is a simple schedule, with only one action that is
8 @8 o( @- ^6 C/ `( U6 R // just repeated every time. See jmousetrap for more
! w' J& v+ G6 |4 d- ] // complicated schedules.
- o: R( L: x) x" Q: M. g- R
/ I' [. ^; ]% }2 g: I modelSchedule = new ScheduleImpl (getZone (), 1);
. p+ v9 `0 \, q. J( { modelSchedule.at$createAction (0, modelActions);
3 W0 Z, Z& y+ ]/ O3 R# _2 @
' X6 S2 n/ G2 _( k$ Q: p return this;+ W$ q, k7 P7 v
} |