HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% }& m0 @5 Y' @! p$ [
* t8 o% f/ p c6 I% R public Object buildActions () { X* g8 V) m0 }$ @# c! W
super.buildActions();5 H* }1 `0 b* U5 T9 @
# c0 g( T# O* ~4 t& q // Create the list of simulation actions. We put these in' o- s6 Q" e; }5 L6 z
// an action group, because we want these actions to be6 h( ]" A$ y4 Y; V9 q# M9 c9 i" e
// executed in a specific order, but these steps should" E. Y5 N/ P: Y; }! R! B( g- n2 |8 {; J
// take no (simulated) time. The M(foo) means "The message4 ?0 l/ c* y6 w9 U
// called <foo>". You can send a message To a particular* Z+ z8 r. p. t' Y
// object, or ForEach object in a collection.
; i0 ?( v" r2 s/ K- g2 r; I
. A: i9 J# `8 P/ b' F* w // Note we update the heatspace in two phases: first run
. S7 n0 ]! Q0 @* |% B+ P }3 P/ @ // diffusion, then run "updateWorld" to actually enact the
! k) V0 O; B. X6 Q* m& T% K: } // changes the heatbugs have made. The ordering here is, C, g7 n. r- Q* I
// significant!
: g7 Y: K3 ?4 | / |2 J# i& A; v; R: {
// Note also, that with the additional5 w5 B% R$ R0 ?8 X9 `. O7 p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ {* j# l5 q. n/ S1 O+ f* { // randomize the order in which the bugs actually run% z# i8 E) Z! |8 R& J2 R5 ]* e2 B
// their step rule. This has the effect of removing any
; j8 z+ D0 y2 Q: |/ T/ Z5 Q) f: J' t // systematic bias in the iteration throught the heatbug
! }- h5 G) X. Y2 `6 j8 S( V1 y // list from timestep to timestep: h. L! |0 l5 {
0 l1 I6 |2 X' i$ J
// By default, all `createActionForEach' modelActions have$ B1 e$ r" C6 ?# W, }* ~
// a default order of `Sequential', which means that the
7 L) L2 X; R/ }6 w6 c% T // order of iteration through the `heatbugList' will be% G. |, w9 O5 V/ ~5 R5 v# [) H4 [2 b/ g
// identical (assuming the list order is not changed4 i1 {, c) t2 {- p+ p
// indirectly by some other process).2 Y. K! k* X6 F$ ]# c. b9 O
$ G* ]0 P; D' F+ _
modelActions = new ActionGroupImpl (getZone ());
' E& P" x. U: _2 ?5 Y1 r) o
/ m! ?! Z/ }, [. P! I6 n; a try {! y: x5 Y# [$ a! I; f
modelActions.createActionTo$message
, J5 d( G0 X- A (heat, new Selector (heat.getClass (), "stepRule", false));
3 R; H0 g, V; k! [$ U } catch (Exception e) {
6 X, j: V5 A; e; h6 l2 u System.err.println ("Exception stepRule: " + e.getMessage ());! ^% y& t( L1 I1 z+ h
}1 b! ^4 l: v0 U
) s" v0 Z8 A) c* S+ r! Z
try {
N; m1 h F) f Heatbug proto = (Heatbug) heatbugList.get (0);
. E' a6 I6 [/ q+ ]" O. M4 y Selector sel = 9 {$ m8 Z* P( c+ b/ J( [
new Selector (proto.getClass (), "heatbugStep", false);1 T, c8 b) K" B6 i# ^/ N" Z
actionForEach =
" p# ?# x6 T# ~; q. z) ~/ M3 U modelActions.createFActionForEachHomogeneous$call
; e4 t1 D& w3 S$ z3 \7 A (heatbugList,
8 g4 T4 x# O( R" H3 }: M$ g% u4 D new FCallImpl (this, proto, sel,2 M7 }6 B( _: t& D$ W
new FArgumentsImpl (this, sel)));. t6 m. [) Q/ z4 f& ^
} catch (Exception e) {. ~1 F3 y' E( G& U
e.printStackTrace (System.err);4 K1 K' U3 |8 _3 R1 u, U/ Q
}
& P: _& i [3 Z4 [
j) O) J7 A5 g9 V syncUpdateOrder ();% y, V+ O) i. J- `
, o# `; O) T/ M3 l
try {% v* L5 @* M9 E ?0 T9 w
modelActions.createActionTo$message
. R6 E; W9 j a9 C# v3 p& Z- N (heat, new Selector (heat.getClass (), "updateLattice", false));( E3 y$ \/ c' D
} catch (Exception e) {' X1 Y1 e3 @- |' m
System.err.println("Exception updateLattice: " + e.getMessage ());/ ^% }8 k( @# @* s
}. e! e, v6 p6 B6 A( o; G
% z) s/ S0 n, a8 l4 C/ R // Then we create a schedule that executes the4 ?- v: r# w. _8 ]: e3 p
// modelActions. modelActions is an ActionGroup, by itself it" {) T' b8 z4 ~$ h
// has no notion of time. In order to have it executed in
% `6 [8 T+ h: P i' H8 Z // time, we create a Schedule that says to use the
5 {% W1 _* j, `) q! A m+ B // modelActions ActionGroup at particular times. This- V# @4 @& u: ^' ^, q9 P
// schedule has a repeat interval of 1, it will loop every/ y, j2 Y4 h" v# [! @ R
// time step. The action is executed at time 0 relative to: H: i- [) n5 ^: q# L s: _# e
// the beginning of the loop.
# e' B- s) o% K4 f; ~ \1 K; Z1 s# P: t
// This is a simple schedule, with only one action that is8 _. d" y! Y2 b9 l% t& ?2 {; J. W
// just repeated every time. See jmousetrap for more' ?; J% r# U0 s) B) ]
// complicated schedules.1 Q+ ~8 M& ^: f" x- M4 i* v" ~
) x1 q. A# J: v5 s$ e) @ modelSchedule = new ScheduleImpl (getZone (), 1);3 v: ]6 @. N+ J( |& Q
modelSchedule.at$createAction (0, modelActions);
j' {0 ?" n; a& t% U, Y; b / s1 H( p8 @/ l' Z; \. }6 F
return this;% H0 u# y2 b! w# A7 i3 r
} |