HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ E# ? e* W$ a/ f; w6 M
, F/ k9 r: Y. F, |4 M g- x public Object buildActions () {( v, U& N9 \* p" L& u
super.buildActions();: f* q2 D9 z, N/ E" {
. f) t( `* z% S; h# n
// Create the list of simulation actions. We put these in. I2 T1 [! ^* P. T4 X5 Z& ~
// an action group, because we want these actions to be
( F/ Q$ M. n0 F. y+ x* D4 z // executed in a specific order, but these steps should' X7 f7 R S& p, t( W
// take no (simulated) time. The M(foo) means "The message4 m, F( ^9 L- H# \: N/ ]2 \
// called <foo>". You can send a message To a particular" ~2 `. k! H% Y" }" z/ [
// object, or ForEach object in a collection.$ m9 t# J$ _5 ^0 I. N! G. J
* d1 V% ?- @) u. | // Note we update the heatspace in two phases: first run
$ Y6 k1 {% K) B8 \ // diffusion, then run "updateWorld" to actually enact the
; ~/ w* ^4 a' a: s1 ~: T // changes the heatbugs have made. The ordering here is, K1 `% N5 E. c6 q, l2 A0 c
// significant!
- t& z* T1 W; o* L9 z# F: n
, [/ F5 e% B( f // Note also, that with the additional
- ?7 @2 l) c$ y- p: z // `randomizeHeatbugUpdateOrder' Boolean flag we can9 n7 K0 P( t' Q7 H: G% O& c1 N+ O
// randomize the order in which the bugs actually run p% \ F9 f6 ^2 A& Q" S, S3 v: a
// their step rule. This has the effect of removing any ~" p( r U% Y; W% D5 g, U
// systematic bias in the iteration throught the heatbug1 `9 N% Q: L* I4 y4 k1 b
// list from timestep to timestep
' }% |- c) z4 q
* Z3 y5 s; x& j* p2 [ // By default, all `createActionForEach' modelActions have+ y6 c. L4 k$ X1 {8 w, }; `
// a default order of `Sequential', which means that the- u( i0 Q- {6 V4 x9 f
// order of iteration through the `heatbugList' will be/ y4 l' r$ [$ ~3 m
// identical (assuming the list order is not changed
& ^. d9 I/ ?: Z3 i3 z- B% t // indirectly by some other process).
6 H/ }$ B+ Y* T5 u4 }7 r 3 Z5 j- W' h# B6 O# _$ {8 v
modelActions = new ActionGroupImpl (getZone ());2 a; I i% g2 w$ j) |- V
7 \3 h1 O7 O, O1 t" B, G5 ^
try {6 ` p2 _9 t; g: x; R
modelActions.createActionTo$message& ^, j8 p, l0 k2 Y5 o( I
(heat, new Selector (heat.getClass (), "stepRule", false));
' r* @2 s8 \/ G _( X+ Z, s1 ~7 r } catch (Exception e) {
- a3 e) U4 [2 W# _ System.err.println ("Exception stepRule: " + e.getMessage ());
* D8 q' u3 N7 ?+ H3 O }
" |6 H$ F* g/ ` Y: B# P+ m7 \7 P D- W ?' p
try {
0 t# \# f4 z: B/ p Heatbug proto = (Heatbug) heatbugList.get (0);
K3 X/ d% B% N+ S9 K) e Selector sel = 1 m% J* U7 W. N3 z" D- d
new Selector (proto.getClass (), "heatbugStep", false);& A( u6 s( ?! b& \. _2 k! z' E5 ?
actionForEach =2 Z( m: y* q$ t* n1 A" h
modelActions.createFActionForEachHomogeneous$call
' K/ j& G0 }5 C( w/ T7 I (heatbugList,: F1 d; C& a8 C$ \; u
new FCallImpl (this, proto, sel,
- Q0 Z/ A$ A. F. h! }' ^, p new FArgumentsImpl (this, sel)));1 @) J9 H; c9 W1 R2 E1 E/ N
} catch (Exception e) {5 o/ U [0 W4 S. ^& c; ~
e.printStackTrace (System.err);, W2 Z* S* K @) F: {" {$ L: g
}
7 S) d# W! \( T& `0 ~) u
4 V( d8 A/ L* V V2 ?2 u syncUpdateOrder ();
5 q# e' z6 Z1 D+ T) c+ t
* f) F$ x K b9 s0 r: \ try {+ N- h8 ~: h U8 w I, }
modelActions.createActionTo$message $ n# H; E, d Z/ w4 B9 V7 d. T
(heat, new Selector (heat.getClass (), "updateLattice", false));9 I/ J, o! W* T9 X5 `* \4 m9 _
} catch (Exception e) {
5 L% J, g! L; W1 L+ Y T System.err.println("Exception updateLattice: " + e.getMessage ());) A8 v* t6 ]- u; f& [" N1 M
}; t2 C! M J+ O' Y" T
( d+ @6 t6 a3 O/ U1 m
// Then we create a schedule that executes the
* X1 ]0 V4 \6 C3 E+ q7 e // modelActions. modelActions is an ActionGroup, by itself it
) J* M& J% L. S3 ^" L$ p; P) o // has no notion of time. In order to have it executed in
# W) s d5 I3 L6 @ // time, we create a Schedule that says to use the
9 ?% O2 A% {/ ]+ u; ~ // modelActions ActionGroup at particular times. This
: N- h+ U" k: f& Y6 ^ // schedule has a repeat interval of 1, it will loop every
# p$ J h2 ]' G1 V( C- h7 z/ r, Q // time step. The action is executed at time 0 relative to( ~7 E9 e* @/ P g9 Y4 Z+ \ `3 H* \
// the beginning of the loop.
$ S% N) p2 L6 K, [
: l3 \- ~: |4 N# ~+ a: } // This is a simple schedule, with only one action that is
0 V" H6 s9 q7 h1 Y* H6 P // just repeated every time. See jmousetrap for more5 k' r& c7 C# m0 ?
// complicated schedules.
2 ^& ~% z% |* f7 a2 l* K) R 9 e' m5 J2 M, l% v3 r6 E6 O9 q
modelSchedule = new ScheduleImpl (getZone (), 1);; }; W* `/ W- A0 H6 ?
modelSchedule.at$createAction (0, modelActions);
( h9 S8 s% T: T. S+ i4 _ 4 ]; p. p8 V4 y' S7 G- R; B- A4 B
return this;
* J! g/ Z7 F2 B" u. b% w/ a } |