HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. l# ?4 A$ Z" D: u' Q1 P) P6 C5 f" S4 g# e! j" Q+ }; M
public Object buildActions () {! e4 b# |2 h% A. i& C' f
super.buildActions();
) @9 | |7 I/ c/ ]* g, y. x * y7 z$ @# r: P4 l: r' m
// Create the list of simulation actions. We put these in
+ G8 E! @; d8 i; E // an action group, because we want these actions to be
2 K: Y8 W4 O: {: x5 k4 f' P& e // executed in a specific order, but these steps should
* X8 V% s0 B2 ~5 e0 _# ?" f // take no (simulated) time. The M(foo) means "The message
+ |+ m9 R: F- W$ ~+ z8 i* k // called <foo>". You can send a message To a particular! x. {) g- j- L) G
// object, or ForEach object in a collection.7 U% t" \) z) ~9 E
8 _7 o( h) L; Z1 E4 }( i4 q // Note we update the heatspace in two phases: first run
& w: @! l2 o" B4 R9 X% J+ U // diffusion, then run "updateWorld" to actually enact the
: W9 ^# K1 O( Y- G // changes the heatbugs have made. The ordering here is5 d2 ^: m. R+ }
// significant!$ g6 f' U$ \! }) w9 J/ K
0 A) f3 ~ t0 m, c6 Z* W
// Note also, that with the additional9 I2 t8 c! u& k+ H5 U
// `randomizeHeatbugUpdateOrder' Boolean flag we can
U# ]2 O2 h' z( r: K& d* n // randomize the order in which the bugs actually run
$ A) ]" P9 p/ B6 P9 V4 B7 F // their step rule. This has the effect of removing any
3 x& b0 Y1 A" d" ~# U // systematic bias in the iteration throught the heatbug
" r6 D4 [& f# ~ \+ _ // list from timestep to timestep3 X% m9 r& L6 t% P6 F2 r \
4 N" a( X& V# u1 s: p8 t
// By default, all `createActionForEach' modelActions have8 g% Q' C$ s6 o' l9 G! l5 t
// a default order of `Sequential', which means that the: |2 B7 Z8 U/ X4 Q- p7 X Z
// order of iteration through the `heatbugList' will be
9 e( ]9 I1 [ q. u( O // identical (assuming the list order is not changed9 t0 Z2 C: _8 M) s4 Z
// indirectly by some other process)., U# ?9 Q# y4 z. G
9 _: ]% F: B6 w% |. T
modelActions = new ActionGroupImpl (getZone ());; T* R3 w, S5 g. a9 x
) x4 j$ f9 P% i. f
try {4 {$ H3 w5 b' d, c: X" {: u
modelActions.createActionTo$message
! Z g4 F y# W' ?* z (heat, new Selector (heat.getClass (), "stepRule", false));
, R7 v" }4 k+ o! D6 i6 d } catch (Exception e) {) `% r- q& T3 y
System.err.println ("Exception stepRule: " + e.getMessage ());
$ P8 {% T/ E+ b ~ r }: X; o9 f, R1 u" ?" }
! z' Y/ B! Y- N* W# C4 m
try {: D. R1 n8 Y7 c# u% A
Heatbug proto = (Heatbug) heatbugList.get (0);
2 L; z% m+ Y/ Y5 w Selector sel = " m$ w2 D5 C+ V
new Selector (proto.getClass (), "heatbugStep", false);2 m& k4 f. E2 ~+ L: P `
actionForEach =
% G8 ~! R. v, M2 a modelActions.createFActionForEachHomogeneous$call
. ?, d8 c7 Z, j" \7 p3 c (heatbugList,
: [; ^2 v. V/ ` new FCallImpl (this, proto, sel,- W f1 ~& O. \5 n' J
new FArgumentsImpl (this, sel)));0 H1 l, e) I2 h- ]8 S
} catch (Exception e) {
; f4 e& X0 s% }3 _9 ~ e.printStackTrace (System.err);: C5 L" |- v2 E' t
}
% B7 h( R6 r) v E
% y) p7 {* v2 o! H# @ syncUpdateOrder ();
9 y9 L% E, s8 e- @ D7 ?3 r1 l% W* B, x. |$ c5 x
try {
6 q: K( D9 g3 j$ z1 l7 F! n modelActions.createActionTo$message
+ I& I: f" x' r5 ]0 I# _% v (heat, new Selector (heat.getClass (), "updateLattice", false));
; w/ M; u3 |( |' g } catch (Exception e) {
9 a3 s9 s+ ~4 |8 H' T% T$ Z System.err.println("Exception updateLattice: " + e.getMessage ());) o4 x ]! ^4 @2 z V0 z% T o- B0 h
}2 a& a0 r$ ?; W& I) J; N
7 s% r( s( L9 _
// Then we create a schedule that executes the
9 K' @4 M I9 P" `6 \ // modelActions. modelActions is an ActionGroup, by itself it
/ h9 F1 o4 [, E3 D, ?$ u& }8 |. t // has no notion of time. In order to have it executed in
( g a @8 K2 b; o" r; X, s // time, we create a Schedule that says to use the
: D4 u4 z4 z$ L. s; g0 q" H3 I4 [ // modelActions ActionGroup at particular times. This9 B2 c4 m% j7 ?9 L `* G! [( Q
// schedule has a repeat interval of 1, it will loop every
1 d8 d7 S3 }9 |, x; u // time step. The action is executed at time 0 relative to6 M+ B2 i" y1 F/ @( z% h! j
// the beginning of the loop.
, l }0 L% p1 J! ?6 c& F7 m4 T* P
( W0 ]) \3 z6 t0 v/ \. W! x // This is a simple schedule, with only one action that is; U5 a& |. R/ I8 F, ?& F" C# D' N
// just repeated every time. See jmousetrap for more C4 k- S) C- b' Y m
// complicated schedules., v1 Z" j* `7 M8 {' v
5 b" m* v" _+ i7 @ modelSchedule = new ScheduleImpl (getZone (), 1);1 Z: y2 d4 `/ S# F4 N
modelSchedule.at$createAction (0, modelActions);! q2 z: o3 Y$ M( |9 c
, V H6 a, Z! [; `. C return this;
$ }" j+ V; g% J. H5 B: t0 ] } |