HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) {' }% P! v( f6 j0 t: W9 a
& v$ E1 e& q: H0 W: V# f! g public Object buildActions () {9 A$ x, s2 o' `
super.buildActions();
0 Q P2 |$ u3 n+ R
* S+ e9 N& V" f1 b- u // Create the list of simulation actions. We put these in
0 l5 P& e" M( s8 h) j% I0 |! P // an action group, because we want these actions to be
0 E( Q5 ]5 ^( J$ c* } // executed in a specific order, but these steps should
' T8 f9 V8 L/ V" o // take no (simulated) time. The M(foo) means "The message4 O* U; L6 F$ w! w. ~0 Z
// called <foo>". You can send a message To a particular
# h, v D+ N1 G& F- w2 U // object, or ForEach object in a collection.
6 i4 T- D4 L) ?6 G
- t# n. M( ?1 X7 k6 \0 j( K // Note we update the heatspace in two phases: first run
- L/ H$ X) m) K: s4 i // diffusion, then run "updateWorld" to actually enact the
% s2 p) y& T: I/ \" N // changes the heatbugs have made. The ordering here is
: @0 o# z$ l- C2 V ^# J // significant!
' x0 \; D8 S9 X
/ d' I: P- ]( O0 W2 I // Note also, that with the additional2 g2 W4 o3 S ~4 o# {
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ T ^" _% t7 b5 |4 ]9 y0 E // randomize the order in which the bugs actually run+ W) f: h) g' N% t9 h& c
// their step rule. This has the effect of removing any0 k9 H+ V, T6 i/ R `
// systematic bias in the iteration throught the heatbug
3 a# h, O& l! W/ J // list from timestep to timestep
- }' G; D% k" \( k
3 F7 y$ N5 {+ _% s+ ?/ V; n // By default, all `createActionForEach' modelActions have
& M# q1 n$ B5 n9 ] a- ]' u // a default order of `Sequential', which means that the) f7 m. n9 `! ]
// order of iteration through the `heatbugList' will be
+ D8 {( d9 x* H% Y! ]3 e% k" k // identical (assuming the list order is not changed
b' a& e' @3 ^& i* @ // indirectly by some other process).
, a. d: h# E7 q ' [ x0 D" K) p( j% V
modelActions = new ActionGroupImpl (getZone ());
3 U5 e! _" q7 \" e9 {% _) b
. f. W' ]+ R8 W" J q5 L j try {. |4 ^( p2 h5 c+ S; S' d; m( |# u
modelActions.createActionTo$message
0 Y2 s0 R* B5 I% H0 q (heat, new Selector (heat.getClass (), "stepRule", false));
! i8 x1 }) F* `/ y' B } catch (Exception e) {
0 @; c. s; ^) G: N5 k System.err.println ("Exception stepRule: " + e.getMessage ());
" U4 ^+ I' L; Q; b) P, @ }
" \$ X7 L# N8 Y7 T4 s1 w6 \
" d1 e& J0 m8 h try {
6 N& g2 n. H' d) E" i Heatbug proto = (Heatbug) heatbugList.get (0);
0 W2 p' U" O; o0 A% {+ }' K2 | Selector sel =
: s. R$ ^& p" s: S new Selector (proto.getClass (), "heatbugStep", false);1 \' E* |4 f+ a; @; x f/ w
actionForEach =$ L- F- v# E; t, u& `2 f
modelActions.createFActionForEachHomogeneous$call- o0 T9 \- v2 Y/ g* X
(heatbugList,
( l9 ^7 J. F2 G2 q new FCallImpl (this, proto, sel,1 ?8 u& ^# F! x! r; F
new FArgumentsImpl (this, sel)));
! X! ^2 y( x0 _8 f3 X } catch (Exception e) {% j* b i2 e; {4 D
e.printStackTrace (System.err);
0 w, K, g/ N) O2 }6 y) F7 g }
2 R$ F- j. ^! K) Q
U: d4 V& Y; B* z- V9 m syncUpdateOrder ();
+ e0 ~* W V+ w5 {4 Z8 ^5 `9 T
2 ^) }$ u6 Y E( I+ I6 ]1 p try {8 y8 q( }" L# T! |9 v9 M$ ?6 I' n- _6 O
modelActions.createActionTo$message 0 I6 y* `6 x3 p3 u; p
(heat, new Selector (heat.getClass (), "updateLattice", false));$ H: w+ E3 f6 u4 {( @8 P$ w
} catch (Exception e) {0 S$ o- n, c& z$ R& O4 e
System.err.println("Exception updateLattice: " + e.getMessage ());
! h. I. [, N) E9 z- k( a }! h9 [9 w4 f3 e( B+ [
) \. u, x7 w5 w& n" ]- l6 u) o // Then we create a schedule that executes the G$ Z, ^. R6 b' V. e
// modelActions. modelActions is an ActionGroup, by itself it
& C; ^; n3 C0 x( H, w // has no notion of time. In order to have it executed in
! k% A$ n! p: j$ |: f$ l // time, we create a Schedule that says to use the
$ e! R% s( ]4 V3 w8 S! | // modelActions ActionGroup at particular times. This
0 q/ T) o5 k) `: \4 F y // schedule has a repeat interval of 1, it will loop every
4 e; f7 |; S. I( O // time step. The action is executed at time 0 relative to1 e) W& b: n r$ g5 {. }' m" K6 O) x: C
// the beginning of the loop.
( ^$ Z; T9 t0 i, I0 c h* a, L' q3 L8 i
// This is a simple schedule, with only one action that is! Y: x& P8 j/ [3 j1 l. H
// just repeated every time. See jmousetrap for more, m# I' J- ~, o( [ ?4 I
// complicated schedules.0 R: p, `) {% C" g6 o
8 _, S; ^: ^" j$ g
modelSchedule = new ScheduleImpl (getZone (), 1);
* K2 H4 {' b1 S3 r# B8 C modelSchedule.at$createAction (0, modelActions);& ~1 k9 K' K9 E* u4 h$ ^" o
5 b! I, {" o8 G3 J! o% o9 Y( q return this;8 v# A/ G, g/ B
} |