HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! V# o+ R- Q' I8 b- y* Z4 |% d' m( K. {0 w- b
public Object buildActions () {. X* S' B' v7 r
super.buildActions();
. r$ H6 W. o/ a9 J# h $ f6 {3 Z o. [+ u6 \+ r/ r9 _
// Create the list of simulation actions. We put these in/ [5 P! ~ q6 D! Z+ j1 s5 u0 ]
// an action group, because we want these actions to be
( h: v, v6 u6 a( p3 q // executed in a specific order, but these steps should
! G/ O+ {0 a5 Y: X8 \, F G0 K // take no (simulated) time. The M(foo) means "The message4 I$ Y" m6 V( D/ A0 X$ n' p
// called <foo>". You can send a message To a particular
7 g* ]2 K. G7 N6 B4 R8 M$ ]: p // object, or ForEach object in a collection.
/ L- Y/ q3 d& |+ v
/ G$ @9 F1 i# ~ // Note we update the heatspace in two phases: first run+ q) j, p/ `3 Z- }
// diffusion, then run "updateWorld" to actually enact the
: } c8 I! H! R6 r& C // changes the heatbugs have made. The ordering here is1 X0 E4 A5 ?' R, p* w
// significant!
: S% Z+ z7 O2 I; y2 d; K# _
7 ]6 o$ k; c; X+ N // Note also, that with the additional
1 C4 w }. ]' P // `randomizeHeatbugUpdateOrder' Boolean flag we can, A9 I5 B4 a' d7 N9 e
// randomize the order in which the bugs actually run% v) X$ Y3 T) p: W9 {* V# R, E
// their step rule. This has the effect of removing any
% O$ l* a$ S2 v( h4 s; M // systematic bias in the iteration throught the heatbug
+ `! J# j6 o% ?7 N# ? // list from timestep to timestep4 B) r! g6 }0 o- v. l! l2 r
; z5 T# p, Q/ W0 p, j- Z! j# L
// By default, all `createActionForEach' modelActions have4 c# F' s9 }! q. Y1 G
// a default order of `Sequential', which means that the
' Z3 z' l9 I$ c" {! c // order of iteration through the `heatbugList' will be3 B! O, d; t, U1 D8 j
// identical (assuming the list order is not changed
, y1 x8 k6 X/ B& O# a // indirectly by some other process).
Q! s* f4 `$ t* H* A
% f4 S7 ?/ N2 D& m modelActions = new ActionGroupImpl (getZone ());1 _; T3 }* F! x7 W( ]7 Q1 i
' S' F' W8 F5 K0 u! } c! | try {
# \' d) H3 Y h4 a# R modelActions.createActionTo$message0 E9 f0 l" @2 v3 X! A
(heat, new Selector (heat.getClass (), "stepRule", false));0 k% J9 y) K' H
} catch (Exception e) {
# q/ ^- A% x; y5 J- c) b+ T System.err.println ("Exception stepRule: " + e.getMessage ());, B/ U s0 x& q1 R6 }9 ~
}
; u) ?9 ]6 E1 t8 w0 m7 }, H: ]) ^ B) d1 R; r; X, I9 S6 X' _
try {) j# w. o6 I7 Y3 M. @
Heatbug proto = (Heatbug) heatbugList.get (0);
$ ~3 a/ Q2 h% j Selector sel = 0 X! N3 a% Y/ w$ ^2 @- p: |# \
new Selector (proto.getClass (), "heatbugStep", false);0 x9 P& s2 F6 s' a. Q! E) Y; I9 L
actionForEach =. J$ P, I x( L3 Y
modelActions.createFActionForEachHomogeneous$call; \: ?! Y. U5 n+ g8 i# W1 t% |* l
(heatbugList,4 _6 }! g( D6 m$ X# B# H
new FCallImpl (this, proto, sel,; I, U9 T( } {" x$ n8 F
new FArgumentsImpl (this, sel)));
+ V7 |# M! _0 K+ _6 q3 _ } catch (Exception e) {: f0 p' G4 t4 N. V# Y" k A$ z& D0 i
e.printStackTrace (System.err); w& S7 T J9 L- ^( ]
}- e9 ^( \! I& I/ m# U
" Z) c# k8 r9 X8 H$ p0 ]0 U7 p+ e( W
syncUpdateOrder ();
: g' {8 S0 c; u$ S. q3 K' o, E8 u! R( U, y
try {
: U6 \% `# a) |6 D4 F* w( t0 m0 o modelActions.createActionTo$message 7 A$ E1 H7 T G. u5 e8 G
(heat, new Selector (heat.getClass (), "updateLattice", false));5 t$ s. E# w& c) A t
} catch (Exception e) {: P; H# b+ [; H' O
System.err.println("Exception updateLattice: " + e.getMessage ());
# a i4 K* C+ H3 p0 U1 ~8 j }
- E, \( f4 y. U$ D & b- e& S2 g( w4 J( y# C) J
// Then we create a schedule that executes the* E4 M, X- f: S2 ?! b6 _/ y, U
// modelActions. modelActions is an ActionGroup, by itself it0 O# F' A1 T d" Q0 }' w8 z# m
// has no notion of time. In order to have it executed in
) ?% e3 `7 Q5 D2 g- B e" a3 s( \+ v // time, we create a Schedule that says to use the
4 [/ d) ]1 B& n; N9 s& H // modelActions ActionGroup at particular times. This
1 j/ s N; e$ R" l& W5 m. C2 v // schedule has a repeat interval of 1, it will loop every
) ^& W* P- {2 f$ u0 f // time step. The action is executed at time 0 relative to
! ?% l) e. X* n5 z9 ^) o( N; | // the beginning of the loop.
" l8 U$ J% l9 ?% Z# f/ |" L- k" b' B. ~- r: |$ @# \0 k/ B1 ^# R
// This is a simple schedule, with only one action that is. T& ^: j& J T
// just repeated every time. See jmousetrap for more$ j* o: i$ Y* F
// complicated schedules.; Q0 n5 q' }% j. Z4 S, \& o
5 m5 l* H# ?7 V7 l& o0 ^4 D4 U. `
modelSchedule = new ScheduleImpl (getZone (), 1);" ?# | v* ~" y# I, r. n
modelSchedule.at$createAction (0, modelActions);
$ |" t5 X- ~/ \. x$ C
* s$ R# B- D# O' r return this;
7 \6 n7 T7 W ~1 K; V } |