HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" O! s) E m9 ]5 J3 i( V- ~2 @, V
4 n0 s' M( e' O0 D' g2 Q, l |
public Object buildActions () {
& P! t2 n4 N* d0 _ super.buildActions();
# p/ V* u! T# N6 g
! D4 ?2 V+ b' g$ K // Create the list of simulation actions. We put these in
- s1 N! ]) P, u, |$ S' k // an action group, because we want these actions to be
& d1 y& |3 a, Y5 i/ @& A5 a2 c* n // executed in a specific order, but these steps should
- v8 V3 S2 {$ z+ v- H# q // take no (simulated) time. The M(foo) means "The message; o; m% }: e4 t) O+ Q( }6 B
// called <foo>". You can send a message To a particular% i7 i$ M) |% n. u7 m$ q. k9 t
// object, or ForEach object in a collection.7 n! }8 g/ f7 ~, I$ m9 X. T
! X7 n6 d& ~, W3 L7 t i% c, ^
// Note we update the heatspace in two phases: first run
' c/ l/ S9 X# Y; A // diffusion, then run "updateWorld" to actually enact the
# P! q9 d: `# N. W6 V // changes the heatbugs have made. The ordering here is& H7 R. m. }3 e4 {6 @
// significant!
( l3 f' I9 T/ ~8 D# U* Q
# S! y6 R) k: y% `( Z$ C // Note also, that with the additional
% f! U' x) e# B // `randomizeHeatbugUpdateOrder' Boolean flag we can
* P2 j7 [) d2 k // randomize the order in which the bugs actually run
3 C, m' D/ V& k8 @8 o; E4 h // their step rule. This has the effect of removing any4 c; \! O9 B {% N) \* w2 b( G% w
// systematic bias in the iteration throught the heatbug
# G# ]' y/ p9 h# A R2 N // list from timestep to timestep- W. D. O/ B: [$ K- G5 }$ }" e+ O
9 u. |. E; n o- |8 o1 `* W // By default, all `createActionForEach' modelActions have
& P5 w; I- j5 s( E, i [ // a default order of `Sequential', which means that the$ B6 |" f7 ]: H$ f1 L t
// order of iteration through the `heatbugList' will be
9 k- c- e) J. ]; ?& h // identical (assuming the list order is not changed0 I7 P" Y( `. d# ~5 z
// indirectly by some other process).# G; C, @0 [% s
* |7 Q! x2 E( R" Q# {4 T
modelActions = new ActionGroupImpl (getZone ());
) |! n- c0 ^4 { l$ g
4 ?* g- \3 C! ? try {
4 K" o6 B' U% D' r# P5 { modelActions.createActionTo$message
/ m* p" G8 B1 D/ R* b4 A1 v (heat, new Selector (heat.getClass (), "stepRule", false));' X+ K B4 N& b0 Y0 ]
} catch (Exception e) {
* h6 c8 |" m* B" X' g0 s; _+ I$ E2 Q System.err.println ("Exception stepRule: " + e.getMessage ());, W- |+ `3 P. \6 x
}
5 G; P, Z: C! ]8 c; M5 [5 r( Q, I( N
) Z( M) f$ l3 E6 v, A5 N try {, ~8 L9 u9 ?0 c9 G
Heatbug proto = (Heatbug) heatbugList.get (0);/ L7 A3 |- z' {
Selector sel = $ j/ i6 V- {# p. ^' ?& p" J" ~
new Selector (proto.getClass (), "heatbugStep", false);% \1 _. h$ d' K6 r0 h S" A
actionForEach =
9 D* E; ^; w) J- _7 U modelActions.createFActionForEachHomogeneous$call; F! Y) Q7 f2 @
(heatbugList,
1 C I7 a1 k! s% w# a- X+ o new FCallImpl (this, proto, sel,+ O) i* x. S- ^2 Q$ c6 j; r0 ~7 ^
new FArgumentsImpl (this, sel)));
/ j3 ~" A- R; b } catch (Exception e) {
/ Z7 |- m- K; j+ V e.printStackTrace (System.err);
# H5 w% F @$ O: T- D }
2 D7 R6 `" B8 w8 D/ B8 X
3 v/ ~1 m9 s' i9 \' O syncUpdateOrder ();
2 m7 ?1 u4 {' O3 { ~: f, v% E3 D0 j$ h, u$ \2 `9 B2 x0 v" ?
try {
; b0 k# f4 M! C$ t modelActions.createActionTo$message 0 f4 Q$ E$ P7 }5 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
& b" ~7 M( I3 e2 T! } } catch (Exception e) {/ @. l4 Q/ D1 q9 j3 a) m/ z P* X
System.err.println("Exception updateLattice: " + e.getMessage ());
: A: Q; ^9 S6 x- W6 S }
2 F( ^8 n0 R" d# P r0 n; I4 M" t9 B$ W/ }
// Then we create a schedule that executes the4 F( T& Q$ s$ u+ T0 e9 B
// modelActions. modelActions is an ActionGroup, by itself it. a e- T0 t" q' [# t1 H Q; w
// has no notion of time. In order to have it executed in
# Y4 A, A3 [& R2 g // time, we create a Schedule that says to use the
3 ~+ s0 {! ^0 x l/ A // modelActions ActionGroup at particular times. This1 }% H3 {3 q ^8 a, x
// schedule has a repeat interval of 1, it will loop every* l$ s9 r0 T; o: }
// time step. The action is executed at time 0 relative to
6 {7 F& F. {7 Z2 A( m! ^ // the beginning of the loop.- ^7 @6 e6 W5 r1 L9 u/ s9 F
" w2 j9 D5 r7 ^' \: o
// This is a simple schedule, with only one action that is
; {0 U: |' i# i9 y' k8 @ // just repeated every time. See jmousetrap for more
: I) g# W: t$ b1 ] // complicated schedules./ I9 s' ~6 T3 c u1 }7 P
8 c# |3 l/ B% ]/ f* l modelSchedule = new ScheduleImpl (getZone (), 1);* R4 g! p6 t- E8 Y/ M; N
modelSchedule.at$createAction (0, modelActions);9 x! c/ A% a3 }
( l3 [! ?( d5 I1 D9 o) R% s K4 T
return this;
1 M+ o2 L3 [& T- g- ? } |