HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 b7 r6 Y) q$ {' ?0 [- @
' U& i/ f0 y) V6 h6 o. {0 ]) H public Object buildActions () {4 \$ ~, W' V! Q4 R: w4 z
super.buildActions();
2 }' L% X" d2 u1 K0 s 2 c8 ?0 d/ g7 Y9 v# Y# K
// Create the list of simulation actions. We put these in y7 `; y$ h4 W& G# ]
// an action group, because we want these actions to be
0 h4 ~4 W/ ?4 |/ |. p, p, g$ a // executed in a specific order, but these steps should
& y3 Z7 {, _6 N& i7 o1 j9 ? // take no (simulated) time. The M(foo) means "The message
5 K: U- K# ?8 ~ // called <foo>". You can send a message To a particular# P) c& p$ H; L" B2 ?: ^) w
// object, or ForEach object in a collection.+ H5 E; g0 `$ \4 Z. ^) U: ^
2 ^3 A# y( G# N4 H( ~8 I // Note we update the heatspace in two phases: first run/ f6 S0 e9 Q4 f
// diffusion, then run "updateWorld" to actually enact the# c2 W: R4 R8 c3 {" _. ^. T
// changes the heatbugs have made. The ordering here is
! C5 n0 a2 N: |( f. w/ b // significant!, k" {' l6 T6 n5 ^; ^0 h
8 @; U/ X9 r2 p5 A. `
// Note also, that with the additional2 L. X# G9 G9 B$ A+ j9 _" k9 V* E4 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 e0 l$ t! t3 e% u // randomize the order in which the bugs actually run* Y& j+ r8 H* N( Z/ }2 u) {
// their step rule. This has the effect of removing any3 C. o; A+ Z% T! T! a) M# y9 y
// systematic bias in the iteration throught the heatbug9 r5 w1 r/ P h8 R1 {; Z
// list from timestep to timestep, m" V0 k* j# u
5 I3 I2 R4 e/ R
// By default, all `createActionForEach' modelActions have
* @/ Z, a" k6 b1 k) o // a default order of `Sequential', which means that the
; O2 K9 p9 b/ V) R& }4 ?7 _ // order of iteration through the `heatbugList' will be
8 H$ Y* H. X3 m5 Q: V7 ]9 m3 z# y // identical (assuming the list order is not changed6 Q$ [( j, o3 R# u, \0 }
// indirectly by some other process).7 W& q& R m" |: d- r+ k
# _$ Z: J9 P* p( `; h3 {" o8 E
modelActions = new ActionGroupImpl (getZone ());
% m+ `- n8 l- d& d+ S( M9 B" S4 A2 u
try {. e7 o! S' x- U+ I# b
modelActions.createActionTo$message3 U* P# I0 c$ H6 u0 b3 F% i" l
(heat, new Selector (heat.getClass (), "stepRule", false));
# V6 j5 B' n* U4 O4 E } catch (Exception e) {8 j* E: U. @% C$ V
System.err.println ("Exception stepRule: " + e.getMessage ());3 x5 Q6 E* f& s; E
}, @1 z& x I$ l1 X! J# T
, [& Z; i& P, L* |
try {6 V% _' |2 i" l( p9 P# b
Heatbug proto = (Heatbug) heatbugList.get (0);
5 c' e+ ]; M3 e Selector sel = & P. A, o9 F! x7 W8 s7 [) v
new Selector (proto.getClass (), "heatbugStep", false);
2 G3 d+ U3 ^$ z4 @. s. A. ] actionForEach =
" |* U( A2 ~, t4 c. k- a modelActions.createFActionForEachHomogeneous$call- w7 q2 n1 Q& q. t
(heatbugList,4 `% W2 N l) a
new FCallImpl (this, proto, sel,& [7 Y8 ]7 n# l( z3 W, @' O
new FArgumentsImpl (this, sel)));
! h6 @5 I* e' A } catch (Exception e) {. ^" p3 q, F. ?! g$ B! l
e.printStackTrace (System.err);* c) o3 i; v5 V- f5 W
}
( c! q3 `5 ^' t
9 Y5 x. o0 u: T9 o- v( m% |# t syncUpdateOrder ();
( K# W# c2 v3 F" `' D# {/ s$ U1 D3 d; {6 X
try {5 Y1 _7 O& X9 T- ^2 [% D& f
modelActions.createActionTo$message
0 p& H y* e# z( S1 B! M (heat, new Selector (heat.getClass (), "updateLattice", false));
T$ o$ z( o0 X* i% _' f& X } catch (Exception e) {# Z6 D* s0 X, r- ~
System.err.println("Exception updateLattice: " + e.getMessage ());1 z1 Q! m4 v+ V S* N% a7 [3 Q
}
7 ]2 m! J. B5 x* s, G' }. \3 K 3 P! r8 G, g0 I( v6 ^0 u
// Then we create a schedule that executes the& B! G7 V, G1 c/ x
// modelActions. modelActions is an ActionGroup, by itself it0 s, F* w6 w/ C5 f0 \# W G
// has no notion of time. In order to have it executed in
( P! m0 T! u/ K5 @4 F- i // time, we create a Schedule that says to use the! m! Q" R( A" b
// modelActions ActionGroup at particular times. This+ y% L- |- V8 V5 p
// schedule has a repeat interval of 1, it will loop every) F2 h6 H8 n j
// time step. The action is executed at time 0 relative to u' n9 k- K! }+ b8 `
// the beginning of the loop.
, s8 h5 a E1 O. g
, S& O' O- h- {% x9 @- q // This is a simple schedule, with only one action that is$ ?" g7 n2 ]; \7 P
// just repeated every time. See jmousetrap for more
, J7 r8 X' N4 `5 S, i6 e // complicated schedules.( j8 t! x: C8 p
8 J& K3 k( p; n4 P modelSchedule = new ScheduleImpl (getZone (), 1);
7 t# o( \/ e! c# M$ n" a. | modelSchedule.at$createAction (0, modelActions);9 ^# }6 c) W4 W6 M/ F2 P2 ?, g& ~
: g. N( W4 s7 L% U" i1 j. A a
return this;
9 v2 h6 D% h2 G, v, U+ O+ W% k: L } |