HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 z' X7 u6 G- Z' Y) K, T# f W
/ D1 f5 S8 a/ R+ ]9 T% a
public Object buildActions () {& L* Q2 }6 l. V4 E+ ^6 W8 ]
super.buildActions();
$ N, v4 @0 u& I 3 b( y5 |% @3 J2 s( M& T( _
// Create the list of simulation actions. We put these in4 }. e8 v. L6 O* {
// an action group, because we want these actions to be3 D0 E# y% k* j% I% ?
// executed in a specific order, but these steps should% e7 o: U, b0 K4 V
// take no (simulated) time. The M(foo) means "The message
* U/ n( c& x$ I+ G1 L // called <foo>". You can send a message To a particular0 q1 a" q ?6 t5 c2 X( |2 I
// object, or ForEach object in a collection.
% R, C& j+ k- u# [0 ]
5 p4 U3 C; a% o5 c // Note we update the heatspace in two phases: first run' m- z( m4 t# M) X; k; Z% r7 S3 F# N
// diffusion, then run "updateWorld" to actually enact the" Z7 m: I; V6 B
// changes the heatbugs have made. The ordering here is7 m1 l1 h8 d6 Y
// significant!- p0 O* Z2 h" b3 A% V9 c9 T! h
0 h+ @- {1 |) m. [ // Note also, that with the additional" [; ]. u0 `0 r- u
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 `: q; s- r3 E! O // randomize the order in which the bugs actually run+ J' A5 f. q+ g M: A& W
// their step rule. This has the effect of removing any& i2 A7 U I1 A! ^0 h+ f7 l; U
// systematic bias in the iteration throught the heatbug
( t2 Q% z" l% y0 U( Y* ^- K$ N // list from timestep to timestep
* S$ S6 Y/ U6 R2 L$ @! y, j* n# J8 l$ f
5 u6 g) O; Y1 s- _) Z // By default, all `createActionForEach' modelActions have
" m+ A( J, u8 A5 n& S( K // a default order of `Sequential', which means that the$ v6 r6 o/ l2 ~) M* P
// order of iteration through the `heatbugList' will be
8 K' o4 ^( [. P // identical (assuming the list order is not changed9 k+ R; x6 A- @/ ~6 g& C
// indirectly by some other process).
" `1 X/ N8 D# T! a! T+ i0 J
3 o1 @7 E4 y9 s6 h! E modelActions = new ActionGroupImpl (getZone ());) Z% O% _+ a9 C( S" G
; S4 F' g2 y( @3 ? E: Q# } try {
d2 \, @" c9 K, C modelActions.createActionTo$message( E6 X6 s9 K# U, u
(heat, new Selector (heat.getClass (), "stepRule", false));
5 w0 L& n# G# \) }. Z" M1 V$ i } catch (Exception e) {
8 l9 ~7 V- Y6 P# L$ I System.err.println ("Exception stepRule: " + e.getMessage ());
0 Y: f+ V- K8 t- @1 y }
& ^+ ?- e# w. q8 U* }2 f
7 z4 O6 ?7 p/ s8 C try {
6 ^- D1 p) T! ^, h% T Heatbug proto = (Heatbug) heatbugList.get (0);4 `/ M2 l a9 {3 B3 N
Selector sel =
# x" ^2 q. x2 g8 R new Selector (proto.getClass (), "heatbugStep", false);6 ~) k; x8 Y) u+ `. @+ Z% f
actionForEach =
( d( ^: @! R6 J8 ^8 m l% m8 C modelActions.createFActionForEachHomogeneous$call2 E5 ~: A$ ~- s+ d4 N& ]6 w; [
(heatbugList,+ y/ j3 M( v( Y; w- L: O
new FCallImpl (this, proto, sel,
* S7 N; Z5 s" r5 z: @1 U2 k new FArgumentsImpl (this, sel)));! [4 y% R! `( l; b+ L
} catch (Exception e) {, l5 {1 J9 c( b/ N$ l
e.printStackTrace (System.err);
0 Q: E0 t' G2 @) c' C }
* Z' t V1 c; H4 R7 g% r ; l K; b# _1 L2 s
syncUpdateOrder ();
, T# E: {: |' t1 n; w7 i
! z; i- c6 L y: J: G try {
$ {% N1 `' p8 n; I0 z modelActions.createActionTo$message $ ^6 q: a2 ?, c) S" n
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ w, N) R' x" D- a; R6 U: Z } catch (Exception e) {# j+ f7 n' t6 ^/ p2 x0 V. m
System.err.println("Exception updateLattice: " + e.getMessage ());3 @ |* g# H/ W' k" B S) x# J
}
& i6 P) l- r: D# ?) N3 @& M4 s# W
+ f1 ]1 z* d# o9 _' k4 C- w // Then we create a schedule that executes the
$ u+ ~, F# F, r$ Y7 c // modelActions. modelActions is an ActionGroup, by itself it
- K8 b, D( |1 B0 f% f% \6 v // has no notion of time. In order to have it executed in9 Q2 @$ C" c1 w. K1 |/ t
// time, we create a Schedule that says to use the
9 x, q) D( t6 @ // modelActions ActionGroup at particular times. This( X! @/ M2 A" f9 Y2 P
// schedule has a repeat interval of 1, it will loop every
# a' s7 `7 c& C Q; X8 ~1 \ // time step. The action is executed at time 0 relative to
# Q( w8 S: W; G) I8 W# ?+ Y4 g // the beginning of the loop.
% S5 ^) }/ V* @% [# X( g$ E! U: n# x7 f. C# V
// This is a simple schedule, with only one action that is
: n( t! J d$ D // just repeated every time. See jmousetrap for more# b; M- P* X; h5 d; ~
// complicated schedules.4 R Q" m! \1 ~/ i. ~
5 c" K8 }% c C9 }) n7 c modelSchedule = new ScheduleImpl (getZone (), 1);
& C8 J& [$ r2 `& `, X modelSchedule.at$createAction (0, modelActions);9 x _* F" j% h
4 i- X9 \) }* ^5 a$ Y return this; ~3 [& \9 ]0 A; I0 A P. z& b
} |