HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! G+ y5 P& n- A5 _
9 i% q, y3 }: _ public Object buildActions () {5 w, l" O; e; N \& i0 O
super.buildActions();
4 O9 V* a% [5 Q# K! E$ L 6 @& _2 m+ t \- k/ k g
// Create the list of simulation actions. We put these in% X) S1 n6 T7 M# [5 x2 P5 D" x
// an action group, because we want these actions to be! s9 s. o1 h% K
// executed in a specific order, but these steps should
, F; p$ U; H) @- K! \; T // take no (simulated) time. The M(foo) means "The message
" X9 I8 `* t1 e6 ?, J1 S // called <foo>". You can send a message To a particular
5 I" f9 a) y; H( z: R" R // object, or ForEach object in a collection.5 {+ s: R5 R9 t2 K) Z
8 b/ h/ L- c& j2 ]2 ]: s& s0 ^; I
// Note we update the heatspace in two phases: first run
) c7 P) x( x3 }1 U3 i // diffusion, then run "updateWorld" to actually enact the6 r' m7 i- |; s- N' l) {
// changes the heatbugs have made. The ordering here is
3 B, z4 v% j6 k% c# H- J! C // significant!
4 n% K/ Y8 q8 y: H, ?
8 b6 E! I! s" r& P$ I // Note also, that with the additional
. g1 }$ g+ f5 f" Y# ^5 n8 j // `randomizeHeatbugUpdateOrder' Boolean flag we can
- D' K7 d$ ~5 z4 f // randomize the order in which the bugs actually run* \1 s6 L4 P5 ?( t+ ?9 ~3 ?
// their step rule. This has the effect of removing any
, ~0 ]0 \, j9 O // systematic bias in the iteration throught the heatbug
7 S( ?" e. p/ `. v/ { // list from timestep to timestep( c$ }9 C& j1 y( g
& d+ F, _. R- i ?
// By default, all `createActionForEach' modelActions have
, K! l0 @4 k2 u // a default order of `Sequential', which means that the7 T H# f- d1 Y. F2 n; x
// order of iteration through the `heatbugList' will be) @4 B8 c" \2 W! `; U
// identical (assuming the list order is not changed
. a; P: } ^6 Z+ X G% x0 [ // indirectly by some other process).
: W' l* A! Q2 N* C$ |
8 u; e9 W0 ~5 \% T3 o% N* S/ X modelActions = new ActionGroupImpl (getZone ());& Z0 p# l) _. u* a" `0 I( B7 q
3 n* J, w+ E( U# P
try {- g) i, y3 s& \ k1 @& U, G; K. i- Q
modelActions.createActionTo$message5 T. Z7 l& Z4 k* G8 b3 h X7 q
(heat, new Selector (heat.getClass (), "stepRule", false));+ |2 T5 T; v" A6 I6 ? M# c
} catch (Exception e) {- z* ~* u4 Y w! K! c
System.err.println ("Exception stepRule: " + e.getMessage ());
. F B+ F' B4 T4 i4 f }' a$ k, t1 }2 U3 o
) F5 K: G& _- D! ?: A) y" U! f
try {2 Q. H, ^1 K3 I, c
Heatbug proto = (Heatbug) heatbugList.get (0);
/ u7 K- A0 V, n7 H* m' v4 ^* o Selector sel = / }& \5 a& D# r- J3 k) A
new Selector (proto.getClass (), "heatbugStep", false);
( k* d0 ?7 }3 y& i5 T1 M actionForEach =
" L7 p9 P2 o( S5 f% i modelActions.createFActionForEachHomogeneous$call% s- M0 Y3 A4 {0 N
(heatbugList,
& k: t8 e1 I! b" Z$ H' ^4 z new FCallImpl (this, proto, sel,
5 m1 ]: F$ K3 @ new FArgumentsImpl (this, sel)));3 E1 _( m9 v5 ?6 y/ g
} catch (Exception e) {. G) v6 M! C3 x- A, V# G% i
e.printStackTrace (System.err);( d2 q* {2 D" i1 N; T J
}' f- ]4 y* ~& J+ r7 _+ p# K- L# P n
; L6 s! ^ F6 O- E
syncUpdateOrder ();
" \9 t2 z! P1 m' q& Y0 N1 d, R
' K8 A6 w% A: q& ?& l# f try {
. v# D+ x! Z, H+ M: p* ]6 a modelActions.createActionTo$message 5 L7 @* H# U" M1 O" J
(heat, new Selector (heat.getClass (), "updateLattice", false));
! ?3 }- A# Z- w' i, d } catch (Exception e) {5 e, i9 a; _6 Y5 q( [$ P! W
System.err.println("Exception updateLattice: " + e.getMessage ());
: z4 B$ K9 r. X# T1 o, O3 J }
& `, j+ W/ ~0 r " f4 C3 d1 n, z n# V6 S+ `
// Then we create a schedule that executes the) m& E$ [4 M( N8 f
// modelActions. modelActions is an ActionGroup, by itself it) w9 t$ s. o+ t6 f1 d2 G% V0 f
// has no notion of time. In order to have it executed in
' w( U! X. ^) @/ i; I0 {6 w // time, we create a Schedule that says to use the6 v: s( d' p" b e. {8 l$ K
// modelActions ActionGroup at particular times. This9 g, i* a7 [+ G- N# q# O6 Y6 W
// schedule has a repeat interval of 1, it will loop every/ x% m9 p: D. x. \$ k
// time step. The action is executed at time 0 relative to4 c; T8 M: y* ?5 @3 E3 p5 O1 c
// the beginning of the loop.0 ~4 c( n/ z( n5 `4 l% M1 ^( O" j
5 i3 m7 [: O o
// This is a simple schedule, with only one action that is
% o( G. J- N0 w0 L# U5 b // just repeated every time. See jmousetrap for more) e1 U! B& f3 `6 F7 P: p
// complicated schedules.
& F- q3 B" `" f9 _) X) g 3 Y: x& k" n+ p
modelSchedule = new ScheduleImpl (getZone (), 1);
% J, R2 h) E6 W( w0 J modelSchedule.at$createAction (0, modelActions);" ]0 D, ~( z2 w8 [. |' p
8 `& Q/ n0 v& z# u/ x2 ?( Z
return this;
" x v3 `! e7 J# a/ F" { } |