HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! T+ W9 T, U" @4 d1 M
$ ^/ c5 D% A8 @5 N public Object buildActions () {
- ~% @2 k( N1 B2 c9 w1 g5 o0 I super.buildActions();( o0 W2 u; X- F5 l
8 p" M8 d! J( f. u0 N5 L // Create the list of simulation actions. We put these in( K% p5 l" ?7 A' ~9 X% R
// an action group, because we want these actions to be: a# J$ j) m/ c9 G
// executed in a specific order, but these steps should( }4 D7 E2 E( e
// take no (simulated) time. The M(foo) means "The message+ f$ w% z: P9 u/ E( w
// called <foo>". You can send a message To a particular
6 f1 _3 N4 E' s+ s: ~3 U4 f" W' e& b a$ G // object, or ForEach object in a collection.
4 C6 i5 U* r0 o- v: ?, t4 E8 |( E & [8 p- y7 v: ?3 X
// Note we update the heatspace in two phases: first run9 v( _+ D, M) P/ o- h, D
// diffusion, then run "updateWorld" to actually enact the, a: \6 E% R) f( p7 a
// changes the heatbugs have made. The ordering here is7 ^$ {2 j; y4 K% {& @1 d
// significant!! V0 t, g# Y* x8 x: b5 l
5 K' c- y7 H% G1 B: s
// Note also, that with the additional8 r3 }; x+ ]8 ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' K1 N9 b7 D) G' y. `+ r0 e // randomize the order in which the bugs actually run
. n1 w! W2 q# ]* E% C& Q7 D \ // their step rule. This has the effect of removing any# R/ |% N1 f! r6 _; e; e0 ~1 E
// systematic bias in the iteration throught the heatbug% W# L; e6 i% G0 N. t$ s
// list from timestep to timestep+ K. i3 b% s I+ c8 B/ L H6 s
. D! v% h. L7 \. B; y' ~) X( ]; Y // By default, all `createActionForEach' modelActions have& V5 A9 [9 @: ]: u0 K
// a default order of `Sequential', which means that the
% K( H% J6 a" d- w% V/ q/ h# x // order of iteration through the `heatbugList' will be! Y, r; m# f1 {; t* L
// identical (assuming the list order is not changed
8 X" A$ E1 T4 Y! ?. S4 G' ~ // indirectly by some other process).
3 {9 U( O- m0 n# A - G7 a& p ^4 R6 y
modelActions = new ActionGroupImpl (getZone ());
2 z! N3 }0 o' F; C; ?7 @
1 Y$ H! c0 h1 _# p) Y try {/ B4 I; g" _& \+ G- B% {
modelActions.createActionTo$message0 _2 ^: y8 T, X6 i8 v, l$ J$ k2 t
(heat, new Selector (heat.getClass (), "stepRule", false));$ |% c" O& R, _* ]' ~: B
} catch (Exception e) {
& v( Y4 [& P( {3 W" m System.err.println ("Exception stepRule: " + e.getMessage ());6 w: S9 ]! @3 ~8 V. x6 ]
}9 Q4 Z9 _# t3 d* k3 u G
- N. G. V! E% M7 K4 j: E( Q
try {
, K+ V$ b0 D+ f a/ }0 u" [ Heatbug proto = (Heatbug) heatbugList.get (0);
6 f" O/ a* d/ n9 V3 z$ h' L Selector sel =
& N2 D0 [5 E: O# S new Selector (proto.getClass (), "heatbugStep", false);
4 o# K0 k/ i, F actionForEach =8 V# Q: V2 O. `+ d- ~! Q7 M e+ E
modelActions.createFActionForEachHomogeneous$call; W+ R. D. H/ ~* |, Z7 @6 g6 S2 N
(heatbugList,5 u; Q$ f4 U, w. P7 O, _
new FCallImpl (this, proto, sel,: n/ Z: ^ }' v0 G/ F
new FArgumentsImpl (this, sel)));
7 Y7 v3 N' k& j } catch (Exception e) {
' w& B; R& k, [- P9 C e.printStackTrace (System.err);
p3 Q3 J# }; D h }: R, r: u- W" n) Q
$ A' ^5 P6 Y. }) t# i. M$ O5 {
syncUpdateOrder ();; H4 F- s# ]' z$ @0 ]3 n
' I! c( e+ _0 D- @ try {
0 F7 d. U" p( | Q: | modelActions.createActionTo$message : y+ s1 \) E7 Z; ^& s2 p1 }
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 j# d- E8 f7 K Z4 J R" N } catch (Exception e) {
" B) I$ M [) n( C' Z3 [/ t6 P4 Y System.err.println("Exception updateLattice: " + e.getMessage ());
4 ^! e' f6 F9 M0 Z O; F0 u, i }# L8 @5 r7 D$ X: e8 b. j
2 a- c9 n# M4 L$ @ // Then we create a schedule that executes the }: M, p9 V9 }: U4 |; ?: f) Z
// modelActions. modelActions is an ActionGroup, by itself it- `; c, Z- b: c/ t
// has no notion of time. In order to have it executed in
: p; z/ L- g* T6 A9 D9 ?! q // time, we create a Schedule that says to use the8 P( U' _. A8 O g# T: I
// modelActions ActionGroup at particular times. This9 Y3 P- s7 u) H8 j. B/ X% O' {
// schedule has a repeat interval of 1, it will loop every$ G. f" o7 z( I
// time step. The action is executed at time 0 relative to
~: F9 ~4 v- N // the beginning of the loop.
) |/ D! Z% n% C* N3 A' V- v
( _- O% U/ o" \. [" R // This is a simple schedule, with only one action that is
: a6 x. u2 l9 P# {" {/ N // just repeated every time. See jmousetrap for more0 _$ V+ o5 u0 q4 F q6 b. @
// complicated schedules.
. O4 m! \- ?9 S% k* K" @6 C+ @
! o! _/ Z% x7 ?- F( D$ c modelSchedule = new ScheduleImpl (getZone (), 1);* V; ^# w$ L8 o1 a/ X1 ?- h4 ]
modelSchedule.at$createAction (0, modelActions);
% `3 M( y. @" l& z+ y5 B
, j5 c3 p9 M4 I, O$ P return this;# t3 _ Y6 F" N) y, M( i( b+ a
} |