HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' U; g; h7 T8 v3 y; d) {
6 F2 P3 y- u8 {( J8 P. I
public Object buildActions () {
! S. H, G0 d9 U8 w2 a9 P5 r; e9 x7 e super.buildActions();7 H7 ?. {$ i/ S1 G% W% Z+ n. N
; C/ s( |* v& x5 ~. W! t/ _% E // Create the list of simulation actions. We put these in
, Z. o2 K9 O. h, I7 B+ n3 _ // an action group, because we want these actions to be
& C) B$ j9 m8 I3 M8 } // executed in a specific order, but these steps should
, u) r, O( b+ x c0 h" x( e( D // take no (simulated) time. The M(foo) means "The message
$ y% z: [2 R& \# _ // called <foo>". You can send a message To a particular& P) l- Y2 T7 _ j/ W/ \6 v
// object, or ForEach object in a collection.
& |# O' F3 q% I2 I& M / R+ O7 _9 u4 f% u6 u' X! f
// Note we update the heatspace in two phases: first run
) ?! n( L6 ^( a9 Z. S5 J // diffusion, then run "updateWorld" to actually enact the
% V6 \4 Y! n5 O% X1 S/ [ // changes the heatbugs have made. The ordering here is
- A4 Q. o" O; L4 [! I" n // significant!
7 ?4 e5 V7 q/ H& c s
9 H) E8 j4 |" E // Note also, that with the additional
# f+ G: y# {; T2 d9 c* J9 i7 X9 J" ^7 k // `randomizeHeatbugUpdateOrder' Boolean flag we can# t1 K4 f; T8 X$ T. n, k
// randomize the order in which the bugs actually run
0 `+ y' N. `: Y/ m8 p& S' t3 c // their step rule. This has the effect of removing any
+ z4 ^, \/ Y( W4 Z$ S // systematic bias in the iteration throught the heatbug
" }; C3 o0 }) J // list from timestep to timestep
+ U& T! O$ L6 j" P4 H j/ B8 O
7 I, v. l1 S& a- X# } // By default, all `createActionForEach' modelActions have* j+ ^+ F0 S4 p( Y) e7 j
// a default order of `Sequential', which means that the
. f5 r2 w& {& F/ s/ N // order of iteration through the `heatbugList' will be; i( U% U: J3 z& C* g
// identical (assuming the list order is not changed9 O& j2 s, R1 L% ^& J/ p% l1 G$ i/ p' F
// indirectly by some other process).+ b$ k* t9 E/ p6 J
2 Y' X8 v3 W" ]. [7 K6 _. q k+ c9 M modelActions = new ActionGroupImpl (getZone ());0 \/ d/ r: d) k
0 M! a7 |) C# v+ J" S% t% `
try {& A; Q- C7 H8 d0 J( ~# ^9 F- F
modelActions.createActionTo$message
E3 N+ ^$ w8 @( u (heat, new Selector (heat.getClass (), "stepRule", false));
; D) N3 ~2 d3 K" O3 `' o6 H } catch (Exception e) {+ u8 O2 Y: @" p9 F2 m3 p' P5 H) q
System.err.println ("Exception stepRule: " + e.getMessage ());
9 R" B3 F8 v8 ^$ V4 i }) ^ g5 u/ C! o4 z3 _3 L
9 n& f* K9 w$ C
try {
% T% d' O, g5 C* V" S. w3 T Heatbug proto = (Heatbug) heatbugList.get (0);
. E! v% O& \" {( b$ F8 G2 P Selector sel = , g, B( }; c$ \7 U
new Selector (proto.getClass (), "heatbugStep", false);
+ Q4 C& w# t; I+ p! I actionForEach =
3 ]- ^$ z! Z# ~' K, t modelActions.createFActionForEachHomogeneous$call
6 E2 j& ^5 q p2 E (heatbugList,
: b O( P7 a: ^' k- o; S" E new FCallImpl (this, proto, sel,
- P8 p+ V8 \9 |& [ new FArgumentsImpl (this, sel)));7 c6 G; `3 V" `+ G% p
} catch (Exception e) {) ^. l0 f# ?$ \+ b; U1 W4 b
e.printStackTrace (System.err);3 x7 G; {. z! Y& S
}/ B- l$ Q+ ]. s
9 K( x B$ J4 S5 P* \/ Y9 }( a syncUpdateOrder ();3 w3 F' l0 ]# y. I5 X
7 `- ^) d8 X% {1 X try {$ s+ z- K4 y: J6 x& ^( r* a
modelActions.createActionTo$message
+ M1 }% m* L) s, q3 N( U9 h1 n8 { (heat, new Selector (heat.getClass (), "updateLattice", false)); K' U% F# U9 k/ Q
} catch (Exception e) {
1 N8 s" ?( K2 Y# `4 ]0 a8 O* O System.err.println("Exception updateLattice: " + e.getMessage ());6 x+ M, t( w+ {+ _* S
}/ F0 G! \' P1 j# g6 u
: A. | F9 U, l, w% T3 Q$ R
// Then we create a schedule that executes the
8 P/ I" Q6 p6 H% J // modelActions. modelActions is an ActionGroup, by itself it( g- O% R& N0 x
// has no notion of time. In order to have it executed in$ L8 |* P: H! Y) ~' M
// time, we create a Schedule that says to use the
9 u* j4 v7 q) Y, m; C2 R // modelActions ActionGroup at particular times. This9 O6 f- g' r% h' P, l
// schedule has a repeat interval of 1, it will loop every
: G6 @ ?1 z- N // time step. The action is executed at time 0 relative to
* z( M4 b: {# ?( w( V+ V // the beginning of the loop.! F* ]0 n/ C! q5 @. b
0 H( N2 h1 h6 G* N' A0 t
// This is a simple schedule, with only one action that is4 @, @) b% \" D, a- n) O: k
// just repeated every time. See jmousetrap for more
5 U7 ^ k u: w: W+ b( y // complicated schedules./ y. Z, C3 N' Z7 f' G
8 j( H" C' |$ d, p+ y) O i. s modelSchedule = new ScheduleImpl (getZone (), 1);+ _/ Q/ E+ r* ?8 k) ?
modelSchedule.at$createAction (0, modelActions);
; H% `4 W# [% a' [2 F- i) b 8 j; i! X# B4 }9 j* a0 J
return this;
4 P7 ]2 Z$ c! C/ T/ I } |