HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# j! o: i/ o! x( ^# \, S8 K2 a# e6 U; S) g* U( o3 |9 r8 G
public Object buildActions () {/ ?) }3 _8 J6 j: @* k o
super.buildActions();9 j7 y+ i( m) y( F1 e. M
* s0 E$ T4 R. I% \! A* T" f6 K
// Create the list of simulation actions. We put these in
) Q) q% {0 ?6 l8 |" Y; p // an action group, because we want these actions to be& ]2 Z. j( P1 M* ]! X& S) m
// executed in a specific order, but these steps should
& v0 q4 c8 x+ b: \5 W // take no (simulated) time. The M(foo) means "The message
0 x2 `! H0 X' V, e // called <foo>". You can send a message To a particular
& u" }1 C/ [6 a5 E( {- t7 g t0 H // object, or ForEach object in a collection.7 E: O( w t& w: L! d3 m9 q
/ c. ~' D8 v' |
// Note we update the heatspace in two phases: first run
5 [ \ F" S% y* n) q // diffusion, then run "updateWorld" to actually enact the. B" N$ H5 P( x% ^. X- S, n
// changes the heatbugs have made. The ordering here is+ Z! v" F( @6 X7 t. ~0 W
// significant!
" ]4 w$ [) t7 v3 E' D R: s: m# S ! Z ~' G5 Q$ W6 m
// Note also, that with the additional
' N2 z7 A, P) Z4 x/ G8 M // `randomizeHeatbugUpdateOrder' Boolean flag we can, B2 m* Z# ~3 }$ E. X0 f
// randomize the order in which the bugs actually run& R7 k. ^* w8 C3 V) \
// their step rule. This has the effect of removing any
2 m- }5 A6 Z) G // systematic bias in the iteration throught the heatbug
( n- M2 n p/ H4 Y9 y, L% ? // list from timestep to timestep! p8 l( s# R1 z
& M5 U2 w8 m7 ~- g# g7 C! m! U6 B // By default, all `createActionForEach' modelActions have
1 A5 W' N* t4 v8 A4 c // a default order of `Sequential', which means that the
- b8 t5 ^/ p' o5 z) R( i // order of iteration through the `heatbugList' will be
! q! c# m9 E. d, Q- j1 w7 w // identical (assuming the list order is not changed3 _+ t9 r9 f: P. ~. x- ]7 K
// indirectly by some other process).
& j1 E) ^, @6 P4 W& e7 i) R0 d- E
' i6 L) ^% B! x& o3 S. T modelActions = new ActionGroupImpl (getZone ());0 C) V$ C: H" n6 ?1 M9 v
1 U) F; H* C) K. d
try {
& Z$ |5 o O9 j7 w modelActions.createActionTo$message8 ~- A3 {: A) t, X, V' ^+ U5 b% e
(heat, new Selector (heat.getClass (), "stepRule", false));4 c8 c- v7 g7 L* H4 W7 F+ K% t
} catch (Exception e) {
7 D7 j( Q6 ?0 X+ Z" U/ L4 }* t' H& Z System.err.println ("Exception stepRule: " + e.getMessage ());
4 O; A" e( `! F, q- |+ R2 { }) ~# ^- s4 E9 F$ o T! t3 l6 f
) U0 v. m( E& {* \. ?5 T' _ try {
' Z& Z( h3 b, q ?4 d Heatbug proto = (Heatbug) heatbugList.get (0);
* x- ^) R; l: B- E Selector sel =
+ d; P# x8 W; E! W7 t. y new Selector (proto.getClass (), "heatbugStep", false);' `6 I7 L1 ]# R* W
actionForEach =3 j8 N% O q% B# y6 N g2 O+ w6 c
modelActions.createFActionForEachHomogeneous$call; _( C, r& r. E/ X) |* d
(heatbugList,: F9 w& x. b+ k9 O
new FCallImpl (this, proto, sel,2 {! R& C/ r6 A& l: D4 _1 N
new FArgumentsImpl (this, sel)));
6 t' I. _, l2 O' e' P8 Q } catch (Exception e) {
- [3 M4 x' Z1 o e.printStackTrace (System.err);3 d! b( I: n% a7 W8 b j; ^$ N7 r
}
?0 @" x; F& H4 U$ x+ H6 [" ] 8 B7 [- r" Z2 T* j5 |
syncUpdateOrder (); j" r2 |3 a0 D- e8 F2 C4 _' l6 U4 v
4 q0 @! c4 F ]1 \" a7 [' ? try {
' l! @! b5 j x- p, k+ D* w2 o% F& { modelActions.createActionTo$message
/ }* C1 _" R+ p! q8 ] ]5 W (heat, new Selector (heat.getClass (), "updateLattice", false));
4 D# x% n* r' o/ w } catch (Exception e) {+ o( y X7 S$ U0 j, E" Q
System.err.println("Exception updateLattice: " + e.getMessage ());- T h! A% y' I; g, h/ b
}
/ `" D' h7 ]8 d& c9 U a
* k8 o* S7 q' G0 k // Then we create a schedule that executes the
0 v( [1 |. {7 h$ M/ d, ~) n2 n // modelActions. modelActions is an ActionGroup, by itself it! B2 t4 R* f! B
// has no notion of time. In order to have it executed in
3 ^% k/ z0 u1 d- g O, e // time, we create a Schedule that says to use the4 L9 S/ c8 L; [0 O6 H, m4 ]
// modelActions ActionGroup at particular times. This
" V- @5 Y/ Q0 k' L // schedule has a repeat interval of 1, it will loop every
) N5 G% P/ T1 z+ T4 i# J // time step. The action is executed at time 0 relative to G; O0 ?- f. s* P& j2 L
// the beginning of the loop.. V8 [# T- f! A" l6 ]- t
; i$ U0 v, f* e! j4 I3 d( p/ P
// This is a simple schedule, with only one action that is+ o, ^: ~4 {; A- X$ A+ m: {
// just repeated every time. See jmousetrap for more& e' }2 L( _ f
// complicated schedules.
& J7 e# x+ N# T7 r 4 v2 f6 r+ N6 z5 t+ ?7 o
modelSchedule = new ScheduleImpl (getZone (), 1);
, E5 X5 C/ t8 d5 a# h8 ` modelSchedule.at$createAction (0, modelActions);# A6 S4 k. ^1 l- A' c
, W- B3 N" M' a! w# ?, x return this;8 ]+ l- S$ K7 y, v* ^' z5 T2 W
} |