HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 V; ?( D" ^3 M# T$ i- O8 I" z* P
: q8 w# P9 Z# s
public Object buildActions () {
) N( C, H6 c# [+ R super.buildActions();
5 _. j) v! l5 \( S/ d$ n& T " j# x0 }- }' }( Y$ |+ B. n0 J0 x
// Create the list of simulation actions. We put these in
5 Z) r. [! X) E) s // an action group, because we want these actions to be
2 C% d# ^( t, Y" M) ^, N$ k // executed in a specific order, but these steps should
1 @6 {+ G1 }% z // take no (simulated) time. The M(foo) means "The message* J" r4 X! @# k2 s- h( p
// called <foo>". You can send a message To a particular
) z. }" f, z1 K. e // object, or ForEach object in a collection.& W2 Z* F" K2 i# s
% P, E {6 D6 O7 Q' ? // Note we update the heatspace in two phases: first run
5 z& ^. g+ u8 r* M // diffusion, then run "updateWorld" to actually enact the
0 w/ K9 J( v/ g7 k4 [# z // changes the heatbugs have made. The ordering here is, M* t4 c5 {( n7 C" F, V, o
// significant!
3 H( a8 ?8 z1 ^: A% f
( m+ A a4 S8 `- M. _ // Note also, that with the additional
4 r8 y0 |, o2 W/ E" M // `randomizeHeatbugUpdateOrder' Boolean flag we can
" b. Q* o5 c& I/ N // randomize the order in which the bugs actually run
+ ?6 D! ]# f7 Z, ~; Q: ~1 _ // their step rule. This has the effect of removing any
: S0 E/ I( n ^( m! K // systematic bias in the iteration throught the heatbug1 I, y: u+ A, ~+ E& J+ @
// list from timestep to timestep
+ Z5 ^" G: f4 P8 ^1 d% c A# z9 W2 m' L2 Y8 g8 e$ T
// By default, all `createActionForEach' modelActions have+ w# A& c# Z; V$ l' C
// a default order of `Sequential', which means that the
# p5 B4 d& i! c l) r // order of iteration through the `heatbugList' will be
' R# k+ q/ B% P6 Z' B ~ // identical (assuming the list order is not changed
( N$ b, u4 _0 _3 A // indirectly by some other process).
6 {1 U1 X" _0 @3 I
0 x1 P9 |- E4 F! t9 l. U modelActions = new ActionGroupImpl (getZone ());
! O& {+ }0 R& T$ m5 p/ F! d; M5 ]! Q3 M) A5 C2 ?5 L
try {# X6 ~$ S2 b6 E' O
modelActions.createActionTo$message( N6 A) r \5 G. Y) d, s, _. a
(heat, new Selector (heat.getClass (), "stepRule", false));" O& D! K: N$ @* i) M- N
} catch (Exception e) {
0 }+ z2 O8 p8 E0 X( B C4 Y System.err.println ("Exception stepRule: " + e.getMessage ());. @: @# y" `2 F! |
}
) g7 C& l- s4 {, N8 M) b- R% D5 u* Q/ h0 f# _
try {$ o% U4 H4 j, a) U
Heatbug proto = (Heatbug) heatbugList.get (0);
& ^2 N0 M( o& o4 V: C$ y( y Selector sel =
& S# S6 v. t4 o4 s new Selector (proto.getClass (), "heatbugStep", false);5 P) L v- }0 v, P' k( J
actionForEach =: Z# L! Z, i; |8 H0 ^ o
modelActions.createFActionForEachHomogeneous$call0 I6 y8 d' E: g4 T. O- T% O. X
(heatbugList,
: V/ K9 b. J% b* y1 j+ O1 @ new FCallImpl (this, proto, sel,- e7 y: j( s! a8 B
new FArgumentsImpl (this, sel)));& y9 @1 P% j/ ^1 K9 \4 B4 G
} catch (Exception e) {
/ l) E3 y" [) n e.printStackTrace (System.err);* ?+ |$ i' ^# H, P/ z
}
$ w( u5 ?; u, P3 w
3 d) F- ?! Q* {( T b M& L syncUpdateOrder ();0 p& z, w" j: z: A
$ E$ C3 N" p' O$ e4 n) d
try {; _- z0 C% r8 w, J m2 @" s8 r+ T
modelActions.createActionTo$message
0 u! F2 s4 ?+ H' M D (heat, new Selector (heat.getClass (), "updateLattice", false));! c1 o6 u2 Q k& N
} catch (Exception e) {
" c9 A) Z& ?9 ~ O- ?/ J. j System.err.println("Exception updateLattice: " + e.getMessage ());
, ~: M2 C/ r/ C0 u1 y* T; I }+ c7 h; E+ n* K" T% y* y8 H
3 J1 |, u8 Y5 A8 z% L- z // Then we create a schedule that executes the0 a0 w, U) q. X( Y
// modelActions. modelActions is an ActionGroup, by itself it
+ I: u9 S" m' Z7 Y- Y% D1 D // has no notion of time. In order to have it executed in
+ I% @; z5 a: t/ r // time, we create a Schedule that says to use the: m, H2 d# i$ j6 c% K
// modelActions ActionGroup at particular times. This
; i+ z# P* ]' t% m+ Z) [2 ` // schedule has a repeat interval of 1, it will loop every0 {8 Q; g$ Q" m! W' J8 d7 B8 a
// time step. The action is executed at time 0 relative to
1 ?. z& m8 L6 I/ n // the beginning of the loop.
* S* e: G% E8 t) a, F. ] }' y- ^/ v9 I& b3 Z" o/ t
// This is a simple schedule, with only one action that is
/ z/ B+ N/ P* ?, v, {( @6 d/ q // just repeated every time. See jmousetrap for more7 o1 G% W/ N9 m) X( w
// complicated schedules.+ ?6 A7 R6 F" B& O# q$ z' Q( s
" s* T& ~* Q8 X" _: B modelSchedule = new ScheduleImpl (getZone (), 1);. D; L5 ^* h/ t6 d2 r4 ^1 b8 _
modelSchedule.at$createAction (0, modelActions);
! o7 N- S- c+ f* D6 g0 v
r+ ], K8 S/ I$ q% t/ X/ V0 Q9 G" @: E return this;1 i' l: m- |0 W9 f
} |