HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 l& k2 t- H4 {! j9 K1 `0 v' T+ h: p* w6 A, h& R
public Object buildActions () {0 u. Z* {* x5 n$ n H. m
super.buildActions();
: ~; M) r& I% o, ~2 _# {: [
1 w c4 m; @' e5 J4 m // Create the list of simulation actions. We put these in
2 o3 x2 o, h7 ^* z1 X" S) B // an action group, because we want these actions to be, X/ d: a/ S0 z/ E, b6 Q
// executed in a specific order, but these steps should; l4 s; M; Z5 R: X8 T$ e
// take no (simulated) time. The M(foo) means "The message
, T9 `2 j3 L* d$ X) g // called <foo>". You can send a message To a particular! l3 f! | h8 K. T
// object, or ForEach object in a collection.
5 @4 |& P9 [3 P+ s" U
/ n D# {& W: s // Note we update the heatspace in two phases: first run5 t0 L$ F C/ b7 `1 D- |' g5 c$ s$ P
// diffusion, then run "updateWorld" to actually enact the2 m+ u/ [* }' i2 t. M; b8 z* l; E
// changes the heatbugs have made. The ordering here is5 J; I# U7 q- i" i1 m7 _( v, Y
// significant!
4 @, C- }( C5 H, Q , @; c' D8 ?$ F; `) R' K3 i0 Z
// Note also, that with the additional
1 [/ n; x* I% a( o' U // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 G. l) v( v( z# v5 g; z& } // randomize the order in which the bugs actually run
# ` Z- i+ i4 @* z$ y! d; i2 w // their step rule. This has the effect of removing any' X! {1 D1 T3 }
// systematic bias in the iteration throught the heatbug
2 y# U1 z0 Q! \ // list from timestep to timestep
: t1 }* x3 t" } - y6 G! y7 w( L4 c( S# _
// By default, all `createActionForEach' modelActions have) r+ J, g [! z
// a default order of `Sequential', which means that the) e$ e$ ?# Y: [ v# T
// order of iteration through the `heatbugList' will be9 a# Z" {7 I' v/ E
// identical (assuming the list order is not changed2 d7 b/ C7 o% }
// indirectly by some other process).
! [" r1 e- T& z4 ? 7 I1 q* {8 P; g
modelActions = new ActionGroupImpl (getZone ());& K" |: M# _$ J+ V, Z
4 j$ o0 g/ e, Z8 I( r
try {
+ V Q6 i* C9 G- V modelActions.createActionTo$message; w: f2 u# ]8 d; i6 m
(heat, new Selector (heat.getClass (), "stepRule", false));
7 G T' G ^. L' x7 z, n4 p2 m) f } catch (Exception e) {
) Z$ J# O' I$ z% [ System.err.println ("Exception stepRule: " + e.getMessage ());( E; k+ W' [. P% ~
}; r( }& }/ H+ t3 I) C) L
! c$ g6 k9 L; n+ N& o% E: Z try {0 l1 W s. x, a; U! F
Heatbug proto = (Heatbug) heatbugList.get (0);& g' I# w/ i Q1 m6 h5 s% ]2 r
Selector sel = # G2 ]7 M ]& U& y# f1 v4 j. A
new Selector (proto.getClass (), "heatbugStep", false);' w3 e' m) N. L9 m! R5 X5 T1 U+ K
actionForEach =: P9 ]: o; d6 F8 i; K
modelActions.createFActionForEachHomogeneous$call; Y' u- I4 ~4 T; C# [
(heatbugList,2 h2 }: {+ g. l; p" z
new FCallImpl (this, proto, sel,1 |* G" K- C, Q0 V$ `8 j: Z; ~' J8 A
new FArgumentsImpl (this, sel)));
3 K6 Q8 ~# i8 f2 J* h. q: @ } catch (Exception e) {
* V n9 }' n- T. I! q0 U' D8 p( I* y: I e.printStackTrace (System.err);
6 B a& H9 C5 f5 r; ]- z }7 f& B0 C) z- u9 w7 h/ Q B" u
2 M2 c' T; p' b0 Y. |! ^3 l q
syncUpdateOrder ();5 {3 z n% i" X! z
; S8 u& R! G1 P' B5 t9 H- O" o8 R6 Y try {
( h5 a6 f; V3 W n, J modelActions.createActionTo$message * ?7 y8 Y% f( n& }' X8 b q$ Q
(heat, new Selector (heat.getClass (), "updateLattice", false));, P3 \+ D3 i0 o* P& r2 F
} catch (Exception e) {
J1 f) q; A+ u1 R System.err.println("Exception updateLattice: " + e.getMessage ());# t: H( h# [ N" u) H: a2 ?6 e* }+ W2 q2 u
}8 c: B8 K* ]6 ?& y$ ^4 g# [
9 _" t6 g; o# `- u2 T' n) q
// Then we create a schedule that executes the
) o- _& S5 E* I0 t( O! { // modelActions. modelActions is an ActionGroup, by itself it
8 A: R5 ~$ S$ {. ]% I1 V // has no notion of time. In order to have it executed in
7 ?$ d2 k v4 @6 }: J {: \ // time, we create a Schedule that says to use the3 I. h4 L. I7 y) M
// modelActions ActionGroup at particular times. This
5 H3 b- [5 n1 k4 D$ _8 ]+ d // schedule has a repeat interval of 1, it will loop every
7 g2 X; O) p7 j( ?0 D+ K1 W/ o // time step. The action is executed at time 0 relative to' M5 a. X, v& R- I. x4 V+ g
// the beginning of the loop.9 M# c+ g/ X# c# Q Y
. m y8 h- n) ]% B
// This is a simple schedule, with only one action that is T5 s! k B$ ^; s" u+ d; c" ~9 g
// just repeated every time. See jmousetrap for more4 O6 X+ h; ^. [2 l" O8 t
// complicated schedules.' K V5 ^8 f; `9 i% i. ^
$ `6 H- z( R( y modelSchedule = new ScheduleImpl (getZone (), 1);
, a: e$ n3 Y7 h( {( v modelSchedule.at$createAction (0, modelActions);
3 c! p: c' F5 c7 z& C4 v' @ 5 f3 Z' L$ g% {4 N& i/ |1 M
return this; ?- T5 |4 j6 E
} |