HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ R' `' D$ B) d+ J' _
% k9 t! |9 _% C1 M% Q: T4 u public Object buildActions () {9 m: Y) G9 u; Z4 i/ P9 t9 x
super.buildActions();- Q6 o' H* W' N- b9 W
r% f) w! O2 h0 | // Create the list of simulation actions. We put these in/ f/ D) W5 F3 Z" e4 w6 ]/ _' s7 }. ]
// an action group, because we want these actions to be( b5 E& W5 _3 B' t% @! z
// executed in a specific order, but these steps should
) d7 h$ p8 ^! z, R // take no (simulated) time. The M(foo) means "The message8 ?4 S& U) e( I9 b
// called <foo>". You can send a message To a particular! d, d( `3 q$ j" @! {6 R; r2 `
// object, or ForEach object in a collection.
6 S+ X' p2 y" r- k 3 b$ G8 C7 @1 S F: {; c
// Note we update the heatspace in two phases: first run" Q! w4 g B1 o0 t X
// diffusion, then run "updateWorld" to actually enact the
5 G4 L- x" f' ^$ Y2 K6 n7 Y // changes the heatbugs have made. The ordering here is9 p+ d8 X* k% i" j m
// significant!
, @) \8 H* L% Q( y. U, k" h 6 z1 ~# K1 D Q1 G% w
// Note also, that with the additional. @* V+ @# A& N9 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 B7 [& L1 T3 O4 w
// randomize the order in which the bugs actually run4 J% A+ y0 e+ R% q
// their step rule. This has the effect of removing any2 V' \; Y+ J5 E3 d7 k$ K- y
// systematic bias in the iteration throught the heatbug
$ j( X! _ D& D3 ~ // list from timestep to timestep
! n. F! r# u( [; R" n2 G
# S; O+ B0 i2 O( e& a // By default, all `createActionForEach' modelActions have1 k( I0 S8 j, B6 a7 W
// a default order of `Sequential', which means that the
# T9 y' G( G H // order of iteration through the `heatbugList' will be
* r! C# V! V9 _+ \; J // identical (assuming the list order is not changed
; }5 S4 \. Z) M6 K4 J: P // indirectly by some other process).) P" K0 P2 L6 q6 I3 s0 Q
0 R" [) Y1 O& g# u% n8 C
modelActions = new ActionGroupImpl (getZone ());
( {) L% q; t, I/ A7 B3 j6 b" s3 x4 z6 |
try {
; [2 m( X% k/ O) D4 B; \& B modelActions.createActionTo$message
& W C) |' e3 s& M9 E (heat, new Selector (heat.getClass (), "stepRule", false));
/ c3 @9 R; j6 k } catch (Exception e) {
# x) U. b! K8 E2 U System.err.println ("Exception stepRule: " + e.getMessage ()); N7 b" n4 g" `/ v8 h R# Y( p
}
+ J% P) h n, F
, p( d& {" D1 m) S/ e& O try {
! ~$ K* I$ U. q Heatbug proto = (Heatbug) heatbugList.get (0);
9 z$ a2 w- Y: z4 t* r+ y1 o Selector sel = 9 n+ ?" P$ T% o/ h U6 ?
new Selector (proto.getClass (), "heatbugStep", false);
' a3 u: m! H+ \ actionForEach =
- w. U; Y* u$ V% ] modelActions.createFActionForEachHomogeneous$call
0 _. z) Y9 Y9 p- b (heatbugList,
2 c S: }8 Y- f) O new FCallImpl (this, proto, sel,
4 S* T& T( {" ^$ z; k new FArgumentsImpl (this, sel)));4 V. Y( e9 i- Y5 u; V' [, U0 a$ P
} catch (Exception e) {8 P. B: \# C' Y6 }4 W
e.printStackTrace (System.err);( K6 N0 F8 `! m
}4 l/ x! q) F. b0 ^) u- P
& U% k8 W% X4 e/ K% i# u% | syncUpdateOrder ();: N4 |5 p/ Q' Q2 t2 a: b+ b) `
) T; T8 n; r3 N% P$ N3 ~
try {
5 [/ }. {) n, T. h2 K( {$ N modelActions.createActionTo$message
) p; Q2 ]: N" w5 r (heat, new Selector (heat.getClass (), "updateLattice", false));( R `) s/ c% Q+ H% y4 D
} catch (Exception e) {
9 N& w0 ^' U/ a+ g/ S) R. | System.err.println("Exception updateLattice: " + e.getMessage ());4 T9 J$ C' g- v; U$ A
}
5 q7 C) l# \/ a" ^ 0 n- T' c- x( I3 P4 v% G
// Then we create a schedule that executes the
* W, Q Z: D2 a, [% V2 S$ D3 o // modelActions. modelActions is an ActionGroup, by itself it4 h# Q+ ~/ f( n
// has no notion of time. In order to have it executed in
$ N1 m. Q" W1 ^1 T // time, we create a Schedule that says to use the
* g' M: Y$ S& |" j; O // modelActions ActionGroup at particular times. This9 u+ x( w% X, j5 \$ w+ [' g% S) {
// schedule has a repeat interval of 1, it will loop every
: k4 ?2 l/ U: ]) \" x9 D // time step. The action is executed at time 0 relative to7 c9 d( u9 [. \1 G3 g
// the beginning of the loop.7 f2 H! k% v' Y+ c+ `
; J8 ^, J0 A2 \1 X2 E% ~/ s // This is a simple schedule, with only one action that is
1 Y3 M5 F, r. p( M1 U // just repeated every time. See jmousetrap for more$ L3 Y( k3 w3 e ^7 R0 l# W
// complicated schedules.) x/ |- g; m5 ^6 y( U
' d/ l8 s0 o% B" F- U
modelSchedule = new ScheduleImpl (getZone (), 1);
4 ]0 ?% x8 U+ _4 q! {9 c( @8 ~9 O modelSchedule.at$createAction (0, modelActions);
( m& C! x, U5 C) G 7 F1 C4 `% A# ^! `/ X+ R! u
return this;7 r5 X& ]2 `8 {0 o
} |