HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
U4 `# u0 A z* c g" r/ ^! o2 l
public Object buildActions () {
2 M0 l. q# b" x9 g+ D( e2 S( h& ` super.buildActions();0 d1 f. f- x# O5 D8 p
/ g+ @+ S" |6 g+ F+ F- j
// Create the list of simulation actions. We put these in4 Z9 l/ h6 {8 q
// an action group, because we want these actions to be( y; [4 D: l) z
// executed in a specific order, but these steps should8 r+ k4 ^+ F8 ^8 h! Z
// take no (simulated) time. The M(foo) means "The message
+ v3 b& S/ i0 Q( r" U // called <foo>". You can send a message To a particular8 p. F1 v& B* L/ b" _
// object, or ForEach object in a collection.
( q I! {) _. t: m 4 p s5 Z& v5 ~" w
// Note we update the heatspace in two phases: first run
" q$ r. g/ o1 q6 K [ // diffusion, then run "updateWorld" to actually enact the
. f$ k- \5 U6 q r // changes the heatbugs have made. The ordering here is
/ R/ V8 O# h; e. O3 n // significant!
. ~3 V3 [! R7 L
4 Y6 p+ t; `3 _2 l6 P' w // Note also, that with the additional! r7 Z( W9 A8 W3 r& [& g6 r
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 z: S" g- y! y" j& h, J8 t k- s
// randomize the order in which the bugs actually run
4 c t! n; O& O; f9 Z // their step rule. This has the effect of removing any- V/ M, s |: a
// systematic bias in the iteration throught the heatbug
2 K3 j5 \1 {# |+ {. p1 m // list from timestep to timestep
0 C0 T# P5 X8 v+ b1 p2 z$ v* C . |* @7 O a2 k! _
// By default, all `createActionForEach' modelActions have) g8 b1 d1 a* y2 T6 \( Z9 U- I
// a default order of `Sequential', which means that the
' u7 d% D7 ^# P' ~4 y! d // order of iteration through the `heatbugList' will be5 R; J6 P. `' X2 m4 F" q8 y# a
// identical (assuming the list order is not changed# \; w. d r. A+ _! K( X
// indirectly by some other process).5 b N7 d/ E L; K/ C
. R5 W! d, L8 N+ K( d. H: Z' O0 ]& M modelActions = new ActionGroupImpl (getZone ());
R H" g+ v$ }- |
' o& O" ^3 R& }: N% m try {+ E& C+ l1 t! T
modelActions.createActionTo$message" n1 L" N. b8 h; F7 ]* `
(heat, new Selector (heat.getClass (), "stepRule", false));
. B3 \/ C2 H+ S5 G( m3 W } catch (Exception e) {) i* j* y2 a) B, ?
System.err.println ("Exception stepRule: " + e.getMessage ());
- a0 k0 q- V, y% f$ d$ n7 c3 f }4 d7 ?) J ~. ]1 }
: Y1 m2 c6 C; G( K" n3 W( z
try {0 t- m( `- {; K; |
Heatbug proto = (Heatbug) heatbugList.get (0);
# B P; n7 C. ~8 _: h+ J Selector sel = 5 h5 `- W: K K6 P! F( `3 D
new Selector (proto.getClass (), "heatbugStep", false);
' ?; ]& H$ s: @3 H1 y0 c$ j actionForEach =
* ^9 f, ]; U! u% s$ k' s# r modelActions.createFActionForEachHomogeneous$call
& V' Z: h. P; [- _% O (heatbugList,
1 h$ T' y- M+ L! l' n& z new FCallImpl (this, proto, sel,
9 V2 O8 R S! n6 Z& `" r! U2 r new FArgumentsImpl (this, sel)));
2 S0 _: t" N* I } catch (Exception e) {
9 `% u+ P R! c e.printStackTrace (System.err);
0 r3 Q5 b; g0 D; L }
; u y h; Z$ k7 h; f : l0 E% V9 A% ?
syncUpdateOrder ();& N# ~; X7 S8 p) _
) [0 c1 f8 A' q! Z- K try {
. w! s. j6 F& j modelActions.createActionTo$message
" o7 ~4 T& \% v2 J1 N" H (heat, new Selector (heat.getClass (), "updateLattice", false));* F8 n5 K G2 n: S! H0 l$ e
} catch (Exception e) {
2 ^: B: G/ z% u) _- N9 N; q System.err.println("Exception updateLattice: " + e.getMessage ());4 N+ g& P8 Y5 I9 s( F
}4 [) N. d% G2 A' `3 C4 z- w
5 |' P/ ?, N8 I2 ? // Then we create a schedule that executes the
( t- U7 y0 l6 \9 A8 f* E( W0 l4 g" s2 \ // modelActions. modelActions is an ActionGroup, by itself it m# q$ `' w9 z" y
// has no notion of time. In order to have it executed in
( V( f/ v& k* _1 g. Q // time, we create a Schedule that says to use the
' g4 H6 R1 R) X$ }! v8 ?2 Z6 G // modelActions ActionGroup at particular times. This( M9 _4 \0 |* h! C! e) M0 E2 J/ {$ B0 v
// schedule has a repeat interval of 1, it will loop every C6 v0 u' I2 H+ o& @7 Q
// time step. The action is executed at time 0 relative to
0 }) X# M7 N; P/ |1 Y: P0 L // the beginning of the loop.
& W5 ~- H8 g1 t9 a- e/ `2 Q8 G$ X5 x* G+ }9 s( q( A" R
// This is a simple schedule, with only one action that is+ G' n( I% J) x5 L$ J. l/ E( ~
// just repeated every time. See jmousetrap for more% y. ], s$ v* m# f
// complicated schedules.
% J& M" Y9 e: ^; c; n
! R! h0 I: x4 ?0 T" M" | modelSchedule = new ScheduleImpl (getZone (), 1);% \ X3 ?; N7 i @* C% \
modelSchedule.at$createAction (0, modelActions);' e' K6 _) r% S# n' H
! n1 j0 |; P1 l9 d+ f N return this;
1 G B4 D& z, K. X& P7 @ } |