HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! g/ ~* Q# J. ?& j* z L, D4 j( c7 T% E3 `$ V) [! i- r1 L/ e
public Object buildActions () {
# |9 i, f5 c/ ?* y3 ]+ o3 R super.buildActions();7 \/ u5 l7 _! y# P3 Q
0 q T/ b) V0 O# x2 W // Create the list of simulation actions. We put these in2 g$ K" H3 L$ W8 M$ {5 [+ V9 j
// an action group, because we want these actions to be
. G, r# s/ v3 v+ o. t# h; ?1 { // executed in a specific order, but these steps should8 @3 g4 N: i+ X: \
// take no (simulated) time. The M(foo) means "The message
- B0 ^% L {& ?+ u1 X // called <foo>". You can send a message To a particular
) _+ {& D7 l1 n8 C0 L // object, or ForEach object in a collection.2 \' |9 v& k5 z1 \- Z0 I' I
% N+ m% B9 P3 h# \ // Note we update the heatspace in two phases: first run
7 p9 l$ P% M |, P // diffusion, then run "updateWorld" to actually enact the
/ U/ h' P2 ?+ J) \* n H // changes the heatbugs have made. The ordering here is: P4 c9 v( j7 v2 S6 D! |
// significant!
+ |# x* K& u7 X + | w' y, `5 p' j* H
// Note also, that with the additional% |6 \' H" F8 ~% e' p0 p0 @4 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 J4 @3 P3 e# g1 d2 { // randomize the order in which the bugs actually run
( l" p+ q0 |# y" N4 A; m4 u // their step rule. This has the effect of removing any
& H8 ?$ [& W3 [; _( y // systematic bias in the iteration throught the heatbug
3 B5 m# j; ~2 {& s' @3 Z- J // list from timestep to timestep
; g2 F) ]+ X* q# v% L
6 v9 e: I+ {( C/ j" n5 y7 u( [ // By default, all `createActionForEach' modelActions have$ n+ N" l6 t- b) G, D
// a default order of `Sequential', which means that the9 x* T$ A! d! r
// order of iteration through the `heatbugList' will be# |; i# |* q V, e/ ~
// identical (assuming the list order is not changed& N6 d( y" h! r; P: @3 ]
// indirectly by some other process).# O, \6 D9 D% U J3 R0 {3 r
; s7 s/ n6 O7 M7 T3 B modelActions = new ActionGroupImpl (getZone ());( C+ m5 j0 I8 o Y2 _
- s( B, z) ?: h$ t1 _/ J8 p1 w' I5 f try {( t1 ^7 p, [8 N- }: e1 c, i
modelActions.createActionTo$message
+ B8 d3 @5 H' E: _ (heat, new Selector (heat.getClass (), "stepRule", false));8 Z: [& D( {9 F& E5 m+ K# X
} catch (Exception e) {% M" N3 x( ~# J0 G1 ~2 t* o9 \& S( ]
System.err.println ("Exception stepRule: " + e.getMessage ());; B8 A ?$ E& Q b% ?) S i0 I
}9 T l1 l4 J# Q/ A: P6 U8 h$ P
% I* O2 }2 V1 y C9 U4 ]9 G' H) }
try {2 d4 S, F% w8 ~" O- _# _
Heatbug proto = (Heatbug) heatbugList.get (0);
" f3 y# W' o* o+ p8 U Selector sel = 6 l+ o: _- D' }8 R0 d4 k. v
new Selector (proto.getClass (), "heatbugStep", false);8 h2 m- a0 e7 l, D' p/ K5 F; a" p
actionForEach =
8 M5 T1 {' y, L5 F5 T/ H" j' D3 ^ O4 V modelActions.createFActionForEachHomogeneous$call
: e' Q6 q% y7 ?5 y% @/ F+ j4 C (heatbugList,
8 i! Q# ~" x. D; f6 k new FCallImpl (this, proto, sel,( w5 F; m7 q# M& Y% e. E4 ~) Z+ ~
new FArgumentsImpl (this, sel)));
5 G8 l' K! q7 ] } catch (Exception e) {
% W0 |0 a8 ?. j) _3 u1 i e.printStackTrace (System.err);
, N( s# f6 D9 m6 W" [+ |( P2 T) ? }
6 e7 Q Y# B: w . O- e/ h- J% i2 v7 m
syncUpdateOrder ();
6 T$ w3 G# }# E6 a9 {4 A+ ~
- v! c% ^+ y3 S, M' [( G0 g try {
( Y4 n0 j8 M" k1 ?7 L) h; M modelActions.createActionTo$message / c) @" W" J) w0 ?! ]/ i6 C# }
(heat, new Selector (heat.getClass (), "updateLattice", false));9 ~- a- |# R) e5 F; ]4 o! w
} catch (Exception e) {
9 L4 r, T* ]5 Y8 V& d k: J System.err.println("Exception updateLattice: " + e.getMessage ());, v% O: X2 T; X0 [+ x* J' z
}' A v; D- ^& r- U* B
/ [6 T2 Y4 a) m$ T; v- c" |
// Then we create a schedule that executes the# ^, O" ~* Q$ i: @* o
// modelActions. modelActions is an ActionGroup, by itself it
+ f9 r3 o2 ]+ d // has no notion of time. In order to have it executed in
- X4 s% s6 V. s y, e0 U: U/ d // time, we create a Schedule that says to use the
$ B( v. O5 |: T& F // modelActions ActionGroup at particular times. This; [% e' C3 C) W+ g$ i! }6 W" e$ z
// schedule has a repeat interval of 1, it will loop every
7 V6 y! s: D9 T) s' L' Y! u // time step. The action is executed at time 0 relative to
" g7 i+ w0 t( L. A' z, G // the beginning of the loop.
x2 H# W/ h5 P
6 i" Z6 Y6 i( j3 X // This is a simple schedule, with only one action that is
; M# S6 N5 Y; d# o$ ?7 I // just repeated every time. See jmousetrap for more
' W; U- p9 T# ` // complicated schedules.6 b A! _+ ?9 ~$ J
5 d2 e' c9 p: Q. w6 N modelSchedule = new ScheduleImpl (getZone (), 1);. A" E0 x9 l. B2 R0 c( M7 p
modelSchedule.at$createAction (0, modelActions);
$ Z* [3 W# z* F# U6 {; I2 P ! d& k% i* U- M, s% z2 L. U
return this;( X' x; h0 ~5 `6 k2 d4 o
} |