HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- }3 P+ f% X# X2 t1 S/ k* x
6 T s9 |3 E! N/ o; n# h( ]. r public Object buildActions () {
+ ~7 i" o' S2 d. Y7 h6 G+ h+ @0 v super.buildActions();
3 D8 c0 m- c ^" F i& |# Q1 x
6 b' `; S1 o r // Create the list of simulation actions. We put these in
" M: b! i7 z$ F" J7 _/ { // an action group, because we want these actions to be6 j1 L' z) N5 Z" R/ n7 X9 l
// executed in a specific order, but these steps should
0 h& s! t) q2 r3 h // take no (simulated) time. The M(foo) means "The message1 A$ Z, N8 y8 I3 B. A
// called <foo>". You can send a message To a particular; m1 N1 V7 @; b
// object, or ForEach object in a collection.
7 _% {* ]! e# Y , B! ?* _6 r+ r" b
// Note we update the heatspace in two phases: first run
: }6 p0 ~! z, p3 z2 n) k7 A4 N // diffusion, then run "updateWorld" to actually enact the
( L& Z& G: Y6 F // changes the heatbugs have made. The ordering here is* O# d$ v V0 F3 ?. y/ G5 H
// significant! N; ?; z8 S- R2 W% _# @# I9 C7 J
# `* n$ y' p3 P7 A2 c
// Note also, that with the additional! u. @9 b, {7 v; {3 Y, b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 J6 R [% B7 T6 d5 w8 t3 w @, C // randomize the order in which the bugs actually run
" T3 D1 \ [6 w- M+ p- @9 e // their step rule. This has the effect of removing any" g# C0 a9 \4 u/ A! }4 g! ]
// systematic bias in the iteration throught the heatbug$ P6 F4 b9 t5 U( t/ f
// list from timestep to timestep
* s. u g9 H8 y0 Y7 e" ]
7 V1 R) G9 ~. v+ X$ m6 t) P- G // By default, all `createActionForEach' modelActions have
6 ]/ y0 q7 C$ j) @9 s- M4 m // a default order of `Sequential', which means that the
9 ^4 z4 W1 l) i/ Q2 L // order of iteration through the `heatbugList' will be
: b/ v Q1 e' {# U // identical (assuming the list order is not changed# S N- h2 m, F7 m
// indirectly by some other process).
2 X0 V. J' K+ f6 E# p6 E' P5 J 8 U2 d( K8 m7 J% x2 p$ R5 `
modelActions = new ActionGroupImpl (getZone ());
( E+ q( M8 F7 `4 a( Z8 ]& g0 b, H* ^
try {
" T# d* L$ K# U modelActions.createActionTo$message* n |1 S( r, [' \8 z
(heat, new Selector (heat.getClass (), "stepRule", false));
9 M, Z) K4 }( ]' y+ I" G* N( a } catch (Exception e) {# i0 E {0 g- u" n5 l
System.err.println ("Exception stepRule: " + e.getMessage ());5 b! J; a( h+ O& n
}( \: u8 f) S1 m! g/ F1 ]
; |3 H1 X) F" Z4 \$ ~ try {1 N3 {; Y& S' ^8 ~+ c4 b0 Y
Heatbug proto = (Heatbug) heatbugList.get (0);+ X2 I" U) ^) G
Selector sel = 9 p7 W4 K7 g( ^4 R2 }
new Selector (proto.getClass (), "heatbugStep", false);
2 i! S# _' g7 \' } actionForEach =
; @* a V7 u" v- [$ ]$ r modelActions.createFActionForEachHomogeneous$call) K q5 T6 N' a( a. g) M# Q
(heatbugList,% R% F/ |, h# X% I
new FCallImpl (this, proto, sel,* T [- D9 h/ f' _* i' x( Q
new FArgumentsImpl (this, sel)));
. k) x6 C. ?- l) O! L( k* g- {7 o } catch (Exception e) {
, L* |) B* j9 N0 l! W e.printStackTrace (System.err);% O' L% q2 ~( U+ a7 L6 x1 c
}
7 S, o7 u; _ P7 Z! A " [; p# b6 |0 v/ o
syncUpdateOrder ();- G+ |" }0 `1 \0 R2 r0 `
( L/ u* {1 ?. K( x
try {
" m7 [" u3 K8 {4 ]; E% ?0 t+ b modelActions.createActionTo$message
9 D+ P- u. Q: W2 _/ i+ S! D7 C (heat, new Selector (heat.getClass (), "updateLattice", false));
# w1 L- l' o1 F$ ~ } catch (Exception e) {2 D+ ~: ^9 S* r; \8 X X% E
System.err.println("Exception updateLattice: " + e.getMessage ());
' [" I4 X. M* m- n% m2 Z# c4 } }& y' @2 _4 q+ X- ^2 B8 a
% t. i! w; L% w; V g* W9 Q7 P // Then we create a schedule that executes the- J/ w3 P0 c$ C- Z1 N, {- t
// modelActions. modelActions is an ActionGroup, by itself it; P- B/ h5 V2 n( F# T1 ]* g" b
// has no notion of time. In order to have it executed in
7 |0 {. [- x: y% O // time, we create a Schedule that says to use the8 m" q* z, C2 k
// modelActions ActionGroup at particular times. This
G4 `3 I, w, b& R7 P, i // schedule has a repeat interval of 1, it will loop every3 L# ~4 v6 ~. L6 u8 F$ y# u
// time step. The action is executed at time 0 relative to
5 |. `2 U$ z) o$ ~$ ~5 T // the beginning of the loop.
4 K, u" U' b- b' h9 q! Z5 W! U; T5 m; b# C, ^ @5 [$ k
// This is a simple schedule, with only one action that is
, Z F# M; e, g9 p o( |' c // just repeated every time. See jmousetrap for more0 v' ~! n" W* ~5 C! p9 X
// complicated schedules.
' I6 F5 x6 f6 k. }4 k. T : a+ o. q: M) M: i
modelSchedule = new ScheduleImpl (getZone (), 1);% m" \" q4 t6 s) d% v- k+ R# U& e
modelSchedule.at$createAction (0, modelActions);
. H: o' ^9 S6 D
* x8 @& x/ G/ |# R0 s* R+ Q return this;
% w* h5 m5 [- N- r) b& R } |