HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& K1 |1 q+ m- K5 k% f5 d+ S1 l' G! I: {
public Object buildActions () {
8 S) X e: H5 |2 a+ d$ U% a, w, Y1 M super.buildActions();
) s+ B' p d" G& ~9 ?
( q4 @ z7 X3 D) ^/ Z% H // Create the list of simulation actions. We put these in3 v! j6 q2 |, u- C7 E6 ?0 V
// an action group, because we want these actions to be& V( Q ^6 \8 W+ M" X) _ l; w
// executed in a specific order, but these steps should' W8 Z* n2 E- W* t5 C- d6 p
// take no (simulated) time. The M(foo) means "The message, i& |3 a( P5 y8 `8 ~, Q( e. W
// called <foo>". You can send a message To a particular& i$ w4 f7 }8 Q ~9 v
// object, or ForEach object in a collection.
% Q [" y- w' W) T6 @# [
% b. j# A# D) k1 F$ |3 A6 U // Note we update the heatspace in two phases: first run4 c/ {) }6 C, W% f
// diffusion, then run "updateWorld" to actually enact the9 L$ Q4 S- U" u) F: Z" A$ o, ?
// changes the heatbugs have made. The ordering here is
, H+ r. C4 S. O // significant!3 d8 M6 g0 Y! A9 v0 |
F( D4 z. P6 h. a/ p% ]8 @6 { // Note also, that with the additional0 \$ e; b( U! ?2 J$ l7 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 I# I( X" m1 C. b8 s' y // randomize the order in which the bugs actually run
1 q* G/ E- u) n) Y+ s // their step rule. This has the effect of removing any( A8 F/ y/ S9 E& W! Y- ^$ Q
// systematic bias in the iteration throught the heatbug5 q( [5 v8 c* U% m
// list from timestep to timestep
+ O# X* n( `6 h( J: E" {) E
7 t5 n4 `& q d6 u$ i9 d- z2 A // By default, all `createActionForEach' modelActions have
1 H) x# s2 i4 ?% D* I, C // a default order of `Sequential', which means that the
: P, z0 ?* e( H+ p6 w; ` N // order of iteration through the `heatbugList' will be
9 L" k% x- I5 _& t9 S // identical (assuming the list order is not changed4 t, h' y$ [$ i8 `9 _/ T
// indirectly by some other process).* ]7 n; c! p2 x% b! D
3 r9 ]6 j( @8 ^8 h* n R modelActions = new ActionGroupImpl (getZone ());
V1 |9 ?& c0 }3 J4 l1 u' V- Q3 } e( m( Y& U4 I& m
try {
$ { m, x8 b8 N modelActions.createActionTo$message
3 z; W( ]8 t# i+ c8 B4 w4 ? (heat, new Selector (heat.getClass (), "stepRule", false));& W, M* {, d3 B
} catch (Exception e) {' A3 u3 m% o' E) q* B1 w; U
System.err.println ("Exception stepRule: " + e.getMessage ());
2 u+ H) ~: s9 {7 g0 q' K- g }) D5 \7 S0 B3 t0 p
8 s# `6 D7 H/ L) m try {
2 y. Z: J. j8 J* b/ _2 Z- S2 X9 M Heatbug proto = (Heatbug) heatbugList.get (0);
. r1 D6 `9 U( o. b5 ~ Selector sel =
/ ^! H4 t+ B; b) H0 S new Selector (proto.getClass (), "heatbugStep", false);, A* U" I9 k1 B; v
actionForEach =4 c& F9 C/ v" I" `0 h/ w
modelActions.createFActionForEachHomogeneous$call- ~& I2 q- \6 P2 i$ R4 \
(heatbugList,! [! z+ c; D3 v) t" ?0 K
new FCallImpl (this, proto, sel,0 C8 b0 ?" Q: _
new FArgumentsImpl (this, sel)));
; X5 D, a+ @/ Q E, ^- \ } catch (Exception e) {
; c9 X" N1 c& W5 f/ H& Y e.printStackTrace (System.err);
# Z5 w# a; h+ H& Z } ]5 k- x4 F" B1 ]
: W# u8 t, m2 d" Z% } syncUpdateOrder ();
( Z3 @# A5 y9 r* a' I' e* h4 b7 O$ s! u) ^- F0 u
try {- E# n& a7 Q4 z N2 [! H1 c
modelActions.createActionTo$message
( G. p, B9 y+ P. k" @4 x5 U1 s$ i (heat, new Selector (heat.getClass (), "updateLattice", false));; Y; ?8 L- L3 d' R. O
} catch (Exception e) {5 s! U- f5 E: n
System.err.println("Exception updateLattice: " + e.getMessage ());
; T9 U! z; h% N ` }8 M- q1 x4 M7 `
* I* b, h* r' [ // Then we create a schedule that executes the
" l$ H: | l5 A9 a // modelActions. modelActions is an ActionGroup, by itself it
% Y/ X. G9 F `& F // has no notion of time. In order to have it executed in) |3 z* ~( e, O' m
// time, we create a Schedule that says to use the
i3 q- |1 L3 ^0 I/ @; ^3 J // modelActions ActionGroup at particular times. This
' V, |. E0 M( {: d // schedule has a repeat interval of 1, it will loop every
+ B1 j- p, B; Y. H // time step. The action is executed at time 0 relative to
2 @) Y! i6 K8 W. k0 W7 H // the beginning of the loop.- L# ^: n( B3 l3 u: y* c
. ~2 m2 l2 {) E. p
// This is a simple schedule, with only one action that is% i3 {, J! h' g8 q# H: z
// just repeated every time. See jmousetrap for more' y- g7 \4 \- t
// complicated schedules.
2 m b( U& w1 V+ D$ H
" i9 K H% i. ^ X1 { modelSchedule = new ScheduleImpl (getZone (), 1);5 C1 _ F- y5 }! w" d
modelSchedule.at$createAction (0, modelActions);
- i0 g+ d0 d: H
: v# s$ R/ @! e5 m5 S9 M0 S; w return this;! |# i! |# F/ u( h9 N/ o
} |