HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) s) V0 Q5 v. F2 O; X( Z p- u Q
public Object buildActions () {
) Q5 t) t' I o5 {; N0 w super.buildActions();- z; k3 J$ ], I+ T
0 P1 C% R# @$ a2 G' i // Create the list of simulation actions. We put these in
% i4 b0 X) |' H# U% l( B. |& O. ~ // an action group, because we want these actions to be! I6 I7 b4 G6 b
// executed in a specific order, but these steps should
! @/ T! w7 A7 j // take no (simulated) time. The M(foo) means "The message
1 j- o; Q8 U1 R2 t, ~% l // called <foo>". You can send a message To a particular# I$ i0 W* X9 ^5 e# s% E* r
// object, or ForEach object in a collection.
" _+ E5 A4 g' T' w: s' |0 N
2 p) }% W! s, R! a // Note we update the heatspace in two phases: first run! F# \1 Q( k$ B; r) R
// diffusion, then run "updateWorld" to actually enact the
( H5 c, B6 h! Q" j: u // changes the heatbugs have made. The ordering here is( Z% a0 g% U' E# p/ x6 e& X1 `/ }4 W
// significant!
^1 ?. u& |+ @" u" u/ e( I 9 P0 p* y, ^3 _. p1 ^% _
// Note also, that with the additional8 V3 T* R7 E- D3 C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 F1 n. Y6 ?- |. G3 n // randomize the order in which the bugs actually run
& H* I2 I8 G" T) v // their step rule. This has the effect of removing any
* \# ~: r1 \( q+ a2 L5 B5 Y7 F6 s // systematic bias in the iteration throught the heatbug1 M( i! E8 y( \2 q& a4 A
// list from timestep to timestep
1 y' V& o, Y8 `3 ^& y9 r; ^( ?
+ `! H& {+ q7 | // By default, all `createActionForEach' modelActions have: A5 z4 f; d0 q1 G
// a default order of `Sequential', which means that the1 t# S; S+ Y( g
// order of iteration through the `heatbugList' will be
' X/ _/ \" N# h- Z5 T; e4 N5 s // identical (assuming the list order is not changed
8 r9 c; `7 Z; E# Y0 Q7 P( l7 ? // indirectly by some other process).1 o: p: R$ G/ V! \* k$ C! B
" A5 i& P/ N5 v5 ^3 R% O9 y modelActions = new ActionGroupImpl (getZone ());
5 t* H3 k+ d! F( n( E1 I1 A1 _5 S* h1 X* L. [) v; ^
try {2 a- V& R1 W1 Z4 T2 O7 }
modelActions.createActionTo$message* z( {) g' @/ v8 u( n) X' q* J
(heat, new Selector (heat.getClass (), "stepRule", false));
/ m# E+ y6 P) g9 M) E& t8 v4 i } catch (Exception e) {
3 J5 v& t* b+ H' d1 e y System.err.println ("Exception stepRule: " + e.getMessage ());
- ?! L B3 Q( C4 [# J+ ?1 K }, S$ T5 y6 H [: ^
6 G& Y+ y4 s3 }* K" } try {, @8 N. t3 e% K. K* n! L4 t
Heatbug proto = (Heatbug) heatbugList.get (0);
: \# e* B% H4 q8 G# O Selector sel = % E: E3 i! {; m- F" [9 y
new Selector (proto.getClass (), "heatbugStep", false);
' P# o; R, U i/ @6 P actionForEach =. |2 e& e, H, H, D
modelActions.createFActionForEachHomogeneous$call
3 p2 {5 j4 ]# J (heatbugList,
! |. o# j J0 `5 ^) l+ T new FCallImpl (this, proto, sel,% \) S. s, L- W6 \# c b. D
new FArgumentsImpl (this, sel)));
4 m$ Q8 {! Z- c2 V3 M. V } catch (Exception e) {
4 ?2 i: }! p+ S" G+ O e.printStackTrace (System.err);7 y% B8 o1 R' {5 @0 n0 h
}% ] D1 u9 x5 v" {5 J9 K
5 E% Q( l' P* [! q3 S
syncUpdateOrder ();; u; Q9 l# W# f# a
& C; b9 m, t1 ]' Q9 e) [
try {3 ` x% ?+ n4 H
modelActions.createActionTo$message , j" ?/ ]0 `, J$ y( U, S; J5 l
(heat, new Selector (heat.getClass (), "updateLattice", false));
: W a9 H$ ?# }0 R8 K; h5 G+ l* l } catch (Exception e) {
) b% [8 t. `; I7 F% o& R' m6 ] System.err.println("Exception updateLattice: " + e.getMessage ());
: _0 c) }+ c/ R) L% [ }
: P0 `$ R6 w# w; I" a/ B& C 4 M/ O, R# I% Y% ?: P3 J
// Then we create a schedule that executes the8 `- R7 j7 A% M+ J! ^. s9 n2 U
// modelActions. modelActions is an ActionGroup, by itself it6 m- V/ @3 s% Z, G0 d/ w3 ]: A
// has no notion of time. In order to have it executed in
" N6 r" a w0 t. L // time, we create a Schedule that says to use the# T2 v4 S" w7 e; y
// modelActions ActionGroup at particular times. This
9 Z& } {! F% D8 Z c o! N0 c0 h. ` // schedule has a repeat interval of 1, it will loop every% G4 P* T6 e* Z Z t8 J! w, M. F
// time step. The action is executed at time 0 relative to
1 t( v3 Q0 N# s8 s4 L" j, f. f // the beginning of the loop.
# D7 x$ j/ A2 e6 |' l' X) h
( J' C) R0 r$ b! j5 Q* c0 a // This is a simple schedule, with only one action that is
+ Y$ C z. k7 o8 G8 A8 y // just repeated every time. See jmousetrap for more
0 J. A# o3 ~: n8 P // complicated schedules.
& D# t' ~7 `' U8 W0 r" S0 J% x' \) F
. y- n3 g5 l+ V( `# l modelSchedule = new ScheduleImpl (getZone (), 1);
; d8 u: e2 @' G9 x5 g5 G3 k; P modelSchedule.at$createAction (0, modelActions);- T: T9 W# i) \8 ?
8 _8 o1 n. k# {4 s5 X. ~
return this;2 X/ @$ i7 C+ @ t; [9 N; H2 {( j
} |