HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# d; J) ^2 b( v Y- ?$ V2 d% z8 q. X; _' W2 P O7 N$ q! f" {
public Object buildActions () {# l# \4 h! b8 x
super.buildActions();. R" W0 k* O0 n8 j; G# X6 k% p: t! [
@* t: y7 V4 h% e5 H2 q // Create the list of simulation actions. We put these in
1 j6 u# @5 E* Z# |: Q // an action group, because we want these actions to be- H) X/ ~: \; [8 l
// executed in a specific order, but these steps should% W2 ?& G( K, k2 U3 {
// take no (simulated) time. The M(foo) means "The message
+ X. {" y$ g' V" P; k$ z // called <foo>". You can send a message To a particular) _( y$ L; t' E, c w
// object, or ForEach object in a collection.4 m2 J* [1 U' \9 O& P4 C
A( d; ?, v) e6 T6 [2 |: x0 p) {
// Note we update the heatspace in two phases: first run
( R3 g2 Y. m; h% W2 n // diffusion, then run "updateWorld" to actually enact the
+ Y1 p u: u8 e( c; P8 Y; Z // changes the heatbugs have made. The ordering here is! q, T6 U) {- o2 H, T
// significant!
/ ~6 b% j( c& x# ?1 v
( w: ^8 }" X5 {* K7 L. y% j \ // Note also, that with the additional
- v6 |# ~% y1 G3 H // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 z8 }( N8 f6 I6 E4 x. K( z# G // randomize the order in which the bugs actually run
% I8 S8 P7 n# e7 z* V5 ] // their step rule. This has the effect of removing any
1 a1 l c, O! p1 r) }& P$ V* V. p // systematic bias in the iteration throught the heatbug: ]# Y0 H$ ]# k
// list from timestep to timestep$ A/ l6 M" X: Z9 K- g
8 x A/ \: l( ]" I8 X! |
// By default, all `createActionForEach' modelActions have
. K6 ^, d& I" E( R // a default order of `Sequential', which means that the$ T' M% U) \8 e
// order of iteration through the `heatbugList' will be" g6 f/ y8 e9 d- f) n
// identical (assuming the list order is not changed8 [- ^$ G+ ^# m' ^
// indirectly by some other process).
8 j1 l2 _) `) A) J , I# d6 }' B- L2 c0 M
modelActions = new ActionGroupImpl (getZone ());# q* x4 n& X! d$ p1 ^
! W3 \+ F n) l2 _2 G$ Q- { try {3 s+ [2 T, b# w$ {: H) I# [8 t
modelActions.createActionTo$message
7 J2 ^; e) ]; v. ] (heat, new Selector (heat.getClass (), "stepRule", false));+ {4 u8 E, w7 F# f5 E$ r
} catch (Exception e) {1 j+ k8 F; X( k
System.err.println ("Exception stepRule: " + e.getMessage ());
: Q. i) U; H) k4 z# E }, H) b8 U7 Y# k: ~9 P
7 d Z7 W$ g9 n9 p% z
try {
; G7 \- ]6 [( X! x! e( J0 V" ^ Heatbug proto = (Heatbug) heatbugList.get (0);
; `% N0 q2 }- A$ }- ~" U* S Selector sel = # g* e/ e' f5 n: ^8 \
new Selector (proto.getClass (), "heatbugStep", false);
! T- z9 t) t0 g3 Z actionForEach =
0 O! T# n: ]3 W; g% y modelActions.createFActionForEachHomogeneous$call9 m9 B& d% J) O$ f1 j% k6 {
(heatbugList,$ }2 }1 {7 Z; [) S. R! E
new FCallImpl (this, proto, sel,
9 n1 A n( B% F' D% x, Z9 Y0 \ new FArgumentsImpl (this, sel)));% ^: u! K1 ]2 T
} catch (Exception e) {
0 E- |0 v7 f5 G2 _ e.printStackTrace (System.err);; W: C2 ]6 D) o; _) L$ W
}
( U2 ^. S6 {( z) U( b
+ @* u* q" p$ b1 w syncUpdateOrder ();: {4 y4 {, ?! k- }% X1 d# W
* P: U x Q% w. _2 d" o# c9 N
try {
- _; q2 n3 ?: o0 I) a" U modelActions.createActionTo$message ! \4 W7 F3 n% I
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 C* g8 e; }/ O$ A) c+ G3 t } catch (Exception e) {
2 V3 X& M) A% [. H System.err.println("Exception updateLattice: " + e.getMessage ());0 o" l8 K5 p4 }' r r4 L
}
3 J$ b0 [8 ]- D( l) y9 I7 E6 c% f ' F+ m8 u3 d! i h9 J
// Then we create a schedule that executes the# h! N0 w3 o" a/ d
// modelActions. modelActions is an ActionGroup, by itself it
# L: d* Z* F3 ~( K( O; P3 o // has no notion of time. In order to have it executed in
# \5 a% |1 \# ~$ g$ {! z7 |* ^* z // time, we create a Schedule that says to use the
+ \ V+ I O& I# Z // modelActions ActionGroup at particular times. This
. g# Q2 J+ S( h) A // schedule has a repeat interval of 1, it will loop every4 M) s* r4 g0 o! V7 P9 J8 L, r2 G
// time step. The action is executed at time 0 relative to
+ D5 d) L! m1 A8 ]3 w // the beginning of the loop.1 i* p/ i. }0 r4 v
4 ?* ?* p6 w" B( C // This is a simple schedule, with only one action that is8 M7 S) g; A% z
// just repeated every time. See jmousetrap for more, V! |7 |3 K. g* {
// complicated schedules.
0 N" U3 Q% K& R6 A3 T: ^$ T 5 D& k$ D4 P4 o. f' X% o. Y
modelSchedule = new ScheduleImpl (getZone (), 1);+ A8 ]" k1 L+ }0 h8 ]3 X& `) }8 K* V. F
modelSchedule.at$createAction (0, modelActions);
; w' ^6 ]1 K( }1 ]) F9 p9 \6 \3 a - h# K; N# x5 ^; B4 k! H5 n
return this;$ ~' M: K7 ^% b# Z4 a- Z
} |