HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! e2 {5 s( r& P) \4 j) X3 i2 P
" w5 g7 H7 b$ I. w9 p" t0 h/ Z; n public Object buildActions () {
2 G( L9 F& y" Y$ w3 s$ Z! n super.buildActions();
' n3 P+ Z' E& B3 v+ P | + r4 X i& B. }4 u" a
// Create the list of simulation actions. We put these in3 T! B) ?1 @1 N4 C! P
// an action group, because we want these actions to be# W5 G* T& {2 _* @- L
// executed in a specific order, but these steps should2 K& O+ P9 X6 g' b1 q& s! o4 q* G
// take no (simulated) time. The M(foo) means "The message
% i0 j# P1 r I8 h, K( C // called <foo>". You can send a message To a particular3 d# T* h1 r! I3 z4 v
// object, or ForEach object in a collection.
7 ~0 r! ~) P1 v# _
% T* r4 | z! k4 @. e4 p // Note we update the heatspace in two phases: first run
6 A# l. l) {, q" l, `1 l; ]3 Q // diffusion, then run "updateWorld" to actually enact the
/ l+ j9 M3 v& y" C: c1 S: J // changes the heatbugs have made. The ordering here is% [6 q, e$ b. U1 w0 h; k8 U
// significant!
0 a6 m' j# f+ y+ j
; A2 j& u" l% P( w$ s2 n // Note also, that with the additional
( C/ d+ t" F0 M$ a7 l/ B, Q // `randomizeHeatbugUpdateOrder' Boolean flag we can' ^' c, k! W$ `# Z5 E- v3 k' t
// randomize the order in which the bugs actually run
" p5 h+ h7 T# w2 [7 ` // their step rule. This has the effect of removing any
. b) ~' p l. V // systematic bias in the iteration throught the heatbug5 y) \' @- d# ~! v( y9 r X
// list from timestep to timestep
5 v, O4 T% B" ]0 ?' u& n , I9 {; s* W5 H6 q
// By default, all `createActionForEach' modelActions have+ b D ^& T& K( @, ^( ]
// a default order of `Sequential', which means that the
; z% y9 C$ g9 f // order of iteration through the `heatbugList' will be4 o3 R4 l) `: S7 Q5 V
// identical (assuming the list order is not changed
7 W j0 h: R6 Z! e2 ]6 d // indirectly by some other process).
* |* _9 ?0 L, D+ @7 f8 H& A8 l) d
$ U2 J# P$ S/ w modelActions = new ActionGroupImpl (getZone ());" j4 q( v, F. B& l2 q
7 |, l8 l8 O7 U4 K9 R$ B% E try {
I" m9 a x8 I3 v7 p. x) T- @ modelActions.createActionTo$message
9 r+ p5 z* f" \$ U: I. d (heat, new Selector (heat.getClass (), "stepRule", false));
* a% c6 a4 A& _ } catch (Exception e) {
1 N1 r. ~7 k9 O7 C' d5 ^ System.err.println ("Exception stepRule: " + e.getMessage ());
6 W' I* h. a5 M: _( a% l+ D }
# ~5 A2 N. w" b- s) K7 a( I( P
% J+ ^% y$ V, b8 k1 Y4 ?0 [ d try {, B7 R5 A# T" R! L& z: l
Heatbug proto = (Heatbug) heatbugList.get (0);' A2 }3 y% I h2 W: d3 k7 K1 O
Selector sel = ) N! E k+ \- K" o3 F, K
new Selector (proto.getClass (), "heatbugStep", false);" h: |3 r" L: v
actionForEach =
+ d! S! d) | t5 }/ T6 _ modelActions.createFActionForEachHomogeneous$call
7 ~, X$ G4 r/ m( R9 o$ y; }/ A) N (heatbugList,; B3 I) ?8 g/ |
new FCallImpl (this, proto, sel,, g0 m" P/ {* y( p* e0 `
new FArgumentsImpl (this, sel)));9 E% V/ }: p1 j8 [- P X
} catch (Exception e) {& }* v& i; I( b% G7 | |4 v
e.printStackTrace (System.err);0 X) ~2 l9 B( I
}2 t5 O" v3 a$ ^
% z, V4 S9 L W( k, C$ U( \6 |4 m, a syncUpdateOrder ();
$ I. T7 _7 Q" d! t) S- m/ i& J. D; j" `& ^* S
try {+ d, M/ W6 ~ \6 Y* {7 x
modelActions.createActionTo$message
3 @ Q, @2 s9 m, \9 g6 V& h (heat, new Selector (heat.getClass (), "updateLattice", false));
3 o2 f" f/ s4 D6 b' J) F } catch (Exception e) {
. m0 O: ^7 H; ?5 T4 m# x) y) h System.err.println("Exception updateLattice: " + e.getMessage ());( r; A. {, V) j7 {
}* K% F* ?0 x' B; [2 _& x: _: ~* E
( o6 Z1 M$ v6 M% ^: ? // Then we create a schedule that executes the' C' ]) H! j' K0 G
// modelActions. modelActions is an ActionGroup, by itself it
3 H5 x! F" x7 |+ m) ? // has no notion of time. In order to have it executed in K2 j$ m( s4 b8 x
// time, we create a Schedule that says to use the- o& p, `2 p/ Y' f
// modelActions ActionGroup at particular times. This5 X2 d. V6 U( w5 d& v2 _/ y/ U
// schedule has a repeat interval of 1, it will loop every
+ E/ i" |7 @: v2 L! r // time step. The action is executed at time 0 relative to" q8 T2 B$ @9 a( ?) w0 ]
// the beginning of the loop.
6 g3 m% D( L: B3 i. @
% g* A4 y0 a9 y) D" C1 m0 x3 h& H // This is a simple schedule, with only one action that is
; E% b8 c P2 y/ J9 F; Y // just repeated every time. See jmousetrap for more
1 F; l5 p: }/ c. P/ Y* L' q5 H3 } // complicated schedules.
' z; u5 v2 K( r6 Z8 K . _. `" n) d! D, f5 @+ h; T4 u; r" z. M
modelSchedule = new ScheduleImpl (getZone (), 1);3 E3 {3 l( t) b9 w
modelSchedule.at$createAction (0, modelActions);
3 Q* p# F8 o/ W4 X5 n% z8 V/ o ' k5 J) P, i1 M8 v
return this;2 R% y/ E6 O( ~; O4 H# o' | |
} |