HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; @2 m% A. m. e: }
7 {! u/ ]& E* Y" ?9 ` `4 D" Q6 i public Object buildActions () {
) \& x4 w* K8 }' x$ v& \ super.buildActions();# B/ F; w8 @. S0 Z
0 o0 K" R' Y0 L' G1 J+ c1 E! c // Create the list of simulation actions. We put these in
/ I1 H* m6 j4 d5 j- k. R7 f // an action group, because we want these actions to be
7 y' ?- n3 {6 e0 }! O- ` // executed in a specific order, but these steps should
, \- b x4 I' P0 q // take no (simulated) time. The M(foo) means "The message6 c& \" T# J) d
// called <foo>". You can send a message To a particular& M5 Q a5 _9 ]7 d4 V4 |
// object, or ForEach object in a collection." R. b: ~( s& ]; Y; B
/ z7 B1 }6 w3 F4 r. L // Note we update the heatspace in two phases: first run
2 B6 \3 J. C/ q7 _ // diffusion, then run "updateWorld" to actually enact the
" J9 v* Q) n x9 l // changes the heatbugs have made. The ordering here is" _! h% {6 v g" M$ m% T1 f* v' {
// significant!) E5 g9 S# C, Z
4 b/ `/ K) K* A$ Y# p // Note also, that with the additional0 t: d: D( A% |
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' N. q3 C$ m% @: O* n7 k // randomize the order in which the bugs actually run
! j& @, h! ^! |* q8 l! t // their step rule. This has the effect of removing any' i& @4 M! X2 ^, K
// systematic bias in the iteration throught the heatbug+ J1 v7 N4 I$ a7 V! s
// list from timestep to timestep
# y0 Z3 O+ l! F( B- y w% U# P
6 L0 l% R+ ~2 a; N& r // By default, all `createActionForEach' modelActions have3 ]; Y8 v. }/ @1 i# I
// a default order of `Sequential', which means that the
) m" c( d: l* b7 e // order of iteration through the `heatbugList' will be) A) y# Z' _( _( w8 l( p/ s+ F
// identical (assuming the list order is not changed5 V8 s$ c$ p9 Y1 s) \ e' v5 }
// indirectly by some other process).
1 _3 ?5 r6 X/ x* j6 ^ . F' s* P3 N% H( n8 A1 S
modelActions = new ActionGroupImpl (getZone ());5 R H- n& d8 j
1 s2 ~& P+ y5 C7 A# l; V try {
j9 q e) d8 H) ]( ~+ N modelActions.createActionTo$message
& x3 s+ m0 B# K# i. Q (heat, new Selector (heat.getClass (), "stepRule", false));: ?# f& e9 J7 P" V7 R- K7 |" |8 m
} catch (Exception e) {
: D5 Y+ b# t+ \3 } System.err.println ("Exception stepRule: " + e.getMessage ());/ w' ?& y! G' y( T/ a$ |' F
}0 h9 w1 h7 v$ U( L
, E) } j8 Y, o& _! l0 R try {- V/ R. P$ q; D! _' ~5 y1 X2 q+ W
Heatbug proto = (Heatbug) heatbugList.get (0);
: N I5 e q- k( U3 _/ {& n Selector sel =
( k0 v# V7 j# F new Selector (proto.getClass (), "heatbugStep", false);- R8 I$ Q5 |) W" g. J
actionForEach =
0 V% k: M, j2 ^+ n, R g. M. ? modelActions.createFActionForEachHomogeneous$call
" u. T& \/ M- k7 y (heatbugList,
; U: O3 ]: c4 `+ Q new FCallImpl (this, proto, sel,
. r$ N" v: v$ i) Q2 t- W* e- Z4 ~ new FArgumentsImpl (this, sel)));
% d( L( b- u+ B/ y } catch (Exception e) {! `5 Y2 X) K. C3 f
e.printStackTrace (System.err);
+ l0 \$ @+ G B# T9 ~, H, \ }
9 {: u/ ~& f5 _8 I. M - K; a, w$ ^6 G; w( O1 m8 s
syncUpdateOrder ();
3 N5 J, ]5 H( x" J6 ^9 W X7 Q1 e, Z/ c D
try {
0 [8 M% u3 s( A* J7 s( }7 u( O, o modelActions.createActionTo$message - L6 i; [( y1 d: v u: \( L- ]
(heat, new Selector (heat.getClass (), "updateLattice", false));2 k2 T Y. F0 E) w8 K3 y1 P8 d+ S
} catch (Exception e) {( k* V0 i8 F! O( ^) x0 \% Q
System.err.println("Exception updateLattice: " + e.getMessage ());% E' W# l+ R. S
}
: C6 r2 F% n e% X" D8 f& z
$ ?; [$ M% F( K // Then we create a schedule that executes the. b4 m; ~+ k% }8 T( l3 O- v
// modelActions. modelActions is an ActionGroup, by itself it
3 |: v- l/ j; f // has no notion of time. In order to have it executed in
& S3 o' l" a4 t/ m // time, we create a Schedule that says to use the9 R' N6 |* o0 Y) P* u# |
// modelActions ActionGroup at particular times. This7 _! Q; A7 o/ Q# t- h- }/ Z
// schedule has a repeat interval of 1, it will loop every
3 _7 {/ ?4 ?( ]0 F9 j* r. g* N+ E // time step. The action is executed at time 0 relative to
. @, k: z+ \4 m P9 o; E4 z // the beginning of the loop.
9 w! X9 h% Y+ ]2 L; ]( X& i( w; M& o: p! k* B" q
// This is a simple schedule, with only one action that is% X9 P3 I) G, r: f$ B1 L7 w! e
// just repeated every time. See jmousetrap for more0 r& j l$ @5 H7 o
// complicated schedules.
. m! x+ Z# X% m L* p- _5 `
, g Q0 H& q Q$ u4 Q modelSchedule = new ScheduleImpl (getZone (), 1); {1 M! |* J+ W9 D8 _2 J
modelSchedule.at$createAction (0, modelActions);5 ?* q3 `& n& ]) x
. i" ~, I0 Z8 z2 V
return this;# H% N9 k" [3 V) T! J
} |