HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 E8 U/ j- L) z( M" D" T T( W8 `- @' o( P2 v& D( S9 b( N
public Object buildActions () {. h7 l( ^7 B& k5 }
super.buildActions();
6 L/ E1 X' x8 R- b1 H1 z 5 D6 c$ A9 w) i/ W0 x8 @
// Create the list of simulation actions. We put these in
+ z h9 i8 A2 ]# |, a) D // an action group, because we want these actions to be
* E2 Q5 s3 D" X' ]9 e // executed in a specific order, but these steps should" I" p3 T$ f& D, z
// take no (simulated) time. The M(foo) means "The message; l# i$ a- j7 c. l6 [2 n
// called <foo>". You can send a message To a particular! y4 ^6 w( |% Y2 s
// object, or ForEach object in a collection.
8 z0 d7 K$ p8 m% D: G 6 b7 v9 ^3 y% @8 G: U5 P
// Note we update the heatspace in two phases: first run
- U0 v1 i$ ]: J // diffusion, then run "updateWorld" to actually enact the
5 N) [: |5 j! b) ^7 y( [% Y // changes the heatbugs have made. The ordering here is. D, [$ M* i d. Z/ o8 v
// significant!4 ?) d p) E& z! y* U0 l5 O' g
' q2 Y$ x0 U+ @- a0 [ // Note also, that with the additional
8 K6 {2 b( T* S% m4 \: l // `randomizeHeatbugUpdateOrder' Boolean flag we can
" s9 G, Z" N1 b4 B- T // randomize the order in which the bugs actually run
$ S6 z$ f' K9 M6 k$ h7 j // their step rule. This has the effect of removing any& P; I U# Z ^2 h
// systematic bias in the iteration throught the heatbug) O' b- P8 }& R" x
// list from timestep to timestep% ^: |5 i1 N. w" j, u
/ `9 n8 G7 c4 V$ } // By default, all `createActionForEach' modelActions have, {. \# u4 ?$ X9 x' H
// a default order of `Sequential', which means that the
, e: i$ i& b4 ^+ f; C( U6 c* l7 j9 g // order of iteration through the `heatbugList' will be
1 T4 k0 |# ~! L% c7 w // identical (assuming the list order is not changed
5 J0 Z* ~0 n6 }7 @& E: o0 ] // indirectly by some other process).
$ M r- k8 a' B# w7 ^! r 1 L9 D4 o7 q% n' e! L+ K! i2 q
modelActions = new ActionGroupImpl (getZone ());( y4 ~- S, F9 h, U; N/ M
3 ^* F6 Y; }: `& e: c
try {4 B4 H9 B% x0 J2 O6 r K6 O
modelActions.createActionTo$message# |4 ?& x% B- f- C& q$ }
(heat, new Selector (heat.getClass (), "stepRule", false));
. Y: c, L7 { D% {# z } catch (Exception e) {" E3 W8 e. N7 R9 V) ?
System.err.println ("Exception stepRule: " + e.getMessage ());
7 D+ y; x) J/ F }
0 i* x; M' `1 z8 ?7 h* E) T6 I! x& H: W- C# t4 X
try {( f* d- O' `/ F
Heatbug proto = (Heatbug) heatbugList.get (0);+ p9 p3 u' d' F- \' E: l- e b/ i2 X
Selector sel = + v. O3 a# K! a5 c" x) R) i
new Selector (proto.getClass (), "heatbugStep", false);
- D% {4 E# _3 b+ k actionForEach =, ^8 o0 ~" [; \ P8 u/ A* }
modelActions.createFActionForEachHomogeneous$call1 C+ X& O6 r$ S( N- N, j
(heatbugList,
6 N; r: f2 e. R, ]9 C new FCallImpl (this, proto, sel,
2 ^( ?; A0 w+ ~8 p new FArgumentsImpl (this, sel)));- y' E# F& |, `0 U F4 c0 g- R0 }
} catch (Exception e) {7 G+ e! b p$ x: Y" ^
e.printStackTrace (System.err);# ~( X( c) a( O* B; p& |6 d
}
" }% _ l5 b) R0 Z9 u, v# a) q) B
3 D' X- i0 x# F+ _7 w" S) K) V# i syncUpdateOrder ();
% C, k1 X$ q) _/ K- V
. C; R9 @9 z% K3 O try {
; o9 _% E1 R# [, r modelActions.createActionTo$message + L$ }3 V/ e4 E/ Q. A
(heat, new Selector (heat.getClass (), "updateLattice", false));+ z; E0 @% e F( K9 @* H' w% f4 W
} catch (Exception e) {
4 Q! r9 X }/ C, Y System.err.println("Exception updateLattice: " + e.getMessage ());5 K6 m( }5 G- B5 P$ Q" L
}! b+ i. C9 W7 @
' c' o* j+ W: M& T* R% t. e
// Then we create a schedule that executes the- _" w/ o; X. v7 t
// modelActions. modelActions is an ActionGroup, by itself it/ A4 c" }# j" d9 c# L
// has no notion of time. In order to have it executed in
' I Y- {5 E) J0 s // time, we create a Schedule that says to use the
/ A0 u# G0 [, z // modelActions ActionGroup at particular times. This3 G3 k. j2 {6 q/ x: C* |, w0 b
// schedule has a repeat interval of 1, it will loop every
' y+ E( E3 ]- J; E% Y // time step. The action is executed at time 0 relative to: o8 T& @" W' m/ O7 Y: y
// the beginning of the loop.$ j; i0 p2 k* X% `/ c0 f! y4 U0 u; F
7 ]- _- {7 D6 c // This is a simple schedule, with only one action that is
: M! H( ~1 p7 N: h$ H& t // just repeated every time. See jmousetrap for more% N5 N" O- d. V% Y
// complicated schedules.4 I# B: G. U& M% Y; ^
) S, o: f! ?0 b4 K7 |
modelSchedule = new ScheduleImpl (getZone (), 1);
7 p. @, W/ k6 j8 R modelSchedule.at$createAction (0, modelActions);
i) H- F9 Q9 Y& q: }7 g5 R$ o6 f/ ^
& R" \7 h d7 t6 U& H return this;( w, ^, p3 c9 B
} |