HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 |4 _9 {( z- Z3 |7 T0 @; W
: R; K6 D( ] k: F6 p/ T7 A) A public Object buildActions () {
0 R. Q7 `8 {) f H, w2 B1 R! y super.buildActions();7 E3 p: s6 K9 e8 L
* P ]+ E" z2 \% Z* ^
// Create the list of simulation actions. We put these in8 v5 j* ^$ f9 S7 Z
// an action group, because we want these actions to be
0 G* E3 j9 U4 B' O% ~9 F // executed in a specific order, but these steps should1 Q4 m5 G( z# f3 t3 l; I% F, C" Q3 J
// take no (simulated) time. The M(foo) means "The message
( V. H( C9 t- R; R: ^ // called <foo>". You can send a message To a particular5 N3 u/ _* {9 X9 Q4 O1 S2 b& D3 {
// object, or ForEach object in a collection.$ ~3 K' A8 I8 w+ w" Q V3 q A/ `
9 r6 s2 }0 i( m7 F" T9 a
// Note we update the heatspace in two phases: first run2 [. M5 G0 ~1 B1 u4 X* l5 j- c
// diffusion, then run "updateWorld" to actually enact the
& t! p+ Y- S3 M3 @1 t // changes the heatbugs have made. The ordering here is5 Y" |9 c- m H+ n5 p' W
// significant!" R4 H2 r% x- |
$ B+ M% E; p0 s9 `1 l0 k. [
// Note also, that with the additional
3 m. k: _+ o* s! W7 H f4 | // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ `& A% I+ i5 d: I // randomize the order in which the bugs actually run& N4 x% R% u- `- a3 T8 e
// their step rule. This has the effect of removing any
5 H( \! O$ ^& P( Z // systematic bias in the iteration throught the heatbug2 _! q/ I$ x5 @5 b5 |2 `+ p- C M
// list from timestep to timestep+ y0 e9 Z3 j" b1 k+ w
# z/ i* `+ I# A/ i // By default, all `createActionForEach' modelActions have3 Y; w( h7 @8 H8 F, C7 l- U* H
// a default order of `Sequential', which means that the
1 C6 Z! a- r- r& o7 p // order of iteration through the `heatbugList' will be
7 K, X* G" n0 F6 c @ // identical (assuming the list order is not changed. U- C4 b+ Y( x1 H) `5 e. t
// indirectly by some other process).
" O8 _- ?* N& D3 Z3 J% N3 G3 b
* ]; V' a- V4 E9 D modelActions = new ActionGroupImpl (getZone ());3 l, t" V: m3 t
; }; w. w& S4 f5 C0 u0 h try {8 R2 @3 q( M! w8 P2 w
modelActions.createActionTo$message
7 {0 Y" O- y, Q9 d; w1 a (heat, new Selector (heat.getClass (), "stepRule", false));
' S4 z2 p7 E! f7 h$ x. G } catch (Exception e) {2 M! |# {; O" A
System.err.println ("Exception stepRule: " + e.getMessage ());5 ^" h) E# ]1 ~* O
}. M7 X! `8 u& A; J3 c& z9 d
3 i" m$ b1 }5 o. c3 h
try {) M d4 N- m) }8 ]2 X) D2 _' a
Heatbug proto = (Heatbug) heatbugList.get (0);( B5 h1 n$ T! ?/ L4 |4 O
Selector sel =
* K% b. Q( q! N6 c: _* }, f new Selector (proto.getClass (), "heatbugStep", false);
0 G) F. |6 m& x9 h5 \ c actionForEach =
* h! j; ~) F; i- D3 F7 `( { modelActions.createFActionForEachHomogeneous$call
* H c1 R, }5 {5 u" k (heatbugList,
$ d5 b7 T X ~* P. R2 G' f. \9 w, D* V new FCallImpl (this, proto, sel,0 j1 c& D& y9 S [2 X" K/ @
new FArgumentsImpl (this, sel)));6 X/ \" ^$ r2 q) V
} catch (Exception e) {
0 v) D" N; {, l4 I. {# Q e.printStackTrace (System.err);
+ v* ~" h! C( \9 s, m- V, s }, @3 Y) c& L1 z% Z: O
0 U; u4 \3 [& h3 W/ G y3 p
syncUpdateOrder ();
0 ^8 q$ X* O1 c9 e3 L# J4 x" d/ l8 P) P" i" W0 F
try {
3 F4 l b( k" ]4 Q7 R modelActions.createActionTo$message % y, \. c( S* A5 U! a
(heat, new Selector (heat.getClass (), "updateLattice", false));! i. s; O) P/ D! G) g8 E2 t1 I! u5 o
} catch (Exception e) {3 u- @ l5 K! @
System.err.println("Exception updateLattice: " + e.getMessage ());
% e1 Q6 ^4 {) p8 L* I" G9 } } A$ W0 \; o# M
9 |; q) F9 S \0 f# m) B // Then we create a schedule that executes the+ Z+ l1 g1 ]4 g/ G
// modelActions. modelActions is an ActionGroup, by itself it! o& s6 M* m4 K% |. ]* U% I
// has no notion of time. In order to have it executed in
9 ]$ p7 F- Y) |6 }5 f% t# `; B // time, we create a Schedule that says to use the
U- N( A/ ~( {2 r( {; L // modelActions ActionGroup at particular times. This. z1 v1 ^' ~* I2 `, \$ z. a7 p
// schedule has a repeat interval of 1, it will loop every
& u4 Q& c( ^, a$ ]% E // time step. The action is executed at time 0 relative to7 H6 L9 h: y% w+ B; k: F" i
// the beginning of the loop." L6 H2 {* M, x8 v( W: A! R
! E$ Y1 j9 t: {$ j9 F4 S& t // This is a simple schedule, with only one action that is
1 N2 e/ ]1 L$ E E) |/ {% E- d // just repeated every time. See jmousetrap for more+ X/ u' O+ }! H! e; m- d7 y# M! @
// complicated schedules.
2 f( X" @/ V, R. ]% W3 e
) C( D$ p/ L6 w% t: `2 O! l modelSchedule = new ScheduleImpl (getZone (), 1);
+ |! B: u, w9 \; Z2 @! n* ?9 c modelSchedule.at$createAction (0, modelActions);9 f+ H' a9 l8 X! t2 A2 W" ~
4 \- L7 j; w* k; M$ o. V3 `+ Q- H3 t
return this;0 o& r a; u" U7 O4 z) M
} |