HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ q1 P3 A8 c" D( c7 \/ O) O
/ L9 A0 a+ I; t9 _6 o9 a" p public Object buildActions () {# g+ |* D. x: J
super.buildActions();' V! l9 M8 Q+ K% m4 k6 F s, m/ c
4 R* u" O* r1 f9 b: Y6 {& q# b // Create the list of simulation actions. We put these in+ x: \1 m' D3 }! R! x; l1 c( O
// an action group, because we want these actions to be
. T$ b; o/ C; H1 s9 P // executed in a specific order, but these steps should% B4 M: D1 ?2 C
// take no (simulated) time. The M(foo) means "The message1 E) _& A3 z& W; D C% |/ e
// called <foo>". You can send a message To a particular' _- R' C6 _* l0 I2 E
// object, or ForEach object in a collection.
$ g: R. O1 y" r+ n - [, E+ ~# g5 m( m$ {" M- \$ ~# ]
// Note we update the heatspace in two phases: first run) `' q2 g# W4 e; z5 }6 C/ N
// diffusion, then run "updateWorld" to actually enact the
5 J" K I% D- F+ r( |. F. n! A // changes the heatbugs have made. The ordering here is
" b3 R# D% q( A; z% p$ f* n // significant!* Q* r. Y! B1 i* A8 l
! w# U: I6 t6 M5 [6 W3 t // Note also, that with the additional% X4 A3 F- @ o" q& k9 [( b; V! J
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 E1 E$ _ ^" w% W2 u, A" \ // randomize the order in which the bugs actually run7 o* e" Z+ m0 g, l3 ~
// their step rule. This has the effect of removing any3 _% C3 Z4 c: z# B2 Z
// systematic bias in the iteration throught the heatbug
2 d$ q. \8 `% n% v, E // list from timestep to timestep8 N1 ?, a3 q c- H9 | w
~0 L& e0 p: E/ e2 P4 K) X+ x. Z // By default, all `createActionForEach' modelActions have
! V; H! D3 y/ J // a default order of `Sequential', which means that the# s! Y# y7 h: c7 a( [6 o# \
// order of iteration through the `heatbugList' will be, H8 Y! c/ q @- b( t/ q! [
// identical (assuming the list order is not changed$ y; ?3 I T! g% q
// indirectly by some other process).
* ~* b1 @) X$ ^& a# }' y 1 w, N7 y& P" U/ i, t
modelActions = new ActionGroupImpl (getZone ());: k' j4 f9 |0 p4 Q9 M" B
, Q0 _+ b/ T- n try {
* X7 ]/ ~& W% S modelActions.createActionTo$message0 n# }3 `- r% \0 v6 ]8 M. j7 G- ~
(heat, new Selector (heat.getClass (), "stepRule", false));6 D: P9 y4 l; E! Y
} catch (Exception e) {/ k$ S. d( h7 {4 l4 @, {
System.err.println ("Exception stepRule: " + e.getMessage ());0 c1 j( b" O( [: ]
}- L6 L* Y& Q4 Z9 v$ O4 s+ ?9 {
& s5 s, T7 }* c! ]1 E: l try {$ J! j( C1 o! F# ?6 N& [: m
Heatbug proto = (Heatbug) heatbugList.get (0);
( q7 R# p/ U6 @ b Selector sel =
0 Q a4 K* Z. z, L" _ new Selector (proto.getClass (), "heatbugStep", false);
; p* R3 ]/ Y! x) n7 _9 } actionForEach =1 I& R6 h4 ?( e
modelActions.createFActionForEachHomogeneous$call
# U4 V' H$ i: A5 a* {5 C) N) X' V (heatbugList,# t- p6 p: v" Q; T! S* {
new FCallImpl (this, proto, sel,
/ f V8 Z3 K2 r( D9 x4 g new FArgumentsImpl (this, sel))); o b. w _- x- F/ x
} catch (Exception e) {! \0 j7 M! }3 B( s& N
e.printStackTrace (System.err);
2 f/ B9 M3 G' c# l0 x i j- [1 K4 H } l; h- t$ C( `, n) n, d t* V
# K, f* T. Z( V) F syncUpdateOrder ();
) L+ V8 `$ o. t+ e4 @1 c( r8 s8 P5 {, k/ k* E4 r2 w8 Q5 O
try {
( _0 ?5 G3 H$ {; g1 v7 f modelActions.createActionTo$message 5 e, n6 b) h+ b F- e! z
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 s9 H1 H; `2 k! ~6 _, x } catch (Exception e) {
0 J5 B2 g, p- ~; I+ | System.err.println("Exception updateLattice: " + e.getMessage ());
( L8 m: V! A- B( X6 h8 X1 P! Z- p }1 ~0 W. Q5 R5 a( u6 X( x
- A3 }/ k' @; ^) n1 t
// Then we create a schedule that executes the% t* X3 P _ m
// modelActions. modelActions is an ActionGroup, by itself it
, N& [: k$ l# i' P# h // has no notion of time. In order to have it executed in
; h! C( g4 d7 u; m* _" R1 j) R a5 c! N // time, we create a Schedule that says to use the+ U5 `& E( A; n7 {3 @6 b, U" q- t
// modelActions ActionGroup at particular times. This
& ~8 q! e- H! T7 k% a9 L- w; {! |# `/ W // schedule has a repeat interval of 1, it will loop every
$ q! r! P, y+ W // time step. The action is executed at time 0 relative to
# {- a* h0 P" p) h9 W" Q& _ // the beginning of the loop.0 a8 B9 I: v4 b4 y5 I
V1 Q8 k( ]. R- c" B% m // This is a simple schedule, with only one action that is2 C8 z4 @& g2 a9 i$ u' C- V
// just repeated every time. See jmousetrap for more! [% a. S+ S9 s% E9 ~
// complicated schedules.
8 X2 ]; r5 \- M
) O# ]. G2 d! p% H modelSchedule = new ScheduleImpl (getZone (), 1);
; `/ f5 V) a6 C$ j modelSchedule.at$createAction (0, modelActions);9 Z# q6 s1 H' Q# y
$ F. W7 a% ~1 y$ J- Y) m0 Z return this;, n0 X& H0 q9 b$ c9 `8 ^: j6 U
} |