HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 N S9 Z- N: }- |* M* [+ R( j
) |( W. x |6 m# Z4 K& c public Object buildActions () {
7 i0 c) \' j& c* e' u3 t super.buildActions();
% b- i3 g# y) h6 T % V, U O" ^5 i& k
// Create the list of simulation actions. We put these in
. L! Y$ G! k$ Q) s8 N! i0 ^ // an action group, because we want these actions to be) m& H( A* l. c% _/ e
// executed in a specific order, but these steps should
* {8 c/ `% s! x // take no (simulated) time. The M(foo) means "The message+ G- O5 `/ o, i0 j2 V
// called <foo>". You can send a message To a particular" ~8 ^+ |% s& J% M
// object, or ForEach object in a collection.0 U& b1 A D8 b$ G& G
' A* C* a9 @0 H/ o // Note we update the heatspace in two phases: first run
. Y6 x2 U( Q2 l // diffusion, then run "updateWorld" to actually enact the
0 G6 L, F2 z! b% v0 J // changes the heatbugs have made. The ordering here is0 W# P- E4 o! T$ N6 w* s
// significant!
) d' p" g* L7 F4 l4 \0 [ . F0 t, Q8 C- G' v B
// Note also, that with the additional
! J, C+ P0 Y! \' u% [6 P // `randomizeHeatbugUpdateOrder' Boolean flag we can* U$ ]1 ~& D: n8 O1 O% m5 {
// randomize the order in which the bugs actually run
# ^( X, H% o$ \7 g; R. i4 \ // their step rule. This has the effect of removing any& T" T2 q0 n" L# _
// systematic bias in the iteration throught the heatbug
) `% f9 t6 I2 e6 y+ P+ {% p) \ // list from timestep to timestep: R/ Z2 j7 g% }$ M5 Z, r
3 E/ j# d; c. Z; J // By default, all `createActionForEach' modelActions have0 l6 |" p. F" u Y4 ~; e
// a default order of `Sequential', which means that the
& m, _! I: g& C( | // order of iteration through the `heatbugList' will be
8 P+ q, y5 c5 P! d // identical (assuming the list order is not changed
$ L$ M3 l* V" T+ v' { // indirectly by some other process).
3 ]0 B0 t% o0 j) ?$ \, G
7 \+ B) ]7 w( q modelActions = new ActionGroupImpl (getZone ());0 B6 X; r1 W1 z% S
, y1 T c7 U3 d. ~ try {4 d* d% r: N0 H$ H! a. T+ }
modelActions.createActionTo$message& K$ Z o# [3 W1 A1 v
(heat, new Selector (heat.getClass (), "stepRule", false));
9 \- E! ?: S5 L8 \ } catch (Exception e) {8 B, ]2 O) p# r
System.err.println ("Exception stepRule: " + e.getMessage ());5 V4 L! z& ?) [- {' h% k
}+ K0 n) A+ h# ~8 K3 Z7 p
, T2 I ? p% X8 k
try {& z3 n0 ]; u1 N7 l7 k5 q
Heatbug proto = (Heatbug) heatbugList.get (0);
; ^ \9 @: T) E Selector sel = + V" B4 P2 ~8 Q3 P: R3 y @
new Selector (proto.getClass (), "heatbugStep", false);
2 Z3 h# k7 D: ]' ]: q% U actionForEach =+ ^$ ?2 G1 ?& L" ~. Q, t% _
modelActions.createFActionForEachHomogeneous$call
9 j8 g. I% t( x7 [ (heatbugList,
( Q: q7 s* C( T; \0 U9 a new FCallImpl (this, proto, sel,; {! N! J) H& o" `7 c
new FArgumentsImpl (this, sel)));3 D7 A) z% U$ i- h' `
} catch (Exception e) {
( j3 Y8 k0 w2 y8 U, T4 N5 {) x e.printStackTrace (System.err);1 t9 `9 P ^4 e
}
$ ?# `& Z& ]' ?! Z ' v1 _/ l5 d6 t+ z
syncUpdateOrder ();
' w& v8 R# i9 ?" i3 i- a& ~6 D, z( X; k: G k* v
try {+ ^" v0 u0 {# Z" ?; K. K/ [
modelActions.createActionTo$message
^/ T' I9 Q [) u) v (heat, new Selector (heat.getClass (), "updateLattice", false));
q% j& O0 O5 t6 o4 G } catch (Exception e) {
; S: M* F. h3 x0 ^9 \' N. R" t! B System.err.println("Exception updateLattice: " + e.getMessage ());
# V9 L$ |& }6 ~3 H }4 c. G4 P/ k0 W
: A/ j' O7 n' h9 O0 O5 b# o
// Then we create a schedule that executes the) q' }5 b% R' U: |
// modelActions. modelActions is an ActionGroup, by itself it
1 q' S* K. i) e" A2 Z6 F // has no notion of time. In order to have it executed in
3 ?, w3 K. a: q, `$ W$ Y7 m) ^ // time, we create a Schedule that says to use the
+ ^) }9 {9 V5 a& v& X. \1 O3 _ // modelActions ActionGroup at particular times. This
+ \7 V3 X w2 c7 H- h1 e8 h // schedule has a repeat interval of 1, it will loop every
! N! U9 ~( t- g% k // time step. The action is executed at time 0 relative to
# g5 B' X/ p c# X // the beginning of the loop.
/ o/ E' ~3 w6 n$ M( m, b
! R) {. {$ s4 k* b // This is a simple schedule, with only one action that is$ J& N0 h+ U$ i3 z/ }6 L
// just repeated every time. See jmousetrap for more
5 ~! k! @5 ?+ v9 B# h! H // complicated schedules.3 B! ]' ^6 V0 N+ \2 }0 e; g2 e9 \5 A
( w3 }8 j7 O) I/ M# R5 v( g1 W
modelSchedule = new ScheduleImpl (getZone (), 1);
6 e* k* `( Z4 f/ u4 c modelSchedule.at$createAction (0, modelActions);) |5 l/ x5 D) ~, I4 F9 @ N
( q6 }1 Z3 |; {" P
return this;0 K/ ~$ {4 v( E) ]
} |