HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 ]4 t4 ]5 G% l. v! t( v7 [( B" i
9 s0 M5 w4 d, j9 ~
public Object buildActions () { f$ z$ @! N" W1 O' v
super.buildActions();: o8 S; L; a9 Z
. u# K; x+ l4 |9 b
// Create the list of simulation actions. We put these in
- ?. l. M ]! k0 `3 c6 L( ~ // an action group, because we want these actions to be
, N; h1 u3 j0 H* a3 J( m // executed in a specific order, but these steps should
* `8 c/ r e; L1 M5 ~7 B. \ // take no (simulated) time. The M(foo) means "The message
{9 X0 n- e& L f6 r // called <foo>". You can send a message To a particular
$ t# @# f4 `' v, y/ m' z // object, or ForEach object in a collection.
! f' w* a8 P$ ?2 C
# U2 r' r( \" ]! b" f; I/ u // Note we update the heatspace in two phases: first run7 v* y% A& p% F* `) g+ N* n
// diffusion, then run "updateWorld" to actually enact the* W6 H$ Z$ d5 v
// changes the heatbugs have made. The ordering here is
c' ~7 V5 w- d, T7 j // significant!
7 V( q) k7 A, n' u1 E+ U+ x1 N6 r) Y2 ] 0 V( _" g8 Q0 p% g$ Z8 {
// Note also, that with the additional$ [! G5 p* h, l7 j3 f
// `randomizeHeatbugUpdateOrder' Boolean flag we can# N; y7 G! S' P2 o4 z, ^
// randomize the order in which the bugs actually run+ K% Y! ^- V6 u) \7 s1 m$ Q- D* }
// their step rule. This has the effect of removing any
& i7 K! y6 `" c: s: | // systematic bias in the iteration throught the heatbug$ T. ] u" L, H3 Z, D- W
// list from timestep to timestep
0 S) J W% y: {& H6 \2 D. `. H
& W! \- \" w0 R // By default, all `createActionForEach' modelActions have
7 _6 X2 |5 c, F7 W // a default order of `Sequential', which means that the
$ ~- c) z* J5 o! v4 O/ T6 C // order of iteration through the `heatbugList' will be- X/ d- T+ ?) F6 w1 L
// identical (assuming the list order is not changed
5 ?/ K& I+ W! s // indirectly by some other process).
2 J% ~& A" V9 N" V- y 4 u* n8 Y! H- }1 L
modelActions = new ActionGroupImpl (getZone ());2 b2 |& w0 }+ k9 x$ D+ v5 l( X
/ n8 E& V# O6 U, T: w" }( f
try {
* k* H. Y2 m; }& v modelActions.createActionTo$message
$ g& F$ P) h* C" o% f (heat, new Selector (heat.getClass (), "stepRule", false));
y; R) B' e1 ]5 E9 e. L6 Q } catch (Exception e) {
0 s. H9 P) A- @: x5 G# h6 a2 [, C System.err.println ("Exception stepRule: " + e.getMessage ());
$ [1 d% {' U/ `, D4 [# ~) `% s }+ Q# v3 M7 V: H }3 V. F _
, f9 ~$ N/ V/ k3 E4 m+ d, e try {
% f; z2 T) j* J4 _7 J, U Heatbug proto = (Heatbug) heatbugList.get (0);' k. g# r7 E" S" O( q3 E+ K* \
Selector sel = # K9 g) N2 l8 u, x \" j
new Selector (proto.getClass (), "heatbugStep", false);+ E' z9 v6 }+ s; Z% y5 b' ~
actionForEach =
+ f) A2 G+ m6 ^3 ~9 I modelActions.createFActionForEachHomogeneous$call: V, f* y" e7 C! ~" y6 Q3 ^. f$ l ?& I
(heatbugList,
1 L8 M8 M, O. [# e' J2 c new FCallImpl (this, proto, sel,
3 L, F/ \# Y: P0 ^# f$ @ new FArgumentsImpl (this, sel)));
; Q: d4 ~+ `* y% {' @; k } catch (Exception e) {
+ T% `- I/ H3 e2 h$ d" Q8 ~2 { e.printStackTrace (System.err);
3 V8 E" I& |- O. r# Q$ o* n& O }* G3 Z5 n5 U9 {9 a
1 Y8 s) s a) j syncUpdateOrder ();) H8 X7 ]& j$ V* ~- j9 V% l
6 p: @1 D8 M. M; S try {
7 N6 x1 @' |7 b# B modelActions.createActionTo$message z8 a2 v. k! |$ s/ ~8 A( S
(heat, new Selector (heat.getClass (), "updateLattice", false));
; @' \, Z0 d8 H3 i. a: P } catch (Exception e) {
& i* m8 G1 i5 f- k6 v System.err.println("Exception updateLattice: " + e.getMessage ());
0 k8 k3 n, o* H* w( y }
8 R, c. h+ l* F, _4 ~ - I, A$ k+ _+ H! g" C
// Then we create a schedule that executes the p+ ~+ n8 H, V0 M# }6 f
// modelActions. modelActions is an ActionGroup, by itself it
1 z7 h( S: ~7 f9 o // has no notion of time. In order to have it executed in
5 E" @& A3 A! ~% r7 R; {9 ` // time, we create a Schedule that says to use the
# I. R% }2 _, r) a& U& z5 x* B // modelActions ActionGroup at particular times. This
1 l3 T) `9 {) S- L6 t // schedule has a repeat interval of 1, it will loop every
5 `9 H! ~' {/ O% X% x6 C; } // time step. The action is executed at time 0 relative to
* ~& B; Y- b& O+ y' O" c5 k' i& d6 c // the beginning of the loop.
& V- [2 }7 A9 ^; z8 Y
$ d3 Y4 M5 g8 i // This is a simple schedule, with only one action that is6 ?0 A, p" Z$ r" z, r; s/ v- l
// just repeated every time. See jmousetrap for more
8 v/ s8 ]- r( u( [9 i: A2 j // complicated schedules.
" T @' N3 y8 D$ w+ D) p
. L h# \% ~8 ?) P modelSchedule = new ScheduleImpl (getZone (), 1);$ A& O6 Q* c6 u7 O* z0 D4 p7 M/ n
modelSchedule.at$createAction (0, modelActions);3 ~# @% ]1 Q. \
" i: J* a5 i7 I9 M( c return this;
& e# V& H7 r- v; Z1 z0 c } |