HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ Z: u8 w/ \' o0 |4 U
0 N. r; M* z+ a public Object buildActions () {
* T7 X- H) M7 i% J super.buildActions();
+ |2 g( U( U' _/ O+ Q' |; I1 v, Q 9 m/ {* T: K, V* X) L1 @
// Create the list of simulation actions. We put these in
( c: h" D$ z/ L+ Z" q // an action group, because we want these actions to be3 }1 o2 i; c, Q& @9 I3 d
// executed in a specific order, but these steps should
6 a' ~3 v$ B& O9 i9 L/ I7 W // take no (simulated) time. The M(foo) means "The message
; F) ]1 F8 W# q% ? // called <foo>". You can send a message To a particular
v S3 E1 b$ I; I // object, or ForEach object in a collection.
7 ~" T9 m# l5 U& H6 ]$ }8 T 9 v" N5 e- C# m2 @ |3 T
// Note we update the heatspace in two phases: first run
" i: z% A( G6 m: n8 ^ // diffusion, then run "updateWorld" to actually enact the
4 q r2 s% i6 y. \( ` // changes the heatbugs have made. The ordering here is$ n7 o3 [- I6 L4 T5 ?* m
// significant!
3 F: k" y6 _' _ R' K" G' r7 h3 J
6 H! x8 J: @8 Z9 l8 f# i2 y // Note also, that with the additional
- I+ y& R5 w9 @8 F // `randomizeHeatbugUpdateOrder' Boolean flag we can: U$ B6 O z8 x0 w3 n
// randomize the order in which the bugs actually run
+ y' s& ]9 n$ b0 \2 B8 T v; S( y // their step rule. This has the effect of removing any
, ^; o0 J w$ x9 k // systematic bias in the iteration throught the heatbug
* O6 a9 Y& q# X) a' E6 | // list from timestep to timestep( [4 S& O( d9 ~4 h$ g* L! L1 G
, @; C# R& C8 V7 O+ w" A
// By default, all `createActionForEach' modelActions have2 N _) N: v* Y- K6 Q% |
// a default order of `Sequential', which means that the
, h& U/ x; `6 H. C+ L/ o# Y // order of iteration through the `heatbugList' will be0 `% X. B1 M) Z
// identical (assuming the list order is not changed
" S/ r1 `7 q) z6 x0 O // indirectly by some other process).& U1 h4 D9 n4 s; F, H2 I3 [+ ?7 R: a
6 k) A& z7 |4 P$ v5 n) e- v% D
modelActions = new ActionGroupImpl (getZone ());" B' ?6 ~; n' M: l. ]
* q0 r( Q; N* [' j" k* c8 d, w try {) S" a" ?. V* @ k( h
modelActions.createActionTo$message% T8 {( l0 z! M6 j. {1 D
(heat, new Selector (heat.getClass (), "stepRule", false));8 H/ {5 `& p6 }/ v+ l
} catch (Exception e) {2 m t0 _' h2 R
System.err.println ("Exception stepRule: " + e.getMessage ()); @% ]9 s1 ?0 n( |, q
}7 F3 i$ B; U) o6 ?/ w& i' ^! A1 [
1 u& W& d( c1 L& z- t0 i0 G' C- B try {
+ D$ b9 m; I* {. O1 L Heatbug proto = (Heatbug) heatbugList.get (0);% T# [5 q; w% P7 t" h2 }
Selector sel =
3 R; `. ]/ O6 p3 N" g" k2 p! X c- Q new Selector (proto.getClass (), "heatbugStep", false);
& G1 L. J* q3 ?- |+ A- Y$ w actionForEach =
& Y2 s. d9 P& F/ _, W; p modelActions.createFActionForEachHomogeneous$call) E: x- e# {- y2 z
(heatbugList,
/ n! R9 E0 w# L( N+ M6 G new FCallImpl (this, proto, sel," c% q5 @3 L9 Z5 L5 ]" F d! g" N# g
new FArgumentsImpl (this, sel)));0 C6 R# C* l6 l6 d3 m' @9 C
} catch (Exception e) {
0 x" ?8 q# R4 ]/ \, B. b6 T4 T e.printStackTrace (System.err);
6 i5 H' x6 u. q' u. D }
) ?9 } @2 L8 W6 u# ^/ X
) W# a% ^: U# h% T syncUpdateOrder ();
N( r$ i" u H6 e; |9 d' f9 A5 E% f3 p' X8 ]
try {
1 e& G, A6 M5 g3 j modelActions.createActionTo$message
) M) [8 x( r; {; q (heat, new Selector (heat.getClass (), "updateLattice", false));/ m' h H: b9 ~- l5 P7 T9 W; X' R! q
} catch (Exception e) {1 a% b% z' O* J% H% q1 H- ]- L9 J
System.err.println("Exception updateLattice: " + e.getMessage ());
p+ m0 x* O/ Z1 C. o2 @( x9 g }
n! D; J6 s! |3 A * r F- J6 |+ q& R) s7 s) I
// Then we create a schedule that executes the
) E: }3 h& k" I' X* i // modelActions. modelActions is an ActionGroup, by itself it
. A( ^+ T8 S1 h) `* Y3 _& L/ i# A& W // has no notion of time. In order to have it executed in3 j5 {6 ]0 g( w4 ^$ M' j. q! P
// time, we create a Schedule that says to use the
' O7 H9 J, ^2 X3 S7 n // modelActions ActionGroup at particular times. This
2 Q1 ^) n4 C) a // schedule has a repeat interval of 1, it will loop every
9 n$ |* m5 b7 f% q+ I // time step. The action is executed at time 0 relative to- H& ~/ `* d6 D9 ]) n% y3 c
// the beginning of the loop.+ J' ^- P* {) C3 Q
& h' \/ b8 u _$ n$ L
// This is a simple schedule, with only one action that is
6 T; A5 v% T4 d; b // just repeated every time. See jmousetrap for more. [: U( M, J" ]) {' [5 k2 w
// complicated schedules.7 S$ h1 \; P! R' s' p
A* ]! J4 f d4 B9 R: h; A
modelSchedule = new ScheduleImpl (getZone (), 1);
) e. ~ d* x3 j0 ^6 c! X9 z6 g modelSchedule.at$createAction (0, modelActions);/ {2 {7 k; k. p# V+ o0 o X4 J
! D6 I# D1 R- z3 e return this;8 h# {/ J$ C( {+ d S4 y$ A, S
} |