HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( x# i* t4 b& @, e7 u
1 h# N( O Q8 M% z8 e
public Object buildActions () {
/ {/ H$ P% Y6 d5 R9 o+ { super.buildActions();9 p1 Z8 [1 @- s9 ^- M) q$ u% X' c( ]
! J# T7 ]4 j5 J D u1 {0 J
// Create the list of simulation actions. We put these in
9 `% _0 |4 p! M // an action group, because we want these actions to be
; _: k9 J, [# ]# `9 m& n3 A0 V // executed in a specific order, but these steps should3 y* Z% y1 g. h) G: I8 H
// take no (simulated) time. The M(foo) means "The message5 |% x/ C+ p/ c0 g: t3 ~" [
// called <foo>". You can send a message To a particular, k9 q" z* ? y
// object, or ForEach object in a collection.- U* y, y2 ~/ l1 T* O% `2 ^
! v' u, c& F, M& x9 s# }1 U/ [7 O // Note we update the heatspace in two phases: first run- T; ^' B- d. o( u1 d# O4 U
// diffusion, then run "updateWorld" to actually enact the U( R. w. `4 L, G; R0 W
// changes the heatbugs have made. The ordering here is
- E3 w* k- m' m! A5 P( j // significant!2 |! h/ x+ U! D; O: Z8 g: C
( F' l3 y( a7 ]8 G# I
// Note also, that with the additional
9 y1 C; `# e1 y& R% F+ A+ h# p // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 w3 x. w* A# g8 c5 O // randomize the order in which the bugs actually run
7 F9 B. D" G2 v4 b1 e8 i // their step rule. This has the effect of removing any" d1 |3 P$ f, M) {
// systematic bias in the iteration throught the heatbug
/ X& F, H& f3 [. s // list from timestep to timestep, x5 i8 G9 g% o7 @: {1 i
3 k3 f* ~2 g+ M
// By default, all `createActionForEach' modelActions have
8 c0 w' d8 i/ E$ W% k // a default order of `Sequential', which means that the
* }$ @' X! A# w8 O* q$ Z) ~+ D // order of iteration through the `heatbugList' will be6 N# ^! Y% _# G5 e" _5 X C! m
// identical (assuming the list order is not changed% v: ?" q8 D8 U9 J) T" p& o
// indirectly by some other process).
. G# K% d3 v$ k& \& {; G; ` ; z( c$ W0 l3 S7 W# h2 T6 V
modelActions = new ActionGroupImpl (getZone ());
) c% l/ [$ C8 L4 E) {
6 N, h7 e. t* t( z try {
: r+ f6 f! c2 `1 n6 M) {4 u/ w1 X* C modelActions.createActionTo$message
6 U5 K8 _- _8 t( ^) x' ?* ?. q' L (heat, new Selector (heat.getClass (), "stepRule", false));* X6 Q# ~) d) e5 `" H3 B: V6 w" t
} catch (Exception e) {
/ }" f; h7 D/ Y* T+ g) \% q; o System.err.println ("Exception stepRule: " + e.getMessage ());1 N4 K# \- E* d# g* T+ G+ Q
}
% \/ A. a/ i& m4 H; m( h) g$ E/ O$ m
try {
$ N& F& L- ? O4 d |9 { Heatbug proto = (Heatbug) heatbugList.get (0);
I0 ~" m* A9 f4 a! X2 y2 _3 z0 J Selector sel =
; s/ X- F* ~4 X7 i$ p8 c new Selector (proto.getClass (), "heatbugStep", false);$ t$ }5 N1 k% i" l' u( x' y
actionForEach =- D4 E, a; |5 |" p% e, I
modelActions.createFActionForEachHomogeneous$call( B5 d) u- d4 M3 ~. E! A6 o
(heatbugList,; n! R0 \3 I L) ?! |+ [3 ]- t
new FCallImpl (this, proto, sel,0 y9 @9 }( N1 \2 q
new FArgumentsImpl (this, sel)));
: m5 J* k1 C4 B- T% e$ y# f } catch (Exception e) {
! q2 Q! Y8 m8 b5 K" ^+ T X e.printStackTrace (System.err);
$ E* m1 h" S$ j& { }
$ ~" i* {5 f+ [ k2 o6 k! I% j$ { ! j2 ]7 f0 r' P+ C3 X
syncUpdateOrder ();9 [. ^8 c# n) y$ V7 @1 l# I
1 Q4 G8 ~9 W" T
try {
- {; q0 Q# Z1 M+ A3 G" T+ s modelActions.createActionTo$message 5 G$ ?! ~9 ?+ I. M: O. a
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 C% Q5 y7 t; n/ g } catch (Exception e) {
9 X; E! e& i8 s0 p- r. B4 h5 A System.err.println("Exception updateLattice: " + e.getMessage ());
0 j; r4 _+ L9 M' D# X }
2 V0 J, z8 T X* _' m4 n $ Z" t6 {" s% ~% E/ q1 C1 p8 o
// Then we create a schedule that executes the" y7 n! Q# ~& u, t
// modelActions. modelActions is an ActionGroup, by itself it
" s7 s/ e. W# `. f( u7 U' e+ ? // has no notion of time. In order to have it executed in
; S% p7 \9 [" F // time, we create a Schedule that says to use the7 ^. ~. |; K3 ?5 I
// modelActions ActionGroup at particular times. This
3 y* ~. i9 P2 m // schedule has a repeat interval of 1, it will loop every
$ Q5 G2 a( J" }& \1 J+ x* E // time step. The action is executed at time 0 relative to! Y8 I K% u+ X! W: u
// the beginning of the loop., X$ _1 @1 b3 o
% `, ~9 S6 s1 w E // This is a simple schedule, with only one action that is# F- S3 e' V) D6 B% O: z
// just repeated every time. See jmousetrap for more& l" R W2 I4 ~) ?
// complicated schedules.
! D' v' w1 W9 l% b& A/ ~" b
4 P) \' Y: a: X; [) O modelSchedule = new ScheduleImpl (getZone (), 1);% e% l5 B8 l! e" @
modelSchedule.at$createAction (0, modelActions);' k5 M y2 t1 O6 B
: t0 f% X) E: j) A, s
return this;8 C3 Z+ ^6 t* V, r2 Y
} |