HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 C+ Q* M9 u, P$ A
4 J7 A5 a7 Y" V8 X* ? public Object buildActions () {
( q' Y2 R1 ?( @- Y6 T; _5 \8 n super.buildActions();4 h' [2 m, q% C8 c. F$ \# E6 T
3 w$ n" A4 w9 }( L
// Create the list of simulation actions. We put these in+ u9 ~. c9 }+ c5 W$ o* H4 o9 x
// an action group, because we want these actions to be
7 L) k: N# G* n: Y // executed in a specific order, but these steps should
" _; ~6 u) `( n9 b$ a( p5 `% E- R // take no (simulated) time. The M(foo) means "The message/ G* k( _9 j8 z% M) u
// called <foo>". You can send a message To a particular
1 M: n) {/ E: T! y( L // object, or ForEach object in a collection.
% r5 _( g0 t( h9 n
+ S5 r, j3 n$ C // Note we update the heatspace in two phases: first run
0 m# m7 A+ b& N, [- ?; r // diffusion, then run "updateWorld" to actually enact the; [1 M8 e$ O7 T7 ?1 K8 E( Q- d) _& J
// changes the heatbugs have made. The ordering here is& |! H) `) `. H6 x2 |8 a. [, _' O
// significant!9 D0 p, a/ T0 d( `4 ^9 D" g
1 ]- S( J! w9 n0 z$ ~) s0 H
// Note also, that with the additional
/ B# w9 n1 f: z: |/ L! r // `randomizeHeatbugUpdateOrder' Boolean flag we can) {- |! @* _" o M
// randomize the order in which the bugs actually run
2 s* s: \3 d3 R# A9 L$ d( @+ c // their step rule. This has the effect of removing any
4 f& O4 `) {5 E! C! w" Y // systematic bias in the iteration throught the heatbug" W; y' @( D- O! C- I/ C; Q
// list from timestep to timestep5 z0 y# T9 ?/ F& R; g; S4 c
y ^9 k1 K# G+ I9 G
// By default, all `createActionForEach' modelActions have
( ^5 r7 b" ^8 t% V! [3 g // a default order of `Sequential', which means that the
/ k' q# D: I7 D7 }! m // order of iteration through the `heatbugList' will be
/ Q7 ?' n# O2 @! b9 P2 A& h; O // identical (assuming the list order is not changed: ?, T0 h/ W3 o6 c9 G
// indirectly by some other process).
L+ Y, Y. ? X. c 9 f$ I: W- b: ]
modelActions = new ActionGroupImpl (getZone ());- j0 x7 D. n+ D1 I6 |
+ }( q. g- ?. J# S# L. V" w; A4 z. L try {
# q# P, N( Z$ e1 O; s1 a2 ^% ? modelActions.createActionTo$message
" J. y8 g$ T" b0 _& k (heat, new Selector (heat.getClass (), "stepRule", false));
t6 x" n9 g+ _: `, o! q! M& h } catch (Exception e) {+ A& }/ c: x. j3 ?4 |9 t' h! H: j! u
System.err.println ("Exception stepRule: " + e.getMessage ());7 t% }8 ^" b6 k% x( Q" c/ F
}
: D! [0 D: L5 H! y% h3 {1 T8 ^7 U, N$ `# e- L5 C
try {/ i1 W5 C9 s E" S$ X8 J
Heatbug proto = (Heatbug) heatbugList.get (0);$ @( O- @5 j* I. v
Selector sel =
r9 X9 k# M) R8 i2 G1 \ new Selector (proto.getClass (), "heatbugStep", false);0 h7 ^6 {" T& ?: K7 \
actionForEach =1 g$ l4 z6 N3 N' g; ]# W
modelActions.createFActionForEachHomogeneous$call
4 v' c2 X2 k% Z* Y5 ]- B3 L (heatbugList,
4 G" H) c1 ^* n8 C5 U* u new FCallImpl (this, proto, sel,
9 P4 u1 }3 z8 f' M( ]7 | new FArgumentsImpl (this, sel)));
( h9 ~! c1 ]5 M } catch (Exception e) {# _# ^: F* w/ a, `- y
e.printStackTrace (System.err);
7 u+ {& ?% H( B) E) C }! s1 y8 g' g9 o) z+ l
' ^+ d0 M* X/ r G
syncUpdateOrder ();. X* }& Z4 ^6 q% N. y- @
8 c3 d" Z1 |, b8 d4 { M" n @5 B. ] try {& {* |$ T5 k. K* n
modelActions.createActionTo$message . {' I- L2 }8 v# \
(heat, new Selector (heat.getClass (), "updateLattice", false));5 t$ [0 D) Z6 I# J6 R, v
} catch (Exception e) {
% e: b ^! Z6 q8 W. p/ e System.err.println("Exception updateLattice: " + e.getMessage ());
' H6 v% Q# ]& [1 l }
. \) ^+ ~6 Z$ D+ S) m& K7 ? 9 O+ p. a# t$ B6 t7 A5 I
// Then we create a schedule that executes the" C F) p& a/ x: T2 [/ g2 M
// modelActions. modelActions is an ActionGroup, by itself it
! Y2 T+ X/ f) c# x J // has no notion of time. In order to have it executed in
! @# ~: }& |6 k- B' Y1 k // time, we create a Schedule that says to use the& p& y+ P' H3 [9 k
// modelActions ActionGroup at particular times. This. P) T: u( O) Z. s
// schedule has a repeat interval of 1, it will loop every
" O! v- j5 a7 F7 g // time step. The action is executed at time 0 relative to) J: P; Z6 F- }" C0 }7 J
// the beginning of the loop.
3 a: F) B0 ^% C! n# D5 ^- D
9 l3 x2 N2 b' j2 z5 x // This is a simple schedule, with only one action that is
3 I( G+ F4 M! z8 E4 Y% M5 r // just repeated every time. See jmousetrap for more
% Z' u4 l% e7 y // complicated schedules.: ]- n; b: _5 P9 s6 c
. T' w. L3 J6 Y2 ]
modelSchedule = new ScheduleImpl (getZone (), 1);2 m1 P# E: z) r
modelSchedule.at$createAction (0, modelActions);' y }* k1 n% l1 Y+ q
9 X6 @- ~! u) x/ E0 e8 W
return this;
5 X7 S& }$ Q- B+ n- R& t } |