HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ T- c1 H4 h1 L- s& }% O' F5 ]. ^& ^" `8 J9 O
public Object buildActions () {
}0 e! i; x$ x5 q' h% L super.buildActions();* H6 g! _0 @' a R: u
2 o4 G) z/ x& f2 p5 C$ O5 d# \
// Create the list of simulation actions. We put these in
% k! D) d; K, m+ g // an action group, because we want these actions to be
& G3 n! m2 e& K# @ // executed in a specific order, but these steps should- ]# e7 g- Y& A) L( g0 s
// take no (simulated) time. The M(foo) means "The message
, W+ R: ~: [3 p9 @8 p# J+ G4 A2 M% z // called <foo>". You can send a message To a particular6 H( H( X9 U! y- |
// object, or ForEach object in a collection., S# ~" Z8 n- A w/ B3 A
; [! g1 E( b8 S7 A+ }9 r& O2 T
// Note we update the heatspace in two phases: first run. G1 k. U r& ]( T( x
// diffusion, then run "updateWorld" to actually enact the
& g/ m9 b. p4 ] // changes the heatbugs have made. The ordering here is
- q/ b/ N x/ j // significant!
0 I4 @0 B: L) S9 G 5 x( t- B3 C5 G& E
// Note also, that with the additional
: { y7 S% ^) [: A2 ?' Q; @ // `randomizeHeatbugUpdateOrder' Boolean flag we can, S1 z" X2 D0 r7 R
// randomize the order in which the bugs actually run
) H' S1 n. ^4 v$ H( k // their step rule. This has the effect of removing any
( ]( |8 X+ s0 K // systematic bias in the iteration throught the heatbug
# S/ e+ J, D1 c. W( z. _3 e // list from timestep to timestep
3 |' ~0 F3 F1 a# A! `% Z2 x0 }
Z9 `( k4 Q$ ?9 B4 h& ]' E // By default, all `createActionForEach' modelActions have8 M' w1 U9 R& G/ P. ~8 E) v
// a default order of `Sequential', which means that the: s4 q( Q; q, E$ s1 X
// order of iteration through the `heatbugList' will be. Y- e+ r' K& X! S- x
// identical (assuming the list order is not changed+ q9 V1 M- t# Q& O0 S
// indirectly by some other process).+ a8 A" N/ l3 m( T( X6 E% Z9 T- W
" w }% }9 v4 x. r' g
modelActions = new ActionGroupImpl (getZone ());- o9 ~% f: a! H- L& p, g0 E1 Q
8 s4 a4 }4 b1 K; ^5 i8 V2 L' P
try {
# R! L& `5 K% e modelActions.createActionTo$message7 s, r: k5 R+ r; o1 ?
(heat, new Selector (heat.getClass (), "stepRule", false));
/ ^) L0 r5 N. ?5 X6 |9 | } catch (Exception e) {
# W1 j# K' g0 w% w# u6 } System.err.println ("Exception stepRule: " + e.getMessage ());- {% J/ H% v7 B+ |+ o3 N
}( B6 j; R1 q: @/ |3 \( q
' h: ?0 \" w& g7 I2 V5 K try {
$ \( q* ^ [' @; X9 L Heatbug proto = (Heatbug) heatbugList.get (0);& i2 Z4 r6 V8 ?& H2 W! F8 c. ~
Selector sel = ) Q* R; ~5 m4 H+ P( x+ z
new Selector (proto.getClass (), "heatbugStep", false);" o" O* x5 m! ?! F
actionForEach =4 C' D. f( ~7 H' `! m
modelActions.createFActionForEachHomogeneous$call
) Q: v! ?4 b8 h* W. s (heatbugList,3 v) B# G( L* G, R, { r
new FCallImpl (this, proto, sel, h, Z8 o. x1 V; D' @" `
new FArgumentsImpl (this, sel)));+ m0 i3 m5 k! h4 d
} catch (Exception e) {
; E9 ]6 \( I* \/ b7 y( ` e.printStackTrace (System.err);$ L+ v/ O3 S/ q, G
}
6 v( v8 N& ~: ]
6 i& n( Q4 `7 h# _/ E9 f syncUpdateOrder (); m! |, E3 L; T. M1 k
9 |: q" s3 |$ U4 H* K; H# H, U( y- x try {
( y0 |" o% G) c+ n l' ~ modelActions.createActionTo$message
0 e3 c5 t% m& g% C8 q( N! z, | (heat, new Selector (heat.getClass (), "updateLattice", false));9 _6 Q0 @: k5 z. I/ F9 [
} catch (Exception e) {
( F+ |2 t5 H& ?8 r! V% X8 z System.err.println("Exception updateLattice: " + e.getMessage ());
0 c, }0 o7 `7 t7 | }0 Z! f2 f4 @% t. V% m5 ~/ K
; ?" D8 V' ], j% b' S! t7 V/ k // Then we create a schedule that executes the5 @ N- Q! [9 f$ _. ^
// modelActions. modelActions is an ActionGroup, by itself it
8 s# @: N- I( B5 N0 c/ b. E5 x // has no notion of time. In order to have it executed in
' D/ c$ |! o4 `3 ]+ H // time, we create a Schedule that says to use the( n& S6 R0 U* }
// modelActions ActionGroup at particular times. This
9 w c+ i" O* c {" T // schedule has a repeat interval of 1, it will loop every
. w' g" ]0 e) i: U" a# [8 Q6 W // time step. The action is executed at time 0 relative to5 h, }6 z/ C, Y+ O
// the beginning of the loop.
% b9 Z, [6 C" j2 ^1 `/ K! S4 E7 e; l4 q
// This is a simple schedule, with only one action that is7 f+ c8 @$ f$ y" H9 i4 @' Z
// just repeated every time. See jmousetrap for more+ F9 P+ t% N" Q$ ] r7 U4 P1 k
// complicated schedules.( |- E6 s- J, D* x; W/ }
, D9 G1 Q" f! C
modelSchedule = new ScheduleImpl (getZone (), 1);' l5 i& \5 h$ |
modelSchedule.at$createAction (0, modelActions);
3 ^ y* T# e, M. x$ a
9 r; D8 ~1 X/ H N# V+ o* F return this;
' e: P& q3 X: ~& N5 j } |