HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 ? u* `7 c9 p H: r) U3 o
& I) K( `4 r+ ^% \' H! n- H public Object buildActions () {" z/ ?3 x" E/ _ E6 |: P
super.buildActions(); Z8 |5 G+ r5 \ M" X
/ w( D9 g. O$ q
// Create the list of simulation actions. We put these in: l# v6 L+ R+ z" h
// an action group, because we want these actions to be+ y" B- d5 ~6 P# n" h8 L9 d
// executed in a specific order, but these steps should4 l! l9 ]* D: a/ D
// take no (simulated) time. The M(foo) means "The message: G! V9 B& A, x) q: i
// called <foo>". You can send a message To a particular/ n8 X1 ~7 U! W$ d2 \* B" N3 ~9 N
// object, or ForEach object in a collection.
' Q _: i& v/ V5 H" \ + d3 j- l; s( Q L
// Note we update the heatspace in two phases: first run
" |& ~3 W6 |8 ]& F5 t+ x7 e. g // diffusion, then run "updateWorld" to actually enact the
. ]# m# [& }) ? // changes the heatbugs have made. The ordering here is
4 z2 m' i6 A+ L- u' \ // significant!
; h3 A8 t7 C S6 P9 P 8 J( O# i+ M" w+ I0 E5 `; r" y
// Note also, that with the additional
. b3 H! H9 i( a% [- C" o6 P; } // `randomizeHeatbugUpdateOrder' Boolean flag we can- E2 F" D5 V% @ }
// randomize the order in which the bugs actually run
; ~ f, Z7 R+ y1 Z; M3 v* {) ^+ L1 U" C // their step rule. This has the effect of removing any
6 y6 T" r+ X ~! E% A4 H1 u // systematic bias in the iteration throught the heatbug7 q% P: S, `9 k- S7 p; s( E: O& U
// list from timestep to timestep) O7 l8 t, R8 l9 b0 S3 i! A x
, E# M: o! B# v# D# R& V9 N
// By default, all `createActionForEach' modelActions have
# U8 n6 S7 \. ^5 ? // a default order of `Sequential', which means that the
, M1 ], G; d l // order of iteration through the `heatbugList' will be
% ]4 h( u% f1 \3 e" W // identical (assuming the list order is not changed
7 z+ m9 Y; s! p8 ~ // indirectly by some other process).
6 j, R2 k# P5 S3 _: k$ c
8 j' d+ v- p, [9 Z0 ` modelActions = new ActionGroupImpl (getZone ());# z! V) ^1 R" p- p+ J- |8 Z- d
& f" ?$ r7 V1 j6 P* U# z: W
try {
7 b; T- e" d; n* I# J5 } modelActions.createActionTo$message
* `6 L6 ?$ i$ O/ e! F$ n. k4 e (heat, new Selector (heat.getClass (), "stepRule", false));- B0 f% S7 k6 Z& Y2 W9 @
} catch (Exception e) {
3 r$ v/ o/ W! ~9 n# M System.err.println ("Exception stepRule: " + e.getMessage ());; E! n$ S. u. ?& ]
}" w3 z( V' z( j1 q, E
$ s3 S3 V- r5 ^! v% x
try {
2 e( M1 q6 N# z2 P6 }' [9 ?2 K) T/ Y Heatbug proto = (Heatbug) heatbugList.get (0);
, f W' _# c# t: z Selector sel =
$ h2 j0 C. a% M' [: q new Selector (proto.getClass (), "heatbugStep", false);
3 p2 k0 L5 j4 C) Q" C actionForEach =6 P J( X# ]$ `: t+ m: [
modelActions.createFActionForEachHomogeneous$call, t8 S5 d& F/ ^; ^3 \7 J
(heatbugList,, @1 O2 R$ M) \; B8 U. Y h
new FCallImpl (this, proto, sel,
/ t/ ^- w5 @- n7 J& J3 i7 J6 |% d new FArgumentsImpl (this, sel)));5 |2 F5 P9 E9 c+ i i& w
} catch (Exception e) {; p% a; @! T) \- R2 Z: [3 f
e.printStackTrace (System.err);
+ R" h( h: o. T }& r" }/ |" k: y) T
6 K, r0 p7 s' G! f1 I x* ` syncUpdateOrder ();8 }! v% e, t @+ X/ \" i) x0 v5 h1 k, p
0 y3 Q2 E7 [' X* s) t; p try {; B- [6 c# T: \# [
modelActions.createActionTo$message 4 B n5 C$ e( t, v* G1 q) n
(heat, new Selector (heat.getClass (), "updateLattice", false));; E T3 Z( e* k0 ~2 ]
} catch (Exception e) {
Z7 c1 U0 J4 `7 O% @8 F# [ System.err.println("Exception updateLattice: " + e.getMessage ());& ^7 v/ h8 M5 K) ?* F
}
; U, d. O. H: H h! o% q5 Z
" y6 f5 P$ x# C: A( s3 A // Then we create a schedule that executes the
+ F" H% l4 \: Q' u+ s' J/ w // modelActions. modelActions is an ActionGroup, by itself it3 G5 k- m( C8 t, I( }; y5 |
// has no notion of time. In order to have it executed in4 Y4 z1 a% ^0 e7 b3 n. ]
// time, we create a Schedule that says to use the+ Z; E2 t$ I4 y" m8 \
// modelActions ActionGroup at particular times. This; ?- g( a8 o% q: i z" O
// schedule has a repeat interval of 1, it will loop every
" u, r! b* U3 B // time step. The action is executed at time 0 relative to. ^( U0 a! K* Y) t- R- d. X/ Z
// the beginning of the loop.
7 W7 c$ t5 F9 q: A- n1 J [5 _5 O7 O' f6 v, h
// This is a simple schedule, with only one action that is% x' A! i7 v/ Q+ l. @# F6 _& Z
// just repeated every time. See jmousetrap for more- H% R) j6 g7 z
// complicated schedules.
# k' N1 a+ r8 k' {. R0 V- k
+ ~1 B- A0 F4 d, C5 \* X modelSchedule = new ScheduleImpl (getZone (), 1);: L, q5 J" z& B2 {
modelSchedule.at$createAction (0, modelActions);
! O6 a' I/ J* F/ B 1 U+ W, T( h8 u& M
return this;6 ~% s& o+ v+ C, T. R
} |