HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 N9 X: @5 Y' T4 ^
2 u$ G7 w6 x3 n6 l- w9 m9 ~& g4 n public Object buildActions () {
1 Q5 h+ ^- L2 T& `: O0 I super.buildActions();' V1 k) g3 L8 e6 ?7 D
3 R q! B1 Z% h: q& Q6 P, `1 E // Create the list of simulation actions. We put these in' U' \3 ]2 Y8 Z- S9 L
// an action group, because we want these actions to be' [- c) V1 S5 g1 I
// executed in a specific order, but these steps should. D2 \3 l+ r- V1 W2 A8 g4 ^
// take no (simulated) time. The M(foo) means "The message
5 b7 ^0 Q# U2 G) J1 O // called <foo>". You can send a message To a particular
0 F h- p0 [" n! h0 [ W: D // object, or ForEach object in a collection.
6 d% E9 A1 x! y& T+ [
7 M' \4 D w" `3 W // Note we update the heatspace in two phases: first run' m5 x0 ~& B9 X
// diffusion, then run "updateWorld" to actually enact the6 q% i* Z. c6 ~) [
// changes the heatbugs have made. The ordering here is
' }! z* `- r- C/ }& f // significant!6 K, r }' I! _; F4 f' }
0 T3 O: J' j) h1 H6 l+ K // Note also, that with the additional
$ } h9 _( ?$ {8 W // `randomizeHeatbugUpdateOrder' Boolean flag we can% E+ f; k; \7 |) U
// randomize the order in which the bugs actually run' [. l" S5 Y7 Y. B: R
// their step rule. This has the effect of removing any0 T- T1 V' s, q% ]
// systematic bias in the iteration throught the heatbug
1 P& w* Q) x$ q' @6 g+ ]4 \- ? // list from timestep to timestep3 O- A$ g0 N8 O+ t
; H0 v3 O2 t9 h/ v+ }
// By default, all `createActionForEach' modelActions have/ q/ _6 q; N7 P1 [' D6 o/ Z. d' G
// a default order of `Sequential', which means that the
1 f, X8 m# r7 l: L* {" ] // order of iteration through the `heatbugList' will be
. ?$ T% O' X6 `+ [8 c% k // identical (assuming the list order is not changed
* y$ |9 ~9 j1 _2 b% {& r& W // indirectly by some other process)., I: A1 R1 t5 Y: y
" X% R* y! w: d" ]( h
modelActions = new ActionGroupImpl (getZone ());- X, ]- L! B( \* M% G4 f! P+ c
# l$ ~/ |; i. O' j! P try {
! s5 m. D7 h8 j# `, A: b modelActions.createActionTo$message. V, {8 C+ q R# b9 s7 p$ }0 K v
(heat, new Selector (heat.getClass (), "stepRule", false));5 j" q( P. m0 E' L
} catch (Exception e) {9 |6 [2 P7 V X
System.err.println ("Exception stepRule: " + e.getMessage ());
4 p+ W, K1 D3 m" d }( U1 h: e( x4 m5 Q" e
9 d" \8 j. k! ]7 ~
try {' I8 |* u" e' `! }/ P# r+ O: c
Heatbug proto = (Heatbug) heatbugList.get (0);
( u. J7 `! C+ y0 y8 z Selector sel = 5 X" W5 h% u, p
new Selector (proto.getClass (), "heatbugStep", false);
/ Z9 i0 B' L( B$ Y' q actionForEach =
n1 H0 C6 ]1 H0 V ~4 E modelActions.createFActionForEachHomogeneous$call; S) e5 N- \0 Z9 v
(heatbugList,0 G* w4 z, o Q5 {
new FCallImpl (this, proto, sel,
3 n& F" [" {: E: ^9 Q% T new FArgumentsImpl (this, sel)));
R! k& G. Y2 g' J' p9 L) T } catch (Exception e) {7 Z+ e2 N0 W" ~$ [# @- k+ Z( d
e.printStackTrace (System.err);- `. @& b4 ?' T2 o* ~, Q; _
}; I7 c& t8 J" a8 e. c
* c) U8 g$ F7 [8 z- V/ x syncUpdateOrder (); p6 f1 P. ^! \$ w1 L2 U/ x
4 ^# u8 I" S7 D" O" Y* @3 A0 z! M
try {
1 q, ?9 F4 x. n modelActions.createActionTo$message
6 R# Y) L" w. b$ T q (heat, new Selector (heat.getClass (), "updateLattice", false));
. ~0 o$ w% F% c2 f8 [4 b; g } catch (Exception e) {; D, q+ o) X0 @, _, P- f
System.err.println("Exception updateLattice: " + e.getMessage ());
2 [. I) @) Q7 w5 k5 U4 U! C0 z }2 b; [5 ~$ h( m9 r+ W9 p+ C Q3 Z. F
, ^4 s+ ^- G" o, R- } // Then we create a schedule that executes the
) a1 J8 d O" V // modelActions. modelActions is an ActionGroup, by itself it
" Y) `) A) @7 D# d' \, G // has no notion of time. In order to have it executed in- p& H# N2 L& n; F J2 W
// time, we create a Schedule that says to use the
! }0 s( [2 y7 N% B( Z // modelActions ActionGroup at particular times. This
% [1 L( r$ R" X4 I" w! y L/ N // schedule has a repeat interval of 1, it will loop every8 M" r% V- X. T! H/ y. w
// time step. The action is executed at time 0 relative to
/ M' o H2 m8 ` // the beginning of the loop., h3 n/ i9 A: D
; E0 ~) g" G* Y: S& Y, r9 c) ] // This is a simple schedule, with only one action that is
; d- W. ]9 u8 H4 O- G/ B // just repeated every time. See jmousetrap for more$ {* ?0 e: | q9 b( ~
// complicated schedules.7 B$ c1 C; ?! m( b1 J
0 G; B: b+ J) G. {" `1 }( \
modelSchedule = new ScheduleImpl (getZone (), 1);( H) Y6 X: `$ J7 `& o* p l5 j
modelSchedule.at$createAction (0, modelActions);0 x. ^3 L# F. j/ A- s N+ y3 y
% N. j$ S. Y# i. K! H( I! A return this;
- L/ L" }4 d8 o% E6 x } |