HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 s9 e3 x5 k3 q5 A- [2 L! x& G
3 z2 J( X- {' ~ public Object buildActions () {
; G3 L# i G" @4 l, o$ a( D super.buildActions();
) _' @5 w8 R) S0 {' i( X9 b, V/ C
5 S6 q3 d. g$ U& y4 e/ H // Create the list of simulation actions. We put these in/ {: d3 ^5 w" x2 v0 z6 E
// an action group, because we want these actions to be) d x7 u6 v) b. }$ T
// executed in a specific order, but these steps should
1 j; q8 V# ]4 H F/ G9 `4 o4 u // take no (simulated) time. The M(foo) means "The message
( Z' W' ^* g6 U // called <foo>". You can send a message To a particular3 h( a7 L; u" H& L
// object, or ForEach object in a collection.+ n4 J& n6 m7 B; X5 h/ g
$ c: A/ N: W% h6 O4 I0 E // Note we update the heatspace in two phases: first run
& X" b2 |1 K- Y" X // diffusion, then run "updateWorld" to actually enact the
7 a+ U. j. I( j+ Z7 q' W // changes the heatbugs have made. The ordering here is
9 y- z# U* M% i0 B- ?4 @) H // significant!% h. S! }) ^, _% X- D
( F/ D/ g( `# o7 v9 o2 t, `
// Note also, that with the additional
1 O6 i+ C( Q6 ~9 F. t) t# w9 K // `randomizeHeatbugUpdateOrder' Boolean flag we can# g7 `) q2 x0 C+ T5 i" A6 C+ j
// randomize the order in which the bugs actually run2 p0 |, K& V. o$ B! C
// their step rule. This has the effect of removing any w4 @+ X Q, c% ]7 n; @" b
// systematic bias in the iteration throught the heatbug$ W6 U% H! K7 l; m" R
// list from timestep to timestep
7 y# Q3 M( C- T9 u. p( ]1 U/ l
* n/ z o1 z) H% S& s8 `1 } // By default, all `createActionForEach' modelActions have
* b; y3 M3 g { // a default order of `Sequential', which means that the4 g' [- c" ?# R* w0 Y
// order of iteration through the `heatbugList' will be
: k/ Y! e7 D$ q // identical (assuming the list order is not changed# Y( @7 J' o+ \$ Z: r7 t! E" `! j" e- t
// indirectly by some other process)." S) h/ f/ @6 J9 f* i, a- ?
6 o4 ~9 c/ ` X1 L4 R! A
modelActions = new ActionGroupImpl (getZone ());
7 h M6 X! s, ~+ q3 t
: k& B. w2 Z1 y8 K4 ? ^2 m! R% @2 N try {
* I8 o& E1 O, H) P2 J# X1 B modelActions.createActionTo$message% L; Z) ~+ t/ j9 Y5 ]5 `
(heat, new Selector (heat.getClass (), "stepRule", false));
! t' Z' R, H7 {$ t' ~ } catch (Exception e) {2 ^: U5 H9 e& h
System.err.println ("Exception stepRule: " + e.getMessage ());9 V) h* t- @: R# X% V+ W+ R# t
}' H) f- x- G9 P, Y* ^& N% b4 N
" }" W) q# u5 A! Q
try {
, q" s* n2 n- k3 p7 ^: l Heatbug proto = (Heatbug) heatbugList.get (0);. O" c1 E& @. F# x, h- I% g' F
Selector sel =
0 x# l: m7 _( g9 K4 b' l& r new Selector (proto.getClass (), "heatbugStep", false);( e# O8 A3 `4 Q9 Q
actionForEach =* X# \) I. O: [% H6 q4 j/ p; A# A
modelActions.createFActionForEachHomogeneous$call
; e5 y @2 `. S7 P (heatbugList,
1 T" |: Y. Y: }' t' k3 U2 G9 a' F, p new FCallImpl (this, proto, sel,
: H e5 } U' n% ? new FArgumentsImpl (this, sel)));5 s9 @$ Q! M. }2 t
} catch (Exception e) {
9 J& N/ n* W/ r: b& P6 W e.printStackTrace (System.err);7 Y9 J( u1 q9 H9 [" t { | n+ J3 B" H
}
+ X( ]) f0 V, d# m; ]3 o1 c9 G
$ x4 r: h) u/ U( s. @ syncUpdateOrder ();
- f2 G. q2 h" b3 a
. v) c/ j* i$ l1 z- n- r& n try {
4 d( K, c" ?" a modelActions.createActionTo$message 1 n5 c3 E4 o y2 t- H3 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 B5 ]: v7 G. H4 B; V/ i: \, R } catch (Exception e) {
+ Z- D8 n9 f. w& V System.err.println("Exception updateLattice: " + e.getMessage ());3 I3 ]& s$ q4 |, W3 A r
}
# d# {0 } ^& R7 W. L
4 U8 ?. s4 F/ B2 A // Then we create a schedule that executes the
( A- _5 X @* R3 Y/ n // modelActions. modelActions is an ActionGroup, by itself it
/ Q+ C2 V: ]# l3 S. N/ z- R // has no notion of time. In order to have it executed in9 \% }: b1 `5 U
// time, we create a Schedule that says to use the
9 b; H, o/ M6 e6 T3 `4 F: y7 ? // modelActions ActionGroup at particular times. This3 p& t# t4 \4 G1 d% M1 a
// schedule has a repeat interval of 1, it will loop every7 h/ K" M# T' }4 ~
// time step. The action is executed at time 0 relative to
* ]/ a! `& k; Z, A, b // the beginning of the loop. X! K5 q/ W0 X; i* S" [
: u/ ~/ z2 b6 Q: n // This is a simple schedule, with only one action that is5 r$ `$ w% w1 @, c
// just repeated every time. See jmousetrap for more! n0 S8 |$ u* {( }
// complicated schedules.- j" E4 J6 x' a& w+ w
6 q& w8 _' K \5 p v% z
modelSchedule = new ScheduleImpl (getZone (), 1);
8 b# a3 c8 A/ v1 E: C/ F modelSchedule.at$createAction (0, modelActions);4 g! a9 s3 B9 w7 ^( {% k
# T2 o g, ~" z return this;
. W1 S$ Y) b8 b1 Y } |