HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 R' m+ }, G2 e, T2 {2 x
' N: c- x8 y9 I' I3 z. ~- ? public Object buildActions () {
3 c5 g7 E4 B0 e super.buildActions();9 w4 D6 J1 n5 ?. Q
0 p6 ~8 t, D' Q, U$ ]& m7 d2 A
// Create the list of simulation actions. We put these in" y. d7 A% o4 F/ Z
// an action group, because we want these actions to be0 E1 N2 h5 X, }. z+ a9 z/ e/ h
// executed in a specific order, but these steps should- {: C' d: @6 G1 q* ^
// take no (simulated) time. The M(foo) means "The message* C6 K2 g% E/ C+ t4 `% w2 m$ c
// called <foo>". You can send a message To a particular/ C, v/ { B' C5 t/ l% g: W% E
// object, or ForEach object in a collection.
; |) Q& y; d7 ]# S9 T* f; P
; ?/ b+ p! t0 Q; k // Note we update the heatspace in two phases: first run
4 @! z4 k* z0 x" w/ r; i // diffusion, then run "updateWorld" to actually enact the9 k7 }4 d/ G. o8 U
// changes the heatbugs have made. The ordering here is
: Y$ {) P+ D# P // significant!6 x' O1 N0 }' O( r* h3 e0 q
+ r$ x# k) h# Y* I! S // Note also, that with the additional
. X+ o8 Z) C- H0 Q8 J // `randomizeHeatbugUpdateOrder' Boolean flag we can; @+ P1 ^. b4 e/ o( X( T
// randomize the order in which the bugs actually run
2 H4 _9 K% T( s1 }: e9 j // their step rule. This has the effect of removing any
2 |8 l; A8 S. d // systematic bias in the iteration throught the heatbug
: a! @( h/ k# q; P2 E1 B // list from timestep to timestep% @7 Y! t; ?; f/ k1 m
0 v7 z" ?6 l1 ?/ | // By default, all `createActionForEach' modelActions have
/ v" ?' B4 g. e3 w% ]& o // a default order of `Sequential', which means that the
! b3 s% V4 D1 x/ O6 b // order of iteration through the `heatbugList' will be
$ l) q+ {: _4 f% M$ J // identical (assuming the list order is not changed
$ o/ F( o2 n% T) N. z5 }: w // indirectly by some other process)./ C( p# r( O5 Y9 D
d) m: ?! A5 X; A modelActions = new ActionGroupImpl (getZone ());
7 P( l2 x- b" q6 X$ ^
! W6 ~* g1 D% p9 `: A- e" n try {
: c( d5 e+ `, b# h1 K* E modelActions.createActionTo$message
3 B1 L6 H: Q; b% |+ U5 k8 { (heat, new Selector (heat.getClass (), "stepRule", false));
* C7 u3 `. W6 E' H& Z3 r } catch (Exception e) {! s( q+ B: M0 `' ?6 Z5 }- `# `
System.err.println ("Exception stepRule: " + e.getMessage ());
/ `; ]* ^9 |: H) ] }
' j* [* j+ C+ R" |4 R1 W
! b, m. b0 A* Q, k6 N* d% z6 G try {
, N) G! }1 E5 r; p Heatbug proto = (Heatbug) heatbugList.get (0);
0 @( V5 [/ Z: L5 N% H, z, }" X Selector sel =
4 s5 n# n, z& ~+ K. b7 [. N( E new Selector (proto.getClass (), "heatbugStep", false);0 t/ H7 v/ b2 [- N! D- h5 t
actionForEach = J/ P6 d5 W5 z$ N# M
modelActions.createFActionForEachHomogeneous$call$ Q6 j ?6 T0 S
(heatbugList,) \! F9 {/ G1 u! M- f" y% t
new FCallImpl (this, proto, sel,3 |& ^. o- V, t* t- I
new FArgumentsImpl (this, sel)));
9 O/ a! F* K7 l1 o } catch (Exception e) {
/ T" `( j4 w: } x e.printStackTrace (System.err);
e: p* j: Z" S/ J8 p6 Z }
" ?$ e: R6 J- G: z* @ 2 X1 Z) I* y, m; u; ^; U% q
syncUpdateOrder ();" q9 k; U! v3 n! P$ T
7 D. \; ?6 s* {$ R5 b
try {5 f' s) ? j+ E9 ~
modelActions.createActionTo$message 1 U8 H& S& T( {9 N- Y( \
(heat, new Selector (heat.getClass (), "updateLattice", false));% w$ j( w& K5 P
} catch (Exception e) {3 O% E/ A6 G1 Q* }
System.err.println("Exception updateLattice: " + e.getMessage ());
! ]) D: U/ J5 ^ }
+ ]0 d8 G* s" y6 q9 f ' ?+ U+ k+ B& E
// Then we create a schedule that executes the
4 v8 L% ^8 ?& N6 K9 x. u! p/ s; B // modelActions. modelActions is an ActionGroup, by itself it
) y6 `) \; |& R% t7 X X; e) ^* I // has no notion of time. In order to have it executed in8 j' I) o* P+ P" U8 ^* @
// time, we create a Schedule that says to use the( K# B- W, h; [: D2 U
// modelActions ActionGroup at particular times. This
8 {( H+ D7 ^0 ?; A% L // schedule has a repeat interval of 1, it will loop every/ ]( E$ T. O' X
// time step. The action is executed at time 0 relative to
* e A$ F% V3 m3 P // the beginning of the loop.6 }% h: a7 e' ^2 Z2 O9 x
* O0 E8 M8 I! [) H% Q
// This is a simple schedule, with only one action that is; m; }% H6 g/ G9 z/ H/ q* R
// just repeated every time. See jmousetrap for more
, B( E3 o, b7 F+ ~ // complicated schedules.2 C5 \9 X7 f+ o% k$ f3 i U3 o: O
) T# y6 y# w4 q. G* } modelSchedule = new ScheduleImpl (getZone (), 1);, \' k* `- H8 x8 h, F }
modelSchedule.at$createAction (0, modelActions);3 G j6 m( S6 o
4 d9 | A6 K4 E0 `% H) P
return this;1 V4 z# Q: l, ~: e9 E) H' L; D7 M% F
} |