HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 q: x1 v8 n( M0 ?" o) a: R
% K# R: A3 q2 m- B2 K) A) ~& X public Object buildActions () {* ]4 i$ E* s, S' H# o% I
super.buildActions();
- a. m B- X8 D; y ) p0 j+ K- ]0 p" q2 j" o4 b0 o
// Create the list of simulation actions. We put these in
) f, W5 X" S% e3 G* U! \( _5 m // an action group, because we want these actions to be2 o7 z: O# ]) D& |# G
// executed in a specific order, but these steps should
! [) `$ ]( n. J6 C3 I% r // take no (simulated) time. The M(foo) means "The message* [, H' _( ?2 r1 H; Q }
// called <foo>". You can send a message To a particular
& [3 _2 {$ K* S/ X+ Y( l3 @ // object, or ForEach object in a collection.
- f7 d9 j0 V2 r5 F
: I( d: _' Z5 d$ f( b // Note we update the heatspace in two phases: first run, t% D: @9 b: l" z& B
// diffusion, then run "updateWorld" to actually enact the" l; N( N& B7 n; K' T
// changes the heatbugs have made. The ordering here is+ n$ k3 U1 l$ _% p7 h2 s) h
// significant!
; d: [4 G; G5 [" y( J
: w7 h" Y2 p7 _) p/ s8 ]9 k7 ~; @ // Note also, that with the additional
5 @5 ?& ~$ q/ K' m6 c( Q) V // `randomizeHeatbugUpdateOrder' Boolean flag we can6 I& g! X% u/ g# X. T% _6 v0 y0 c9 {
// randomize the order in which the bugs actually run
2 g3 D$ J q* r/ t) y; c1 T( f // their step rule. This has the effect of removing any' ]% ~6 `) N; g# A0 o/ e
// systematic bias in the iteration throught the heatbug
# g7 u) ^; k: } // list from timestep to timestep. R! N$ _, J5 r
# z8 T1 _7 g6 C5 D$ e( B2 `& z
// By default, all `createActionForEach' modelActions have) D5 I& z: m. v5 `" u7 T( w5 E
// a default order of `Sequential', which means that the; K! ]; c1 e8 @8 P: ]6 m f/ x7 k
// order of iteration through the `heatbugList' will be4 U$ V% I! f1 B0 t# J
// identical (assuming the list order is not changed! ~ b7 l1 b# A( R6 \
// indirectly by some other process).& P& N2 ?# f* R
$ K* J) [! J& q9 e% {) V& Z8 [ modelActions = new ActionGroupImpl (getZone ());
# o$ w% K" Q. q; K3 P/ l# I9 H8 O: c6 y8 a
try {
* |4 z5 H8 p. R, l modelActions.createActionTo$message5 ^5 H+ ]7 R# O2 r
(heat, new Selector (heat.getClass (), "stepRule", false));8 O" z' `; l3 R& @' Z/ U. |" d1 H
} catch (Exception e) {
6 T. k1 [& o+ b% S! ~( N! n System.err.println ("Exception stepRule: " + e.getMessage ());
: u' O H1 B& K }" {6 h+ c4 W. V! E( H' {. A2 G A
2 a& x1 T9 E% |) @( p+ n7 x try {7 A% h* ^5 T" _3 T$ O
Heatbug proto = (Heatbug) heatbugList.get (0);* I& S0 c0 [4 c {4 n5 g
Selector sel = 7 o6 w/ U, E0 V7 s. P2 a# I
new Selector (proto.getClass (), "heatbugStep", false);
2 A* _, C, S' q. U0 l1 c) G9 t actionForEach =
, u' X! Q7 ^4 [; P% Z modelActions.createFActionForEachHomogeneous$call
5 u/ g6 Y5 M% j6 g (heatbugList,
; e' k1 q8 q# C, ?0 V+ j, T' t1 { new FCallImpl (this, proto, sel,2 ? G5 s2 ^* \, K$ q' Z( P
new FArgumentsImpl (this, sel)));
4 ?$ U8 e; W$ t/ ?" P7 p+ j } catch (Exception e) {
1 |4 J. e# V; A" Z* ] e.printStackTrace (System.err);
! I. S0 E, d0 n0 d. \- \ }
2 T& W2 C' q3 _5 g / N8 U% ?# U- k1 l4 M9 }5 ^0 n
syncUpdateOrder ();' ]3 m( {+ i; x: A' L
5 V0 c3 W) J/ B try {
( ~# d1 F; s' J% F modelActions.createActionTo$message 3 D' t# t1 D& \; |$ |$ A4 B
(heat, new Selector (heat.getClass (), "updateLattice", false));
% n A/ s5 [' B2 c& n } catch (Exception e) {
: r+ D5 ? Z+ S, u System.err.println("Exception updateLattice: " + e.getMessage ());) c5 x; x& z: a0 _! R
}
% j2 d1 L. R6 F$ j $ l: D7 K- n0 H/ b
// Then we create a schedule that executes the8 E8 x6 j. s' j, h# x6 z0 ]) \$ b7 v
// modelActions. modelActions is an ActionGroup, by itself it
8 h: j6 U$ [) y2 K% P1 s8 N% } // has no notion of time. In order to have it executed in
& z9 D6 n" u" |$ Q4 z // time, we create a Schedule that says to use the$ q6 f. t& o* E/ O! j
// modelActions ActionGroup at particular times. This2 u% J1 U% h0 U
// schedule has a repeat interval of 1, it will loop every
. T' ~5 I- ]* K7 O+ u1 [ // time step. The action is executed at time 0 relative to
, V7 l. T r& e7 s // the beginning of the loop.
s) i0 l3 O6 x! K7 V$ S
6 r4 i. s+ }, i9 d: @9 B // This is a simple schedule, with only one action that is
0 y6 P/ @! B& q" D2 A2 U3 H // just repeated every time. See jmousetrap for more; I6 ?7 w/ t3 `
// complicated schedules.0 G! q/ T4 N4 c
- g; h' o) m. ?$ I
modelSchedule = new ScheduleImpl (getZone (), 1);4 c9 c- l, y4 ?# U; g0 b
modelSchedule.at$createAction (0, modelActions);
5 i2 O' E3 [* o$ p& C; P6 |8 }. W2 T
* o6 {/ B/ v1 j+ Z# M# W' h return this;
$ T w& g( c4 x1 z6 `) B$ S } |