HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) B! o( x1 m( F6 L
# ~0 H: v9 v, {- k: ^/ M, s public Object buildActions () {! a( j) H& E5 Y
super.buildActions();
+ {$ E: e2 `7 i" k2 I8 I : K. `/ r+ n0 Q/ l j- d/ O1 C1 d
// Create the list of simulation actions. We put these in+ J! f3 l5 F- s8 | P" f z
// an action group, because we want these actions to be
T- |; d% j1 N- w- F; m# N% Y // executed in a specific order, but these steps should5 ]* W3 L/ f" z: J9 @
// take no (simulated) time. The M(foo) means "The message r0 q1 z1 E, F- D4 }$ x/ @5 G
// called <foo>". You can send a message To a particular- E& @8 `/ T3 ~% Z2 I, R) c) E
// object, or ForEach object in a collection.
) ?; d) x0 S" S$ l
1 Q8 T2 v. V5 a: G! a // Note we update the heatspace in two phases: first run$ ^+ u: S. V6 o9 C3 T
// diffusion, then run "updateWorld" to actually enact the0 I4 T2 I z! t& Q+ ?8 N
// changes the heatbugs have made. The ordering here is
8 D7 S1 B: @& @/ k+ D# J // significant!) p+ W$ g9 P/ ?( r
/ o8 o' }5 g- F% b // Note also, that with the additional% O/ z' o9 S" d+ w0 L. c# R
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 b) }3 s N# I8 g) F! x) Y5 K
// randomize the order in which the bugs actually run
5 f5 |2 w; h- ~8 z // their step rule. This has the effect of removing any" ?& v5 K8 x/ d! b. |- d
// systematic bias in the iteration throught the heatbug5 c9 W, y! q# Q* g+ C
// list from timestep to timestep
( c- L1 ~' e4 u$ }+ i. `
6 k$ a' L1 G: b9 ~( ?, w( C // By default, all `createActionForEach' modelActions have
7 N! T9 f# n) n; P: V // a default order of `Sequential', which means that the
4 X( I, j5 |' V; p6 w! J // order of iteration through the `heatbugList' will be
8 z. m) \ }. k3 D // identical (assuming the list order is not changed k- ^! o9 j& n' E6 p2 f
// indirectly by some other process).. d3 W- L; D% d$ A( R. e# J
) Z: H* N1 L3 i2 Y S
modelActions = new ActionGroupImpl (getZone ());6 B0 o6 }9 G" b
1 A, t( w" I3 D( n8 q4 E( E
try {! A. r& ? o( |5 U7 I/ l
modelActions.createActionTo$message
+ V8 V+ ^% Y& ? (heat, new Selector (heat.getClass (), "stepRule", false));1 O9 u: d9 u6 W O
} catch (Exception e) {2 @6 g! b' I' T7 R
System.err.println ("Exception stepRule: " + e.getMessage ());
% e2 G% S/ y1 c+ L9 u& u! N3 ] }/ Y* z( s2 ^+ w& A
4 v# f0 K: H( L, I
try {. T, O$ V3 f4 M$ \' X" b3 p! l2 l
Heatbug proto = (Heatbug) heatbugList.get (0);* d" ], ~5 O: L- ]4 A% T
Selector sel = ; I. l0 e9 C* G- O7 V8 l- H
new Selector (proto.getClass (), "heatbugStep", false);, x4 w5 \6 N- E# B% c
actionForEach =
" w6 T% c6 i' k2 _ modelActions.createFActionForEachHomogeneous$call
1 p! F6 T3 ]0 p, ~5 w' \ (heatbugList,
6 B5 L: i: e/ F( D' j- m& ` new FCallImpl (this, proto, sel,2 X2 R3 ~4 p& b4 D
new FArgumentsImpl (this, sel)));
" Z" n/ A9 G4 r7 s! E4 ?( j5 ? } catch (Exception e) {
# `* K1 } K1 |3 b e.printStackTrace (System.err);
4 o* P* G/ v& S: F& B: n& C0 V }
) ]1 y3 j: C7 C 6 ~3 i' ^: s/ j' O; H1 R
syncUpdateOrder ();' n5 C9 J8 z* i8 z; F$ j
4 T9 a4 Q4 U! d
try {$ X* q/ f7 B w4 A$ L0 k- J
modelActions.createActionTo$message
; o8 Y% }1 v- A5 p: J+ G (heat, new Selector (heat.getClass (), "updateLattice", false));' K+ {8 O5 l8 {2 }# R
} catch (Exception e) {
% r$ k, r9 E1 K3 [ System.err.println("Exception updateLattice: " + e.getMessage ());# z& j7 y6 U8 B
}
4 c7 N( d" ]% e9 g2 _ ' \' U! i" R1 E" t$ R
// Then we create a schedule that executes the# X0 t$ }! X0 U9 T7 p2 ?" o+ |6 V( i
// modelActions. modelActions is an ActionGroup, by itself it5 o! g' T- V) n; E' K( u2 D. W
// has no notion of time. In order to have it executed in
9 o) D: j4 q1 W) |, G: o+ y6 i // time, we create a Schedule that says to use the
- d8 U i3 H# A& @ h$ V // modelActions ActionGroup at particular times. This
3 F) y: \. {0 _7 a // schedule has a repeat interval of 1, it will loop every
- N" E8 k: t! O6 p; w // time step. The action is executed at time 0 relative to A8 G. ^; |9 ~( @! G) `/ X
// the beginning of the loop.
8 `( {' z% _1 K
, v+ Q7 d& |2 S6 D! B, a5 t0 l5 t // This is a simple schedule, with only one action that is
; y& g; {* F0 y' d' l0 T9 S7 k // just repeated every time. See jmousetrap for more2 m" A! C) v) \2 g
// complicated schedules.$ m. {* F4 e: f" ^' i& T
! h* }: M7 A, f+ c
modelSchedule = new ScheduleImpl (getZone (), 1);5 B- F/ c9 ?) w' x6 p! X7 p' [
modelSchedule.at$createAction (0, modelActions);- {1 c2 n: n' Y" A9 _
9 N$ O4 t9 [; C' x. [
return this;) U& J$ X8 }, G
} |