HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: ~1 _/ r$ M8 R! a6 v7 q8 v
) I9 w* b% v+ }$ R
public Object buildActions () {' `& M2 t$ g9 {9 ?9 o" z7 K
super.buildActions();
8 z3 ?9 R- T$ R* d$ L
' ^0 ^8 E, \: |; P$ ^: j w/ Q // Create the list of simulation actions. We put these in, k# X- {0 l# s: H
// an action group, because we want these actions to be r4 k7 m M8 K6 x2 V
// executed in a specific order, but these steps should1 `! D- Q% V" ^; _2 U
// take no (simulated) time. The M(foo) means "The message1 V/ H' O: T2 Z0 }! I
// called <foo>". You can send a message To a particular
# k+ w3 ]' E* `+ E, c: r5 U // object, or ForEach object in a collection.% o9 T) q5 f. i* e1 G" L6 B
2 _* F8 g2 M# O7 R+ i- K' W
// Note we update the heatspace in two phases: first run- U/ m' P1 Z k- L2 X
// diffusion, then run "updateWorld" to actually enact the
; c- S1 W- y1 `/ a# W/ P2 ? // changes the heatbugs have made. The ordering here is
' D: k8 A9 G2 h0 _% o. R) t0 ?6 \ // significant!; f g, P% }7 _1 e" P% i( V
# Z1 g2 ~' A0 S/ }
// Note also, that with the additional7 ?- s; o& R/ ?0 D, m. C* ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 F& |+ m6 l( y7 ^1 z // randomize the order in which the bugs actually run' b! w5 D! ?. r1 w p& u
// their step rule. This has the effect of removing any$ V. M* S6 Y' s8 ]2 r2 k( s
// systematic bias in the iteration throught the heatbug, n4 w* K2 K9 l9 [ d3 i
// list from timestep to timestep: t6 d6 V" ]' G) y0 L3 @
5 a* e D0 [2 E) y" q // By default, all `createActionForEach' modelActions have
6 O6 ~ M* `4 j, D5 G/ U1 V // a default order of `Sequential', which means that the$ c: k B- R5 `+ R6 r" z
// order of iteration through the `heatbugList' will be. P i2 o# X' h' j5 A+ W+ ]) s3 W
// identical (assuming the list order is not changed3 _* U* {$ A- }; H; e
// indirectly by some other process). @3 o' v B* u) N* v
1 [5 N7 v. b& ` modelActions = new ActionGroupImpl (getZone ());4 h0 H# w* A. J- Z
' n0 f; p) U0 w: e- a. z$ K$ E H
try {# k7 ~9 h2 B7 @2 s
modelActions.createActionTo$message
$ r7 m; a& D4 y( _5 }; {$ ]7 d1 ` (heat, new Selector (heat.getClass (), "stepRule", false));
; |* ]7 B+ }1 H9 H3 d; N. R/ ?# V } catch (Exception e) {
# j2 c$ y, d. }' n; }, v% L: y+ E! j% ~ System.err.println ("Exception stepRule: " + e.getMessage ());2 I6 m! I2 ~* E7 }/ y7 S5 H
}
! W9 {! q2 q0 `/ |: X7 p0 F
- y# t! f: q/ U( ]( }* @ j& D1 {* N try {
" z9 S' l h3 z6 K" q Heatbug proto = (Heatbug) heatbugList.get (0);# x8 i# l1 a, s
Selector sel =
& V# b- j' J% p+ f+ x# [ new Selector (proto.getClass (), "heatbugStep", false);
; U( M$ I7 c7 c$ y* r actionForEach =/ P4 Z# I% a V) R+ t( @
modelActions.createFActionForEachHomogeneous$call w- u1 ] Z+ P1 o
(heatbugList,6 k: m: E7 d$ h
new FCallImpl (this, proto, sel,
; j$ C' a' o8 t1 g/ [: L+ w new FArgumentsImpl (this, sel)));8 R8 W9 C4 L8 K# U3 A8 N
} catch (Exception e) {! C6 T9 y3 u) e" ^
e.printStackTrace (System.err);3 B/ v. v5 O' A4 B% W, g
}, T& B" U+ V; S9 ]. `
* o7 ?, Z+ ?: D j/ h6 ~
syncUpdateOrder ();
$ `! v. C3 x* k" X0 z. ?( E+ P! Z" u
try {
9 |1 i" q6 o- T modelActions.createActionTo$message
' |% f* N \' g0 X (heat, new Selector (heat.getClass (), "updateLattice", false));2 h( h* a% v2 n/ d+ g- I2 H
} catch (Exception e) {
9 M: ]3 Z/ m7 g s7 I& ^ System.err.println("Exception updateLattice: " + e.getMessage ());" @. |# o- K5 q' a* E
}
$ a( c. v! n, n2 A1 p; d( W$ q4 `
# I" B4 ^. s# Q, f5 k3 s // Then we create a schedule that executes the
& R3 U, n$ ]5 O, s // modelActions. modelActions is an ActionGroup, by itself it
7 H/ r0 W, c J2 s5 A m/ X // has no notion of time. In order to have it executed in
0 L, ]5 {$ U+ E& N$ F3 |, M: I' \ // time, we create a Schedule that says to use the
, }0 y, N1 n$ o% F/ d. ~ // modelActions ActionGroup at particular times. This
: |" ]) A. ?1 ^ // schedule has a repeat interval of 1, it will loop every* J2 M4 L5 Z9 X! T/ R
// time step. The action is executed at time 0 relative to
( h2 {4 ?3 R& j7 ?) e Z4 ~ // the beginning of the loop.
& y0 ^! T! Y# w/ l! n2 X& G
0 u1 j4 l; M/ p1 _+ s0 n // This is a simple schedule, with only one action that is1 ]" B9 S- O0 D8 q4 h$ U+ Z
// just repeated every time. See jmousetrap for more
9 }& C9 c# E3 A; h$ @ // complicated schedules.
& [* T. ?! ^% u7 E9 r& @
# [: ?6 ~ J5 T modelSchedule = new ScheduleImpl (getZone (), 1);
8 {+ `3 v" p$ k- } modelSchedule.at$createAction (0, modelActions);* g( r, f: H( a* p* O/ x p) t1 _! h
6 E3 b& M4 J: I return this;0 b% W5 x& t9 J {
} |