HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' J) S9 T3 A; m5 V0 w9 w$ ?- C* p& R. O3 c/ I) \6 L' ~
public Object buildActions () {
( s# v! [% W2 e& O2 E super.buildActions();! H; O; t' n1 D7 Q1 e
4 M0 E8 r' x5 m; b: C // Create the list of simulation actions. We put these in8 i/ Y; B3 }7 f, Y/ H
// an action group, because we want these actions to be
9 g. S/ u( y* R' _: W9 n! ^ // executed in a specific order, but these steps should; h8 t; I3 Z S7 f+ V3 `
// take no (simulated) time. The M(foo) means "The message( b% }7 @; F/ | p+ m& m
// called <foo>". You can send a message To a particular& j# C) l) c1 V9 k4 V1 R
// object, or ForEach object in a collection.
3 h0 w3 t6 f4 S1 ?, R ( ?0 W) W. R! k7 B7 [ V6 s
// Note we update the heatspace in two phases: first run
H% v X4 n" k // diffusion, then run "updateWorld" to actually enact the
, o. S$ b: X' _4 h // changes the heatbugs have made. The ordering here is
$ K3 t, W+ g c& f+ [- d // significant!9 l$ {2 U, K% w+ C( l
- ~! z6 A& ]4 p+ e; E // Note also, that with the additional
. F: J. u9 _3 ] // `randomizeHeatbugUpdateOrder' Boolean flag we can/ L6 U$ B* o+ U& d; t8 @' _
// randomize the order in which the bugs actually run5 H" H6 ~# f! S
// their step rule. This has the effect of removing any4 P5 B3 ], I# o
// systematic bias in the iteration throught the heatbug
7 C y4 l- u" T' s0 e" ^ // list from timestep to timestep
; c j7 a8 M- _$ y/ H
& h, _& t) v! z3 y/ _7 k+ j // By default, all `createActionForEach' modelActions have
. y* N: F0 N+ Y' ] // a default order of `Sequential', which means that the Z) I( W- L3 x
// order of iteration through the `heatbugList' will be
7 w4 e0 I. ?+ d5 m // identical (assuming the list order is not changed$ w' { z" D f
// indirectly by some other process).
+ ~. r4 N ?" ~+ I5 S 0 h; V% t( H. N) T
modelActions = new ActionGroupImpl (getZone ());
+ i v( i8 }. ]6 v
7 l! x% ]) }/ k$ k# [ ? try {
1 V, b/ i: C9 ^) g. c modelActions.createActionTo$message
) y$ Y$ W7 l7 O2 q) O (heat, new Selector (heat.getClass (), "stepRule", false));2 G% J% Z0 i, p5 }
} catch (Exception e) {
0 n1 Y8 W/ `8 P/ ` System.err.println ("Exception stepRule: " + e.getMessage ());
) [, e9 [' l( C }
3 k4 k$ J M" O: x( d
7 S5 i! {+ h# I try {
~. s* G: b$ N3 ]+ ]' t Heatbug proto = (Heatbug) heatbugList.get (0);- S% h+ @, C7 l/ Y: l) J0 r% Y
Selector sel = . C( ]4 }& v, n; S% [8 {6 I
new Selector (proto.getClass (), "heatbugStep", false);
& d+ p6 ^. B; k4 T4 `1 x% I actionForEach =
4 Y2 A% \0 |' e1 d' i2 o modelActions.createFActionForEachHomogeneous$call& F6 ~* A) F- ^1 D( k' r Y, A
(heatbugList,# {' W3 _7 Q ~+ D# [
new FCallImpl (this, proto, sel,
) M, f) s8 c: E0 k3 h new FArgumentsImpl (this, sel)));! O- u3 y; P& e9 y( Z
} catch (Exception e) {
2 b8 j; Y) H5 w4 ~1 L) ]! E e.printStackTrace (System.err);
4 \1 h1 |, {/ u. T4 [" Q }/ e- ]$ O' }! O
6 d5 C M9 T0 t5 w- |1 _) q+ T& I syncUpdateOrder ();
4 X" f X) j& w' z. B+ s
! U `. h, j, o* @$ } try {$ n3 G4 K5 M8 o! V: C
modelActions.createActionTo$message
0 h4 w- o! a1 W3 [& {1 s/ a (heat, new Selector (heat.getClass (), "updateLattice", false));8 | P5 Q7 @ ]( o( c3 ? k' O
} catch (Exception e) {- I6 o' X; I5 ]; U1 D
System.err.println("Exception updateLattice: " + e.getMessage ());
) h. S3 {" O) n# \# S$ \" o* A2 m }9 a& V3 N+ F C" U9 P O
! D& p, a: S* G! E" W: E1 c" y, Q // Then we create a schedule that executes the Q9 f3 o) p: d5 J" b
// modelActions. modelActions is an ActionGroup, by itself it
9 M' P7 u: @4 G! `: S5 G& ^- [ // has no notion of time. In order to have it executed in: q3 x8 [2 [, A4 _$ m; M! s5 }
// time, we create a Schedule that says to use the
$ |! z k9 ?+ [' K! b$ U8 ~ w // modelActions ActionGroup at particular times. This
+ W. Y, ~$ m3 [% Q; c // schedule has a repeat interval of 1, it will loop every; X2 R, v( d* ?" W
// time step. The action is executed at time 0 relative to
# P: m7 e: t- t: E W$ g- @2 T // the beginning of the loop.
. a; _% j0 E b
- S, G& |. c9 T! t; e; ^8 r // This is a simple schedule, with only one action that is& n. [1 s7 x* U
// just repeated every time. See jmousetrap for more+ s$ h# c2 S; n/ I3 Y) n
// complicated schedules.
, c% s! d- `" h$ I4 O3 F. q : m( l) P- f: v! D
modelSchedule = new ScheduleImpl (getZone (), 1);
) a( Q9 W$ o* ~; u8 g' R modelSchedule.at$createAction (0, modelActions);1 O6 f4 `% Z1 ^3 T) x% C
5 y2 p! L0 Y5 c0 k
return this;
: O c8 A! t- i, e2 X1 v } |