HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' n6 M1 E/ ]# O% x
) w/ n6 Y8 ^6 E6 F, n6 w0 B/ `1 P
public Object buildActions () {
1 V$ U6 ~# _7 V6 Y super.buildActions();7 h1 C( x. P0 `" o. v
; [( _8 n. e: s! |/ w% M0 C& u // Create the list of simulation actions. We put these in* G4 T+ V; I$ s; L2 x) m/ r
// an action group, because we want these actions to be
: m8 Q$ w4 j) h3 j8 R& J( e9 f // executed in a specific order, but these steps should
% m$ G* F9 f- V ]5 ~ // take no (simulated) time. The M(foo) means "The message
. C' T& K8 A) v) j4 I5 q( \ // called <foo>". You can send a message To a particular0 h3 {9 A1 L; E0 D1 v( ~7 I
// object, or ForEach object in a collection.
, n$ f- {2 V5 O, O) Z 7 K) E0 Y n0 Z- t' V
// Note we update the heatspace in two phases: first run
, c: b+ M E6 L2 l. t // diffusion, then run "updateWorld" to actually enact the5 m& I7 U( `/ k
// changes the heatbugs have made. The ordering here is+ }6 \7 i! o: d! ^7 P" j. A/ ^# l
// significant!/ Y% X1 I! p0 y
. h, u$ V) V$ O7 L
// Note also, that with the additional( F0 X4 j) J l" T
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 {; v. J& \# M6 M# v r3 a$ I
// randomize the order in which the bugs actually run
5 d4 s8 S4 K7 P* l9 \. m // their step rule. This has the effect of removing any0 f6 ?* H8 C& y- b
// systematic bias in the iteration throught the heatbug' C0 O9 F9 n3 J- F' c3 u
// list from timestep to timestep
; p: Z0 Z+ H! A/ |' E ( V! J5 o; z$ P6 i0 M
// By default, all `createActionForEach' modelActions have( {" L, a/ t& u) f% g
// a default order of `Sequential', which means that the
8 z0 ~' s/ Z) ~$ ]7 a // order of iteration through the `heatbugList' will be3 q( P3 d$ ?- t( p6 e4 o
// identical (assuming the list order is not changed
( d/ A$ K( d& L0 |: B5 x9 o7 g // indirectly by some other process).
2 P9 f* \5 t. T- Q( u
. o' d" h' q* q4 D0 F modelActions = new ActionGroupImpl (getZone ());
4 X/ g+ v0 ~" f2 ~1 U, W1 ^3 b! h0 b0 Q7 m# |
try {. v0 g/ N8 p0 T) D, ?
modelActions.createActionTo$message
0 w! F$ o& ?! m2 l2 c! y+ t" X (heat, new Selector (heat.getClass (), "stepRule", false));
; o; {( N+ h% U) E0 t5 Q2 P* R } catch (Exception e) {
* ~3 `$ v. o2 U System.err.println ("Exception stepRule: " + e.getMessage ());6 R$ p' R; q3 d% l6 a9 Z+ }
}+ v2 B' G# K" M: ~
9 K; o0 B- f3 X5 x# B$ S* t) M' y# S
try {0 c1 c6 H! z c) l2 E
Heatbug proto = (Heatbug) heatbugList.get (0);
% r& |5 e3 B! h Selector sel =
1 i% |7 ^& `1 x2 J; ^; J' `, E new Selector (proto.getClass (), "heatbugStep", false);0 Q5 C* n6 u; H
actionForEach =& m# H1 R; ]* B2 Z/ K& i' [6 R
modelActions.createFActionForEachHomogeneous$call
( X4 T% c- P: }( T' A (heatbugList,
: N! O2 S( N) G5 ]; G4 H4 Y new FCallImpl (this, proto, sel,4 ]2 f3 k5 i, \! _ W
new FArgumentsImpl (this, sel)));
9 q+ }; P* t ^1 p } catch (Exception e) {
0 g3 ~4 u* j8 s; g+ Z! w1 b) E e.printStackTrace (System.err);! b$ o% z8 C+ r: P* L+ l7 E
}; c1 E- `" Y- z
0 G! n+ ?! l* {. l syncUpdateOrder ();& T- O! d' [( W0 V
' C' d+ |6 x/ G0 J$ Z
try {
6 p z0 H8 o3 Z; @% t$ d modelActions.createActionTo$message 5 I/ f5 p, _, }3 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ B. w0 ?$ s1 Y$ n- d! y } catch (Exception e) {
7 [' P) v. ~# c System.err.println("Exception updateLattice: " + e.getMessage ());
, J8 v, g' B) A, G U' w( D }+ z F) A' I5 C8 Y' s4 N) m0 b* r
2 ^- D' E% ~. x* T, |& |# ^% Q
// Then we create a schedule that executes the9 t5 f- y' f \# }5 ~ h$ s
// modelActions. modelActions is an ActionGroup, by itself it
5 z: J$ q/ y! c$ X. V) A. O // has no notion of time. In order to have it executed in& Y3 m' u3 {9 e, W% \9 H6 `5 Y
// time, we create a Schedule that says to use the
7 b$ Y% L, C, k( W6 K // modelActions ActionGroup at particular times. This
& `3 ?- s1 Z* B# R; d ?9 C // schedule has a repeat interval of 1, it will loop every* _6 t: K `& w8 a T0 r
// time step. The action is executed at time 0 relative to
W, g2 `5 s8 c8 G- W' P5 d$ i9 m2 D: x* Q // the beginning of the loop.
8 \6 R0 U2 `/ ^( Z& S& k' \
2 G1 C' }' c( W: v, U // This is a simple schedule, with only one action that is
! |/ u. E5 j- H; w4 ? // just repeated every time. See jmousetrap for more+ k {. y$ H7 M: P# X
// complicated schedules.
7 }9 m( c) i, n- s ( p3 D1 q1 p( f9 Z1 u' D) I; @
modelSchedule = new ScheduleImpl (getZone (), 1);
4 `5 J+ d) D6 e% V- B; ]' t modelSchedule.at$createAction (0, modelActions);
/ {! c. U- K9 F5 l9 F6 _+ i 6 l U. D- E* W) ]
return this;' Q" V N, `( B% R
} |