HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 {5 P" i# N; G) h# h y$ j0 K3 ?( }8 U3 z! G1 G
public Object buildActions () {
( e; |8 q0 F: F8 z( {5 s super.buildActions();
7 l5 w5 E) u; b; H2 z9 a# _/ k
! C" W+ y) f" o- c% d/ b4 _ // Create the list of simulation actions. We put these in
# H+ U Y$ Q, M& ^/ |+ g7 P- R# D // an action group, because we want these actions to be
5 K$ D; j3 Z# b+ v) Z1 u$ C // executed in a specific order, but these steps should
% }5 b' w9 x2 I6 O4 v: | // take no (simulated) time. The M(foo) means "The message$ s* J* |4 ?; G1 U
// called <foo>". You can send a message To a particular7 r% V2 l8 }& I6 r
// object, or ForEach object in a collection./ e% w- A/ X, M/ m. e; l1 C- x# W
7 i) T& q( z1 `( ]7 C- F: Y6 M6 ] // Note we update the heatspace in two phases: first run v1 t; V7 U0 h) q, Q+ x$ F0 D
// diffusion, then run "updateWorld" to actually enact the
- M# ?$ N6 e" Q0 C4 V- X/ L. K0 M' V8 P0 m // changes the heatbugs have made. The ordering here is
3 P. B% s* l" ?* Z- J2 ]" t; W // significant!
; D" J$ ]$ x& Z7 o3 _% ]
% K3 Q2 E# C/ {. W // Note also, that with the additional
. Z- ]- L7 t2 f3 @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
' J' X& i' d5 J0 _& ~ // randomize the order in which the bugs actually run$ J/ T2 t' X8 A! ~# I
// their step rule. This has the effect of removing any
- Q9 u; T& }+ q+ r1 g2 V" q // systematic bias in the iteration throught the heatbug0 r# b1 d% u" V' `
// list from timestep to timestep6 x+ a5 y! G5 E
- F" Z1 h t2 F // By default, all `createActionForEach' modelActions have
: o8 E! S* K/ B: i% {7 E // a default order of `Sequential', which means that the2 J8 f$ x$ d0 v8 F" B% m( R
// order of iteration through the `heatbugList' will be
- G; f* q7 U# @; b/ D. d // identical (assuming the list order is not changed$ ?5 U8 r" r1 o Q0 D! {5 Q
// indirectly by some other process).' |. |2 X) |9 \7 E% w
4 D! K% x# _* m4 o! k6 C modelActions = new ActionGroupImpl (getZone ());
. j+ j0 B Q; _) R3 w0 v" L, H0 k# Q0 N. Y! I" `1 r0 G
try {
- W( M2 o3 M! G0 \6 U' S modelActions.createActionTo$message+ B7 Z' j9 v! P$ z
(heat, new Selector (heat.getClass (), "stepRule", false));
' g1 ~+ s3 [3 W/ X& [: c } catch (Exception e) {! O/ \/ y4 }3 b* i6 I
System.err.println ("Exception stepRule: " + e.getMessage ());
- m W3 J: r' k: k }
3 u. p4 i% T' s6 j5 b1 y1 g1 s# a
try {
8 E0 _/ q4 `* Y+ k4 z; d; _ Heatbug proto = (Heatbug) heatbugList.get (0);; V8 d+ j7 m! g) \; D/ J( X
Selector sel =
3 `7 Y3 r1 C2 |/ ^2 a new Selector (proto.getClass (), "heatbugStep", false);( a7 m) `# N! E& ]- I6 Q( B( N
actionForEach = b$ r% U& r2 H1 E$ P: A) L5 {* p
modelActions.createFActionForEachHomogeneous$call
) n+ P/ y5 z1 n (heatbugList,
: }1 o: h/ c, H1 ?% d. y0 x new FCallImpl (this, proto, sel,
, S0 T! U1 C4 N# t# x1 [ new FArgumentsImpl (this, sel)));4 R% c9 q) @ G% x3 a
} catch (Exception e) {! J/ o0 t/ P4 }% _, S! M& c
e.printStackTrace (System.err);/ f5 V+ s1 |/ D+ R: m
}$ C; b- l+ A' B* [9 Z0 w7 c U
; G. s0 T1 S# X2 ]# Z
syncUpdateOrder ();
8 [% g d0 W$ R+ ]5 S, B1 {- J! H3 }% b% D% f9 g9 f
try {
6 K9 W7 k. y8 `! O8 `2 ^' _ modelActions.createActionTo$message 9 G7 W2 Z7 o" A, R
(heat, new Selector (heat.getClass (), "updateLattice", false));! @/ s0 z0 |* s9 E4 g
} catch (Exception e) {
; D b/ ]4 z7 U; | System.err.println("Exception updateLattice: " + e.getMessage ());: q1 b& p/ y k0 ~
}
# {; n& Z- e* T8 I0 @% q+ S) Z9 T
2 W( a* b0 p0 |: U // Then we create a schedule that executes the, T9 h. }0 l( X" f
// modelActions. modelActions is an ActionGroup, by itself it! W7 \0 B2 i d/ d& n. ]$ l
// has no notion of time. In order to have it executed in
4 j# l- y4 p" R# P // time, we create a Schedule that says to use the
" ]) G- j! u+ L" t // modelActions ActionGroup at particular times. This
% D2 Z! } j6 I7 I // schedule has a repeat interval of 1, it will loop every
8 h9 |5 N. }& H // time step. The action is executed at time 0 relative to
6 E' l7 k4 s+ t. y# H. I, w* N6 Z // the beginning of the loop.4 c6 a% N# C, [
( p& p& Y/ x6 a' P0 } // This is a simple schedule, with only one action that is
9 Y8 }& U( h/ J: V( O4 v // just repeated every time. See jmousetrap for more0 ~! F! u. F6 t, `- | G
// complicated schedules.2 O9 V5 \$ I5 D3 b+ w3 q
6 f, Y% \" U) n. s modelSchedule = new ScheduleImpl (getZone (), 1);! d' n7 I/ a0 X# Z/ v& N# k
modelSchedule.at$createAction (0, modelActions);
( P8 k4 r% O5 r$ Y: l9 U + u, o0 F9 {% f
return this;4 p" v4 w6 n. _1 Z0 H! g& M7 n
} |