HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 V7 H. \. e5 \$ z' a
, o2 @$ A8 w; L7 O2 j9 g8 F U public Object buildActions () {
5 ^0 ]4 K; _( w' B* ]) p+ ~ super.buildActions();
8 D5 e9 d* u4 w
; m% h7 W- d; [% N; D // Create the list of simulation actions. We put these in
9 N% U. g: S; ~, _ // an action group, because we want these actions to be
3 W* T& _* s+ v$ S% ?4 c, g n // executed in a specific order, but these steps should
( J8 w9 c% k4 E4 \( j // take no (simulated) time. The M(foo) means "The message) V% M, V) q' J5 C& I! y% @
// called <foo>". You can send a message To a particular
# }2 G4 w# V" t" d. U4 H4 C6 n3 n( E // object, or ForEach object in a collection.& J+ T& E1 t9 r
. g. D/ u+ ^1 w
// Note we update the heatspace in two phases: first run
8 T6 M* J; F4 ~& ~' ~ // diffusion, then run "updateWorld" to actually enact the
- U- \# ~( u1 L& h. w8 r( L // changes the heatbugs have made. The ordering here is6 W% L/ r3 a* D/ q
// significant!$ J' |/ d9 F. U, {6 W2 r7 ^% Q
, e8 [/ O, d' I7 w, W, T" ~8 y: a! F
// Note also, that with the additional( i4 A' U+ ~3 d- p6 R0 \" m
// `randomizeHeatbugUpdateOrder' Boolean flag we can. F" x$ B1 Y) X- O' k" T6 }
// randomize the order in which the bugs actually run& ?: ~- i. G9 v5 H8 ^- Y1 T( r& ]1 u
// their step rule. This has the effect of removing any/ K r) ~9 O3 U6 y
// systematic bias in the iteration throught the heatbug
+ Q% H4 r. X e; {6 P ~* @ // list from timestep to timestep
% @2 e, ^. J: i. Y
: l) Z: r/ K7 q$ |3 J. J // By default, all `createActionForEach' modelActions have, f5 ?6 o& m! u5 k& m3 h2 F+ g2 z5 t9 |
// a default order of `Sequential', which means that the. [2 b) r6 R: ]( r
// order of iteration through the `heatbugList' will be0 v' {/ k" W4 e4 f3 {) }/ }8 m
// identical (assuming the list order is not changed
$ e+ j1 H, g, ]$ \$ |4 y // indirectly by some other process)./ x6 k/ O( u" Z$ R
. X, t0 m" d/ l( U. K7 A0 X* V% g
modelActions = new ActionGroupImpl (getZone ());" [9 R+ M# r( V2 z& l
% C. p9 m6 ]0 J3 m5 x- i; R try {
' X8 D& n) j+ ?" y% _9 Y1 T+ l& y" u5 O modelActions.createActionTo$message. }! q4 L/ G+ B0 t
(heat, new Selector (heat.getClass (), "stepRule", false));
5 R* ?2 p+ o; U/ P4 G } catch (Exception e) {6 H+ u: x( R4 ?) }
System.err.println ("Exception stepRule: " + e.getMessage ());
. V7 Q( z& o2 `- o& H% ] }- }3 i8 F9 ?2 V, N
: ~. Q4 N: F1 Z
try {
1 H) B$ S6 `% a0 M; g Heatbug proto = (Heatbug) heatbugList.get (0);4 M& K' n' S8 o! {
Selector sel =
. A' z/ k" \0 c4 b8 L3 o new Selector (proto.getClass (), "heatbugStep", false);3 D* g- Y; {/ ?& f5 |4 e
actionForEach =
! ?; F7 U0 C0 ^4 d9 Y4 t N modelActions.createFActionForEachHomogeneous$call
: l4 ^, c* t6 Y. ^9 j (heatbugList, _; ^" X1 F: c- X- X1 s) G$ x) w
new FCallImpl (this, proto, sel,) o% K% W, r' U% Z0 E4 f
new FArgumentsImpl (this, sel)));
. }* O) Z: W: k5 d' N7 [3 D3 Y" s } catch (Exception e) {
& R2 h0 ^7 F. q e.printStackTrace (System.err);9 K2 Y4 j0 k3 w2 {) R7 A9 G" J$ d
}
: ]2 P- o5 f3 d- d! I' b2 H M
# U8 c/ W+ V! h- h/ E! O* v# K* T5 l syncUpdateOrder ();, g# N! V( T- n; w0 n; s
5 P0 l% D0 I0 U try {0 j0 G6 u U* {) T; ]
modelActions.createActionTo$message
9 s1 v: [4 u& G" o' g! x! o (heat, new Selector (heat.getClass (), "updateLattice", false));
! m5 i9 q( Y/ l: { } catch (Exception e) {/ K3 m0 o8 e4 _6 Q" N- N q4 q J: \- P
System.err.println("Exception updateLattice: " + e.getMessage ());
# J% r% N, G% q" L4 v, y3 }9 ^ }' v% B1 Z. g& Q4 Z: y2 ]/ ~
% e0 Z! z( C+ z, d. q0 T // Then we create a schedule that executes the/ D6 c9 T9 k9 l- B. ~
// modelActions. modelActions is an ActionGroup, by itself it
7 v' p7 _* J. R( F: q // has no notion of time. In order to have it executed in& {, L4 O" O) }6 ^& `( \
// time, we create a Schedule that says to use the
8 Z/ v M% [' U. `8 w: h- }2 h; ], q // modelActions ActionGroup at particular times. This$ c9 F" z# H: w' r# j4 l
// schedule has a repeat interval of 1, it will loop every
i6 f- v1 M. g% B) N // time step. The action is executed at time 0 relative to
5 P, J% d& A. G0 ~5 t3 E // the beginning of the loop.6 O' k1 K0 q' p/ J9 L6 Z& b
8 m! o/ e+ T+ ~( V' v( [& F
// This is a simple schedule, with only one action that is
( ? g0 M8 g a& c // just repeated every time. See jmousetrap for more4 f4 N0 C9 c: o" X5 K' X
// complicated schedules.1 |" h- Y; v8 F( f4 S; x
, G( }4 U( u+ u& E' p w modelSchedule = new ScheduleImpl (getZone (), 1);
/ C! H2 F1 R7 p( u* ] modelSchedule.at$createAction (0, modelActions);
& \! J0 e* V0 [2 N+ K, j# ?
. v1 L1 G9 n; A return this;
2 n- f) O/ c* l } |