HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ g- X9 ~' u% l- S# L0 D. K/ H; s/ ?' @& D2 E; S
public Object buildActions () {
" C* N2 r; M1 G/ r3 i super.buildActions();1 O3 | u, D4 C
% P4 W u/ X, v; \0 @ // Create the list of simulation actions. We put these in
% i0 e8 J, Z& M+ P, i6 U3 f // an action group, because we want these actions to be8 l0 i' W/ T5 b
// executed in a specific order, but these steps should7 t0 c" I' v$ ?$ B# H1 O
// take no (simulated) time. The M(foo) means "The message
+ D, [' n. P6 ^/ K3 V // called <foo>". You can send a message To a particular
3 J$ J/ b. x M) q // object, or ForEach object in a collection.5 d9 `" ]$ P3 v
" t. F" H2 ~& o* B8 b6 ?
// Note we update the heatspace in two phases: first run/ G& z. V7 Q% L+ g
// diffusion, then run "updateWorld" to actually enact the5 E9 U8 n1 g5 T% j" n
// changes the heatbugs have made. The ordering here is
# J8 Q U* ?3 [ // significant!8 C6 k) S0 W% n0 I( g0 g
7 C% L6 x+ Q7 H( {$ _' R // Note also, that with the additional- h7 x" C* ~1 Y: f9 P* h4 c
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 w+ W3 D2 V) F/ T7 B& N8 j0 v
// randomize the order in which the bugs actually run, j8 [" y V( M* n+ V8 d9 Y P, t
// their step rule. This has the effect of removing any" N4 _$ k6 O) z# F4 f( i
// systematic bias in the iteration throught the heatbug
3 s( Y' v* W) H ?+ H0 U // list from timestep to timestep. {! h2 G8 ^9 u; H* _6 |/ E+ g
8 z! t V% \! y4 W( y
// By default, all `createActionForEach' modelActions have
5 T3 K. o1 c9 {2 _* J/ E1 i // a default order of `Sequential', which means that the
; _, r; V! G. E // order of iteration through the `heatbugList' will be
$ ^) Y+ A! Z7 P- j$ @) k# }* ? // identical (assuming the list order is not changed! l; k- Q) O* s. L) R1 X
// indirectly by some other process).9 B1 K; {! }* Y+ y, z' f
' s8 k% n7 C2 c% c- g& N modelActions = new ActionGroupImpl (getZone ());3 v& r- s2 N7 w3 k
* D& s i* z ^' [
try {5 e# b; [( _% F5 Q% O' h/ }) ~, D
modelActions.createActionTo$message
A6 J* V, @' x& @& k8 L6 _ (heat, new Selector (heat.getClass (), "stepRule", false));
6 ?5 G" t; w: o } catch (Exception e) {
# u1 A2 r" B4 ~% ~4 r) ~6 ~0 l System.err.println ("Exception stepRule: " + e.getMessage ());
; _) S0 ~5 P9 `$ o }( w. n+ \0 F$ r- K
9 i: B2 Q4 q" _6 C- P0 ?4 ` try {- U+ m7 }# {4 |, ]4 o
Heatbug proto = (Heatbug) heatbugList.get (0);0 T" g9 K# Y5 G9 p- _
Selector sel =
1 [$ ?9 z P8 \4 f$ W2 l5 g new Selector (proto.getClass (), "heatbugStep", false);
* V) z1 \. k( B actionForEach =+ ^/ ^5 b) J3 k4 l" o# Y% c
modelActions.createFActionForEachHomogeneous$call
; a! C) ~+ `+ S( u! z (heatbugList,
* w+ B5 G$ c' }9 Y( K- V new FCallImpl (this, proto, sel,2 D3 D' n9 `1 F1 }8 y; k) U) k7 }
new FArgumentsImpl (this, sel)));2 ]( a3 x' w) b5 T" Z/ S: \
} catch (Exception e) {
% }' c5 L3 U3 |4 N0 ~$ X) Z4 F" f e.printStackTrace (System.err);
, Z' y! B# G6 Z2 g2 {, D% F9 P }
2 r. m+ `: m8 p3 W6 T% [
7 _, S) A! s' m; G" u# j2 I: g syncUpdateOrder ();
) |) A% q( ~* N( h1 ?( Y
+ Q# Q1 @0 u0 V# [ try {4 w( r5 }; ?& C% w9 i/ c
modelActions.createActionTo$message 5 `4 c; ~5 M o& b( S
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ M A# ~$ H% ]2 l } catch (Exception e) {) ?: o2 E+ D/ G5 q
System.err.println("Exception updateLattice: " + e.getMessage ());3 k% M+ q) x. a o- ]6 C
}8 x x. q& J, r' I+ }+ V: S( O
6 ]" \/ S' T, o // Then we create a schedule that executes the
- d8 n$ P( ~# a$ ]0 ^( S/ G // modelActions. modelActions is an ActionGroup, by itself it" P/ g4 }" E6 Y: A: X, Z3 h9 K9 s
// has no notion of time. In order to have it executed in
) f/ l0 z! \; n" v2 r+ ? // time, we create a Schedule that says to use the
( l$ v- V2 Y. ]( S4 { // modelActions ActionGroup at particular times. This
3 ^) E% m6 o0 P- G // schedule has a repeat interval of 1, it will loop every
4 k9 l; c! `' ]: `1 F // time step. The action is executed at time 0 relative to
# c3 L; a& x5 F; o4 G; @/ K // the beginning of the loop.
' A _, m" U# m L9 F; m4 O T( h# \6 c6 x/ X4 z1 I
// This is a simple schedule, with only one action that is
0 @7 n n/ R9 B8 K" ~ // just repeated every time. See jmousetrap for more3 Z+ f3 E9 x+ N0 _
// complicated schedules./ V" b9 I3 x$ N" w9 z3 o
$ j9 `, o3 S a4 Q# i* y
modelSchedule = new ScheduleImpl (getZone (), 1);4 \$ b: |8 O5 L. X. g! _2 ]5 y/ X$ N
modelSchedule.at$createAction (0, modelActions);
9 z' r' {, N" V. g% q" S 6 E: ?' S9 v* s% A1 V4 _4 w
return this;
; y. Y+ }9 {8 }: y5 ^2 f7 G } |