HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: ~1 Y6 G a; Z7 A4 ?( j+ t( B8 _! K' D8 a. Q: P: v9 d' F' [
public Object buildActions () {
8 \/ a, j C+ N super.buildActions();) R! F8 [0 N8 q, E: b) L
9 A$ I. C& x- ~; ^
// Create the list of simulation actions. We put these in
8 z% \9 o, w3 |+ |- c* W3 P' W! x1 a2 `1 t1 A // an action group, because we want these actions to be9 x( _8 p" O' o0 k0 y$ I! N+ G; q/ R2 w1 Z
// executed in a specific order, but these steps should" g; A& {% a0 b' ~
// take no (simulated) time. The M(foo) means "The message. Y6 h8 C" D0 p
// called <foo>". You can send a message To a particular+ g- X1 R1 u- [3 j7 o, y
// object, or ForEach object in a collection. \% T V* L. ~0 l7 _3 e
2 E7 z* J4 r+ e+ v: l( w5 F // Note we update the heatspace in two phases: first run
5 O. N% }6 D0 ?5 L6 D& {( C6 Z // diffusion, then run "updateWorld" to actually enact the; P2 Q: j! z. \" H0 U7 T4 r |
// changes the heatbugs have made. The ordering here is
' \# n* H5 u/ L; ^0 X // significant!
* K2 \+ Z" i; e5 h: ] ! X% S% [3 l0 `
// Note also, that with the additional) ^7 A8 j4 g4 r6 u: x# a' a& j1 X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: M. g( o2 e$ u // randomize the order in which the bugs actually run+ Y9 k j1 t% ]; T9 H8 B, k4 g# `
// their step rule. This has the effect of removing any9 O1 I' z2 a7 y4 s. L( \
// systematic bias in the iteration throught the heatbug
' P4 @) G7 {' T$ E# g$ O // list from timestep to timestep
$ q1 o$ U0 S- }7 V; H1 t' Z( P5 \ Z( R 0 w& F! N+ n0 j" Q- B+ {! T
// By default, all `createActionForEach' modelActions have
. m+ t2 ~0 G% @3 E; u // a default order of `Sequential', which means that the2 A9 V6 b* z- L3 ^: m; p
// order of iteration through the `heatbugList' will be
9 o: e7 q: Y8 O& @; g' i // identical (assuming the list order is not changed
+ _% V8 f( a3 ?* R // indirectly by some other process)." I* e& v& {* E( b+ j, d
0 O3 d$ C3 t( G; ?/ u
modelActions = new ActionGroupImpl (getZone ());
9 t# L) {1 }' u4 w$ R5 y4 t: K4 F' x: C+ Z5 V. m5 R t
try {
; G0 U7 l: a% O modelActions.createActionTo$message1 H2 Q; T1 _( E" c9 U2 N
(heat, new Selector (heat.getClass (), "stepRule", false));! F- J/ z7 }, [. {3 U
} catch (Exception e) {
% M) a' n5 l! I System.err.println ("Exception stepRule: " + e.getMessage ());: f: h: b D, o7 z8 Y: t
}
/ Z. W6 R! o- n# f# d, t
7 ^% R5 a6 z0 p& q% j; @3 v) H, s try {
* |3 ^3 A9 L, } Heatbug proto = (Heatbug) heatbugList.get (0);
) ^8 n' g9 Z3 P9 q9 c Selector sel = $ z& X) }3 a7 j) x8 @
new Selector (proto.getClass (), "heatbugStep", false);4 u4 d0 y8 t# o* f. ^: D& P& n
actionForEach =9 B4 l& z# @! z( T; R
modelActions.createFActionForEachHomogeneous$call8 \8 O. [; @7 q, D7 Y" ]
(heatbugList,
5 m: V" F# L, z# X6 M( M new FCallImpl (this, proto, sel,/ m# s, k) I- n
new FArgumentsImpl (this, sel)));7 A6 g2 c: Y5 ^$ t: }/ g
} catch (Exception e) {
8 x- D) [( D' B# F+ `9 B e.printStackTrace (System.err);. O* u7 d1 \2 p
}
( h4 H- L9 \/ N/ T/ Q ( z" X+ J: D/ _6 J* q. a2 ~' ^
syncUpdateOrder ();
0 s& a9 p" L U& B( f! [
5 ~$ ~ n: J! u8 c try {
, [1 r7 b7 }# j modelActions.createActionTo$message & y% p" N! D5 V4 a/ q$ B. i
(heat, new Selector (heat.getClass (), "updateLattice", false));! U: N$ O) T+ f1 c l: P
} catch (Exception e) {
! V7 W" a K$ W, r# ]7 J System.err.println("Exception updateLattice: " + e.getMessage ());
# ~0 z' q/ A: {& N }5 _9 q& I. t! ]+ z& u/ F5 M4 U
|# n7 s3 |% f+ U. p
// Then we create a schedule that executes the; ]4 u) |* m, D8 _2 v. L
// modelActions. modelActions is an ActionGroup, by itself it' a( B7 O; E4 ], n |* v
// has no notion of time. In order to have it executed in
# P }8 P2 ^7 l; S // time, we create a Schedule that says to use the
- F X( P) P; u8 x6 f- B // modelActions ActionGroup at particular times. This
+ c2 `4 J0 h0 Q! a // schedule has a repeat interval of 1, it will loop every j9 t- Y9 u7 R- w( D2 {' h
// time step. The action is executed at time 0 relative to$ X- z& O9 V1 u# T, x
// the beginning of the loop.; C, D; j3 K5 ? B; B
% ?, @% d; X* o$ Q& V- L; i // This is a simple schedule, with only one action that is( s# K# E6 C# _- U6 }1 u
// just repeated every time. See jmousetrap for more5 `0 X/ M& h* R D6 x
// complicated schedules.
5 F, b; C0 I" T. e: y. K; {( a ( y& u& c9 o. [# ~& V6 i; a
modelSchedule = new ScheduleImpl (getZone (), 1);
! b# X0 T6 N; ] B modelSchedule.at$createAction (0, modelActions);
0 P n+ ], B% ~0 Z4 t* m! }
' o* @4 r- O* r return this;( l A3 o' i7 L' ^' b' Z$ g
} |