HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 x3 _- {) Q; N v6 g
. e8 \" ?: B& _* S
public Object buildActions () {5 g7 a6 U. k a
super.buildActions();" R3 u6 e8 a4 X$ i! ?
0 ^/ ]9 @$ f2 Z) o1 N1 M" ?+ [; x9 O // Create the list of simulation actions. We put these in$ `! ^4 A$ a2 B) o. U
// an action group, because we want these actions to be
, {% a0 x0 K6 p // executed in a specific order, but these steps should
$ t" q0 Y7 m! l3 m$ f // take no (simulated) time. The M(foo) means "The message
% c! V1 y& C6 J- a // called <foo>". You can send a message To a particular
; w7 J% b9 y6 {" q- {9 `# \1 R4 p( X // object, or ForEach object in a collection.; }* L% F$ W: H l, Y
) q; n6 e$ S$ w9 r/ L // Note we update the heatspace in two phases: first run D5 o u9 b9 B, h/ X, n* ^
// diffusion, then run "updateWorld" to actually enact the
/ Q3 p5 n+ f! I" ~1 G& W7 P // changes the heatbugs have made. The ordering here is: X( s! W% A |
// significant!( F: k* c: @0 v/ X3 O/ i
* Y P* o0 R3 i! X1 ^; }9 v# j // Note also, that with the additional
+ p6 R8 \: w6 q: m# _ // `randomizeHeatbugUpdateOrder' Boolean flag we can7 @+ A: F: W3 P% S: B+ r
// randomize the order in which the bugs actually run9 R, h) t, q2 t+ ?# ?
// their step rule. This has the effect of removing any
" [4 X- D2 W. D$ a // systematic bias in the iteration throught the heatbug
/ L7 o3 `& o/ v, B+ | // list from timestep to timestep
0 A: U0 _: v* J- Q$ P( t
$ U1 F7 _; o# @4 l- N" f // By default, all `createActionForEach' modelActions have
% R6 T9 o0 K+ ?( }) \. g8 w // a default order of `Sequential', which means that the' W7 d$ L+ G: [) w; @
// order of iteration through the `heatbugList' will be
( M2 n* b/ P4 ^3 ~/ J // identical (assuming the list order is not changed
" q* Z# z- M. W6 z) m // indirectly by some other process).- f0 I6 D c) O
- d# ?* F! |- ]; V+ ^. E& G modelActions = new ActionGroupImpl (getZone ());
8 L4 C% \; Q3 {$ F4 b4 h/ ^
. }2 Z# L; O# @! \4 H9 E try {
% T' o" V9 X- n! x9 j9 v6 o. V: { modelActions.createActionTo$message; |3 `1 H2 u0 f
(heat, new Selector (heat.getClass (), "stepRule", false));
8 `$ R- |; Q9 p E* M4 k } catch (Exception e) {
0 C/ k" u# ?9 C0 P System.err.println ("Exception stepRule: " + e.getMessage ());
3 q* G- b8 n5 u }5 @0 b" x8 Q% w
$ r! \1 x( Q E+ \" ?: N" R
try {
4 \) o% c4 F' Y2 L Heatbug proto = (Heatbug) heatbugList.get (0);. o1 M) l0 P7 G, y
Selector sel = 0 L7 Q" J l) |: o& P0 O
new Selector (proto.getClass (), "heatbugStep", false); a6 p5 ~5 `9 ~' j5 F, z. g! i
actionForEach =
) v. q# O) S# q modelActions.createFActionForEachHomogeneous$call7 [! k) v5 L2 {
(heatbugList,
& ^+ i% }) K! B2 _ new FCallImpl (this, proto, sel,0 J2 P' k) Z( r% ^: l$ v9 V' p
new FArgumentsImpl (this, sel)));9 ]$ C/ B( y, O5 _
} catch (Exception e) {: j/ ?- n, B& g( n( n
e.printStackTrace (System.err);1 u/ l+ Z5 T$ t1 P% j. Z* I
}
9 G) h2 O4 x& | 6 e9 T4 T) \, E# G' k
syncUpdateOrder ();
: W. C, V. h7 R* d8 P, m& T; O3 I0 s/ K
try {9 H- F, O! @5 d" h* J
modelActions.createActionTo$message 5 k, f/ R" k- |, ]& {) b& D/ m U H
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 W5 p3 V+ T/ o; u% l0 z; b9 r& M } catch (Exception e) {8 c9 f1 k3 [. _" E9 H
System.err.println("Exception updateLattice: " + e.getMessage ());( e j+ v* d: o9 l" b
}/ X" ^& S8 G+ }2 Y7 n
7 c& b6 x4 s3 _4 y3 U! P: x1 D
// Then we create a schedule that executes the o( M! L, J, |8 U* h: H
// modelActions. modelActions is an ActionGroup, by itself it
2 w" @2 X6 T) i \+ c& i/ H // has no notion of time. In order to have it executed in
) S& c+ G, E4 [7 X // time, we create a Schedule that says to use the
- x" K. V" T: e$ U! Q0 u // modelActions ActionGroup at particular times. This' E! K" b/ _: ~0 L. R/ D
// schedule has a repeat interval of 1, it will loop every
& s3 H% g. B; P S% N( R5 ? // time step. The action is executed at time 0 relative to- @/ K: q* i2 M# N; h- f
// the beginning of the loop.- S |2 G0 e1 ^' k
$ I4 M+ x) x! j/ G4 d
// This is a simple schedule, with only one action that is3 _5 o0 I& k6 i9 \) V B
// just repeated every time. See jmousetrap for more
$ a! x" N) A9 {, G, E+ Z // complicated schedules.
: Q" [( I$ x# F. u" i, B
, [4 `( z8 i2 @5 {9 u modelSchedule = new ScheduleImpl (getZone (), 1);2 f; O7 k+ x! O0 e% y# v
modelSchedule.at$createAction (0, modelActions);2 s! v1 C/ n E2 M6 j
) ]0 E5 H! K1 ]7 r& s9 k, @ return this;
' A" p& G5 t6 W; i9 ^" q% G. b } |