HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, l/ Z! y3 t0 t6 \( B
3 D8 x% Y% s7 g8 n9 R" ^& k public Object buildActions () {
5 B+ l+ F: D o2 {1 o) w super.buildActions();
" h: T$ @1 Q/ `. ~
! t5 o4 s. Z+ W- Z! t- E // Create the list of simulation actions. We put these in
' ` q% C. N( F# G6 _ // an action group, because we want these actions to be
1 w i7 t% `- J2 K8 S8 W* b // executed in a specific order, but these steps should
( J5 N+ s. a8 y8 ^4 W. | // take no (simulated) time. The M(foo) means "The message
1 L* f! z' s0 p+ |( @# @- y // called <foo>". You can send a message To a particular
7 u. i, t( L" F$ q3 Q0 ^( R // object, or ForEach object in a collection.
5 ? v9 w3 i6 j. c
" \! j& @8 T) N6 _: A+ k1 I // Note we update the heatspace in two phases: first run6 ?( A( K7 o" u' f# d7 O7 E
// diffusion, then run "updateWorld" to actually enact the; N# T4 H; ^& X6 T8 X' M- F+ R% {
// changes the heatbugs have made. The ordering here is* L7 @1 }6 U$ P( O
// significant!
5 [! y( N( Y0 \+ B
( u0 q1 l0 }+ D- l; `5 J- C- b9 @ // Note also, that with the additional# X! i/ z% l% |+ w, U" t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 F% k1 i! G! z' s) ^3 o7 u // randomize the order in which the bugs actually run0 v, u/ s4 w5 `8 J
// their step rule. This has the effect of removing any8 {2 @: b. `. F H) i2 U) h
// systematic bias in the iteration throught the heatbug/ D7 R% z, o1 X
// list from timestep to timestep m& z7 C3 I% v1 L& q3 e2 p
6 e6 }* ?1 S7 R1 T8 O( x. h // By default, all `createActionForEach' modelActions have
( h! m' c& \/ P7 {# U- x8 n // a default order of `Sequential', which means that the
6 \! |2 m, o; \0 d // order of iteration through the `heatbugList' will be2 c* {; ^- \7 `5 z! C
// identical (assuming the list order is not changed
0 G7 G1 b: }+ F; w // indirectly by some other process).
% x b# N% V; @7 f7 W. ]
8 L0 _3 m3 ]" Z modelActions = new ActionGroupImpl (getZone ());
3 ]4 b) Z! y3 Q# @7 E, |3 R3 f& ^2 E$ K* w! W( k$ f) Z" p- e5 [
try {
% V6 D& k" P* R$ x modelActions.createActionTo$message
9 H/ B1 G6 A. y (heat, new Selector (heat.getClass (), "stepRule", false));
" M" Q2 e# R8 }; D5 U! C } catch (Exception e) {6 _. [% B1 d! S
System.err.println ("Exception stepRule: " + e.getMessage ());
% b2 j+ M1 L; m9 I3 G: ~- { }! |" u2 n. v9 k/ f+ h5 C
1 m) H3 _ I! ] D try { U/ x: o/ o/ J+ R
Heatbug proto = (Heatbug) heatbugList.get (0);( a6 r r6 _' P+ h# k$ b$ x' a
Selector sel =
5 z8 k( ?) F) a& m new Selector (proto.getClass (), "heatbugStep", false);5 M0 a$ W8 V6 ?. k- W9 L! j# [# j; X
actionForEach =- l! O! Q3 A& N
modelActions.createFActionForEachHomogeneous$call
; |4 Z' H4 B; j3 Z (heatbugList,
0 m& r5 U0 k+ G4 m* t- r+ G new FCallImpl (this, proto, sel,
1 r) W* {# z. y* ]! Q new FArgumentsImpl (this, sel)));
% |( G: Z7 I0 p" e X( y; w; Y } catch (Exception e) {0 t+ c6 i1 n9 N
e.printStackTrace (System.err);
8 O+ V' h% \* k y4 Z }
6 C8 W6 y \" V" z( h1 N& G
$ U! h# ^6 c& m8 c: A2 J syncUpdateOrder ();
& a1 H( X) R1 v$ L, f F% A, d
" @8 X. w& r: O" v) r try {
6 z* o3 F, k6 n$ x" w) U: r modelActions.createActionTo$message
( _2 b+ K8 g+ A6 ]* e5 E# y (heat, new Selector (heat.getClass (), "updateLattice", false));
/ _7 v$ @" }$ n2 v, Y } catch (Exception e) {
+ X. P, V0 p5 J+ N System.err.println("Exception updateLattice: " + e.getMessage ());
% r0 R9 }- E3 _: R. O }
% s K+ u( W& [+ a$ `0 {; L% h9 q: g8 N 9 n7 X( A$ y* J
// Then we create a schedule that executes the
, [( ], t. t8 B$ p9 U // modelActions. modelActions is an ActionGroup, by itself it9 P4 m# Q/ M! l* O
// has no notion of time. In order to have it executed in. Z- i8 |5 {! }; S3 E
// time, we create a Schedule that says to use the+ s! R& V c( B' l% }/ W: f
// modelActions ActionGroup at particular times. This3 v3 S6 d: z& D3 G) y$ k. O! i
// schedule has a repeat interval of 1, it will loop every5 G; L& h; H5 v7 R) r$ P
// time step. The action is executed at time 0 relative to2 X& q8 y( t, y _$ I
// the beginning of the loop.+ W2 z9 q3 w1 l9 V2 M
2 f# `* e9 r! m5 Z // This is a simple schedule, with only one action that is# T7 \# P o+ M* G9 ~
// just repeated every time. See jmousetrap for more
* A# B( m% D9 z1 j" g* M; ? // complicated schedules./ O* ]: p' K+ m5 Y7 ~
: ?& J& A/ S$ W" K; s: Z" I modelSchedule = new ScheduleImpl (getZone (), 1);
$ }3 l9 P9 j) [( [% r% J: |) w" M$ d modelSchedule.at$createAction (0, modelActions);9 u8 K3 i9 y5 d/ V- N. F
8 O, [3 _% u, w) a
return this;
, s# V# {2 c3 t( L0 @ } |