HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" E" B: B" F0 d* N2 v7 |( p8 h5 A: K, l2 L
public Object buildActions () {
% L' P# ]4 E8 w2 h super.buildActions();' T% R5 v9 @6 v- L
6 n. }5 r- h- N0 o) R
// Create the list of simulation actions. We put these in( R; E" Q* g2 E- L% ^
// an action group, because we want these actions to be+ X) n; x# \$ @( d$ ?
// executed in a specific order, but these steps should
5 q* U$ S. v4 G# y // take no (simulated) time. The M(foo) means "The message
9 G- Q7 ], h' |" d // called <foo>". You can send a message To a particular4 i7 u$ H$ M# _' U% w; }
// object, or ForEach object in a collection.4 P5 N( f4 @( C# S+ H7 [ c4 Y
6 d" K+ D( _. w1 s: R
// Note we update the heatspace in two phases: first run3 M. c* l ^7 T
// diffusion, then run "updateWorld" to actually enact the
. Y9 V% m- s& t+ M( ^4 I // changes the heatbugs have made. The ordering here is
1 u5 t( ~1 u! @( O0 _& D+ q // significant!# I: u- m, i" ~; c( M
; t5 x" X$ W! S4 ~' B1 Q
// Note also, that with the additional
; w7 S+ j( |. L8 A7 D. O4 k // `randomizeHeatbugUpdateOrder' Boolean flag we can9 [5 J9 ]; ]: O. i& l! o2 M
// randomize the order in which the bugs actually run
/ N7 U5 L E9 U& Y // their step rule. This has the effect of removing any! U0 e8 m _4 Y( Y6 v) {) U3 `
// systematic bias in the iteration throught the heatbug. o6 h0 w2 v. m4 h9 Q) q
// list from timestep to timestep
; a! V$ o6 q* s7 F) Y $ h: `( ^" C- U( Y4 n4 K6 p
// By default, all `createActionForEach' modelActions have# Z# Y8 a: V4 w" T
// a default order of `Sequential', which means that the
1 F% z, g, f) Q; f // order of iteration through the `heatbugList' will be9 \, f+ \$ C# }. v! B& U
// identical (assuming the list order is not changed
0 f. X/ o b) @! q5 C' U // indirectly by some other process).9 p6 b. I4 X0 S7 ~
( h: X3 }$ D1 }1 }( H$ V
modelActions = new ActionGroupImpl (getZone ());
3 q0 ~6 a N7 d. i5 W* C ?! M! t
/ w5 s% Z: V; X: | try {
( w. j0 g5 ^2 W' |# m modelActions.createActionTo$message2 d+ o/ J" N% o( _+ S' N f
(heat, new Selector (heat.getClass (), "stepRule", false));
Q9 `5 J( Z# B2 @' _ c0 M5 N( E } catch (Exception e) {
0 O; w; J& ]7 z& E! _. V7 J3 i System.err.println ("Exception stepRule: " + e.getMessage ());
: g8 l$ e7 n; k8 P+ v }, _& E; \- j+ A; i' p! H& x. j
& z4 Z' o: C, W; J8 q
try {9 I) p# l( ]( u+ `' c2 l
Heatbug proto = (Heatbug) heatbugList.get (0);
9 u; }# D7 G% }: V: o/ L6 r Selector sel =
# \2 Q( M3 ~5 K( k% z' J new Selector (proto.getClass (), "heatbugStep", false);
( @. g# k" [0 x! j6 G* X2 s3 k actionForEach =6 c+ N9 c, N, o% E
modelActions.createFActionForEachHomogeneous$call7 t7 e' h6 C/ r7 O- O6 e) x
(heatbugList,
% d" j% F3 b1 Q, l8 t new FCallImpl (this, proto, sel,
0 E# d+ e& o/ [) q; H9 K new FArgumentsImpl (this, sel)));
" g0 D: x0 b7 U( s } catch (Exception e) {
( F9 |+ Q6 t! Q2 N! I7 A e.printStackTrace (System.err);0 r/ E5 h4 Y1 s6 j7 o0 o9 h, {
}
- `( W) [4 `! F3 E * ?. ]" q! h3 q7 {5 `$ y* y
syncUpdateOrder ();
' Z' w5 ^1 y' s3 u P
2 g: F/ w% A$ v try {
1 R6 u3 G1 n0 _$ v2 b modelActions.createActionTo$message , D8 B# z, e8 n0 F. x
(heat, new Selector (heat.getClass (), "updateLattice", false));" w- D0 e! U3 G2 r8 b1 J) {, @
} catch (Exception e) {
% p4 q ?8 t& e/ {) r System.err.println("Exception updateLattice: " + e.getMessage ());
% F2 J4 P* n/ S7 |8 a4 v& H& Y }
. J! N0 ]1 W* j : o6 y2 l9 N0 |. i
// Then we create a schedule that executes the
9 S# t! `5 g% f8 I) ~* k1 A9 m7 c0 {; a // modelActions. modelActions is an ActionGroup, by itself it8 L' Y% J r. \) j+ ^+ z
// has no notion of time. In order to have it executed in2 [, l2 P& y9 u/ p* j; i! I
// time, we create a Schedule that says to use the
$ g6 o# l- }# C$ g# `* M // modelActions ActionGroup at particular times. This8 p* [4 d3 U2 e8 M3 }& t+ q" g! u" R6 A
// schedule has a repeat interval of 1, it will loop every
$ w- @* t6 X% V- @2 C // time step. The action is executed at time 0 relative to
0 F! C3 x" y. K- z" M // the beginning of the loop.
2 b* c9 m; w; c: d. C
9 d- E* ~& @ v3 _ // This is a simple schedule, with only one action that is
% A2 d) |& c# x // just repeated every time. See jmousetrap for more/ i3 \+ R, d# Q6 g
// complicated schedules.
1 _( G" ]: K7 l2 u8 C
5 b3 r% Q6 O7 S+ m modelSchedule = new ScheduleImpl (getZone (), 1);1 P' R6 E. Q+ J* F1 N
modelSchedule.at$createAction (0, modelActions);0 n, j2 T X/ O
- H4 L* Q" p4 }" w, M; L2 z, X
return this;
! R# X' Y" v; f- J" l% i5 S } |