HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% \# _4 A# y; C: Y3 |' G) p/ r
' }& i+ v( F3 Y8 f, a! g# Z public Object buildActions () {
: \0 F+ O+ l2 Q. w; M6 d+ j( m$ B% e super.buildActions();! O& N6 z$ z; l: o7 e+ J; Z
7 c1 k) G7 _6 X. ?1 l
// Create the list of simulation actions. We put these in
( n( ?& e8 i: R: ^$ B3 [1 }5 t6 K // an action group, because we want these actions to be0 C! G6 H% C% G
// executed in a specific order, but these steps should
; J- p1 |2 k6 G: m8 r+ H# L // take no (simulated) time. The M(foo) means "The message
7 z/ |: n3 W: }1 f- z! o8 w+ R+ h // called <foo>". You can send a message To a particular. e/ g% @3 N# ^) R# |; W h
// object, or ForEach object in a collection.0 K& U/ j- a: r7 {- l+ l
& B4 }( M; P3 @. v
// Note we update the heatspace in two phases: first run
3 D {8 m3 f5 P // diffusion, then run "updateWorld" to actually enact the9 T/ M s( s/ }0 D' @, `
// changes the heatbugs have made. The ordering here is7 c: K7 C* c2 ~# d+ H* U
// significant!
9 n& p5 f% s! Q' u2 b7 |' i , V, M" E/ I, o2 Y
// Note also, that with the additional( Z6 m- Y6 i9 t4 `- p
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 n! o } I g+ U% Q8 r, b* x
// randomize the order in which the bugs actually run, C5 m- m+ X2 I* o- a. |
// their step rule. This has the effect of removing any
7 k8 D: @6 }+ @( {. X" _+ g8 Q // systematic bias in the iteration throught the heatbug
& F# o0 S& R2 N8 U/ {$ ?+ ]+ D: a& j // list from timestep to timestep) G! G7 J' ^/ s" o( k
+ ^7 a. }; C/ F5 c Z9 y/ s // By default, all `createActionForEach' modelActions have
" R+ J7 U5 a( B // a default order of `Sequential', which means that the8 @) f. p5 t5 A' G
// order of iteration through the `heatbugList' will be
; Q s- h6 v# T7 R" K& @$ @; C) D, ^ // identical (assuming the list order is not changed
$ b9 x1 k' J: q( m/ k // indirectly by some other process).
0 r2 D! U" i5 B: c; R6 w
5 G2 e, I' _! M+ c' g modelActions = new ActionGroupImpl (getZone ());
/ {! A1 ~$ s. [$ }6 r7 T. k6 T$ K5 K' i o, G. W
try {! x& w6 G2 b# A/ `9 b1 \- T
modelActions.createActionTo$message
0 X2 z7 e8 i7 E2 P- S (heat, new Selector (heat.getClass (), "stepRule", false));; g/ y2 C; |7 N3 V# G: a8 v( |3 }; g
} catch (Exception e) {' |% ~/ F( U, Q$ ]& F7 e) ~
System.err.println ("Exception stepRule: " + e.getMessage ());) h2 A" ]. `( N0 j3 p" F% ?
}8 y7 c! g5 w# v4 K7 Y, ? h
1 E7 e4 e7 v' K9 n7 L Y
try {
& `2 H0 J) C7 }0 \$ |& I Heatbug proto = (Heatbug) heatbugList.get (0);
G7 R2 I3 [2 C8 M! b, N Selector sel = : y# z; D& T% p# |
new Selector (proto.getClass (), "heatbugStep", false);$ f6 B! W$ T/ N8 z, n$ T6 l
actionForEach =
) E, n$ K0 ^4 Z: g ~; l$ A modelActions.createFActionForEachHomogeneous$call; F! E( T3 l' ^9 R/ p$ u
(heatbugList,
# U7 n! X6 Z8 q8 W- o new FCallImpl (this, proto, sel,
2 ~4 ?( k0 h$ q9 v# V2 r4 Y new FArgumentsImpl (this, sel)));
0 g0 ~6 H& R7 Z0 c9 J/ z } catch (Exception e) {
% A, P' R7 K' s" E$ p& q e.printStackTrace (System.err);! s) q; o5 p$ g8 X% [4 P
}
% W, H- a/ x* q, ?7 \ `6 j3 D' ]& p% {' {- u) O [5 O8 r p
syncUpdateOrder ();; T( {# J2 U6 z5 J( m* ^+ q7 o' }' P& |4 H
) Y8 ]! H8 `9 I: t- O+ ^
try {* x% x9 U, W% Y1 q' S/ L) |
modelActions.createActionTo$message
# T& P' o0 _: |2 Z (heat, new Selector (heat.getClass (), "updateLattice", false));
' ^/ X5 v( b$ m+ ] } catch (Exception e) {9 T5 k5 j. G0 q9 a# M% H; H
System.err.println("Exception updateLattice: " + e.getMessage ());! Z, X2 ^: g8 b. q7 G
}
) U5 H( S5 H2 E. K$ [
+ O! |, R2 h! m" C // Then we create a schedule that executes the+ w$ [% j/ a2 F3 h [" F. I
// modelActions. modelActions is an ActionGroup, by itself it- E; d1 J6 A+ n' _
// has no notion of time. In order to have it executed in4 J. O' @# F* q4 |
// time, we create a Schedule that says to use the
: I$ Z( ?: q+ @( J3 y // modelActions ActionGroup at particular times. This. P. s- ~, A8 `: F+ D
// schedule has a repeat interval of 1, it will loop every' F- L+ K5 ?; ~& o9 J
// time step. The action is executed at time 0 relative to5 }# p# {. e4 y" v
// the beginning of the loop., t$ _# a2 h) u8 O
( R) K5 p+ J2 p& o3 Q
// This is a simple schedule, with only one action that is
) t/ S C% n+ Y9 [# r // just repeated every time. See jmousetrap for more, L1 \; m' N% l# [. d1 ^
// complicated schedules.
# p! Q9 Z2 k: T% i L( m4 `
9 t- i7 _: d% ?7 D3 G& }1 t modelSchedule = new ScheduleImpl (getZone (), 1);
. g5 n* t, ~8 p4 h modelSchedule.at$createAction (0, modelActions);; ~3 s! |/ M" S. F$ P$ \6 ?
3 ?/ j' l D8 j" Q' x return this;
4 a/ f/ E: l- K \1 |, L" d4 K. g } |