HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 p3 S6 \1 \/ f" t1 D+ C1 u
6 c2 Q( k; g" R+ Z: T6 n# e$ j
public Object buildActions () {+ _" J0 E) ^: @: M
super.buildActions();5 w! [) y0 a! d
& A4 x/ l+ C; R6 h* G) }& ^ // Create the list of simulation actions. We put these in
7 ?6 a4 b$ w. i F2 I- |$ X // an action group, because we want these actions to be% W- [" k! B+ q
// executed in a specific order, but these steps should
0 A% A; y* v7 z9 r* e1 ^8 u // take no (simulated) time. The M(foo) means "The message
: f w( D. _. l5 n0 G+ c9 P2 s* o // called <foo>". You can send a message To a particular, X0 J8 [* b1 X4 l- {- O7 E) v
// object, or ForEach object in a collection.
. ?; F) L5 N( Y* b t8 |" X8 {% q c! Q ( J% u9 z( K4 `* ?
// Note we update the heatspace in two phases: first run
/ O# _7 D5 K' I/ L // diffusion, then run "updateWorld" to actually enact the
. v" I2 ]( S2 }7 X4 w! A // changes the heatbugs have made. The ordering here is9 e: r5 L2 I1 i8 J. v1 Z# x( S
// significant!
" |) E m! O3 K4 ^/ b" Z+ R
& W2 R# d( C/ y: c" F // Note also, that with the additional4 ?7 \( Y3 g2 ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# T& F) M/ }/ W // randomize the order in which the bugs actually run
2 E# t- K7 O( g0 E& e* D( n; w // their step rule. This has the effect of removing any
& p/ _ @. Z" ^: ` // systematic bias in the iteration throught the heatbug
/ w% O- ~2 @9 {8 w2 S. y3 C% h2 E // list from timestep to timestep
- }( I5 p9 F3 U2 ^" V* _
' A2 r+ d1 h7 n+ I. j // By default, all `createActionForEach' modelActions have
2 m+ {6 B$ a n" q; s" e2 _+ N // a default order of `Sequential', which means that the
) C% q% G& z: ]7 k // order of iteration through the `heatbugList' will be# b2 i9 e5 V8 C Y! t8 x, @
// identical (assuming the list order is not changed
1 _ |2 E0 s# v // indirectly by some other process).* d+ {* x) E% \- j. Q; K% g3 w+ L
4 j. Z. R) l2 t& I0 K- X- J* a
modelActions = new ActionGroupImpl (getZone ());6 g; ~/ r) W8 T
& @7 a. X1 ^7 v# ?5 _- y try {
/ `* |6 Z8 S9 J! M7 O9 H modelActions.createActionTo$message, H- o$ K% v5 I( {: w# V$ L+ `0 |
(heat, new Selector (heat.getClass (), "stepRule", false));- f: X/ s: U/ X( x4 }" J4 a
} catch (Exception e) {
, W7 @# L8 Q7 Q% u System.err.println ("Exception stepRule: " + e.getMessage ());) V A+ _/ Y. G8 f
}
, t5 d" p* F, r1 Q
6 h: F0 Q9 X' v' R try {* C k- q# l# g1 c3 z" G5 L
Heatbug proto = (Heatbug) heatbugList.get (0);1 w# c; @& N$ ^+ J5 I
Selector sel = - y* X1 @- G" K
new Selector (proto.getClass (), "heatbugStep", false);
% t6 G, @, i h actionForEach =
: j. h- r1 M0 @1 k( n, m modelActions.createFActionForEachHomogeneous$call* u' m$ H3 K) M8 K
(heatbugList,7 |/ H7 J. u7 Z4 Q4 f! [+ T
new FCallImpl (this, proto, sel,% M6 p7 D: v: A$ z
new FArgumentsImpl (this, sel)));# c; R1 u- z3 e: Q
} catch (Exception e) {
" y% C" Q' p, v# d e.printStackTrace (System.err);
5 V1 x: w, `* ~7 k C. l& l" S6 ^6 | }2 x) H( p9 U# A( h# P" b
8 i `# N; L2 _5 @. ^+ y9 n0 X4 C
syncUpdateOrder ();
6 P' D9 E# C; o
' G9 i+ x, d( z! Z1 a1 |1 F try {$ C% R* a% w4 I* M
modelActions.createActionTo$message 8 D1 P# G, [( F1 \" ~
(heat, new Selector (heat.getClass (), "updateLattice", false));
' T+ s* R z: ]' Q+ v } catch (Exception e) {& k u! l/ `" _+ o
System.err.println("Exception updateLattice: " + e.getMessage ());
" G& A1 M' E( q7 N9 Q7 E+ M }
" G2 @& S" l- }) k# Q
5 O( d8 y8 f ~2 s; H // Then we create a schedule that executes the! v) l$ u6 a; w% C# R
// modelActions. modelActions is an ActionGroup, by itself it
( f, H9 [4 {/ ^: n1 h5 R1 s8 k // has no notion of time. In order to have it executed in; h& D; d |$ b
// time, we create a Schedule that says to use the
- G- J* U3 ]* F0 o# f5 i) I // modelActions ActionGroup at particular times. This/ l1 k/ G5 R( g. g. b" O+ {
// schedule has a repeat interval of 1, it will loop every1 m. h% [6 e( u7 B
// time step. The action is executed at time 0 relative to# _( p6 ]2 }7 w$ h& S; I5 Y1 t4 O
// the beginning of the loop.
+ ]+ B0 T) U# |; r; h, s# o) I: G+ t& e' P' z
// This is a simple schedule, with only one action that is4 |* }6 L/ N5 I' }" _7 o5 o
// just repeated every time. See jmousetrap for more
) W' y4 o) ^* y0 V // complicated schedules.5 h+ y0 d' `* t8 d
- ]' ~2 k# ^; ]7 M modelSchedule = new ScheduleImpl (getZone (), 1);
- Z( [: R, n& ` @ modelSchedule.at$createAction (0, modelActions);4 w: }/ L; c% G' a$ q' F
1 q" d- _* F+ X7 i4 h) |; V' s return this;
k% P7 j* O$ r( o' r } |