HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- M' p2 G o$ S; T7 | g5 m
' t* Y- |+ m8 H' e
public Object buildActions () {
" y4 N1 d' L. H6 L: G super.buildActions();& F9 W$ t" b2 y0 D- B+ y. y6 }
* l' }1 S; E( R( T // Create the list of simulation actions. We put these in& g$ h0 o) k9 b8 I# C
// an action group, because we want these actions to be6 [# U5 r8 h8 @% m. p
// executed in a specific order, but these steps should
" W7 `5 k _ \9 K$ R // take no (simulated) time. The M(foo) means "The message; \+ Q6 E) }, z: W( [- U& H3 U* K
// called <foo>". You can send a message To a particular# M5 R$ ^" z9 j8 `3 [
// object, or ForEach object in a collection.
" l0 j& e m: u0 L4 \# U% K5 }+ ~ * D$ U0 H7 V9 M5 o1 l0 [
// Note we update the heatspace in two phases: first run1 |% f) Q/ L. C0 M0 Y: C
// diffusion, then run "updateWorld" to actually enact the% u. t Y% s' H
// changes the heatbugs have made. The ordering here is
; B p; U* N! k& p- v // significant!
! j2 N; w* k) T, [6 n5 W# ? ! d6 e, m8 E0 l, t5 [% a( ?
// Note also, that with the additional9 ?& x7 c( w+ I, m5 S6 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 l8 C) p/ h$ L$ ?( E# L // randomize the order in which the bugs actually run
" k W6 h. u$ b2 J* a. @ // their step rule. This has the effect of removing any
7 `: \$ ~2 a# H7 y2 ]0 ^+ \ // systematic bias in the iteration throught the heatbug
3 \' n& e" y: k6 ]2 B5 E // list from timestep to timestep
- ]- H) i% t4 Y \, r- f) v0 g& x+ i
// By default, all `createActionForEach' modelActions have3 A" M) H% o/ M5 [. g3 d. X) \/ u
// a default order of `Sequential', which means that the
5 [+ H5 d s& K( m" E2 o( k; H // order of iteration through the `heatbugList' will be
' z1 g; ?( Z- r% w // identical (assuming the list order is not changed
4 e5 t9 d, R) m s // indirectly by some other process).4 J- {6 i E& Z
" F+ g8 L- t |$ | modelActions = new ActionGroupImpl (getZone ());( ]- a8 P n6 J+ w
$ B f7 ?% d+ L% `
try {
& C" C0 X+ I: {+ _7 A) L modelActions.createActionTo$message
' p0 ]6 C S& N) z" _7 w (heat, new Selector (heat.getClass (), "stepRule", false));& w! e. x$ B6 v' Q! M
} catch (Exception e) {
- X5 c2 h! I4 ]! {; p+ u System.err.println ("Exception stepRule: " + e.getMessage ());
3 H, D5 `! [: W4 c }( J- U2 ~) g8 ]) M T8 v. R7 R
9 f% [& |7 c& g5 E( S try {
3 J8 K. G& _7 d( l+ u Heatbug proto = (Heatbug) heatbugList.get (0);/ m! O; N& ?- m& X
Selector sel =
! G- _1 J' M: O% S F" e0 I new Selector (proto.getClass (), "heatbugStep", false);
7 h/ E/ e! e' J- `- }; c; O9 | actionForEach =
. p9 ^+ n& \9 V- p x modelActions.createFActionForEachHomogeneous$call
2 E2 w8 x r. ^8 N7 [- y1 K8 B( B (heatbugList,
% w0 e5 R; l- q( R: I( {" R new FCallImpl (this, proto, sel,
* a; b% U, s. V new FArgumentsImpl (this, sel)));
, n' ]) X% m4 K6 V. I } catch (Exception e) {3 }# }& }8 v; u1 N
e.printStackTrace (System.err);
! @9 j' |5 b: T. T5 M' {5 \# t* s }8 i, x1 q' \) U1 a' |
' j& B& H! Y/ Z
syncUpdateOrder ();
l2 ^, s: o/ _0 j( A8 _) E) I6 ]% u( u8 c
try {
0 B* t+ [4 ?& `7 l+ R6 ?, C c v, M modelActions.createActionTo$message / J0 }. K1 X6 x5 u J" Y
(heat, new Selector (heat.getClass (), "updateLattice", false));$ q: J" d, r; M
} catch (Exception e) {
: {6 ]0 V6 U# i2 {; Z* { System.err.println("Exception updateLattice: " + e.getMessage ());
) G# l, e* D; O M" a1 c }
8 q+ o3 h1 F- ?
4 i# y- w6 K7 H' [ // Then we create a schedule that executes the
+ X7 i. h0 ~& W0 g$ ^; ^' G // modelActions. modelActions is an ActionGroup, by itself it
( m& j0 `1 V" q) S1 L* { // has no notion of time. In order to have it executed in7 b t" ^" Y/ L P* E! U
// time, we create a Schedule that says to use the
# o- X: B' |% u& J- f1 k // modelActions ActionGroup at particular times. This
P: |7 a( [( b. q; }. M$ Y // schedule has a repeat interval of 1, it will loop every
4 R5 X- T5 a1 r; K8 ^ // time step. The action is executed at time 0 relative to0 s7 ^" w" J3 J
// the beginning of the loop.
$ w$ ^& u5 b( P* y, w5 H
2 W7 s& n& u' o6 w. ]6 E // This is a simple schedule, with only one action that is4 s. m r3 o# m3 P# }# s$ E
// just repeated every time. See jmousetrap for more
% S6 ^0 J+ A8 R! Q7 a // complicated schedules.
* |' n, ?0 {6 r/ n& l% Q, e
" C3 l3 F0 b5 F5 F: {9 W modelSchedule = new ScheduleImpl (getZone (), 1);
+ R& y7 X* v* ]2 s) ?6 T) G' l modelSchedule.at$createAction (0, modelActions);
$ K6 d4 S! f+ p% m1 i$ J- p A3 U
+ w0 y" u( c! _$ E6 }' H% J return this;% Z n9 W4 t2 F. g% j
} |