HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 D6 o8 ^- A& G9 j2 ?4 s1 Y7 U! p' z
; E' H+ t+ H4 y public Object buildActions () {% h8 w* a" t5 J5 {
super.buildActions();
0 K3 c% g7 b; {2 k5 i" Y& t0 q
7 h/ S Q% I- G+ m" N; ^2 D // Create the list of simulation actions. We put these in
4 O8 v2 Y! B! ~. P& C3 m0 N // an action group, because we want these actions to be
) Y0 _3 g) ?' _ // executed in a specific order, but these steps should
: b; R5 }- h9 v9 g! M // take no (simulated) time. The M(foo) means "The message
# Q0 ]( c. m$ v // called <foo>". You can send a message To a particular" W! X$ t6 w0 o
// object, or ForEach object in a collection.; t! t) u, D9 Q1 M6 c
! }! x2 y5 l' c$ y! y // Note we update the heatspace in two phases: first run" |1 j- d* ]5 m) C1 `+ t3 \
// diffusion, then run "updateWorld" to actually enact the T2 D; \; e4 m: H% [
// changes the heatbugs have made. The ordering here is
2 ]5 ~7 ^4 c1 n7 i1 P // significant!5 N, P* T4 V2 C# }* h' H ~
7 n" d( }) |1 b% | // Note also, that with the additional
# H/ h& c2 _# P4 h) K( a& h // `randomizeHeatbugUpdateOrder' Boolean flag we can- b6 y. e: {/ M
// randomize the order in which the bugs actually run
9 }7 l* ?( g( c5 \ // their step rule. This has the effect of removing any+ d+ F6 [- D0 b/ k" n7 O$ R8 o
// systematic bias in the iteration throught the heatbug1 S# ]! _; [$ P& {
// list from timestep to timestep# H1 A" Q, X6 V* z7 Y+ H( N
& ^8 ~, x1 o0 s% F7 K
// By default, all `createActionForEach' modelActions have
6 {8 ?& W' h# S+ q // a default order of `Sequential', which means that the$ L* a/ S/ ^6 a/ U" |) H: M
// order of iteration through the `heatbugList' will be+ G9 _: ~) U* I2 b: w* o4 g) C( i
// identical (assuming the list order is not changed
8 G* ?( j9 O4 m$ T // indirectly by some other process).* `( \& d- }5 U6 N' Z2 H( G7 o
2 d- h) w" p& p( [9 ? modelActions = new ActionGroupImpl (getZone ());
. z+ ~7 B; b# \* M
$ s6 D n& s. |& [; k try {
0 r; o* T& P H/ \/ A# J* a modelActions.createActionTo$message) V1 o% V. h; X, T& ^+ ]
(heat, new Selector (heat.getClass (), "stepRule", false));
. k7 G6 U3 P' F5 C. ] ~ } catch (Exception e) {8 u5 l- }9 i9 ]1 W
System.err.println ("Exception stepRule: " + e.getMessage ());
% T- x2 C; |3 I: u6 E; C; Y/ g }
0 { E; ~* E; m* I/ g' _% D# B+ W) e7 r" i s5 d( l
try {
" N Q9 Z! W$ x6 s% B! g& K Heatbug proto = (Heatbug) heatbugList.get (0);4 ^2 Q& y, ]( N5 _
Selector sel =
7 e, o3 W C0 @, a- V new Selector (proto.getClass (), "heatbugStep", false);3 {% O- R$ m8 Y' g# W
actionForEach =
9 J M3 r6 w# i" o+ } modelActions.createFActionForEachHomogeneous$call
$ {5 {* y/ e: e% V! ]; Q) j4 | (heatbugList,
8 q3 S# ?" B8 j t new FCallImpl (this, proto, sel,
6 w, K) g$ o1 _* y6 Z new FArgumentsImpl (this, sel)));" b7 Z4 z* m5 t) e; }2 K
} catch (Exception e) {9 Q1 h$ v# i- B
e.printStackTrace (System.err);
' L( J( c: K- t4 c; A# w5 | }5 i7 v# p2 x# _4 |4 a/ [3 J7 N; }
( z% q0 t- r4 ]" r) z) [5 K
syncUpdateOrder ();
1 Q1 k- L8 C j; Q" G7 H# m/ E6 m5 e
try {
$ Z; B7 o3 s& J# S modelActions.createActionTo$message
- n3 t# L1 Y- Y" J1 f4 B$ Q6 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
) ]4 b: x: v0 O& y j2 e } catch (Exception e) {
2 i l1 [+ Q+ G3 q System.err.println("Exception updateLattice: " + e.getMessage ());
9 ^8 w& S0 U4 C }% c- L$ h5 b/ _7 r+ o* L) W
. Q8 `6 M! g6 T: x3 j
// Then we create a schedule that executes the6 B1 q0 y! d5 |. N( b3 a4 g# b
// modelActions. modelActions is an ActionGroup, by itself it
a. A: S7 W5 a- V+ o+ t! y. j // has no notion of time. In order to have it executed in
7 g3 g9 e+ C3 j+ R3 ^2 j // time, we create a Schedule that says to use the3 O0 r7 [- ^7 g
// modelActions ActionGroup at particular times. This7 G m. Y R; e7 x) R' @
// schedule has a repeat interval of 1, it will loop every
% J6 D( Z M5 E // time step. The action is executed at time 0 relative to. ?& b p" `0 u) ?4 b4 Q5 b
// the beginning of the loop.
- Z2 N: j6 Q4 c, j* Q" c: h% c" U
7 k3 c) U% C C& Y // This is a simple schedule, with only one action that is
3 W) D! h( z3 J& p2 p8 Q- e+ K# r // just repeated every time. See jmousetrap for more; T* @5 x+ F- I I
// complicated schedules.
; B4 f3 X0 |) F0 O 8 ^) Z7 U" d# [/ N7 H( I
modelSchedule = new ScheduleImpl (getZone (), 1);
& w$ U) R' k ?/ d. O/ N modelSchedule.at$createAction (0, modelActions);
7 v" ^8 q: ] m: W0 i3 t. {0 ? . @. o8 {; o9 R* B' ^0 ^1 }
return this; F) u: d) z7 R6 i; O2 y% u
} |