HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) U- k; W% m8 X/ A2 z3 y; `5 j, S } {) V, k A1 P! U ^
public Object buildActions () {: O" E X. u- g
super.buildActions();4 l2 j4 q2 ?# w% D& a2 L0 o
! d1 d a1 Z/ z: j" R4 y1 z9 p // Create the list of simulation actions. We put these in |: ]* v$ b% l3 ^6 z
// an action group, because we want these actions to be1 {* S* }5 B2 H0 ^* E7 ^ n
// executed in a specific order, but these steps should
0 \4 P; r! N/ I // take no (simulated) time. The M(foo) means "The message8 }. u, G- Q( Z: T2 \1 K
// called <foo>". You can send a message To a particular. c9 T; K M) S0 U1 {& q% K! l% ^
// object, or ForEach object in a collection.
" r* m0 m9 t! O : p" o) Y* j3 V3 W& M0 I
// Note we update the heatspace in two phases: first run, v4 ?* |9 t @4 p/ |
// diffusion, then run "updateWorld" to actually enact the. I2 }8 R% L, L$ ?; f1 `3 l$ S* l
// changes the heatbugs have made. The ordering here is
X3 |+ i. V |3 g$ w6 Q( ~% h6 g // significant!
8 v1 f) ~$ Y9 Y2 t
1 d5 u9 ^2 O o7 G( r6 K // Note also, that with the additional4 r0 j& I$ X/ \; n2 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 p0 F; x+ z1 y* Z5 ~. h
// randomize the order in which the bugs actually run
- \6 g$ Z. X4 L0 v( L2 U // their step rule. This has the effect of removing any4 f0 h/ H/ V p* Z( e: h
// systematic bias in the iteration throught the heatbug! E' X4 a. O7 b1 f
// list from timestep to timestep
8 G) E( I: _2 c- S3 ~
- e# h) N8 \/ E5 t# I4 J! _) H/ A! G // By default, all `createActionForEach' modelActions have6 Z- O b& [ n8 A1 K& d
// a default order of `Sequential', which means that the( R# L. P/ t" e( E) ]% r! e5 F
// order of iteration through the `heatbugList' will be8 |3 |8 p* x- V' D
// identical (assuming the list order is not changed
! ^- k7 ?3 x+ _: I* c( Z/ P4 H; y& M0 A // indirectly by some other process).. X" H( \& X# ~% [9 V
! W. Y2 ?. G* J4 g, f+ k1 V
modelActions = new ActionGroupImpl (getZone ());6 U+ N0 q `# c. P/ d
" |' j- S7 w' N# C try {
% l" Y" x8 I7 Y0 q7 F) v0 C3 N modelActions.createActionTo$message
: z1 v3 P8 e" O/ R2 ^ (heat, new Selector (heat.getClass (), "stepRule", false));& j9 c1 [0 ]! N, a+ k& k
} catch (Exception e) {& r' |/ `" a( ?3 u3 ~& j
System.err.println ("Exception stepRule: " + e.getMessage ());
' A! q& V! _& S }; N0 `- E r: f2 y: N
8 n' ?& j- |$ ?' ]% @! o
try {4 p! v/ g- F- F: E8 ?" {
Heatbug proto = (Heatbug) heatbugList.get (0);. \5 e+ ^; T- w
Selector sel =
0 F9 L% t; y; x5 {1 o new Selector (proto.getClass (), "heatbugStep", false);
4 A) q/ U/ K7 E8 p _ actionForEach =
O7 B* q9 } D! z6 Y" u modelActions.createFActionForEachHomogeneous$call
8 x$ b- C# J, V4 n; S2 i (heatbugList,
# }8 @4 f. |, `7 [" W! f. |# { new FCallImpl (this, proto, sel,
2 j6 k" O, C! v0 c new FArgumentsImpl (this, sel)));
1 t) P0 b1 M& w0 Y } catch (Exception e) {
3 v$ @; q! t" {: u6 X: [ e.printStackTrace (System.err);
/ P, U7 E1 _9 } }4 W# ?4 r# t. v9 c: J" I3 G, t8 o
?3 ^9 m+ P( f- ~6 P. R syncUpdateOrder ();
# t& [$ C! ^) G2 K! O" e
8 b2 _ s; G" \; I# L try {$ H1 q+ ?% S) |
modelActions.createActionTo$message
/ k) H( \9 D% y, m6 E9 h" @ (heat, new Selector (heat.getClass (), "updateLattice", false));
% O! _" s f5 N/ F } catch (Exception e) {6 e, ?6 A8 D4 e* \" \
System.err.println("Exception updateLattice: " + e.getMessage ());
, S! [/ x; J N }
+ m! v/ G$ Z1 V2 W) Y9 Y# Y ; R. S. Z B/ G2 n! x
// Then we create a schedule that executes the! K0 G) ]- p; ?& D
// modelActions. modelActions is an ActionGroup, by itself it
* T, F: }2 @% M! s2 \# L3 x // has no notion of time. In order to have it executed in m4 ^, O" c, f0 ]2 O$ y
// time, we create a Schedule that says to use the
9 K* z6 w: @8 _1 o+ z // modelActions ActionGroup at particular times. This
1 i/ h. L: r# V# f0 s4 J8 u // schedule has a repeat interval of 1, it will loop every' E. t1 c. i5 ^3 l: I# U
// time step. The action is executed at time 0 relative to
$ E; a5 j* y$ O2 A3 s( t // the beginning of the loop." p1 C$ F# O' x! y
% u3 @' e7 I' m
// This is a simple schedule, with only one action that is* d, K1 B' F' `- I% V
// just repeated every time. See jmousetrap for more$ V) d3 L3 s4 l$ d- G/ [
// complicated schedules.. y4 _2 |$ M5 G: @$ @) {. g
' s$ S3 n# Q6 J modelSchedule = new ScheduleImpl (getZone (), 1);
5 y9 L9 k4 b+ `) e" k5 X; f, p: s. j modelSchedule.at$createAction (0, modelActions);2 q9 Z6 H8 h# _3 P$ g
9 V2 X9 o. l& Z. d. K; _
return this;. V# y* a* b6 E; D$ v
} |