HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ C9 c L# @/ {+ o+ c
& p7 E9 ~9 a- s$ w/ Y public Object buildActions () {
7 f! l9 `. X, y" i! Y' ]/ L/ @ super.buildActions();
6 ]+ L, t3 i6 N3 R 7 P8 c2 r! J, V0 x! m1 }5 l
// Create the list of simulation actions. We put these in2 l/ J( z* V& N7 A7 M* h3 u! i" h
// an action group, because we want these actions to be4 `! e/ b5 D7 M7 f8 R( ^
// executed in a specific order, but these steps should
$ g% K5 v$ e1 [) v" w6 |2 t$ Z // take no (simulated) time. The M(foo) means "The message
L8 _4 ?, m) i, H+ m& F // called <foo>". You can send a message To a particular
3 P N; I/ \/ V4 s7 \ // object, or ForEach object in a collection.
# w' w9 B4 M& R, y' _4 E& @
& Y; D9 N; i, f5 X2 j // Note we update the heatspace in two phases: first run
) c9 s0 k$ e; Q5 }& w0 K // diffusion, then run "updateWorld" to actually enact the, n# g: ` R1 ~ e5 `( M
// changes the heatbugs have made. The ordering here is
6 x6 x. s5 v6 p // significant!
/ _: n4 B {0 ~. I
: Y+ S4 m* X1 |$ v4 |& Q0 q // Note also, that with the additional! _6 _+ s5 A6 M3 o3 L9 n2 `$ ?6 \7 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& U$ K. f1 p7 D% R // randomize the order in which the bugs actually run) S6 g4 u. X7 d- Q+ [1 h
// their step rule. This has the effect of removing any
, B! s+ o. v$ r$ F# }( c/ Q // systematic bias in the iteration throught the heatbug
) E5 _6 m& |1 x/ M. @ // list from timestep to timestep) R& Q; L9 z4 I7 e/ ^/ S- V
4 a4 a$ J& C! }2 P
// By default, all `createActionForEach' modelActions have$ O8 ^/ z/ z: E
// a default order of `Sequential', which means that the
& U9 s! g$ ~1 s4 K // order of iteration through the `heatbugList' will be0 R8 x. i% L+ h$ @7 I' q/ E/ F
// identical (assuming the list order is not changed
, P3 v5 h: Z: q* ]' W // indirectly by some other process).
. A2 w) Z* p- W$ e5 R
o' v8 T- E& }, x modelActions = new ActionGroupImpl (getZone ());+ V+ G: Z+ k4 U! e% d$ d/ ?
0 y. _% L- \6 k$ V
try {
) p) o) G/ ~/ l+ J& U modelActions.createActionTo$message
9 n6 K) X2 Q, Q5 | g (heat, new Selector (heat.getClass (), "stepRule", false));
% d& X5 m0 P! A } catch (Exception e) {
; D+ f' C, P& }& @$ y8 F System.err.println ("Exception stepRule: " + e.getMessage ());
R9 ]* S) r/ A( w, n& r7 F }3 E/ L! \2 C% W B( z: f
! X7 k% h5 R1 N) C8 p, H7 e try {
( q- i' N9 l$ z0 F Heatbug proto = (Heatbug) heatbugList.get (0);5 H& Z0 L% l' r i ]/ w5 N8 b3 k
Selector sel = ! o% j3 y: S* W! ?
new Selector (proto.getClass (), "heatbugStep", false);
& x9 r2 _' Q% o' k9 ~ actionForEach =) c1 I- Q5 ~6 j* t9 f" T( d
modelActions.createFActionForEachHomogeneous$call
6 l7 I/ h6 X: i8 t2 i }/ |( g; U+ r (heatbugList,
7 `2 K4 r* A3 W; U5 t& w6 v' L* J6 @ new FCallImpl (this, proto, sel, B! D# V8 X1 c9 v! w! w
new FArgumentsImpl (this, sel)));, j3 Z* m" w! g5 u8 w
} catch (Exception e) {
8 E* b/ S! Y- c, S8 D( f9 D e.printStackTrace (System.err);& C2 { C9 n, w0 O" F
}. k6 X) M r6 }7 e; f
" P% N; H, W1 `& d+ `4 Y# E# P: k
syncUpdateOrder ();
# t1 R6 h" {0 h( N9 m" t, W+ M( x# L# W8 h8 l9 Q
try {/ r% {, l$ v9 C2 Y4 F( E2 d
modelActions.createActionTo$message ) Q" U3 |# e" X. P+ Z8 K2 q+ ]
(heat, new Selector (heat.getClass (), "updateLattice", false));- e, g5 Q7 L, n0 L+ x7 r6 h6 y. |3 U
} catch (Exception e) {" w F, V1 b4 v7 N& L, `
System.err.println("Exception updateLattice: " + e.getMessage ());- z: c; o9 t! t4 y
}
' r1 i0 F! t# A& k. y* G y0 R- F! C$ j( G6 U2 b5 ?
// Then we create a schedule that executes the( Z! R2 [+ ]4 x( E! E
// modelActions. modelActions is an ActionGroup, by itself it+ {# f5 O7 P* |" ]' b0 n
// has no notion of time. In order to have it executed in
, k+ T% d# `4 m) L# c // time, we create a Schedule that says to use the
$ F9 J, @4 m/ e4 C I6 X& s- D // modelActions ActionGroup at particular times. This+ y% {( ]6 `) W- [- D3 R7 ?
// schedule has a repeat interval of 1, it will loop every
/ |$ v) v& e3 u5 f // time step. The action is executed at time 0 relative to) {) ?# u* k9 \" _
// the beginning of the loop. E# M: l9 p4 U1 t2 C ^/ Y
$ m3 T* K+ {0 G* K5 o+ o# L2 Y // This is a simple schedule, with only one action that is
! S; [0 l$ ~0 [8 d8 A! y // just repeated every time. See jmousetrap for more3 Q) g! o8 B8 O8 M8 E
// complicated schedules.
/ f. d. x& A4 ~' [: l( z7 ^6 |; ~ : l2 X2 j2 g' |1 F- D0 M. s
modelSchedule = new ScheduleImpl (getZone (), 1);6 s* h q! U) g' Q2 K- m" D! o
modelSchedule.at$createAction (0, modelActions);
6 h8 [# l$ L _5 F: r- ?' L* ^
( k! i7 D4 x9 r# F. u6 X0 V. R8 ` return this;4 Z) P# H. e) h5 o
} |