HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" X# Y q/ D+ r: g# F( f0 f
# T+ P5 x" a' Z3 X3 [0 [
public Object buildActions () {! B6 f6 a, P( I( M7 U' e
super.buildActions();
5 p3 j' E$ a$ s1 b
- [7 l6 \/ p" e& n$ G7 u // Create the list of simulation actions. We put these in" r6 I* o a! ?& h
// an action group, because we want these actions to be/ o1 M# S# Q! o' ]+ Z- d8 o
// executed in a specific order, but these steps should
3 ]( u) l- T8 ] A2 j // take no (simulated) time. The M(foo) means "The message4 K1 G2 P) Q0 x" ^8 y3 K
// called <foo>". You can send a message To a particular; y- x: X8 d3 O$ n
// object, or ForEach object in a collection.
0 R+ ?* ?& x! p( ^% p9 A + i9 i) d" K, \0 H; R
// Note we update the heatspace in two phases: first run) w* j1 f* I* ]/ R C
// diffusion, then run "updateWorld" to actually enact the
* f% O3 c1 [" f" ?1 k i // changes the heatbugs have made. The ordering here is
) _9 q4 ?5 Z5 p! z6 y6 ? // significant!
/ Y5 N) p: a9 T+ O ! x2 |5 j, a! D1 v) H
// Note also, that with the additional
! c/ a- L" W7 M. P' x; z // `randomizeHeatbugUpdateOrder' Boolean flag we can
( k6 t8 i' T/ n, W // randomize the order in which the bugs actually run
- p' q7 ?, l( d: Z/ S9 e7 B- ` // their step rule. This has the effect of removing any
1 K4 J E1 s) k- _! p$ } // systematic bias in the iteration throught the heatbug# L/ _6 `+ }/ A
// list from timestep to timestep
# ^: d4 j/ y4 ~, T
n' C6 ?6 q6 h0 L, u // By default, all `createActionForEach' modelActions have0 _- h9 H; }! W
// a default order of `Sequential', which means that the% W8 M# U, I6 t6 w
// order of iteration through the `heatbugList' will be
- q$ k& ~6 k# B1 k // identical (assuming the list order is not changed
' f( @- Z4 ~% e6 m // indirectly by some other process).
( }( j* _( F6 A8 ?# A7 ~- r
2 l4 \0 X$ h4 ]& F modelActions = new ActionGroupImpl (getZone ());
9 c* K. p% A. H- ~/ n* z, h) a! R, J8 F6 E# D6 M
try {% e4 D W3 b$ Z) R* R
modelActions.createActionTo$message3 S! E/ @1 ?0 ~, \' X" f- _
(heat, new Selector (heat.getClass (), "stepRule", false));
6 [" G+ v" B O3 n$ a X l } catch (Exception e) {
, c7 f" e" d# E9 D- j, E) B, N System.err.println ("Exception stepRule: " + e.getMessage ());) G% J+ W8 @+ x! a3 H
}) d1 L+ Q1 y6 H- s
. A2 h" u: J6 C+ W2 u' q
try {; j9 L5 n- N' r% W- D" ]. ^3 [3 T m
Heatbug proto = (Heatbug) heatbugList.get (0);
8 u7 K% I4 A' P7 J/ I& q0 i9 C Selector sel =
/ C% V; X4 B" Z new Selector (proto.getClass (), "heatbugStep", false);. f, t7 k3 z' ]% S. |. A0 z3 \# X
actionForEach =
+ G3 a# K# n( n, Y modelActions.createFActionForEachHomogeneous$call
1 U% @% S' [" k7 n6 ^ (heatbugList,: V3 Z* `7 U1 R! ^& O* M
new FCallImpl (this, proto, sel,7 P/ i* Q) B# W+ K
new FArgumentsImpl (this, sel)));# {1 ?+ t) T$ W+ _3 Y8 H
} catch (Exception e) {, B$ D: C. i$ U8 t
e.printStackTrace (System.err);
% X8 s. A- S" L* J4 t }9 u- o! t; g; N& n' `4 }1 Q6 }
. c( \; c* M4 g4 k; s2 [% E# K syncUpdateOrder ();
4 L, }' M8 X7 `$ }9 [' X1 `" D6 R ~ ^9 h& I% ]! ]( w
try {
* @3 K B$ ]" r0 i1 j modelActions.createActionTo$message * ^2 `) ~, y s$ x, z5 v
(heat, new Selector (heat.getClass (), "updateLattice", false));
) W& [+ x4 A. R' t/ e0 w4 N, T } catch (Exception e) {7 J3 o+ {6 G* }6 q& `( T
System.err.println("Exception updateLattice: " + e.getMessage ());7 k5 q. u2 R+ P" R' a4 u
}0 h k! U8 v8 `& e( G* ?8 _# U
6 h/ K# f. x& {: m' v4 f
// Then we create a schedule that executes the
7 ?3 g+ P- n5 A- F3 q1 } // modelActions. modelActions is an ActionGroup, by itself it* G0 o) F! l9 }, e3 K+ _5 Y
// has no notion of time. In order to have it executed in; ~7 C9 w$ z6 _; ~, M: {1 h: a0 \
// time, we create a Schedule that says to use the5 m& S( E6 E; q0 c* }% X
// modelActions ActionGroup at particular times. This) A2 x3 d2 Y& {$ R6 ~: x1 U2 S' [
// schedule has a repeat interval of 1, it will loop every# E1 D& Y+ ]) ~) D) W' y8 {% j
// time step. The action is executed at time 0 relative to, D$ ~. h1 Y4 K! F
// the beginning of the loop.0 q% k, @4 t- n2 I
0 [; u. N; `( N; B' U% u$ s. h // This is a simple schedule, with only one action that is6 P1 e8 ]4 ^. N( V# C- R
// just repeated every time. See jmousetrap for more( d! K0 c3 f7 w/ r6 O0 D" s: c9 Z
// complicated schedules.
; h1 l" E; o+ x& B ?, M3 s( N$ A: |( C
modelSchedule = new ScheduleImpl (getZone (), 1);! e2 x; k( y& `! _! R' X
modelSchedule.at$createAction (0, modelActions);
9 ?( }! \& N$ ~+ K- z# b! I) P+ Z / i8 c$ B$ w- K5 d1 r& l
return this;
4 D0 s9 O3 c) ~$ ?: ]" G8 l } |