HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ L. c" w4 L( S% [" ~/ L: E- p9 {# f0 z; w$ h
public Object buildActions () {$ [# c0 n! }4 a" S
super.buildActions();
9 n9 X) f7 b0 t% U5 N
) F3 d& N3 E/ O. G // Create the list of simulation actions. We put these in
8 W$ Z( V6 A e* A5 q // an action group, because we want these actions to be/ x: }& }. ~5 W. \: }+ O- Z
// executed in a specific order, but these steps should5 d5 t# n$ j1 D% k. H
// take no (simulated) time. The M(foo) means "The message
7 g( v+ d: Q R; |! x3 E // called <foo>". You can send a message To a particular$ f9 M2 x3 j9 h7 ^4 S1 @/ k3 U' @
// object, or ForEach object in a collection.
* m! X5 j, D; q9 b% {' j 4 G1 ?( E: M' S ~
// Note we update the heatspace in two phases: first run
2 X/ u0 D& F9 Q& Q7 }! p+ @ // diffusion, then run "updateWorld" to actually enact the
2 I5 F" X5 x6 z9 { // changes the heatbugs have made. The ordering here is7 X `9 I! X/ m2 T/ E( V
// significant!
; K% g \0 {2 h1 _
! l1 z% r9 b$ G' [3 m' R0 n // Note also, that with the additional; Y2 P1 y. T8 h: g, I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& m8 v5 g4 _# k, h& p" _3 f; ^: R // randomize the order in which the bugs actually run4 P( j I( y" J6 F, F" }; p
// their step rule. This has the effect of removing any
0 |0 ]6 U. N: g. ~3 g1 d; J // systematic bias in the iteration throught the heatbug5 Y8 Q# u+ ?1 N1 S; c; ^8 t- X; b3 _
// list from timestep to timestep
3 }# n' I/ F! S/ W3 Z
1 K; Y' Y* Q* D9 ^6 D // By default, all `createActionForEach' modelActions have
( u/ T3 i" R& X4 |. G // a default order of `Sequential', which means that the' i2 T3 h! r3 E' w7 \. x
// order of iteration through the `heatbugList' will be
* ]- c! F' i. y, E7 _: ^ // identical (assuming the list order is not changed0 o% t3 R J9 ~$ O- z) ~' R+ q
// indirectly by some other process).
+ w ^9 o5 ~2 {1 \# i3 m1 \6 @ $ \$ c$ u- [5 ?; n9 M
modelActions = new ActionGroupImpl (getZone ());: L- @4 b8 ^/ j$ J/ ]
4 N- k; P/ s8 J try {
( L5 W5 i) j; H8 |. r3 Z+ b( m modelActions.createActionTo$message0 Y- C6 n# F A# ~
(heat, new Selector (heat.getClass (), "stepRule", false));; F, N5 |9 o% u2 ~7 l4 n0 t) k! U
} catch (Exception e) {
% _9 x/ N9 D2 `5 F& Y( i System.err.println ("Exception stepRule: " + e.getMessage ());9 L) _ |" |$ Y( }0 K; Y& m% `
}8 N: I) S3 |/ c
m, F% I" l9 U! v, h8 N try {
4 f C2 ^4 h* Q t Heatbug proto = (Heatbug) heatbugList.get (0);+ q! O' H' O' M1 j
Selector sel =
3 Q" `" E2 ]& c9 _/ \* N new Selector (proto.getClass (), "heatbugStep", false);( m1 p: ~3 J, z
actionForEach =
) S- J' Z) {/ K. E8 H modelActions.createFActionForEachHomogeneous$call
. C/ c+ C& v2 } J) x) t (heatbugList,/ [% v8 A7 \' Q4 [! J
new FCallImpl (this, proto, sel,: H; y9 }: O2 X N- j Q9 v
new FArgumentsImpl (this, sel)));
# s& w5 h8 t. A1 h } catch (Exception e) {
+ E% f0 L( [- _6 z4 |$ Z# o% D e.printStackTrace (System.err);; Y9 \! a( N% V# L, Z1 O
}
) q3 Q# t) H, G! c% M7 ?1 C7 S
# Z$ W/ k+ n, J syncUpdateOrder ();; C( B( Y2 \) A" x/ U
9 l" K/ I6 }/ _! g8 M
try {
3 z$ x C* X6 y$ M+ H' A modelActions.createActionTo$message
! m/ p1 }/ `$ w (heat, new Selector (heat.getClass (), "updateLattice", false));1 i" U7 T) [) Y- n$ v$ z
} catch (Exception e) {
1 s7 N2 J1 S! @9 Q' v System.err.println("Exception updateLattice: " + e.getMessage ());1 r) p2 O* N! e+ x$ h# ^
}
: Y* O" v+ f& c( T; _9 _8 e & b, W) \0 P% M& K- O3 E
// Then we create a schedule that executes the) G& b: R. S' D- I% ^: I
// modelActions. modelActions is an ActionGroup, by itself it; D& h9 k3 X, \
// has no notion of time. In order to have it executed in1 }+ { X1 L& Q f% i3 Q
// time, we create a Schedule that says to use the
4 F7 ]0 y4 [8 l3 h. w // modelActions ActionGroup at particular times. This: o9 ^3 _4 Z5 S7 K( s, {! d2 s
// schedule has a repeat interval of 1, it will loop every& K! [" I' v! v# j3 V
// time step. The action is executed at time 0 relative to$ u `" W! y: t( i$ y/ p
// the beginning of the loop.7 |" ^$ j3 g% [) Q$ g# V
! E9 o9 K( X1 n; ~
// This is a simple schedule, with only one action that is* @2 i5 m; H7 A" X
// just repeated every time. See jmousetrap for more
, f$ L# \' ^, T- `; B. \5 d+ J // complicated schedules.
: A+ Y; W ?& c) c) y/ D : F) D. f6 p# y6 v5 M
modelSchedule = new ScheduleImpl (getZone (), 1);
" Z9 Z' a# P1 K modelSchedule.at$createAction (0, modelActions);) w! f0 X0 c! _9 Q3 U
" L$ G+ }& l9 H' p+ x! A return this;
% B1 `, y i% R } |