HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) q$ F2 [- B5 O/ k* \ J D
/ h5 \0 o1 V6 ?9 t- [3 X public Object buildActions () {5 l8 a* ^; d% r- m6 W f! o
super.buildActions();
$ i0 i; r4 o2 q0 h/ Y$ w0 ] 8 `! t& a# P ?+ U0 T h
// Create the list of simulation actions. We put these in
$ W' y' ?: O1 v% y: W5 Y, R // an action group, because we want these actions to be6 {' {5 P) D9 ?
// executed in a specific order, but these steps should
, f* ]/ q) C" O H // take no (simulated) time. The M(foo) means "The message
( u/ s5 K/ ^* Y/ l# ~/ B" V% P& e // called <foo>". You can send a message To a particular
$ Y/ G, A. j, P9 L! } // object, or ForEach object in a collection.
, m8 v: b' L0 l+ n6 y2 }
& _' x, y& s1 h8 ]) U' j+ j9 x% B4 ? // Note we update the heatspace in two phases: first run
' J' T5 \8 U+ r( v. h5 |# R, H+ V // diffusion, then run "updateWorld" to actually enact the
( S, v1 \; i; I0 ~: h5 L1 l // changes the heatbugs have made. The ordering here is
9 v, r$ t3 `6 h1 M // significant!
8 |+ \4 z& c$ y7 R; l& z
' N) R) X6 Z, D // Note also, that with the additional
" W+ A$ G: w/ o& C% C8 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 V% b3 ?& h S // randomize the order in which the bugs actually run
9 W7 j+ I& j* q, A8 ~ // their step rule. This has the effect of removing any
. _0 B* W |( V5 J7 z3 l // systematic bias in the iteration throught the heatbug S' r/ L$ E9 S
// list from timestep to timestep
4 Z$ y9 U* C' Y3 j- Y
# p4 ]* s! z1 ]9 o* Y // By default, all `createActionForEach' modelActions have
/ f0 [2 M2 B' o t$ g5 p // a default order of `Sequential', which means that the- n4 z3 b% a8 `6 _) F' D( ^( X
// order of iteration through the `heatbugList' will be/ n" {: x8 n+ ]0 Z8 J& H# m
// identical (assuming the list order is not changed" i3 U, b4 C" r' D1 j
// indirectly by some other process).
$ @9 k9 K- }! `* I: F+ C
* g7 e9 g7 h/ o6 G; J' ~+ x modelActions = new ActionGroupImpl (getZone ());
! F: g. A7 r" P: O& c$ y0 a& y+ {) `& q
try {7 f) v b/ K4 }. u; P! B$ W
modelActions.createActionTo$message
I; n5 ?/ t$ h# G: }* i1 ^: Z (heat, new Selector (heat.getClass (), "stepRule", false));
, Z- h' K7 E4 A } catch (Exception e) {# k+ \! B# v y* ~
System.err.println ("Exception stepRule: " + e.getMessage ());5 |0 b+ i1 U, Q* C% X, W0 F
}9 W$ \ m& l3 D. ]) |$ v, d
; H, L! v: K& I5 k7 i F( z try {/ J; M. Y- [( X8 V
Heatbug proto = (Heatbug) heatbugList.get (0);; v ?! V# A3 {- A R, J1 k
Selector sel = R; K8 \+ u/ w2 {( M% C+ i+ @
new Selector (proto.getClass (), "heatbugStep", false);& r' o1 B Z }1 C/ I. ]
actionForEach =
0 c- M Z& Y- O; f- I6 v modelActions.createFActionForEachHomogeneous$call
" x. x' `3 g0 S2 l* A (heatbugList,
, j8 y$ S) }0 \' x new FCallImpl (this, proto, sel,: |2 K0 i1 A+ p: P/ R% |" b
new FArgumentsImpl (this, sel)));
( c6 H+ G3 U, o! u2 ]" J% Z9 Q1 x } catch (Exception e) {
$ K5 D$ X7 }8 R! Y% |. i6 } e.printStackTrace (System.err);/ _4 s8 H8 W6 ^7 I6 A# w8 i
}( m1 a r, W k* }+ V# Z: |$ p( m/ `
& l# {! g0 ] `, N0 Q
syncUpdateOrder ();1 z0 a. {( ]7 k! b$ e! @
6 x$ K9 j4 `6 V2 T, d try {
* L4 M1 l/ Z$ ?, T: t; t, T5 A modelActions.createActionTo$message $ j) k" ~1 V- a$ w* B: y- k
(heat, new Selector (heat.getClass (), "updateLattice", false));4 C4 v* t3 s J, k0 r6 K8 O
} catch (Exception e) {
( R6 t) F6 g- ]+ V# p System.err.println("Exception updateLattice: " + e.getMessage ());
5 {9 H" \* M) s( F }3 n: g, Z1 d8 Z y3 w
- D2 I; }" R* A$ R& i
// Then we create a schedule that executes the
8 ~( ~' A3 g: P // modelActions. modelActions is an ActionGroup, by itself it+ D* g( N0 ~/ Q' ~; H/ {
// has no notion of time. In order to have it executed in
- H/ y1 @- l; _ V& o" x8 S: i0 b // time, we create a Schedule that says to use the! _" Z' [6 S ~' i0 E
// modelActions ActionGroup at particular times. This
( E) ~$ Q7 M+ x7 L) v // schedule has a repeat interval of 1, it will loop every
' J& G5 x( Y; K6 `) H // time step. The action is executed at time 0 relative to& L2 l# K+ O9 ?; V: d4 k
// the beginning of the loop.9 O: P [% \: m- w" D9 s
. @8 m2 {6 I X* r1 M* H // This is a simple schedule, with only one action that is1 b5 r) V+ U$ V( w1 X
// just repeated every time. See jmousetrap for more
8 o" q2 f' E4 ~& t) U9 v" n // complicated schedules.
* c0 ?. G' g) f/ o
$ _( G% R! F- M! f1 d+ }2 h modelSchedule = new ScheduleImpl (getZone (), 1);2 `! W0 ]/ L5 l/ q, S3 I6 T
modelSchedule.at$createAction (0, modelActions);
! B S% d' N8 X' j3 `) y2 t 1 Q0 H! D3 I9 ^3 h
return this;
9 I7 X, e {- w. d2 N4 d3 P- v } |