HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- b `/ t1 F% M& a9 F
+ ~2 m& j' V9 t2 B& I; h: i public Object buildActions () {2 ~ }9 X- t4 N: x! N! h: x* ]8 ~
super.buildActions();
5 \% [3 O( `+ L$ V( A 9 m( P' h$ p- ]; B
// Create the list of simulation actions. We put these in$ x, C. X9 D2 Z
// an action group, because we want these actions to be2 G* H! g. {+ a' f6 \6 w ]: P5 k
// executed in a specific order, but these steps should
. s- [. l! v# C+ r5 d; z // take no (simulated) time. The M(foo) means "The message1 R5 C2 r5 d3 q* p! W
// called <foo>". You can send a message To a particular5 E+ k. ^. j* n# o
// object, or ForEach object in a collection.
9 k- m4 {) K- c2 [; ]
& E* o2 W9 r5 f1 j4 z" F // Note we update the heatspace in two phases: first run& Z4 g8 r/ A1 ?+ `5 Z
// diffusion, then run "updateWorld" to actually enact the8 f: f8 j! O/ @; s2 Y: q
// changes the heatbugs have made. The ordering here is
s* `" q2 M, \" b2 s* f# `+ m' V // significant!/ |' @% O6 ?* D# Q7 b
+ g+ Y! @( c- ]- q& L9 i
// Note also, that with the additional
$ D$ I$ B* A& F) O // `randomizeHeatbugUpdateOrder' Boolean flag we can# x1 M% ~" D+ F0 i' _/ L
// randomize the order in which the bugs actually run
: j7 Z1 R3 I7 [! U" @, o // their step rule. This has the effect of removing any' Y6 w. u: ]' l4 g8 M# F
// systematic bias in the iteration throught the heatbug
7 N/ D; G# q( H0 s! T // list from timestep to timestep9 g* y X6 c# P0 ^- n# \
" n' _ Y# z4 k, ` // By default, all `createActionForEach' modelActions have4 z2 J4 Q. c5 a, G2 O
// a default order of `Sequential', which means that the4 |- j& E% r3 M3 m f
// order of iteration through the `heatbugList' will be
: N2 Z2 q9 k. F6 p1 x // identical (assuming the list order is not changed7 N$ j- o7 e9 i8 l3 M7 E
// indirectly by some other process).
' J$ \ d" R1 Z3 v2 h* C" g
. l6 i' J$ m! G( y) E modelActions = new ActionGroupImpl (getZone ()); u' D; `9 h0 E( x
8 i; m% a. ^0 c* n" y
try {1 P# m1 V* X! H9 _' ]( ~
modelActions.createActionTo$message
' _9 j3 z l; M. k& {: y& h( e$ u ` (heat, new Selector (heat.getClass (), "stepRule", false));
) U, c/ u7 W9 k: ~' M- {2 M } catch (Exception e) {
* z* ?0 l/ x: b' B# w) d+ d System.err.println ("Exception stepRule: " + e.getMessage ());
% j7 F4 F* e" e# K, | g4 ~: o1 a }! C+ H+ v) |' K6 B4 S
6 e' ^3 h6 v. o1 a% C
try {2 b9 R. T0 E, I! i, f% [
Heatbug proto = (Heatbug) heatbugList.get (0);
; r' k* A* u( P# z+ M+ T Selector sel =
% X$ N. G9 `9 j3 s: N! n% T new Selector (proto.getClass (), "heatbugStep", false);
' n6 L9 w- o, Z actionForEach =( U. Y. D" N/ N: b3 C" U1 w/ G2 d# f
modelActions.createFActionForEachHomogeneous$call. i: Q8 b F$ n
(heatbugList,
: c; i [2 g' Z+ Y new FCallImpl (this, proto, sel,, G+ \1 {5 E. I1 v- J6 |6 }; A
new FArgumentsImpl (this, sel)));
C* P5 I6 \' W" p: y- ~' h k( T } catch (Exception e) {5 H+ S. D2 \" h) k: @+ Y: K6 C
e.printStackTrace (System.err);
) {, m8 K% V' \! Q2 @. i' ?+ e }# j Y/ P: v# |3 n$ R
+ Z1 f4 D: [8 ] V5 | syncUpdateOrder ();8 h- d4 Q" [0 P0 X! q0 H
) j; I, w0 @9 H9 i \9 Y6 a try {, A( h" g# h0 C8 X8 z1 \6 W5 w
modelActions.createActionTo$message $ }+ h5 r8 S" v1 C
(heat, new Selector (heat.getClass (), "updateLattice", false));; @. H9 U' N* f- k
} catch (Exception e) {5 p8 J# r+ Z0 F, j6 c/ @/ O) S
System.err.println("Exception updateLattice: " + e.getMessage ());
2 p3 f m4 |5 P1 R/ [7 `; M6 h! T }
x; q, A7 z' I% f( b' ~
; Y( c* @% g- I) V // Then we create a schedule that executes the! i- q. \# p) F8 ~. w/ `
// modelActions. modelActions is an ActionGroup, by itself it6 d1 B1 Y9 c/ A) F
// has no notion of time. In order to have it executed in
9 ?4 I- d4 X- g // time, we create a Schedule that says to use the
8 T9 k& Z/ Z( W; L$ g // modelActions ActionGroup at particular times. This
3 x7 @) c- M1 y4 {$ j) e: G6 ` // schedule has a repeat interval of 1, it will loop every. t3 U3 v9 [2 n ^; r- j
// time step. The action is executed at time 0 relative to
6 V1 a2 g; g) y0 F$ ]# m* \7 t // the beginning of the loop./ L9 L0 r2 _+ Z; x
/ `: M+ m! N7 B
// This is a simple schedule, with only one action that is
/ C2 |# j- T( x/ h# `6 @% g // just repeated every time. See jmousetrap for more
0 \/ t) X s( @7 c // complicated schedules.! u$ y7 r6 l$ I; H
4 m# k7 T$ A* S+ b0 D' w
modelSchedule = new ScheduleImpl (getZone (), 1);
) J3 ]) n! Y# ~1 t" | modelSchedule.at$createAction (0, modelActions);9 y" ?% b: x# O8 h; A; n- c X! A
?2 V2 ^* S% u8 D4 A( o! ?: {! n4 U
return this;4 g7 `' U! {$ Q- S
} |