HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- w0 [1 `2 O7 M1 N1 u/ n! F
0 V0 R$ ~* z* ?- A3 S
public Object buildActions () { a( j0 B! t" E, Z, ]% T/ h
super.buildActions();% W9 R% a& I# ~% e; f
7 ?2 E" `- w1 U2 a& G9 E, U
// Create the list of simulation actions. We put these in, n8 h) E! p- r/ Y2 f& f
// an action group, because we want these actions to be
! B) U: _: h; V8 D p5 Y, p+ R // executed in a specific order, but these steps should7 P' r8 y V" {" B9 A6 S D
// take no (simulated) time. The M(foo) means "The message
+ r$ ]- _ F8 W' o% i9 M // called <foo>". You can send a message To a particular8 Y% f. ?! v" x- H: m( b
// object, or ForEach object in a collection.8 Y( [- g7 a0 E
b9 g& D- b" p // Note we update the heatspace in two phases: first run
# E: q! Q8 _6 T! [: n // diffusion, then run "updateWorld" to actually enact the6 h2 G$ }4 S. M! `" Y3 \) C( g
// changes the heatbugs have made. The ordering here is1 L W8 b( m" g% D
// significant!
/ f0 o2 P4 Z4 [* G0 ?* P9 C : Y% y. o& ~5 [1 l W7 H
// Note also, that with the additional
2 ?: u( v8 w: i/ T2 W* K // `randomizeHeatbugUpdateOrder' Boolean flag we can
^2 ?4 R' B/ @/ v // randomize the order in which the bugs actually run; F' p" n. A4 n4 N9 L8 F" {
// their step rule. This has the effect of removing any
1 B: i R1 R8 P! f8 z" S // systematic bias in the iteration throught the heatbug
( d2 _. J1 I2 G, w // list from timestep to timestep: Q y8 V5 q1 {/ c1 W' w9 S
" }; @2 o& ^; X5 c o! l // By default, all `createActionForEach' modelActions have8 P' \4 M7 y, F2 w" D r" D
// a default order of `Sequential', which means that the! r8 r1 o- B4 k$ O1 A! m
// order of iteration through the `heatbugList' will be
- I7 d, d: p* d! _ // identical (assuming the list order is not changed
1 q0 d" i. v }+ z- r/ Z // indirectly by some other process).7 c# c6 r! c- w3 g1 j# `4 H
6 U: o4 B+ A, }2 q; h1 Y" B7 _ modelActions = new ActionGroupImpl (getZone ());# l7 O2 N7 Q+ P
. B1 k! P, z% x1 D try {0 U' U* f ~+ l8 J4 t+ u% E+ g
modelActions.createActionTo$message6 }6 K5 b7 r7 S4 R" h7 j
(heat, new Selector (heat.getClass (), "stepRule", false));
5 P: B$ M8 p$ C' `% b } catch (Exception e) {! w5 [; W! N9 G: l. i
System.err.println ("Exception stepRule: " + e.getMessage ());! V" G+ }$ E3 }8 D6 @: t
}8 ~8 `. U+ |0 c8 A+ U& ?
4 t; Y$ S1 v' Y5 V f+ z2 T try {
% f/ H9 P. Q7 f. s Heatbug proto = (Heatbug) heatbugList.get (0);$ J/ N. Q' @; j: v! r2 E
Selector sel = * X+ B$ f8 O* M3 ^
new Selector (proto.getClass (), "heatbugStep", false);
, H0 ~; y# [6 @( x4 `5 W' ] actionForEach =' H6 @; D: ?6 [& p0 y# J' h! O3 u
modelActions.createFActionForEachHomogeneous$call4 R% n1 f# ~8 Y8 `
(heatbugList,
$ ^4 _+ ?, w4 W1 K+ Q new FCallImpl (this, proto, sel," d/ L' A: j) Q. U& G
new FArgumentsImpl (this, sel)));* M4 `7 l( t+ c
} catch (Exception e) {* X3 M+ b" X. @; {
e.printStackTrace (System.err);7 q+ X5 ?: V) n# |" c8 Z4 `
}
% e. A5 d$ @5 s; w
, `% m# N5 o. E) S- Q" D syncUpdateOrder ();! @% s. U, }. d% q0 V* G0 C5 \
* g) H: z/ a3 G! M H. H try {; G8 s+ p- C: F
modelActions.createActionTo$message
; v0 x D! t3 I$ M (heat, new Selector (heat.getClass (), "updateLattice", false));
$ q% h& C2 O0 G) j } catch (Exception e) {
7 G: f% G3 u% `1 U% P+ k- T2 q System.err.println("Exception updateLattice: " + e.getMessage ());
/ ]2 [ `" z2 X1 u& Q( Y- }5 l }
. w& p# @% i9 J# I% I$ R- j
; @* _( I& P+ ~# }. ?8 `% |7 g // Then we create a schedule that executes the
V' j: o5 K8 b# j* M/ \) t' F // modelActions. modelActions is an ActionGroup, by itself it* |6 ~5 Q y! y8 u6 `
// has no notion of time. In order to have it executed in
: J, ~+ V i& P // time, we create a Schedule that says to use the
/ s' X# Z/ m' [1 J, r // modelActions ActionGroup at particular times. This
! n$ B! M" |' @ // schedule has a repeat interval of 1, it will loop every) B$ u! [. H. j1 L" t
// time step. The action is executed at time 0 relative to; k8 l" N2 \/ D& M( P; u) T
// the beginning of the loop.: T: Y. k1 Z" y! A4 _+ F
J' |" P2 c7 z // This is a simple schedule, with only one action that is
5 N2 p9 j9 z+ o; ] E: { // just repeated every time. See jmousetrap for more3 t) ?$ q5 P+ b/ f7 @* P7 D
// complicated schedules.6 l9 s# g( e* d) O) }
+ S3 @5 _7 [4 s$ T4 u! _
modelSchedule = new ScheduleImpl (getZone (), 1);
! Y0 H8 k/ q( d* j- N# ] modelSchedule.at$createAction (0, modelActions);
' ~; n, K1 B, ` V
' {; O& U$ \: o9 F return this;
8 c ~, T: W& U5 b( C' M) N+ m/ A } |