HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 B9 Y" z- m$ l2 g5 t
- a6 ~1 c6 g. H$ s. {" I public Object buildActions () {
" K% }8 ]. Q6 |4 D" S2 Q super.buildActions();9 J1 A% S) x# a8 g ]% V" M
0 J3 [0 Y" b$ L; F9 g5 w9 \# O+ ^
// Create the list of simulation actions. We put these in# _3 f8 K4 U4 ]2 @' u7 a. d
// an action group, because we want these actions to be
" o+ r% l$ `8 z) `- v- g, W // executed in a specific order, but these steps should" f/ ]5 [' R( m C, ^- `* y# o% H
// take no (simulated) time. The M(foo) means "The message
) u1 S% a2 z6 ?& i& D // called <foo>". You can send a message To a particular
1 m: w5 y4 T1 d0 p- ?! B, I // object, or ForEach object in a collection.7 G) u; K, _& \+ Z$ X& ]
) g- H1 u6 h5 Z4 x0 v
// Note we update the heatspace in two phases: first run2 t5 D+ C2 y" s! r, f1 n
// diffusion, then run "updateWorld" to actually enact the8 n* k# B* t' e3 B) J3 q
// changes the heatbugs have made. The ordering here is
; a- M& P- ^5 x) ^% @) T // significant!
: k N* j& }! J b/ g 0 V( ^7 [2 U# E4 x
// Note also, that with the additional+ r2 D& P# Q' H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( I6 d1 b' L5 @- f: F4 u6 p* b // randomize the order in which the bugs actually run, c/ e& G' Q1 P5 K* S. N
// their step rule. This has the effect of removing any: q! a H; o3 X# e& l( o
// systematic bias in the iteration throught the heatbug
. \+ s8 T' U" X // list from timestep to timestep
6 w; N, w @! q6 H7 V
/ K+ s! F" c m9 d( _ // By default, all `createActionForEach' modelActions have
2 p$ b# j! e" R. S3 e( ?: d8 Y // a default order of `Sequential', which means that the
}6 [% u& W$ B // order of iteration through the `heatbugList' will be# n: m/ S; f: U! N
// identical (assuming the list order is not changed) R4 s7 d. [# a! u+ C9 ]
// indirectly by some other process).8 B8 \; D) t( z1 V) ^, y/ T
) L+ q k$ f. { modelActions = new ActionGroupImpl (getZone ());/ ]& P6 Z" D) Y* |! R
. j) E" G1 Z9 T3 T
try {
6 M' I, f V$ S5 H modelActions.createActionTo$message
5 ]8 X$ @6 N h, H# c2 P (heat, new Selector (heat.getClass (), "stepRule", false));
6 o0 y& c! a" w0 r' z } catch (Exception e) {
$ X. z1 ^+ |' X- X8 J5 ^; f System.err.println ("Exception stepRule: " + e.getMessage ());5 j* f9 X1 P6 I
}8 q/ G' Y& [+ I; j( j& e: D2 x
) q5 \" R' q8 R T
try {% n9 A- k: T8 f& m K7 x
Heatbug proto = (Heatbug) heatbugList.get (0);# J) F3 u' I9 |1 w* c" C# r
Selector sel =
4 Z2 \# Q6 w. S: } new Selector (proto.getClass (), "heatbugStep", false);
[/ a/ Q, a0 M! b* r) U actionForEach =3 N- N @0 X4 |+ i
modelActions.createFActionForEachHomogeneous$call( V# R- S8 a% ~% \* W! s
(heatbugList,: s& R: |, [+ X. j# Z
new FCallImpl (this, proto, sel,
' [# F. T! G7 p, t. K. A new FArgumentsImpl (this, sel)));
g5 g0 x$ \* s3 \! l" ] } catch (Exception e) {* w0 t* ~# X+ J7 V) m: L
e.printStackTrace (System.err);
: _' E8 d' G+ n8 X N8 V# `0 ^ }" |/ D, `" ? L' h
0 ?6 k/ f/ y/ x6 h
syncUpdateOrder (); F( o3 Q& ?5 k- X1 ^
8 ~% O; U! e/ A5 a& u try {
; n' i! V# s9 f$ t1 |5 C modelActions.createActionTo$message 2 g3 r8 Z# B$ _, P# k0 r- k+ ]2 F( ~
(heat, new Selector (heat.getClass (), "updateLattice", false));% e' H1 F" j2 t i, E7 r( x1 O
} catch (Exception e) {2 u) S* ^: U! O- S* D! S0 M+ ]
System.err.println("Exception updateLattice: " + e.getMessage ());
: t+ m1 M ^/ o' f1 o }' E; h* ^) `' A2 \$ i
% n' J" `4 a7 @# E* S // Then we create a schedule that executes the
8 D( I2 B- y* Z5 Y* i( s" F9 n: I // modelActions. modelActions is an ActionGroup, by itself it
( P% i7 |3 h3 `9 {& q3 H* F c // has no notion of time. In order to have it executed in+ C2 H; t! _) R1 B7 J7 D3 K
// time, we create a Schedule that says to use the
. a" ?* W8 P5 z* m8 c) j0 n // modelActions ActionGroup at particular times. This
9 y, @) d4 G, S1 a // schedule has a repeat interval of 1, it will loop every6 _4 ~" Z: S, y. i. `; y
// time step. The action is executed at time 0 relative to x7 j4 M Z/ J- y: w- e; C5 B
// the beginning of the loop.
4 t- p4 q) i5 k6 r$ q0 x2 `, t B4 n; J! s
// This is a simple schedule, with only one action that is& Q- g# t7 y- G+ J Q h: O
// just repeated every time. See jmousetrap for more
J7 q u @6 N2 T- a // complicated schedules.+ D4 n! p8 @ \/ x7 ]
1 `( W* l, Y7 A2 L
modelSchedule = new ScheduleImpl (getZone (), 1);7 E* @ q# H$ V/ r6 J* V
modelSchedule.at$createAction (0, modelActions);
% r- R. |4 ^. S* e# n
/ v% J3 \) p& R" r return this;: G4 k% P$ c9 `- m1 c7 ^+ W! t
} |