HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" q3 o' T2 f, [$ E2 t6 U
, [" K. k* ^3 d6 k. O0 @& ~" c. } public Object buildActions () {
2 m! a" F; V$ E( C super.buildActions();/ n' V% r! n% P: C
1 D/ o* d& ]4 K8 e2 l* O // Create the list of simulation actions. We put these in
6 Z1 \" H2 {" ? i3 Q4 M0 \ // an action group, because we want these actions to be
( W' L" A. G/ U2 B- w4 c& @ // executed in a specific order, but these steps should
! Z4 [# \! ]9 ~' n$ L @5 r // take no (simulated) time. The M(foo) means "The message
' U; S- @3 F- ]% X- D3 b4 [ // called <foo>". You can send a message To a particular$ Z$ @; F. V; N
// object, or ForEach object in a collection.
) b3 b& |" L- y6 B* {* y+ E S
: ^$ z9 J1 t8 ^' p# n8 b // Note we update the heatspace in two phases: first run
$ ^' U5 _% o. E // diffusion, then run "updateWorld" to actually enact the" D) }) q" h9 s% `
// changes the heatbugs have made. The ordering here is9 n6 c: [- ]' j
// significant!
; h* |. Q n3 c: \& V$ { * [: t: r7 |2 |5 N! B# R% v: n% l/ a
// Note also, that with the additional# z! p' N% L" y( N& C) }
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ j3 w/ ~! Z/ z
// randomize the order in which the bugs actually run
3 c. ^( G# G5 i" G n+ T. s9 G // their step rule. This has the effect of removing any
7 o& ]+ N2 a6 K) F' S- T5 O // systematic bias in the iteration throught the heatbug0 s3 M) i. n! N( s) k2 i/ j
// list from timestep to timestep" M7 s9 r* [% m: t# d
/ u; u" r: M! {: B
// By default, all `createActionForEach' modelActions have
" P1 F+ a& Q8 W1 r. S5 v // a default order of `Sequential', which means that the# `* [) Q, U/ f& A, R& z3 i7 F0 X
// order of iteration through the `heatbugList' will be( a7 d& y! w8 K7 _% y4 N0 E
// identical (assuming the list order is not changed
% V- y1 m3 w0 z- e- _ // indirectly by some other process).
7 T3 x4 d x$ M+ Y$ U+ V : S# Q6 v; N* i" d
modelActions = new ActionGroupImpl (getZone ());4 a2 ?3 g1 y0 z; o
. I( l( b2 X" I3 d2 ]
try {( [- \: d4 a% T1 E
modelActions.createActionTo$message
9 w4 J0 R" I( j1 O" S l8 f (heat, new Selector (heat.getClass (), "stepRule", false));/ z( D' x3 Q/ T( W+ S
} catch (Exception e) {6 O% Z/ @$ _; T% C5 D
System.err.println ("Exception stepRule: " + e.getMessage ());
4 `& U$ }) {* n) ?" y8 M( o }
' l& f( E3 q0 T
1 N: f% g; a0 i try {% X+ f8 I$ ?( g9 J- i8 ^
Heatbug proto = (Heatbug) heatbugList.get (0);2 V3 |( F5 v6 Q( ~1 T q; {% z7 V
Selector sel =
4 _( [' o) l# Q2 ^$ p+ _1 ]+ u new Selector (proto.getClass (), "heatbugStep", false);
% [0 a1 E e5 O4 m7 m2 H actionForEach =* Y: m6 f. V6 `# y8 P9 _' U
modelActions.createFActionForEachHomogeneous$call
. j3 \7 C. j7 n6 V* f7 Q6 q/ b8 Z+ e% _ (heatbugList,* ~% b) ^' \( `: h1 h
new FCallImpl (this, proto, sel,# T' y1 ?4 F/ u* d1 Y( ]: m" _% l
new FArgumentsImpl (this, sel)));% b0 |3 j* b/ ^7 ^
} catch (Exception e) {! m2 u. e K2 l+ E: |5 \/ G( N
e.printStackTrace (System.err);
8 ]; w g3 R# u$ m6 J7 H }) `3 d2 B4 `7 m: t
3 h; b0 n, Z5 G, }% k) ]7 }
syncUpdateOrder ();: D \& U; e: c; C; F) o3 q% e
' k* `, Y% X2 |4 s) n# ] try {
" M5 z; L; B$ l# Q% [4 C' |+ c modelActions.createActionTo$message ) ?. u b' d/ ~) H
(heat, new Selector (heat.getClass (), "updateLattice", false));9 ~) [4 s6 p+ N. L5 \% Y
} catch (Exception e) {9 B7 F& I6 v0 d1 X
System.err.println("Exception updateLattice: " + e.getMessage ());+ [8 g# q T" O5 M: |3 O m
}" v, u9 i* ]! W5 [3 P' ]
+ ]2 T9 u, K4 n8 }- ^ // Then we create a schedule that executes the
1 v3 q7 `2 F* u a // modelActions. modelActions is an ActionGroup, by itself it
; O7 J1 J. W3 e% k7 J9 e // has no notion of time. In order to have it executed in$ J; B! A" p" p, z& R0 y2 w `# o1 A
// time, we create a Schedule that says to use the
* A8 c! M& {. C+ K( a3 ]: a // modelActions ActionGroup at particular times. This
4 D" o/ s; J3 t // schedule has a repeat interval of 1, it will loop every2 S4 I7 Y* n' W8 s
// time step. The action is executed at time 0 relative to
8 f" j3 x4 @8 c C5 N4 \ // the beginning of the loop.5 F1 U( _: T9 g5 f
$ {2 d, I P+ S( f' |: o) l // This is a simple schedule, with only one action that is
. o9 W2 T! E6 |" [0 I // just repeated every time. See jmousetrap for more% O+ N) n+ f& M
// complicated schedules.7 ~0 v: w; e3 h
$ h5 U8 [4 N' ^, j* K0 b modelSchedule = new ScheduleImpl (getZone (), 1);
; h0 {. E6 c2 j0 L/ y; a( b" c& l modelSchedule.at$createAction (0, modelActions);
& N9 ?- i( q' v
% o1 y/ l6 }2 K B, [1 N) M return this;
) A- Q3 z. O y } } |