HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% ^/ s, Z( \4 M- |! C2 I
, ^4 T6 I3 j( I" H3 V public Object buildActions () {) A6 y4 R D1 V8 d+ V
super.buildActions();
+ w. G0 b4 o& u5 e% X & n5 Z7 j9 D- ~
// Create the list of simulation actions. We put these in) ~& M- N# v5 u; b9 [2 X
// an action group, because we want these actions to be% c6 b) I5 ]$ a/ C0 ~
// executed in a specific order, but these steps should
: K. N$ i7 D0 N) @ Z; w) t // take no (simulated) time. The M(foo) means "The message' y' m$ m. B+ n$ {& l9 ~
// called <foo>". You can send a message To a particular$ w5 l# D/ n9 ~ E: E3 f
// object, or ForEach object in a collection.7 l) n( `0 _5 l( m4 ?' k9 u) q* w
5 D2 W4 O z% q# I
// Note we update the heatspace in two phases: first run% ?0 `- d3 G; G _% c& c
// diffusion, then run "updateWorld" to actually enact the: N2 f! u |. i7 c# e
// changes the heatbugs have made. The ordering here is
5 [ S9 X" N; b // significant!
9 r0 S- J+ G$ E9 k$ p1 [8 F9 W
" h4 O \7 W1 @* f+ Q7 Z& h // Note also, that with the additional' B7 ^& D- t; S; J2 ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can" h5 M+ D+ o- V( n- I5 \" `
// randomize the order in which the bugs actually run9 Y2 |: l# L; C4 G% j
// their step rule. This has the effect of removing any
q( S, z" s9 P$ h) z9 h4 z // systematic bias in the iteration throught the heatbug% {6 W/ |1 {. ]8 c
// list from timestep to timestep
9 q; ~! a' A7 j# Q0 C: W
' D' Z* e& N- M' H5 W, d4 T // By default, all `createActionForEach' modelActions have
% H# c" I- p/ ~0 _+ Y6 P/ { // a default order of `Sequential', which means that the
" G2 s7 e$ o$ A; H* [ // order of iteration through the `heatbugList' will be0 [* a; k& L R" I0 K1 v9 l4 R1 [; H
// identical (assuming the list order is not changed0 w0 v" `% ]) E" k) P1 w4 G
// indirectly by some other process). L/ P U3 n7 m' h; o) a( l5 D
4 l" R6 |+ J* _5 x4 H2 G# x
modelActions = new ActionGroupImpl (getZone ());% k2 Y9 P, d8 W4 k) q$ ^* W, @6 N; U W
8 ^1 k' P% u. r6 S$ D9 O n2 k try {( S* e9 P7 _9 g3 R3 B, b) i
modelActions.createActionTo$message1 b, R$ T! i% s
(heat, new Selector (heat.getClass (), "stepRule", false));
d }; L" p# |& W } catch (Exception e) {5 W t, E2 v0 Q- }. H' U' p
System.err.println ("Exception stepRule: " + e.getMessage ());
# U! x9 i L2 r1 \; f6 E6 v) ] }
: K. ~ T# q! O2 ?1 F% D7 u
; }: J) `- T$ i$ E try {
& m0 \( f. ] p9 N1 E8 q r6 E Heatbug proto = (Heatbug) heatbugList.get (0);7 k/ T% y3 U: i/ }6 x# c% Y
Selector sel = & W) z7 q1 }) b7 L5 U
new Selector (proto.getClass (), "heatbugStep", false);# e5 u6 P( k, w+ a: r. V
actionForEach =
" }$ n) ~) z3 o modelActions.createFActionForEachHomogeneous$call- D) Y& }/ \0 Q6 r
(heatbugList,/ v( n, y% x3 [; D0 Z* ]
new FCallImpl (this, proto, sel,
: @: `; s. `5 M9 i% w7 }/ x9 Z+ C new FArgumentsImpl (this, sel)));: z7 c- D* Q3 _/ s; H. j5 q0 g2 g. h: M
} catch (Exception e) {
/ f. r* h* \9 s e.printStackTrace (System.err);( z5 O8 [- |5 y7 }! I. I
}
7 \8 X( e( {% Q- o- B% X; ]5 U& ] 1 U" s! n7 K7 v/ ~' E. k
syncUpdateOrder ();
5 V2 [3 ? h+ a; v8 y) u
7 C% T7 `$ d, t, ]: X9 ^, Q( R try {
7 h" c4 p9 Q) k8 d1 s modelActions.createActionTo$message : J* p8 m" V0 h, J
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ Q& w0 t& Y3 a2 ~ p# n% E! l } catch (Exception e) {# k# F: w* P8 B" m$ f2 u
System.err.println("Exception updateLattice: " + e.getMessage ());2 P$ a' S! G+ Y
}6 ^# |; |, f _2 \1 L; ?
9 O$ N' v: L) ^6 A4 p
// Then we create a schedule that executes the o& p* X8 F' F- H; x
// modelActions. modelActions is an ActionGroup, by itself it7 l" q( a5 A* ?8 M& T
// has no notion of time. In order to have it executed in4 m2 k6 q. m J9 }3 v
// time, we create a Schedule that says to use the
# I0 {/ @) [/ M# k* a( i7 z // modelActions ActionGroup at particular times. This( w1 d4 k) I2 Y! u5 |
// schedule has a repeat interval of 1, it will loop every" p- Z9 _7 e+ l: h7 T# d3 h3 p& `
// time step. The action is executed at time 0 relative to
$ M+ f7 q, J6 \) b; H6 Y8 g" J // the beginning of the loop.0 e7 o' Z4 c" l2 A
/ [2 G! o O+ H% ^/ X
// This is a simple schedule, with only one action that is
1 M/ o, m. A7 t* m // just repeated every time. See jmousetrap for more
. W/ i; {1 p n. A" M! j$ J3 q, u // complicated schedules.
/ a" p( ^% T# [0 l# z3 e* e ' h6 {6 G. N+ h# T, A" K
modelSchedule = new ScheduleImpl (getZone (), 1);
0 ^+ K/ A5 w: J% Z3 z8 ? modelSchedule.at$createAction (0, modelActions);0 c# f4 H |; ]+ g; I2 f
6 L2 N! b+ P9 \3 n
return this;
# y" |" X. A0 V2 o7 d# S } |