HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
v) U/ h0 Q% r p* S' Q' |1 D% g6 V/ M. T1 R
public Object buildActions () {
8 C' K0 P) q% ^- R0 [. t& D8 A3 Z super.buildActions();
X+ {; g; e" u8 l
, W5 m: q3 K a8 h6 M% M# T3 c8 U. C // Create the list of simulation actions. We put these in) N* t j) y; @% R7 n/ X* q" K% g
// an action group, because we want these actions to be' P; r9 r, m7 m0 L9 J0 p
// executed in a specific order, but these steps should
' y9 ?, }# c8 ?: M // take no (simulated) time. The M(foo) means "The message
- Z' J l4 Y4 v. R0 L5 r // called <foo>". You can send a message To a particular3 H+ G4 n- l; v( P; H. G4 ]) b
// object, or ForEach object in a collection.
m4 v6 e4 S- f4 h, T) Y7 U0 f) W & Z4 F: C& L+ w }2 Q
// Note we update the heatspace in two phases: first run- c! }3 d! a( p. \
// diffusion, then run "updateWorld" to actually enact the
# `* ~& J6 p2 v' P // changes the heatbugs have made. The ordering here is- E; P! u: S) A2 j$ e* n0 G5 L1 g
// significant!
, L1 X) X! i, C; g ( u' P. Z6 L7 r0 g3 c& {; G# r: a
// Note also, that with the additional
: R7 z6 v; S& V+ o2 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 O: K9 A9 ?6 V' f( } // randomize the order in which the bugs actually run+ a( } ?- j( h0 _1 r+ [
// their step rule. This has the effect of removing any; O$ r, Y) }5 E# [4 g( r. `
// systematic bias in the iteration throught the heatbug
: Z1 ~( c7 N3 }% z8 I // list from timestep to timestep
2 c2 b) t% P+ w) x+ C# Z& y ' k4 G: }% R- o+ l* T' D! U& E2 H
// By default, all `createActionForEach' modelActions have& H( E" K T3 e, h, q: T9 `4 u
// a default order of `Sequential', which means that the S) n& C4 _3 z
// order of iteration through the `heatbugList' will be
0 G X% i4 @5 T- C" a3 @ C // identical (assuming the list order is not changed
- c. j4 \/ G0 |, u9 i7 U // indirectly by some other process).
/ D0 T* Z, \0 J- p9 S! O+ J/ w
: @) b- L% U1 x Q/ i modelActions = new ActionGroupImpl (getZone ());
6 h8 K3 F4 Q; y6 S3 `, F% s$ }7 q/ B3 \" Y/ T. D
try {
. r0 M' I, Z9 l modelActions.createActionTo$message B# T% q# D+ H1 ?6 o* T
(heat, new Selector (heat.getClass (), "stepRule", false));$ |5 D7 n. c; v2 g
} catch (Exception e) {
" T8 ~( @) o! i5 F2 M# V System.err.println ("Exception stepRule: " + e.getMessage ());8 {; G. t, c4 E2 h5 n
}, Q3 b0 ~/ ?8 v* L, z6 ]# z; ^- V
; o R9 m3 J8 B* d8 A4 k: p
try {
3 L, t4 l3 Q$ e) h" d. x Heatbug proto = (Heatbug) heatbugList.get (0);
$ U& \0 w4 b6 H1 r) ~+ [8 ?% h5 D! S Selector sel = ) [/ ~" f3 }- Y6 N7 ^/ f4 X
new Selector (proto.getClass (), "heatbugStep", false);
* o3 [" K- A R# m4 W; D5 | actionForEach = b6 k$ i, _2 R' \ O' y
modelActions.createFActionForEachHomogeneous$call, j3 j& j% A+ t2 Y$ x: {# `
(heatbugList,6 l3 g6 g( |. u# ~/ u: n9 _, ?! b" L
new FCallImpl (this, proto, sel,
& S7 `, i9 n/ O4 F; b& G new FArgumentsImpl (this, sel)));
# s- j6 t0 L# s6 C: z } catch (Exception e) {
+ K& v% t& Z9 Q1 ? e.printStackTrace (System.err);7 H% O2 l: {1 ` A% I
}- H: L. i9 e" C! t2 C& _1 m$ X
2 x! `. C2 y7 d: D: D
syncUpdateOrder ();( E6 ^- M7 A @' B" G6 x
; w1 y7 v% A# R- ~8 [
try {" W& b/ O# j& k2 W. n, i9 e
modelActions.createActionTo$message
8 C1 | F3 h+ I0 H (heat, new Selector (heat.getClass (), "updateLattice", false));! w# L, v8 z9 n
} catch (Exception e) {6 c2 [! |" S4 ^/ K
System.err.println("Exception updateLattice: " + e.getMessage ());! U0 x7 S! O+ g7 A! N6 \( B4 p6 L* j
}0 ~- [; p0 O; m
7 K4 W. r1 Q2 q) a. D // Then we create a schedule that executes the
5 n4 ~# V9 |6 N) { // modelActions. modelActions is an ActionGroup, by itself it
z" H( h" F# P! r7 @. Z. }5 H // has no notion of time. In order to have it executed in6 Y" b) m- @; ^+ V
// time, we create a Schedule that says to use the( @- M1 i5 S K2 E
// modelActions ActionGroup at particular times. This: R5 l" V f; D* d6 J. c! [9 {
// schedule has a repeat interval of 1, it will loop every) u2 @. f/ |% N5 e2 ^
// time step. The action is executed at time 0 relative to
5 e/ }: q) a- @ // the beginning of the loop.
[9 Y* b& ~, Q9 J1 X! k' h/ s v6 t: q9 Q
// This is a simple schedule, with only one action that is
- M7 x- U- a7 q/ F- Y9 K0 I // just repeated every time. See jmousetrap for more7 h: J1 U# q$ @6 ~' V2 `
// complicated schedules.6 G; h4 ^6 j- O1 X1 x7 ~5 @4 X; b
( S- t4 Z2 _, y. a3 K e
modelSchedule = new ScheduleImpl (getZone (), 1);; v# n5 F, z+ z6 o* ~
modelSchedule.at$createAction (0, modelActions);
9 `7 t: G6 ?- q" d2 L" n/ q 0 j) x+ i5 S5 i6 h5 d
return this;. w) e' I( g- r y9 k6 \ h# w, f
} |