HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 j$ ]& T8 h9 T* v/ d
6 E% g! P$ \; V j
public Object buildActions () {
' I7 f: R" W$ m3 M5 `8 x* @3 a super.buildActions();/ P; ~0 F+ j3 G# O; [3 i4 i! G
0 C! _% d3 J$ c7 J! T8 \9 R' v // Create the list of simulation actions. We put these in$ o3 n+ I% L0 P, r
// an action group, because we want these actions to be
& E- D; p6 w5 S( f* C. l! j8 a // executed in a specific order, but these steps should
' E2 ~+ N( M0 |5 m7 j // take no (simulated) time. The M(foo) means "The message+ q( e2 |* n0 l0 B& G
// called <foo>". You can send a message To a particular% n G& d7 m8 ]* p) c* d9 k
// object, or ForEach object in a collection.
+ u+ G! w) k* Y0 B+ f% [
! e2 Z5 S. M4 c/ ^2 [, z8 U // Note we update the heatspace in two phases: first run
9 Q& }- W3 ?) A8 ~2 x: j# E7 Z // diffusion, then run "updateWorld" to actually enact the
8 r2 N7 t2 a4 b2 W' Q // changes the heatbugs have made. The ordering here is1 h' `6 r8 A4 u2 ?6 p5 R: n4 C
// significant!
; T+ N/ c9 A+ Y
( e) l0 |3 U2 p* e8 H' z# U // Note also, that with the additional
. w' U9 c6 Q4 _5 | // `randomizeHeatbugUpdateOrder' Boolean flag we can$ q* I7 K4 a: A8 v2 Y4 `
// randomize the order in which the bugs actually run
! D# e- J# o: C* w5 n5 \ // their step rule. This has the effect of removing any) M! X7 M& k j
// systematic bias in the iteration throught the heatbug0 }$ N; u* W! l1 C3 A
// list from timestep to timestep
+ r; f, I; W( S5 R' D0 {- H% ~
4 A/ A; F1 j4 J, M0 T$ m% @ // By default, all `createActionForEach' modelActions have
* j% i4 K6 J6 j" | // a default order of `Sequential', which means that the) y4 z8 W- q5 Z% T
// order of iteration through the `heatbugList' will be) j" ]9 t, P8 G0 j* Q
// identical (assuming the list order is not changed
. l% ]/ c6 J& a* { // indirectly by some other process).. T& j* z/ ]* w- P0 _
+ W3 b5 e$ ]% `! W, b# H* P modelActions = new ActionGroupImpl (getZone ());5 |' z5 b6 Q$ H9 K2 k
& V7 R8 H- [( y$ s, \( D5 @ try {5 \0 J/ N) O: w5 ]; R1 Y1 i
modelActions.createActionTo$message
& L3 }8 t# _+ r+ r3 u8 g+ E (heat, new Selector (heat.getClass (), "stepRule", false));
7 ~1 U' B. t5 e g } catch (Exception e) {
! e& S8 |; S3 O% r5 Y System.err.println ("Exception stepRule: " + e.getMessage ());
) i& [6 ~- Y( H4 S# } }) }" O, T% C3 @6 ?/ V8 B
! w( D5 ]9 l+ r, @/ x try {; a& q4 g n# z( B. |/ w7 I6 S
Heatbug proto = (Heatbug) heatbugList.get (0);
. G5 a7 s+ t+ E6 b: X Selector sel =
$ V" n2 {' J# ` new Selector (proto.getClass (), "heatbugStep", false);
' c1 r, w* ]" [. G$ B! F) L actionForEach =
# D# L: o5 g* I9 z5 t) M modelActions.createFActionForEachHomogeneous$call+ \, T* }& R' @0 c0 M F$ {, K
(heatbugList,
) k9 x0 O- b" ?+ Q new FCallImpl (this, proto, sel,) t! M A D. @% a* \
new FArgumentsImpl (this, sel)));
# [' w' u }5 B* q% X' `4 z, V } catch (Exception e) {
& U6 H, m0 y [+ M6 z ^9 i e.printStackTrace (System.err);* G+ B1 d7 M1 c+ s0 X
} u; _& {+ R; O& Z
+ F0 W, c' x- f( F7 {" Z syncUpdateOrder ();
! G3 A" m. A9 |
6 D7 O* [: w; j0 x# w6 r% u) V try {$ @, Y; S9 a& [& A, I) J* S" p
modelActions.createActionTo$message
" x' h% Z, m& I `1 Z, o( y (heat, new Selector (heat.getClass (), "updateLattice", false));
+ ?% J# P" c* R4 R } catch (Exception e) {
& [* \* A. ^! o& o* {+ {; X, v/ K+ N System.err.println("Exception updateLattice: " + e.getMessage ());
6 @# r2 s1 b) o2 Q } J) T) Y0 U0 S6 L; q' g: l4 ^
5 n# S( L, f3 a
// Then we create a schedule that executes the
' I* i5 `8 D# F5 k" z( n0 J // modelActions. modelActions is an ActionGroup, by itself it
2 a4 \* y/ G# @0 Z) ?" n! g5 F/ { // has no notion of time. In order to have it executed in& U- C/ c6 L- g7 j& R
// time, we create a Schedule that says to use the
( o; @$ X! S. b3 ]& [ // modelActions ActionGroup at particular times. This" Q- {4 D% O. I( I) A) n
// schedule has a repeat interval of 1, it will loop every' n5 x$ e1 U8 U1 i! r0 J! |, v
// time step. The action is executed at time 0 relative to
4 `* u/ I; Q! L( c& Z9 V4 Y% U6 _ // the beginning of the loop.0 {+ Z2 }* d( b* i5 z4 j- ~
/ e8 I6 m# l ~- p0 z* h // This is a simple schedule, with only one action that is; j6 {9 P4 t: D2 y. n( D
// just repeated every time. See jmousetrap for more: l% S& |* j4 |7 ?% F
// complicated schedules.
2 t6 `4 a0 T7 M/ O. d% R4 t
# m% @6 j" Z E1 h6 P. e% w: Z modelSchedule = new ScheduleImpl (getZone (), 1);5 R; K) X) [3 U
modelSchedule.at$createAction (0, modelActions);
8 ~6 }7 A& |2 o, P7 C" ^; f ( F+ }- F: K# M/ s4 K' ~
return this;
, X& q" R# ~. ]$ ? } |