HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 p& l8 T7 \3 ^2 s* [* ~: f# r& H, ^( m9 a+ g
public Object buildActions () {: c" d, a1 ?4 w. t6 H/ ?# Z5 G
super.buildActions();
5 p1 @; C" `+ e: d. d d3 i5 o ) n4 _( |4 M2 z* V
// Create the list of simulation actions. We put these in# P' j: ?% h4 p2 B. B
// an action group, because we want these actions to be
0 E" D' Q4 e6 s. l- D: R // executed in a specific order, but these steps should
6 p2 p' P6 x2 `( B // take no (simulated) time. The M(foo) means "The message
9 B5 Q( [1 _ U& i1 Z7 r$ j // called <foo>". You can send a message To a particular7 w1 e' I0 m5 ^; S
// object, or ForEach object in a collection.
0 }! w8 M. v8 r2 ^0 e 7 ]4 T2 d& u# W$ y" ^' v) a
// Note we update the heatspace in two phases: first run
9 w( v" z/ [8 q; c // diffusion, then run "updateWorld" to actually enact the$ m8 c& R7 Q- E! E! u) d* `! ]4 v
// changes the heatbugs have made. The ordering here is1 R7 I6 S' N9 N! O3 Y6 R2 j
// significant!6 w( l5 D, G% l0 }7 `7 s
) U7 ~) \2 b! p1 W g+ Z
// Note also, that with the additional
! F7 l: Z6 {6 M* ]7 Y. z0 q // `randomizeHeatbugUpdateOrder' Boolean flag we can5 x U# X$ `" H+ q; Q" Q
// randomize the order in which the bugs actually run# `8 {6 E/ G6 a' W' o
// their step rule. This has the effect of removing any; r/ I9 o& [' S% R
// systematic bias in the iteration throught the heatbug
# J# w5 Z! ?) R/ L9 c h // list from timestep to timestep
0 q4 a+ }, N( g5 D3 c! v+ S7 r$ T ' [/ M3 n& p) \ n3 `. S
// By default, all `createActionForEach' modelActions have# U2 o0 s, g1 o4 [6 I
// a default order of `Sequential', which means that the6 @: \" {! d3 p& w4 D5 L
// order of iteration through the `heatbugList' will be
6 q* q9 t4 X' }2 T5 f2 n // identical (assuming the list order is not changed
& M+ h: f6 r0 a$ J // indirectly by some other process).
% I4 h0 ]2 t4 h" _' _
+ `4 s& \. {5 w1 ?. |: p* D/ o6 k1 M modelActions = new ActionGroupImpl (getZone ());3 V4 w T1 e2 K6 H; d* s
" A! l g2 s* Q% m; [5 ]
try {
2 k. Y, {: a- V/ ?; ^# A$ \ modelActions.createActionTo$message
, C1 V4 m& C' _; ] (heat, new Selector (heat.getClass (), "stepRule", false));4 e' A' R, H& g; v. e7 J, b
} catch (Exception e) {
]+ l' B* `/ V8 q9 x2 I System.err.println ("Exception stepRule: " + e.getMessage ());& [0 w9 c. c/ T. A7 g2 D% q2 G
}
0 V) f0 R% ^% [4 K1 t
/ g" K5 |9 B4 d# j3 v6 @ try {$ J( X" f5 z! a9 u/ n
Heatbug proto = (Heatbug) heatbugList.get (0);" v6 [5 Z# N5 }, o! R# N) f
Selector sel = $ j `) O% G, @! x# v9 B) L$ S
new Selector (proto.getClass (), "heatbugStep", false);5 L; U f' G" E6 n( q; A* J
actionForEach =4 h- m' K' X o7 |% b/ B$ @! g& @) U
modelActions.createFActionForEachHomogeneous$call$ h5 q/ n' S; k0 z. n% \; A
(heatbugList,
; C+ z% v$ S+ w new FCallImpl (this, proto, sel,6 _& X/ ?7 [7 e! {: Q' G/ X% e
new FArgumentsImpl (this, sel)));
9 H5 |0 r' Y2 ]9 z0 o/ W } catch (Exception e) {5 u0 k3 ]' D6 n7 J+ J+ o
e.printStackTrace (System.err);9 _2 Z* s3 V5 d; G
}1 L( ^7 Y3 ~* @9 l' c& \# i# x7 R: k( B
8 i6 C. _0 V( R8 [( D1 s2 K
syncUpdateOrder ();: y* p* {" i1 n+ E( d* s6 p
p4 s+ I! ?% J* q
try {+ A4 U8 u' w: v5 T
modelActions.createActionTo$message
; c* S F! Q% r$ K6 E2 e2 F (heat, new Selector (heat.getClass (), "updateLattice", false));* p: r( Y; k0 z. M+ X" J
} catch (Exception e) {
" ~# [: v9 h# F4 I: f System.err.println("Exception updateLattice: " + e.getMessage ());6 C) q% g8 Q) N/ U* Q
}
- {6 K* S# c) b6 i9 `3 V
& p! o; ^( I9 _% O7 |9 f // Then we create a schedule that executes the
# |* g1 T/ ?( }; j' x // modelActions. modelActions is an ActionGroup, by itself it3 F0 a0 v' u$ U& `
// has no notion of time. In order to have it executed in
q, \6 @' }0 Q" f3 }- z& i // time, we create a Schedule that says to use the. M- y) D4 B4 s: _' C) j
// modelActions ActionGroup at particular times. This' t- L3 f9 W- e) Y
// schedule has a repeat interval of 1, it will loop every: E$ S+ g4 \9 _' P$ H
// time step. The action is executed at time 0 relative to
! ?0 Q1 b# E3 G, D0 {/ ~) w F // the beginning of the loop.2 }% n3 A- P' ]% }. Q# X4 n/ k
6 q2 g* R" B4 ] }4 i5 R // This is a simple schedule, with only one action that is2 ^/ |: s! i9 I- M+ U. [
// just repeated every time. See jmousetrap for more' t) J+ N$ B1 Z/ @5 J% I( m
// complicated schedules.1 S; S2 ?1 J0 a$ g7 z& I, A" u
( |2 x: _9 z* ]+ C/ D5 W) I modelSchedule = new ScheduleImpl (getZone (), 1);6 Z, [+ Z+ |* H! u* @. N
modelSchedule.at$createAction (0, modelActions);
' N! ?% z" m$ V: K' k L , D: _, _8 ~+ Y' Y, o
return this;' V6 D. a7 n6 ]2 R$ ~# }9 |* R( M; Y
} |