HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 T N, b/ G V
2 a) Z1 d7 Y' k/ h) \* y1 Y' h" }) ] public Object buildActions () {
" g% S. x+ x# D8 p4 g super.buildActions();/ ]/ s7 l0 z# D5 S7 C+ Q# h- _
& a. q, p; u n7 K% L' s // Create the list of simulation actions. We put these in7 c* \8 f# I3 S2 b
// an action group, because we want these actions to be
7 ^' Z- a; O O8 a' H+ x // executed in a specific order, but these steps should
$ v% c4 i; r5 P1 G4 Z# L, C2 A // take no (simulated) time. The M(foo) means "The message7 N9 n2 P; y1 d9 ^9 N* `
// called <foo>". You can send a message To a particular9 M6 b9 s* I4 i2 R" |% k' X, Y
// object, or ForEach object in a collection.
' t; i$ U% M8 E+ f 4 T. z2 [4 s. a$ c3 D4 y) A/ E
// Note we update the heatspace in two phases: first run% Z# D9 G8 v- M( J
// diffusion, then run "updateWorld" to actually enact the
! ^; \' ^' C3 X" w/ h t y // changes the heatbugs have made. The ordering here is
8 [7 R: J" J1 O1 t$ H // significant!
5 f2 C5 }0 `$ {4 X, x / z$ s/ X: @! j& f$ J/ T k
// Note also, that with the additional
9 J% D/ z U! E; }) N( V // `randomizeHeatbugUpdateOrder' Boolean flag we can
. I8 ^# Z; Y f4 W+ E8 w9 a9 ~. p1 z // randomize the order in which the bugs actually run! D5 s1 Z- i9 u4 M& q
// their step rule. This has the effect of removing any
' Z) l8 C. V. C1 t2 \: X" z( L // systematic bias in the iteration throught the heatbug
. l, ]+ W( E3 `, Y7 Q0 v // list from timestep to timestep" d9 |* }( y9 O+ w- d6 D4 K+ Q9 K) r
4 y+ \/ s5 U/ K/ G3 b6 ^( m& ]
// By default, all `createActionForEach' modelActions have! T" e( J- |3 l, C1 k2 [3 M& ^0 b7 c7 q* Y
// a default order of `Sequential', which means that the
. e3 T$ a4 w0 b' Z$ b& _% B& r) R // order of iteration through the `heatbugList' will be% T1 ~. ]! W- t: E
// identical (assuming the list order is not changed
+ t; @% Y; _9 [+ N7 a( N // indirectly by some other process).$ @2 v$ S6 x% l( D+ R8 p
# E& c7 g# C) @# D# ^7 ]( F: { modelActions = new ActionGroupImpl (getZone ());
3 f0 V8 [. i' f" \3 \0 `
: i) G1 d" b: S6 @ try {7 r7 [& w* i( j+ ]4 J) z
modelActions.createActionTo$message! A) ^' B; c) l0 s2 E! i3 R# j) s w
(heat, new Selector (heat.getClass (), "stepRule", false));
: l! r5 Y5 G$ ^ } catch (Exception e) {
% J- P# I0 Z5 p' A# S' j System.err.println ("Exception stepRule: " + e.getMessage ());
! o8 @# N5 K2 ]9 e }
9 A( T1 S, [' Z6 [# U6 N2 y2 Y' t; @2 a* T: O
try {
& x2 e z# f+ \# ?6 T, Q4 | Heatbug proto = (Heatbug) heatbugList.get (0);- B5 J: f0 |4 C( Q7 S
Selector sel =
) H1 M1 a* E; U3 e$ x2 _. t6 z6 I new Selector (proto.getClass (), "heatbugStep", false);
1 j4 `) D) I+ [4 z8 c5 { actionForEach =
; s9 `' }4 H8 Q% W% Z& p9 O, b modelActions.createFActionForEachHomogeneous$call
' \7 l; V# `6 a8 a (heatbugList,
: z9 F$ \. ~( v- i new FCallImpl (this, proto, sel,1 R& c# K- Y' l- k) Q. Y" o3 j
new FArgumentsImpl (this, sel)));8 j3 s4 U& r, K( H: \7 Q0 A) t
} catch (Exception e) {
# L- I! o' s9 |# S) P e.printStackTrace (System.err);. z! O" g/ }* r m
}
: b. u$ ?: X; [: Z1 E1 n
' m$ \4 l- a; v- _( r syncUpdateOrder ();. b5 u* C& K: s, |. l. |: D" @
- p( ~% h3 g% G* d5 m% C; q3 z. {0 F try {
1 n* i9 E! M1 r! |* D8 j modelActions.createActionTo$message
! t \& I1 j$ m$ q: k/ n* U (heat, new Selector (heat.getClass (), "updateLattice", false));
: r3 ~9 y; f' ?+ }1 n } catch (Exception e) {
8 N# b* `( ?- r: t5 f2 u System.err.println("Exception updateLattice: " + e.getMessage ());
5 B$ ?" z( L+ B3 } }, J- K; A; N+ h' k* d
. J5 ^: Z1 P' v9 P$ K: i2 W2 P // Then we create a schedule that executes the6 ~; R9 A, v% W P/ } O. c
// modelActions. modelActions is an ActionGroup, by itself it ?$ k2 P! R2 V$ z& m8 S
// has no notion of time. In order to have it executed in
5 L# ]. R2 S3 s- P: M // time, we create a Schedule that says to use the( A4 m: @# B8 e$ W7 H e
// modelActions ActionGroup at particular times. This/ {) G5 S% J: Y! B( I, u( z* r
// schedule has a repeat interval of 1, it will loop every3 G* l4 ^, Z* K
// time step. The action is executed at time 0 relative to
2 |% a+ }9 D* r# z1 i // the beginning of the loop.: ]% I) e( K+ O' g3 z5 I2 M, X
& p) @9 i: H5 x7 |9 H& @ // This is a simple schedule, with only one action that is" a* y$ w4 Q+ v" R3 i( L% I
// just repeated every time. See jmousetrap for more
! y3 Z! H6 k) L! X // complicated schedules.
% ], E1 H9 Z; a. ]+ H( t
|( b: ~# A( z A- x Z2 G8 l modelSchedule = new ScheduleImpl (getZone (), 1);
r5 ]8 e/ A% N7 ]+ L6 q modelSchedule.at$createAction (0, modelActions);
. S0 J$ T9 e# Z1 A# E4 o
0 V2 B1 I+ g: d& f; d1 a* m return this;6 Y) E7 d' l( N& S9 e4 h$ ?' O3 g
} |