HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* s+ k: L" E( Q/ X t* E7 }( P- G( J& B: ]' R
public Object buildActions () {
" Z8 |1 |* c' S# I5 P. q2 ^ super.buildActions();
. L0 ]& K9 q/ ]9 N 1 Y- B5 Z" L% i B0 D! ^. {1 y v
// Create the list of simulation actions. We put these in
/ u. o- U9 d+ Z) q* x& M // an action group, because we want these actions to be
8 C0 s1 j* c) ~( Q // executed in a specific order, but these steps should
. ~/ N8 @( ? k6 A9 r% u0 k% S // take no (simulated) time. The M(foo) means "The message, W; p: B/ X! S% f* z3 i$ W
// called <foo>". You can send a message To a particular! y4 }2 s4 x% W- a+ T# O
// object, or ForEach object in a collection.
3 F: Q/ F0 n' d; O
1 A* @" ~ M. _6 S" M // Note we update the heatspace in two phases: first run
+ }7 d) P8 T' | // diffusion, then run "updateWorld" to actually enact the0 w- ]% G2 Y' a" _( t+ g1 `
// changes the heatbugs have made. The ordering here is
/ F4 g: _ O/ Y- d // significant!
' w& Y3 \4 M" ~7 q7 L0 F 9 U! J' ?. h" w7 \) z
// Note also, that with the additional3 x4 |% {% k; b) G" R3 c
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( `: ^* i; Y, z/ h7 {* T // randomize the order in which the bugs actually run
1 T' f! c$ r9 e0 j // their step rule. This has the effect of removing any9 L4 I0 |+ ^$ S0 V4 q& l% t6 X1 ~/ u- d: h
// systematic bias in the iteration throught the heatbug9 @6 _" S' ?; K' ~1 i! V
// list from timestep to timestep
, c/ ?( ~9 z. X1 L! ] 3 `3 `; O: ]7 }9 e/ K; H1 }" K/ b
// By default, all `createActionForEach' modelActions have" X, t1 E n/ j% @5 F8 D& v% q, h
// a default order of `Sequential', which means that the. Z/ d1 J, n/ T+ K" K$ F: \
// order of iteration through the `heatbugList' will be" `- _& X( Q6 S T
// identical (assuming the list order is not changed
' F7 q, f& Y6 d0 E9 \) m // indirectly by some other process).
' b# Y9 u3 Z% h% g1 d8 Z / }3 `+ E8 S! k% {. p! [/ J
modelActions = new ActionGroupImpl (getZone ());
/ K4 f8 j6 I% [ ]
& V" C/ s2 e J# \0 g try {
$ _% K7 X [) m x* Z$ i3 D modelActions.createActionTo$message W. t" q8 M8 z% g
(heat, new Selector (heat.getClass (), "stepRule", false));
7 t5 T5 t. v, M7 X } catch (Exception e) {, h+ u: Y! p8 [5 y, z
System.err.println ("Exception stepRule: " + e.getMessage ());! Y6 ]( a' H8 d2 ]% ]
}) X6 a6 E5 f) n& s# {/ S
! |9 [% A; ^/ y5 R) b try {- U a; [4 f2 K. |
Heatbug proto = (Heatbug) heatbugList.get (0);# r+ e5 n" c. w% C
Selector sel = : |3 x: V; Y/ W
new Selector (proto.getClass (), "heatbugStep", false); R4 q, f9 G% r6 g' { @: c
actionForEach =" E5 c( Z* k& y7 U5 ]
modelActions.createFActionForEachHomogeneous$call
" _+ @4 }0 b5 n/ }$ t0 `) p (heatbugList,
* W) u# L4 S0 l: m5 u0 n; t new FCallImpl (this, proto, sel,
; u1 _# C0 f7 g6 d- P) K' ?+ ]. L0 ~$ s6 v new FArgumentsImpl (this, sel)));/ c1 r6 ^6 F, Y/ x# v) M% j) |
} catch (Exception e) {
( ?2 S6 i& q" f. g% D/ x e.printStackTrace (System.err);4 G# f" h6 Y! v% m. p3 y# [4 [9 _: {
}4 C# W8 X8 P8 t& A% G, u3 o: o
! g! c$ s' E0 _$ c6 s
syncUpdateOrder ();
/ z7 A& I; j+ L8 }* E1 ?1 N4 d; y: k4 f
try {
" V, T1 L* a5 p2 a modelActions.createActionTo$message
' {/ z7 \4 x% w: t (heat, new Selector (heat.getClass (), "updateLattice", false));- }% @( ~9 K% D3 `
} catch (Exception e) {
' w; k! j4 X# B0 y System.err.println("Exception updateLattice: " + e.getMessage ());
7 Y% T+ l- [4 j, d" I }
: n% u# j3 ?; ]7 o2 y . s0 G3 u" q' s) S
// Then we create a schedule that executes the
; j9 {0 e4 v# J/ x) ~2 u$ S9 ] // modelActions. modelActions is an ActionGroup, by itself it# c0 o! w8 }# B5 `5 d6 L$ t$ b4 w6 Q! g2 H
// has no notion of time. In order to have it executed in
0 g3 m! o' A& @# ~: U: S // time, we create a Schedule that says to use the
, H5 d) G- e1 t7 B5 } // modelActions ActionGroup at particular times. This
4 g1 h! ? R% R" U0 }6 N6 U // schedule has a repeat interval of 1, it will loop every- B! F! L6 I- B" e# c' @
// time step. The action is executed at time 0 relative to1 s/ m+ h7 {1 ^" q) B
// the beginning of the loop." H& Y8 s4 e% P p
7 a6 [; l" u0 h" D% l // This is a simple schedule, with only one action that is
7 S' l3 F* h y: v, m! [8 n2 W, i // just repeated every time. See jmousetrap for more L2 L! D9 d0 b4 c" P! e4 Z! J
// complicated schedules.1 `2 [/ F' B* M% b2 Q: f
$ L L% W" e: Q$ ^( T5 F modelSchedule = new ScheduleImpl (getZone (), 1);) H7 T8 [8 G) a, t: J+ f
modelSchedule.at$createAction (0, modelActions);
4 u1 m3 H# x! Y8 b* p/ }# f. t6 [ . t2 X8 z2 @; |, v% C' y
return this;
$ t% N) R' f) C* B/ v0 |$ P } |