HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 Y f% [) K* L' k0 M% y
: E3 G5 U4 Q( D; _9 _. l public Object buildActions () {0 @8 @+ y5 x: ?) J. D' D7 C
super.buildActions();
! Z2 U" S' Y8 b) Y) N4 S. t ! P: v* T' T5 ^
// Create the list of simulation actions. We put these in; k+ r% |5 K F% l
// an action group, because we want these actions to be$ ? C1 d1 j. @" \/ z' j- p8 ^
// executed in a specific order, but these steps should/ Q$ _4 H' F6 U/ j7 C Y
// take no (simulated) time. The M(foo) means "The message
0 g, ?8 i/ Q$ x // called <foo>". You can send a message To a particular; r" y8 {" t* z' _9 x2 e1 `
// object, or ForEach object in a collection.
/ I0 _5 l( I; g) j8 W , R+ h- H0 w4 W& G; o$ b
// Note we update the heatspace in two phases: first run
8 S, e" z2 @9 K- e // diffusion, then run "updateWorld" to actually enact the
7 v: `+ X- r5 O2 p // changes the heatbugs have made. The ordering here is
# t ]& }1 C; W# f/ c // significant!
* _+ ]% Y0 q+ u' z9 T 8 Y d# Z, f1 `1 N2 X
// Note also, that with the additional
1 H% Z* u0 Z+ Q7 _, S6 i- K: E2 j // `randomizeHeatbugUpdateOrder' Boolean flag we can4 B7 W9 s/ F% ~1 O. \
// randomize the order in which the bugs actually run6 Q" w# t" L' S; {' n
// their step rule. This has the effect of removing any
) n, l8 E1 h2 _. m* g/ d5 z // systematic bias in the iteration throught the heatbug
- n1 `: v' H3 R" n // list from timestep to timestep( _* P: ]) O; \# V
, D2 h& L9 B! ^, W+ b // By default, all `createActionForEach' modelActions have# q, g+ M4 a' x1 }' w
// a default order of `Sequential', which means that the
V2 X% T5 X) x& U0 k // order of iteration through the `heatbugList' will be
/ S$ t a; c6 o1 g. `) {' ] // identical (assuming the list order is not changed
2 y( o2 h, G6 J- U, Z, _( r1 x // indirectly by some other process).9 q, X$ l+ q6 a% A2 J3 l
8 z9 h+ [' T) ^) T" ~) m* j
modelActions = new ActionGroupImpl (getZone ());
% ^9 x7 `5 z9 w b( h0 T" h+ v! E; S) L- C
try {
7 w2 p5 ^& I* g( p8 P5 O' g: F modelActions.createActionTo$message: d1 R. G5 l: r) |/ }9 J; F
(heat, new Selector (heat.getClass (), "stepRule", false));; U4 K1 j0 K2 [- L& U
} catch (Exception e) {7 z8 X; G$ _7 }8 q; N
System.err.println ("Exception stepRule: " + e.getMessage ());
" ?4 v- D5 z) F: S7 s, I }
7 z1 N- s$ z3 A* u2 n; V* [
$ o" \4 s* d: j/ |+ N try {
# ] M& f8 L3 e0 A' Y% c7 s" P! \ Heatbug proto = (Heatbug) heatbugList.get (0);5 P& a7 k. V( j' D, C
Selector sel =
I5 C# F$ H( s9 [" ^9 D. H; O new Selector (proto.getClass (), "heatbugStep", false);# v' n1 G6 V/ M+ m. N! m
actionForEach =, g9 m8 ~; @0 M5 ?4 O N
modelActions.createFActionForEachHomogeneous$call! A |: ]1 h! t7 M
(heatbugList,5 C6 k5 b: ]" `, i
new FCallImpl (this, proto, sel,
+ E) t# B: N1 b) V8 ~: p( D new FArgumentsImpl (this, sel)));
Y- c/ _( N; s$ \5 d2 E D } catch (Exception e) {) B' q& J& T2 H9 E' `& N* N, G+ Q
e.printStackTrace (System.err);
/ _ M( _7 X: C2 `, B8 O }, k: s% X# P9 |
3 i( x$ y Z2 O9 M) T
syncUpdateOrder ();
2 s8 N, R$ V3 k2 B3 x4 K
$ i9 G; j }( i try {' } V O: y' f* t: j5 r" R
modelActions.createActionTo$message 6 O+ b0 D7 o7 ?8 G z- t: E
(heat, new Selector (heat.getClass (), "updateLattice", false));) B1 O: C; R8 D8 Z
} catch (Exception e) {
! [7 @( R6 Q* g0 g System.err.println("Exception updateLattice: " + e.getMessage ());
i K! s' }# c }) t0 a( Y5 j9 x
8 `- g1 D7 F0 f% u$ g; ~; k
// Then we create a schedule that executes the9 h9 U' L( ]0 n: @/ M$ m( `
// modelActions. modelActions is an ActionGroup, by itself it/ F& J) b/ h0 i4 a5 g/ O
// has no notion of time. In order to have it executed in8 l3 b, h7 D" K; y# M9 W$ x
// time, we create a Schedule that says to use the
# B7 b/ b# ^1 ?# ?" d6 W* J1 k // modelActions ActionGroup at particular times. This
7 t- M3 w$ m, G+ w9 s$ u // schedule has a repeat interval of 1, it will loop every( \7 S7 {) U# h' z8 \& m& ^. E
// time step. The action is executed at time 0 relative to; u0 E/ k3 Y+ F$ m% I
// the beginning of the loop.' I6 w- e C' g& [7 U3 f5 b$ [: y( V, j
; v2 n0 E3 Q+ s, O, b& ?/ v
// This is a simple schedule, with only one action that is
: P; W- d- V) F; S+ v) N // just repeated every time. See jmousetrap for more
0 `2 l0 S8 k* B( V // complicated schedules.# Q `5 A$ W$ N- K& i: X5 P) J
4 k! l+ [: d3 I+ g' V9 T0 L
modelSchedule = new ScheduleImpl (getZone (), 1);2 A1 h" N {3 d, y: d: Z6 V: m8 a
modelSchedule.at$createAction (0, modelActions);
7 m# o3 Z! B9 [6 s% O5 t 1 O5 S6 q- c' q$ [- P0 d% f6 w5 P! e
return this;9 O) N9 V4 E. S& K
} |