HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 [; J6 L, a" f" r* X# @& c0 T" L! o2 N# q: O
public Object buildActions () {* B9 N4 t. b+ ~) K! p
super.buildActions();+ M* r. e5 ?* B: W# f( s8 n, W
& ?/ E/ g+ E% ?4 J- Q" N( ] // Create the list of simulation actions. We put these in
" P# G& j- h- x) {% Z& F& v+ L$ f1 Z // an action group, because we want these actions to be
6 q! g' _0 x' o9 y3 i+ x8 c- y' K, c( [ // executed in a specific order, but these steps should; f6 }2 p* G2 [: E w8 K1 E
// take no (simulated) time. The M(foo) means "The message6 O& H M- [$ a% b2 z4 T/ z* K9 I
// called <foo>". You can send a message To a particular
0 {4 m8 l+ R+ f i8 n // object, or ForEach object in a collection.% g" g) |9 O6 C3 m" Z
3 w% X& x8 J7 j( Z, {0 I& r, E2 L
// Note we update the heatspace in two phases: first run; Y8 U$ z$ {1 E* V+ e0 z( H
// diffusion, then run "updateWorld" to actually enact the
2 }* x/ a$ k8 o! @4 ~8 g // changes the heatbugs have made. The ordering here is% d" A8 O% N/ p2 Z9 ?# s/ }
// significant!# i8 x; Z2 F( p7 ^6 B
$ P- J+ _0 n; P8 ~4 g, H // Note also, that with the additional
, u, r$ r) W1 Q" I2 c' _ { // `randomizeHeatbugUpdateOrder' Boolean flag we can+ n4 N& A1 [1 `# h4 b7 h# l7 j
// randomize the order in which the bugs actually run
" j# F2 g: Z. B // their step rule. This has the effect of removing any1 _4 F+ V0 \- p' }
// systematic bias in the iteration throught the heatbug
2 R9 u! e$ y; Z/ v) q // list from timestep to timestep
7 f0 y m( D( h: o! L 2 W+ H. _1 }, `1 I
// By default, all `createActionForEach' modelActions have2 a: ]2 k5 r7 `7 S" ]* w% C
// a default order of `Sequential', which means that the- _3 @4 m/ E5 r# O9 P
// order of iteration through the `heatbugList' will be5 k' Y/ q+ T- T; G, D! ~
// identical (assuming the list order is not changed
9 Q6 d: D y1 E' M // indirectly by some other process).
; K3 P ]' L+ x 3 L( n7 Z0 L. \+ ~# R2 |
modelActions = new ActionGroupImpl (getZone ());
: J0 o) k3 ]( B/ }& Z+ i% a
7 s _; T. F5 |6 ^8 f& ? try {
9 R( G& y7 l) `8 f" x modelActions.createActionTo$message. @2 k- \1 ?. e( x
(heat, new Selector (heat.getClass (), "stepRule", false));
' ?" t# \" {+ C$ \ } catch (Exception e) {
" [, e' N" X" x& Z/ L7 i; f System.err.println ("Exception stepRule: " + e.getMessage ());
' ?- J6 a4 {- Y% _ S1 K: Q+ V4 e) S; X }8 F8 c, t$ T) F1 c+ f: @( l
* t9 D% N1 O8 N( m" ^4 q try {" x0 I- b( Z' Q4 w! K0 ]8 `) Z
Heatbug proto = (Heatbug) heatbugList.get (0);( F3 u" a, @3 D2 x% t0 p
Selector sel = ^1 o+ i1 i3 [+ v" r
new Selector (proto.getClass (), "heatbugStep", false);
3 x6 F; C! \: k" B1 M7 z actionForEach =6 c1 _! L' f. t8 i; I3 A+ I( b& V
modelActions.createFActionForEachHomogeneous$call
) @0 E" B0 h ]" c. K: { (heatbugList,
4 V! V) C. z, Z7 ~ new FCallImpl (this, proto, sel,
( g- b$ |& U3 s5 _0 \4 p new FArgumentsImpl (this, sel)));$ n. V1 S0 ~$ }2 R0 h4 N
} catch (Exception e) {
( n- {: H$ q6 h5 ~: R e.printStackTrace (System.err);7 G- O" f0 F- `; y
}$ ^+ D5 u2 X$ {8 _) ?4 j
3 G0 U8 V3 \, k5 ~4 C syncUpdateOrder ();
8 g J& d1 D8 j5 \+ L+ b
1 b) k" z1 ~( v1 ?' j try {, P& W* e; F+ h! s+ K/ i8 W
modelActions.createActionTo$message 5 b- B( d" C; K$ M6 M/ W6 o
(heat, new Selector (heat.getClass (), "updateLattice", false));- r0 [0 Y2 b o" S3 a, N0 V
} catch (Exception e) {/ }' J8 T$ f. n7 {$ @% Z
System.err.println("Exception updateLattice: " + e.getMessage ());( z$ w+ X4 V, m* m$ G% U
}, u$ H$ j: B) c( ?" h; \
, ^4 h/ j2 y4 J# a2 D; ]- r1 p$ _8 o
// Then we create a schedule that executes the
# E6 G! B( K4 s // modelActions. modelActions is an ActionGroup, by itself it! l; \( d- W& u o a9 G7 X1 {
// has no notion of time. In order to have it executed in
3 Z& }- u0 _" Y% y; B // time, we create a Schedule that says to use the& i9 E) X. a( B# u7 ]; u7 ^
// modelActions ActionGroup at particular times. This
, s' }4 I, P( d // schedule has a repeat interval of 1, it will loop every: O: m8 ~1 Z8 o/ P) H
// time step. The action is executed at time 0 relative to
* R) \6 C) f* i6 N! G# n // the beginning of the loop.0 c) K) A- u: G+ H5 ^- n& r
S2 G( \& d. D% M5 _% q // This is a simple schedule, with only one action that is
# q0 y3 u: V! t, [; q. m0 e // just repeated every time. See jmousetrap for more( h- d% T z# ? y9 a) T& F
// complicated schedules.8 |+ n* s9 E8 p3 H& r% n
9 A& e- j/ X5 t6 E modelSchedule = new ScheduleImpl (getZone (), 1);
& C& q' d0 A* F modelSchedule.at$createAction (0, modelActions);4 O! J4 K5 l$ _1 g; I* i0 B
7 R, d1 T; p& T$ o return this;
" a) z& L( i- p G o } |