HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- @# s* _( i0 e; A5 W6 C
: g$ w/ ^% x5 l& Y2 I public Object buildActions () {8 U+ P( ?7 m1 Y3 I f
super.buildActions();
/ {; t5 E: T* P7 ~ 6 r+ o4 ]3 o: X3 P: B; u
// Create the list of simulation actions. We put these in! y3 `+ |5 h9 s u, z! x
// an action group, because we want these actions to be
" }! \$ R+ Y2 Z0 o; y // executed in a specific order, but these steps should
. _( i/ s) x* }( P- S // take no (simulated) time. The M(foo) means "The message$ r8 C- u( G5 v, l
// called <foo>". You can send a message To a particular4 y2 f. G# ]+ A6 g: Y% P) E
// object, or ForEach object in a collection.
- D: u/ B9 p: a: v% R$ W T8 h " r, w) ~, Y* V4 R% \/ n& l
// Note we update the heatspace in two phases: first run
+ Y7 p( i, g; _( @ T // diffusion, then run "updateWorld" to actually enact the( @0 z9 z) Z- t. |
// changes the heatbugs have made. The ordering here is( S3 ~' w7 L; _$ L5 f+ U/ D5 b
// significant!. q! f6 V9 I$ W0 z
$ Z4 B( Y; p) Q* O5 ^" V' d# C
// Note also, that with the additional9 Q$ C" o* [0 ?; x' I2 |; t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" f! I- |+ k# H9 y) v H/ A, K // randomize the order in which the bugs actually run5 O; N& O4 a0 H0 h- {' r
// their step rule. This has the effect of removing any y( T" [4 m) ?3 z
// systematic bias in the iteration throught the heatbug$ e2 L3 }2 \- @$ t
// list from timestep to timestep3 j$ T; n# f" @* }! C
[- q4 P3 i9 O2 v' A5 j // By default, all `createActionForEach' modelActions have
4 G$ J" @& f+ C3 c // a default order of `Sequential', which means that the
* h7 X. r5 W+ {4 D& h7 i // order of iteration through the `heatbugList' will be5 t8 ]" |- a2 |: y0 Q" y* F& k
// identical (assuming the list order is not changed
/ t( q/ Y& F8 n9 f$ e2 h5 `, q // indirectly by some other process).
% k2 b0 n# e8 K7 H; J( _ l m5 u" g8 T; [1 {
modelActions = new ActionGroupImpl (getZone ());+ @. H& w0 i# {) d
! k @) w' p% ~. c2 c! ^1 P# H+ u
try {! Z# t+ A, C6 M+ E
modelActions.createActionTo$message
2 N1 \0 a8 t/ c; @; M (heat, new Selector (heat.getClass (), "stepRule", false));5 ~1 ^1 x$ U( L/ U! a* M$ W/ V2 i
} catch (Exception e) {
& A% Q7 s, y: ]; [! z9 Q/ z: e System.err.println ("Exception stepRule: " + e.getMessage ());0 X8 ]. a5 N. M" n p
}
- M! T$ v4 A' i" P8 |( T# y( v0 @% L' N& N
try {
8 X- h. J7 X+ r1 W, |7 u Heatbug proto = (Heatbug) heatbugList.get (0);
t9 N2 o1 _9 L Selector sel = ( k9 L: T$ h3 O3 q1 w0 W1 F
new Selector (proto.getClass (), "heatbugStep", false);5 v' l/ x( f4 @2 r
actionForEach =" w- l7 B& Q$ O' v% U* Q
modelActions.createFActionForEachHomogeneous$call
2 M- G3 `! g, T$ \: o (heatbugList,2 W) H% ?) c% @0 b; W" W3 e
new FCallImpl (this, proto, sel,
- {$ d* i* T" s1 k2 N7 J" ` new FArgumentsImpl (this, sel)));
/ y2 g" d/ w4 v2 J } catch (Exception e) {. B/ }: Q: f* z& I/ `7 y
e.printStackTrace (System.err);
. o& [$ ]: _$ A( |. ^ }, y+ O6 v: B% J' C# {$ ^
) {+ Y/ c; p; @, q syncUpdateOrder ();
" Y4 w0 l/ I+ M6 X: c/ l$ h: Y# H( B$ o) i# }# q) q% U$ |% E! V8 L
try {* I# s, N9 d, K9 s3 z3 d$ s' E, G
modelActions.createActionTo$message
; w; v/ f; a0 J, v. T! M, X (heat, new Selector (heat.getClass (), "updateLattice", false));
! v" [$ K7 R/ G. f' c' s } catch (Exception e) {
4 @& \0 ~6 r6 i" x4 R: Q* ] System.err.println("Exception updateLattice: " + e.getMessage ());8 B4 |$ f/ w7 U, t M
}: S7 |5 G# N* E% L$ A/ g6 [0 _
; e9 K! K- _+ e" j$ ^
// Then we create a schedule that executes the# w; e I0 |8 ]( [, V d: m
// modelActions. modelActions is an ActionGroup, by itself it
6 J1 f X* N& `& o // has no notion of time. In order to have it executed in
+ P7 c8 F% q, K. Q1 j5 t // time, we create a Schedule that says to use the
: k) @) X o; ~ // modelActions ActionGroup at particular times. This
5 F" a0 I) i2 D* H* z& i6 K; p2 E) P // schedule has a repeat interval of 1, it will loop every1 ~% r' E r: U# o
// time step. The action is executed at time 0 relative to
- L _# [0 V# j* U // the beginning of the loop./ W' X% s# h; P6 ?" K
* u3 [8 p L' q$ U
// This is a simple schedule, with only one action that is) V8 G# T5 n5 ~
// just repeated every time. See jmousetrap for more3 E& z! Z& K% r j
// complicated schedules.- b* o( W) W5 t2 T* |1 h
9 J- o4 x- x+ Q7 V3 {7 t+ f0 F modelSchedule = new ScheduleImpl (getZone (), 1);7 E3 A# v% Z9 s* M7 }' T* b
modelSchedule.at$createAction (0, modelActions);6 x( M! P3 S# J% F' B$ E' p! G9 l3 f
( r; ~( {" _5 ^6 e
return this;! b: y& b2 t) v
} |