HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 T! `2 W: ^8 F) S8 o# |9 o+ g" B& z' M: P7 ^* L
public Object buildActions () {
) s3 A, h" L5 G8 K' W; _ super.buildActions();4 e: t4 J: U0 m3 T5 g/ j
# [1 E' D, D7 Y7 ^- t! f // Create the list of simulation actions. We put these in
0 u) X6 S0 G( n, f6 D // an action group, because we want these actions to be8 j5 _* G3 Y7 q( A8 \8 U
// executed in a specific order, but these steps should
6 s: \8 g s5 x0 S9 f! f* L // take no (simulated) time. The M(foo) means "The message9 L* B) D- k7 C; A. f
// called <foo>". You can send a message To a particular& b4 _" U# b5 O7 I( K3 M5 x) {
// object, or ForEach object in a collection.
* J! r3 @7 e. e1 [$ B$ \4 A 7 V1 M( @ [6 K
// Note we update the heatspace in two phases: first run' t( h! x N0 K- f2 D+ T/ Z, T
// diffusion, then run "updateWorld" to actually enact the2 f" D7 U" V" H% q
// changes the heatbugs have made. The ordering here is2 ]' W9 R7 g7 S
// significant!
$ S* \. @+ ]% R+ l ( S! x( ?& c1 u
// Note also, that with the additional# F9 X N- m4 e! Q$ c# z9 U# X2 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; |/ O. b& F. i // randomize the order in which the bugs actually run
: n! W5 I3 d, e$ B; o9 ?. A7 A% { // their step rule. This has the effect of removing any
: t2 W: \. F9 e$ V9 `/ x // systematic bias in the iteration throught the heatbug. \3 \! v6 A8 `$ ~. x! X
// list from timestep to timestep
$ }* ^0 z+ e2 s5 B ^" F # U: F; ]- {. l- R) m
// By default, all `createActionForEach' modelActions have" Z/ e6 A& u0 A9 k! v
// a default order of `Sequential', which means that the! h! q5 F) ?) V* j4 O
// order of iteration through the `heatbugList' will be
9 N3 |% k) {. F4 I* U+ _ // identical (assuming the list order is not changed" r1 O5 T9 Y4 q8 e2 K: J
// indirectly by some other process). X% ~9 ^- Q+ S6 L
2 @! Z8 X$ j2 h
modelActions = new ActionGroupImpl (getZone ()); h0 g. [/ ~3 }8 v3 p
4 Y% s/ L, h6 }2 m' _$ `
try {/ \$ j0 P: O) }# R& }' ~
modelActions.createActionTo$message5 h+ `3 l/ k$ R. f' a9 i- `) P
(heat, new Selector (heat.getClass (), "stepRule", false));8 W1 \$ h6 W" H8 }9 M. {- x6 b
} catch (Exception e) {! o! Y+ A5 O* T) j
System.err.println ("Exception stepRule: " + e.getMessage ());
) @4 Y, P' O0 g. I }
; G2 |$ f! G2 A7 L' t( x3 v: m% q% B& H
try {
, L! H. Z6 y* T& { Heatbug proto = (Heatbug) heatbugList.get (0);
: R" h: ^: l- b( d2 z, v Selector sel = , o5 J, O+ U% \2 w3 s
new Selector (proto.getClass (), "heatbugStep", false);8 [* Z8 K: g" B7 f P
actionForEach =, t- l q. J4 i W* g6 Z a
modelActions.createFActionForEachHomogeneous$call, s1 r2 ]4 V' [" b# C+ \% c @: e
(heatbugList,
. }% _/ D7 }1 D/ ^ new FCallImpl (this, proto, sel,/ _. l- j' T# M: R% d3 ^4 U' P
new FArgumentsImpl (this, sel)));0 {7 R+ |1 G x) V' {9 C
} catch (Exception e) {
% A' c0 l& X0 B0 |: x' [ e.printStackTrace (System.err);
& V" T) b# }) `* U2 \% G: F }" V) h ~# W9 L$ F, E5 }* L
! a, t. n: g* [+ g: x. X. ^
syncUpdateOrder ();
, h: v- @* z! X; J/ B
. S' N: v: U% G$ i! f try {
- o8 m5 v3 C2 \* G W+ h modelActions.createActionTo$message
$ p7 U/ |+ h m% p' A# V (heat, new Selector (heat.getClass (), "updateLattice", false));. ^' x+ r, ?1 z2 A, A% o- T- I
} catch (Exception e) {
" A! w2 ?) c' k System.err.println("Exception updateLattice: " + e.getMessage ());
- W. m; y8 D8 G7 _/ B) p! ^8 B+ g }, o$ y9 a/ ]: p2 m# \1 t( M& Q
" {2 b1 `% ^# k; F) J$ h
// Then we create a schedule that executes the$ [1 v; p; ~) J2 z5 y7 |& N5 P1 v
// modelActions. modelActions is an ActionGroup, by itself it' E* v9 l: G& T* Z' y
// has no notion of time. In order to have it executed in
! ]4 M6 j' i$ W+ G+ I$ H; R1 f) ? // time, we create a Schedule that says to use the$ k2 U: Q- C; d) u; k a
// modelActions ActionGroup at particular times. This5 G* |- r! h; @8 i" i |
// schedule has a repeat interval of 1, it will loop every+ [- r" Z! R/ Y* p1 q; C
// time step. The action is executed at time 0 relative to5 ~2 o& K7 v( g
// the beginning of the loop.
& _4 J5 f' S9 j) K% {2 b) ~" H% ]( H- N4 W& K6 i: O+ A( F
// This is a simple schedule, with only one action that is# a/ w0 w/ w* d0 f
// just repeated every time. See jmousetrap for more% P0 T" L) j' R2 h5 o
// complicated schedules.4 Z1 [, {& A! i! v
% `8 s; F4 ~; Q, O modelSchedule = new ScheduleImpl (getZone (), 1);6 }! i6 \. {2 X& o* X$ {
modelSchedule.at$createAction (0, modelActions);6 N" L: q& N5 H E
0 E+ R4 ~- ?8 Z) V, b
return this;% W! M1 d% q- Z7 |* F( W
} |