HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 s) }7 x+ T) J1 H9 x
$ U; A1 h; n6 l% s6 a% m( s public Object buildActions () {4 l; A, H9 ^8 Z# H
super.buildActions();
8 ]( m0 w* D. I1 Y4 e9 i
# L5 _( N/ v; G( G // Create the list of simulation actions. We put these in3 T5 c" m% u9 C
// an action group, because we want these actions to be& ^0 B8 X( ^/ {8 [. s$ V8 P8 D; C
// executed in a specific order, but these steps should, y1 o. g a- A
// take no (simulated) time. The M(foo) means "The message
7 U# E, g$ C% O) ~! u5 }: M // called <foo>". You can send a message To a particular3 [# J; B& l5 k
// object, or ForEach object in a collection.$ a, A$ M8 @4 G0 N& `% f. T! J
" g+ y! T' [' m/ W+ U
// Note we update the heatspace in two phases: first run
' L7 i" K6 C( w/ I8 L k // diffusion, then run "updateWorld" to actually enact the, A; E" u; `; R5 s
// changes the heatbugs have made. The ordering here is
/ W1 i7 u/ ?# ^0 X3 }- _ // significant!
% I3 h2 l7 T& {
# M- G1 f4 U* e* f' v // Note also, that with the additional X" K0 n" ^4 W: H$ u: Y/ l/ @
// `randomizeHeatbugUpdateOrder' Boolean flag we can' W( c# X: A u# {
// randomize the order in which the bugs actually run
9 e2 T. d, N# W4 Y // their step rule. This has the effect of removing any
" Z% d8 {+ n9 A" x6 s( q2 `" @ // systematic bias in the iteration throught the heatbug& _9 ^: e% t2 Z& `/ j( z3 Z U' G
// list from timestep to timestep
8 ?0 X8 D7 v( s0 L7 W
* J9 ]) i/ p4 i: o+ R2 j // By default, all `createActionForEach' modelActions have! I, x! ~- H u% E
// a default order of `Sequential', which means that the, s4 l3 R r* s1 A( Z- O* w; U
// order of iteration through the `heatbugList' will be
* @ `% y2 @; h7 z // identical (assuming the list order is not changed
4 i: P( h/ [+ u7 a* b) Z* n7 C // indirectly by some other process).+ T) H# \+ ?% g. z
. l; S# _" S T/ ^5 F$ ?8 ? modelActions = new ActionGroupImpl (getZone ());
4 Q5 {4 S: s; Z* X; I) I; U9 O$ p" ~7 V- W: O8 Z- t
try {
& d: [+ V, m0 h) ]6 L modelActions.createActionTo$message
: p; v7 n4 }2 Z* E7 Z (heat, new Selector (heat.getClass (), "stepRule", false));2 L3 ~) S: G3 m: }0 o5 [+ R$ J
} catch (Exception e) {2 M4 E; y+ L' n( y
System.err.println ("Exception stepRule: " + e.getMessage ());. x9 d% b. g' s. B
}
% z. o3 B$ `* ], v5 k ]
$ h9 Q O" e( N try {" l) [. ^0 F8 k2 U5 I
Heatbug proto = (Heatbug) heatbugList.get (0);
1 K: B! p' `) F8 k( Z4 Q Selector sel = - z' Y9 J2 e3 K) B
new Selector (proto.getClass (), "heatbugStep", false);
1 r" @7 d8 e; \! k. h, x actionForEach =0 r9 @* }, N) l) |, L* h
modelActions.createFActionForEachHomogeneous$call
: p) K# P3 l, W$ t/ x3 @ (heatbugList,8 `2 S: A8 s% G w- W6 U
new FCallImpl (this, proto, sel,5 y8 R5 r2 L! Y, U, `, ^
new FArgumentsImpl (this, sel)));' I: s. G4 F0 w
} catch (Exception e) {
+ a& `& v, k2 y4 G) e9 H A5 O e.printStackTrace (System.err);+ h7 X, U- R9 Y* B# \
}
/ f @$ m3 _8 k9 l
) B! O) G" J; h8 @2 C5 H syncUpdateOrder ();
$ `1 ~9 u; I$ I( a2 o& c U2 T/ s9 W, F J9 N- U& c
try { b3 Z) D* h% ~
modelActions.createActionTo$message 0 b' S6 T3 Q# v1 ?& e! n, y6 B9 R
(heat, new Selector (heat.getClass (), "updateLattice", false));
) o( o6 R Z* G- U$ S } catch (Exception e) {
' i |2 g- n5 i/ m7 K System.err.println("Exception updateLattice: " + e.getMessage ());3 m; K& h7 N4 x( V" f# m
}' k: G8 S/ Z: S
$ v7 V$ _9 f" c) H& k* t$ g/ Z. X // Then we create a schedule that executes the, j1 ~1 G- s/ K2 G: X" [
// modelActions. modelActions is an ActionGroup, by itself it
2 t; H" K: M8 u5 d+ q+ V // has no notion of time. In order to have it executed in; i2 i0 G" I! P6 v8 b' W( ]) X- G* O
// time, we create a Schedule that says to use the. G! y- {/ e% s4 a5 r) ~8 P, [- W0 q
// modelActions ActionGroup at particular times. This
& I. V; b; o$ o) r7 y# q8 m3 B // schedule has a repeat interval of 1, it will loop every
0 N3 |3 N6 {8 K4 Y // time step. The action is executed at time 0 relative to
Z: E8 q `9 Z1 N* x // the beginning of the loop.
3 y# q( s2 N) u8 O; u
6 _, S2 C) ~( E1 v+ j2 r) f; U# ?9 q // This is a simple schedule, with only one action that is+ h+ k( \: k! W: j' O& A3 e
// just repeated every time. See jmousetrap for more- A+ }7 b. f/ X$ A/ m
// complicated schedules.
# q( Y- C" T4 Y9 H8 U- K) q
' S% W8 J( T+ F7 e modelSchedule = new ScheduleImpl (getZone (), 1);8 i2 S+ i- R3 c; R% N3 J! u6 z
modelSchedule.at$createAction (0, modelActions);
" h( m [7 Y, B0 g% Q 5 F$ t0 z2 O+ z1 r
return this;0 _ l( g- ]* E1 k" R! G3 O
} |