HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 t: z) E% \, s: O {% F" M. c" Y3 Q+ J; i
public Object buildActions () {
0 a2 o! R* t- ]) H ~0 s+ o% C super.buildActions();$ q6 p8 ?5 H8 C4 w- s' l
! i' T. M2 q1 A2 j2 Q
// Create the list of simulation actions. We put these in$ \9 c) F$ B, o+ v" c8 N
// an action group, because we want these actions to be
+ a& f: |) e7 W' E. ?) I" p% k8 ^ // executed in a specific order, but these steps should3 M& V9 Q9 i, v
// take no (simulated) time. The M(foo) means "The message A% W* P2 l* B& k/ @
// called <foo>". You can send a message To a particular' b0 L& x* v* s" P* {. @
// object, or ForEach object in a collection.4 D7 t- D& B0 C8 H
, z/ F+ q3 a4 d // Note we update the heatspace in two phases: first run
. k- l% A) I) Z5 T" \' ]4 y# u // diffusion, then run "updateWorld" to actually enact the
5 @) S/ \; h2 p% N // changes the heatbugs have made. The ordering here is( [9 J9 U& [( |4 j
// significant!
2 P7 k2 R8 U) P% @# T
3 z+ F7 `* R' f: q // Note also, that with the additional: i$ R7 `& w( A% j. D$ j) r" u/ L
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 m( `* S2 Q1 I& |& `
// randomize the order in which the bugs actually run
0 w: A: S7 ~/ K, X( u. q // their step rule. This has the effect of removing any: b7 O. V3 P2 X
// systematic bias in the iteration throught the heatbug
8 T6 e, c$ N5 E" d8 s, ]) d // list from timestep to timestep
% R8 e6 E) u; H/ d5 @
" o( u% G4 i5 [2 u7 J+ }$ F% L // By default, all `createActionForEach' modelActions have; e: m! b6 e. _/ [
// a default order of `Sequential', which means that the2 ?* |/ ?+ e) v
// order of iteration through the `heatbugList' will be. a8 r% ~0 c( W& I5 [) ?0 B/ r
// identical (assuming the list order is not changed6 y% y4 ~/ T3 H2 g
// indirectly by some other process).( Z, c+ R! ^" r. x* i# {' M
1 a2 \1 {8 ]8 H, i' | modelActions = new ActionGroupImpl (getZone ());
0 x# {" b, K+ h. ]" l& i' F1 ^+ O
* b4 e9 F: s# X; n- Y! W# K try {
m4 \( {9 P. P modelActions.createActionTo$message
& c4 T5 I3 x1 Z0 D$ k3 ? (heat, new Selector (heat.getClass (), "stepRule", false));
. `, Q8 ]( \4 ? } catch (Exception e) {
; P7 O" X9 w$ P u# F System.err.println ("Exception stepRule: " + e.getMessage ());) j0 o/ S% n! }' |3 B8 p- N
}
9 p/ J2 u- ~$ X( ~# N) @* y6 i5 }2 X8 ?0 F4 D5 _
try {; O8 o* j( l3 Y
Heatbug proto = (Heatbug) heatbugList.get (0);
+ p4 k0 ^# R3 p$ v& u6 J' m2 C. X Selector sel =
3 P) Q; r7 u% Q- z, e; A# F1 | new Selector (proto.getClass (), "heatbugStep", false);
8 y7 }. V: \( i/ R# t actionForEach =
8 v' n$ h0 y3 e modelActions.createFActionForEachHomogeneous$call
; X) j5 N4 a. K7 k" m% J: z (heatbugList,
+ I4 e/ f: Z1 ?$ J' B2 x new FCallImpl (this, proto, sel,* R) V; \; B2 ^2 |
new FArgumentsImpl (this, sel)));' `: k6 D0 t# S& s- Y! h
} catch (Exception e) {5 ~1 N3 a9 V7 k) h2 z
e.printStackTrace (System.err);8 G* Y6 Z f& j
}5 P: W4 n, a ]% A3 g2 P+ \& n
: o3 \6 t! t- e" M4 K$ H syncUpdateOrder ();8 Q9 k* V$ @4 l9 x1 d1 s
) _ G/ G. p3 K' L try {3 h7 `5 a: `3 ?+ ?3 i8 s. n
modelActions.createActionTo$message 8 `0 K; S% o: h. C! S) q3 X5 b+ t5 m
(heat, new Selector (heat.getClass (), "updateLattice", false));8 V) J* g0 ]3 V
} catch (Exception e) {% @8 c4 O& s. ?, C1 L
System.err.println("Exception updateLattice: " + e.getMessage ());9 T6 H) z o3 s0 e/ h C
}8 X. W4 b, V9 B( {" V
" W' g% P3 q9 x& ? // Then we create a schedule that executes the7 O% p3 ^5 ]" f; z- o
// modelActions. modelActions is an ActionGroup, by itself it
) I8 a' L! G. h- O8 ?2 f // has no notion of time. In order to have it executed in
) M. y, B/ I! W/ A6 U // time, we create a Schedule that says to use the
+ V% \# _% M; B. @ // modelActions ActionGroup at particular times. This1 ~1 W4 D- }) E# ?' H7 l
// schedule has a repeat interval of 1, it will loop every
8 M7 X, | |3 H+ N/ d* r/ m6 c+ ` // time step. The action is executed at time 0 relative to$ l" L1 r' `4 f# o* q# U& M
// the beginning of the loop.0 `* D; v+ S: ?/ c: z" ?
7 x4 r; ~; T# G6 o
// This is a simple schedule, with only one action that is5 j( n7 v( B% a; C! ^
// just repeated every time. See jmousetrap for more
) y. @: u# x0 N( R4 ^/ R // complicated schedules.8 i$ n8 c! @7 {* e8 K1 u; J
: O- a& s6 [/ V: X- G( P7 r4 Y modelSchedule = new ScheduleImpl (getZone (), 1);# S0 {3 ]7 [) i6 O: l
modelSchedule.at$createAction (0, modelActions);6 e9 c1 b6 f- s& T! c3 H4 A
3 `6 H/ H) c1 ` b! f7 ^ return this;- ]5 z# t0 W( V
} |