HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 ?4 K$ P" k ?) i5 J6 s# p* m5 Z. _+ X! k$ p
public Object buildActions () {! r! J: c9 b$ e. Y2 t
super.buildActions();9 T8 C1 C" }: R7 r6 Q4 Q
& l4 ^, [5 O% _. V$ v" ]
// Create the list of simulation actions. We put these in5 I) j7 s. J2 m- H5 R
// an action group, because we want these actions to be
; v, r; A; J- N3 Y // executed in a specific order, but these steps should
' ]7 J3 }) R4 ]6 d! _ // take no (simulated) time. The M(foo) means "The message
& ]4 g/ N8 }' _3 j1 y2 D // called <foo>". You can send a message To a particular
) F$ P# z4 d/ P // object, or ForEach object in a collection.- y" {9 p x2 O4 n& C9 `! [
# |: j8 {( V# c* C* B // Note we update the heatspace in two phases: first run- J! n& u6 l5 B& Q$ ^& N" r6 @
// diffusion, then run "updateWorld" to actually enact the
0 U! V: b& \4 D // changes the heatbugs have made. The ordering here is
8 N4 U4 u. `; L! e // significant!
" j1 U8 u$ O2 l2 }+ _. |; v- h/ x/ p
$ v' I, m- n& F4 O // Note also, that with the additional5 h* Y/ j% g0 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# v9 _+ C0 N; J // randomize the order in which the bugs actually run
( A( g9 X8 A& K' v `0 ]5 O // their step rule. This has the effect of removing any& s/ L2 A* { D6 X" Z. H# y( z
// systematic bias in the iteration throught the heatbug
3 Q! | q& O# X, \( r( b // list from timestep to timestep
% F4 a! M. D4 [+ V* N0 P4 W
2 q8 g6 W# k+ z0 J; K // By default, all `createActionForEach' modelActions have
% I' n& m/ C: d2 c) _7 r( B3 U J // a default order of `Sequential', which means that the
3 a, ~8 x3 {+ ~; V+ Y- p // order of iteration through the `heatbugList' will be
4 R2 T$ m3 @* m, |% O6 y. C8 t // identical (assuming the list order is not changed
' h0 f( ]; l4 I- B e' i // indirectly by some other process).
$ S) A& h J5 X; H# M ) L3 ~$ Q1 ?6 X7 M. }. Q' ?
modelActions = new ActionGroupImpl (getZone ());; j( T% h& P1 A
0 `" J4 W$ @: c5 F+ S
try {
6 g8 S9 J1 j# @3 L" r/ |1 O modelActions.createActionTo$message4 c: V, o4 a$ h3 G6 a
(heat, new Selector (heat.getClass (), "stepRule", false));+ f; H' d! k C7 j- A6 k/ {/ j
} catch (Exception e) {( g/ v6 y4 i% @
System.err.println ("Exception stepRule: " + e.getMessage ());
1 T9 P$ V/ K: ^4 m) q }# f. O, ^5 u4 N) k, ~
8 W5 W' a7 J5 t* R V* L try {8 P7 [. ^) ? o1 Q8 \' f4 o+ G5 y
Heatbug proto = (Heatbug) heatbugList.get (0);* w# f) S; }) L K9 W! i
Selector sel = 8 `& F8 F) v0 I9 u0 \+ `' J f
new Selector (proto.getClass (), "heatbugStep", false);
0 ^# s' y9 w" o# m' N3 E1 q actionForEach =, f" [: S( B: o N2 m
modelActions.createFActionForEachHomogeneous$call4 P9 [3 n5 s' v3 i
(heatbugList,, X; S0 w m- _" x4 i2 |
new FCallImpl (this, proto, sel,
' L' y3 N7 i$ y new FArgumentsImpl (this, sel)));
6 K# q; b( f* U- {; E/ o z5 O } catch (Exception e) {
$ ^7 g0 C* Z6 t, [, o% X5 {/ ] e.printStackTrace (System.err);
6 G+ F( C1 I( F6 C+ B2 \ }
, o: r/ b5 A' }8 g5 T* P
" {1 z! D2 s0 D) O, } syncUpdateOrder ();! p9 [+ i! n6 w3 a
3 _* g% A5 G6 N- F& K
try {
3 ^3 f# g8 M% `1 A1 }( `" U modelActions.createActionTo$message
( t' j- g% e3 b& Q (heat, new Selector (heat.getClass (), "updateLattice", false));- j+ G7 W2 {1 B6 V
} catch (Exception e) {
K% F) M5 v6 m+ z1 b% ? System.err.println("Exception updateLattice: " + e.getMessage ());
0 p, j. e) A* F; W# l3 l4 L6 m2 F }
2 b7 x# n: N' i q* \
8 a2 x6 |/ I. Y% M+ ]; K% ?% M // Then we create a schedule that executes the
2 G) I, {9 w4 I1 Z# c. j2 x# t // modelActions. modelActions is an ActionGroup, by itself it
) y& D+ M' k& C% @5 Z // has no notion of time. In order to have it executed in
7 @3 [2 F, Y/ A7 D" r0 |% T, _ // time, we create a Schedule that says to use the( M; d4 T- E E8 w7 i; [
// modelActions ActionGroup at particular times. This
) i) d: q6 V0 f% r& x' j( X // schedule has a repeat interval of 1, it will loop every% w: S5 h$ J' I* \! O5 n2 R
// time step. The action is executed at time 0 relative to- ?# ]) p7 f( T2 L
// the beginning of the loop.
7 K. l, p% c' A* O* Q* e9 F7 \* k( } O7 H* }
// This is a simple schedule, with only one action that is
4 ~& c7 E& P: b( ]9 S // just repeated every time. See jmousetrap for more
: G; X& n/ c! e( O. _* \0 g+ x/ } // complicated schedules.
! v/ H+ b1 ~6 K9 V" ] 1 E8 b5 N; \$ r
modelSchedule = new ScheduleImpl (getZone (), 1);
6 P# ^- C9 A+ o. N1 }7 B: A modelSchedule.at$createAction (0, modelActions);
* K5 g1 b0 m, X1 J! b6 \ ) d- G/ Y7 f9 N+ ^6 W: f
return this;0 E3 q# q! o/ h J! y. |& ]/ k
} |