HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! j% D9 m( Q3 z5 Y
$ m+ f5 H! [. t; Y6 t2 m/ ~6 e7 q public Object buildActions () {' f3 y2 t. c* X4 j2 @
super.buildActions();
- B3 ^$ t: K1 F9 J 7 l: E+ ]* k! v0 k& u& I
// Create the list of simulation actions. We put these in, l9 v4 L: q) `1 h
// an action group, because we want these actions to be- y5 W; s6 g9 I3 Y" ]$ ^. `
// executed in a specific order, but these steps should% D; a( U$ U9 L1 W5 D
// take no (simulated) time. The M(foo) means "The message
' z$ ]8 l% f1 n, Q! J8 X // called <foo>". You can send a message To a particular) z9 U* X+ C" y' G
// object, or ForEach object in a collection.
$ n- i, [6 g5 N$ c ~: j5 @ 2 c+ T9 k' X5 C
// Note we update the heatspace in two phases: first run- C. d+ M, _6 Z4 ~
// diffusion, then run "updateWorld" to actually enact the
6 n F" v" g3 _; Q // changes the heatbugs have made. The ordering here is( M; m; C- T: x# W2 {
// significant!
4 u& f2 q! q8 | * j. N+ M4 ` m; W
// Note also, that with the additional3 F3 z; D9 n9 d Y( U* y9 Y% V- M
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 R* {- z) v0 E1 {6 p# ~) L, X3 b // randomize the order in which the bugs actually run
& l1 z2 y a M6 z; Y // their step rule. This has the effect of removing any
0 s% }$ i# d+ W# w // systematic bias in the iteration throught the heatbug
9 {8 p" ~9 @2 |, W [2 q1 ?, ~ // list from timestep to timestep- i" Q/ b1 G+ I7 ~3 W9 \8 P3 g
M) L# N5 { [# G9 Z" f/ b
// By default, all `createActionForEach' modelActions have
9 x9 S" _! o5 K2 h" q // a default order of `Sequential', which means that the
1 W* b0 }8 s7 p // order of iteration through the `heatbugList' will be) g/ ~8 Y8 P" l" A& d9 v" g$ }
// identical (assuming the list order is not changed: z' C6 Z/ j6 [' Z$ c# P
// indirectly by some other process).4 }, M* P9 L$ S1 ?$ d& j
5 v. O+ P w: [0 h' r# T2 K modelActions = new ActionGroupImpl (getZone ());! x% y5 V7 R$ i7 {+ e. V
8 C8 f/ j. y+ t& Q0 H, @3 x) l9 e7 ?
try {
3 G U' a9 ^$ Z- J! U% s5 C modelActions.createActionTo$message! ^% [ G6 `3 p
(heat, new Selector (heat.getClass (), "stepRule", false));6 f% y4 I/ O- O; i# u5 |
} catch (Exception e) {
1 t( b8 O* u+ W# T+ d System.err.println ("Exception stepRule: " + e.getMessage ());
! n4 K* h/ ?. Y }* N+ u5 |% _1 l0 r R1 V: b
* [, y" \' n9 D7 q) L6 e try {
$ Q) J4 B( c5 |9 C. @ Heatbug proto = (Heatbug) heatbugList.get (0);- v u) K( R+ y9 Y- t
Selector sel =
6 E* Z4 Y: t, l# w new Selector (proto.getClass (), "heatbugStep", false);
2 K: k/ d: n8 p \ O/ ~ actionForEach =( w" r% K' E! L7 F8 e7 {
modelActions.createFActionForEachHomogeneous$call" j y2 C- W, V& w/ W5 r) c J
(heatbugList,3 A0 _. X5 G- A2 W
new FCallImpl (this, proto, sel,0 d6 B# v* z! P! W* f7 S; F1 J
new FArgumentsImpl (this, sel))); F+ z7 |) N5 [+ {/ |( k: Q6 Q9 }
} catch (Exception e) {
# o, ]8 h9 f: K# A2 e7 J e.printStackTrace (System.err);
8 q8 S% u3 J# D! ` }
. l7 D# l! Z/ H3 ^0 e: |
1 W, R" ~2 q2 ]5 \. E ? syncUpdateOrder ();
) g7 }" ~* t: T. N |- d
* b2 Q4 V% [% p1 l: m7 ^ try {- O. t ~; D( b) O2 q3 ?2 _
modelActions.createActionTo$message 2 g) r6 ~8 f+ z) J$ I
(heat, new Selector (heat.getClass (), "updateLattice", false));
& v# Z% u* z4 E. Q; D1 r1 V+ ]+ \ } catch (Exception e) {9 x2 m7 o: q( B; A* _
System.err.println("Exception updateLattice: " + e.getMessage ());
4 J4 o! s f0 y& P9 \4 X: L }6 c6 i1 @% @ q! H5 \3 Z% F$ s/ L
$ q4 I. b4 m, c( X. T6 v
// Then we create a schedule that executes the4 D; |( y `. J% I, t z1 M
// modelActions. modelActions is an ActionGroup, by itself it
# ]4 T7 S# W7 c! F // has no notion of time. In order to have it executed in
. I9 Z& q# R b3 a" D) Q8 p% A // time, we create a Schedule that says to use the
3 t( H9 I( o. `. `5 S // modelActions ActionGroup at particular times. This
% \. e. j2 R) J, e. _" r // schedule has a repeat interval of 1, it will loop every( n/ M$ j$ h2 I, }5 z0 R
// time step. The action is executed at time 0 relative to% ?! x$ y* ^) o6 E% `
// the beginning of the loop.+ J; ~5 E. M c5 \) F
4 s% c1 x4 D; i4 O7 F& Y // This is a simple schedule, with only one action that is
: p. U1 }8 d- D& q/ K+ R // just repeated every time. See jmousetrap for more) O% @! U1 e6 O4 z" g! X# |
// complicated schedules., `5 u. [: D+ K9 I2 `% Z M5 W9 Q
; J1 ]# Z. [4 Y/ X5 K8 q
modelSchedule = new ScheduleImpl (getZone (), 1);, D( b( Q, ~( s# H
modelSchedule.at$createAction (0, modelActions);* V* S5 N' p4 T( q2 R. K1 c% _
7 M0 v9 N& q8 w) V5 z5 n$ h return this; _4 e7 }7 c1 n# }% Q) d
} |