HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ v% ^7 M- \0 n- B3 s5 V& q' s+ D
p# K' x0 G; p9 { public Object buildActions () {
, s' J9 g& I, i( R7 n super.buildActions();
( b* E, H0 B# e, ?3 u
1 {9 J0 \) [& b' Q // Create the list of simulation actions. We put these in7 A- N# v& Q( `6 F
// an action group, because we want these actions to be
/ J% I# Q( N) w# F // executed in a specific order, but these steps should
) `# A" ]8 ?3 Q, A; F$ s$ n) D // take no (simulated) time. The M(foo) means "The message
$ a. u* y! g+ d& a0 h' F // called <foo>". You can send a message To a particular
+ R+ S. R% x+ Z) d( c1 } // object, or ForEach object in a collection.
" T( K. m/ }4 P& |9 M e Y
$ q. ^; j2 a: B6 `( [ // Note we update the heatspace in two phases: first run
/ x0 j# R# b6 _( ^0 l/ E // diffusion, then run "updateWorld" to actually enact the! @# T. k1 @4 V; j% c
// changes the heatbugs have made. The ordering here is' ~. q! R, D) v$ g& b& V
// significant!; K$ H R; ` r L7 F' Q4 J
. v/ C( `8 B8 g% ?9 f0 `8 | // Note also, that with the additional+ R; G: \9 x& m N, |/ ]. F1 @ v
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 c/ c- W$ v/ g/ t5 d
// randomize the order in which the bugs actually run) J4 [0 U& p6 q' A0 m
// their step rule. This has the effect of removing any
( r( H9 n# u# v9 L) T // systematic bias in the iteration throught the heatbug3 e3 A# V* r/ D. R
// list from timestep to timestep' r: e: u2 g: o
+ ~* B7 H4 W- [+ u0 `( D: c3 E0 O
// By default, all `createActionForEach' modelActions have- g* i- F8 v7 N; a& f
// a default order of `Sequential', which means that the
d0 s5 A8 n: M4 k // order of iteration through the `heatbugList' will be
0 m) r8 h+ Y, \, s // identical (assuming the list order is not changed9 X. ~# E2 T1 O. P+ j
// indirectly by some other process).
% _3 p! h( S; R: Z
/ _( Y% K. ?! x6 t modelActions = new ActionGroupImpl (getZone ());1 @3 ?/ X3 f& K T, B8 q
) n5 d* Q0 B) I4 m4 K, B
try {& a1 L7 X; N/ k% m7 t) _
modelActions.createActionTo$message
0 k/ ]6 T7 U" z* j. { (heat, new Selector (heat.getClass (), "stepRule", false));& u0 f3 u6 b- G# t0 U+ j" H2 w [% ~
} catch (Exception e) {
$ b( [% Y- `/ R1 I/ j0 _! w A System.err.println ("Exception stepRule: " + e.getMessage ());7 u& P a: d* b6 j5 D) Y ?
}
) M& F: j* o: Q
7 Q4 M: ^5 ~! L try {# ^ J" Q1 ]8 f. ^
Heatbug proto = (Heatbug) heatbugList.get (0);
" E' r4 i: x1 D1 u Selector sel = ) w4 g! C) S7 k/ ~7 Q# p
new Selector (proto.getClass (), "heatbugStep", false);
( d3 Z2 E% y5 c/ }( q( Q actionForEach =
B3 [+ M$ f- ~! h9 Z( u* ~ modelActions.createFActionForEachHomogeneous$call
- P& @5 C e8 V& a4 Y4 o+ T( e (heatbugList,
8 j, H) s& o5 n9 U% ]! h) ^1 [/ K' p" K new FCallImpl (this, proto, sel,
& `5 ~7 V9 ~) S3 D6 V K. q new FArgumentsImpl (this, sel)));, F7 N4 B& {8 y9 o' Q+ o- n9 K% b% q
} catch (Exception e) {
2 I) O! |) U. \, O$ l) S3 @ e.printStackTrace (System.err);% `# {9 D3 j* d" w4 K8 }3 U r
}
0 }0 q/ K. w2 ~, i8 J, O
: _+ L0 \7 ^8 V' | p! ? syncUpdateOrder ();
7 r" |5 X! h1 j/ q% d4 [- ]" Z! k1 `/ ?/ M: X( L# \
try {7 b |5 b. ~$ m8 J1 g* p/ p
modelActions.createActionTo$message 7 {" _( F6 ~# [* v* _7 B0 L' A
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 F( }' L; C) z `* a8 W# @ } catch (Exception e) {
: C# Z5 }# b8 z: O- H# _6 `7 H U System.err.println("Exception updateLattice: " + e.getMessage ());' P. y2 ~$ b! Y3 f
}
/ M _9 P6 w3 u4 c+ f; |
) v( z. k. N% n! {' E* l7 ? // Then we create a schedule that executes the5 F' i% T. [1 r" ~, R7 w9 \
// modelActions. modelActions is an ActionGroup, by itself it
i; h U) z# G' b$ C // has no notion of time. In order to have it executed in
3 @& O- ^, W9 F6 ~- E5 {4 V // time, we create a Schedule that says to use the
/ N. R7 E3 n! x4 m. ? [& } // modelActions ActionGroup at particular times. This! n6 m% ]4 K P
// schedule has a repeat interval of 1, it will loop every" S' O7 h% J; D" u2 s6 Z" ^
// time step. The action is executed at time 0 relative to& H# A5 e {' A
// the beginning of the loop.
" C7 y# A5 F( ^8 }" O2 T7 O* ~+ @8 d
// This is a simple schedule, with only one action that is
5 F$ }- s! g; @7 B // just repeated every time. See jmousetrap for more
+ D' I7 ?# f- [) B0 N* ? // complicated schedules.$ s6 A2 N9 V0 @8 f3 d
4 s) ~3 ]/ k' p4 _3 v: R) @
modelSchedule = new ScheduleImpl (getZone (), 1);2 L( L% v; _; m/ H7 `6 N
modelSchedule.at$createAction (0, modelActions);
' ?: q, N9 |: h9 k
# ]* D* O+ V, A/ P, O0 e+ ?0 z return this;' g( i0 m; L9 p6 l. [ N" `% I; h
} |