HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 {3 C1 s4 d7 C$ P* [0 U& A: ^$ V% Z
public Object buildActions () {
( G/ T; N, W/ ^7 O, ^% B* d5 c super.buildActions();
5 p; A* O1 t+ R; q $ ^' N" S% }2 o: b/ v3 b( T S
// Create the list of simulation actions. We put these in
3 P( ]& @ @+ P1 Z0 e // an action group, because we want these actions to be
8 c" h% L. q1 I+ [$ d5 W // executed in a specific order, but these steps should3 _1 w0 \' W* c3 k
// take no (simulated) time. The M(foo) means "The message8 y( \6 J) Y% |$ n
// called <foo>". You can send a message To a particular- o$ C0 c; u& p( q8 q
// object, or ForEach object in a collection.2 z' i8 q7 A. K
3 k" f! _; [. T, w) v, p; @& e! s
// Note we update the heatspace in two phases: first run
! T, S8 Y- x* b2 z2 k // diffusion, then run "updateWorld" to actually enact the
+ J- f Z( a+ R& {& | // changes the heatbugs have made. The ordering here is H2 C: I3 [1 o' }) M+ N% O9 T9 f6 B
// significant!
`' N' m# I" h7 I7 E + X6 r( w/ @; v- j" ^5 W7 {. P
// Note also, that with the additional; x2 ?$ j% k% B7 y& ]5 Z1 a& g
// `randomizeHeatbugUpdateOrder' Boolean flag we can. K6 M$ T/ Y: T+ |! M7 `3 z, P2 j- y
// randomize the order in which the bugs actually run
- o* @ s, t1 f; o( F% P // their step rule. This has the effect of removing any3 b+ j: E1 v4 F; V. | d9 d5 w X
// systematic bias in the iteration throught the heatbug
) l9 e7 W+ A! [; \& Z' J // list from timestep to timestep
Z3 E( N8 o2 @3 x4 D5 Q% O+ O `/ a1 |' u2 W( Q9 N7 R2 m& t
// By default, all `createActionForEach' modelActions have
' Q/ ?- A' }- \9 G; s8 S( m // a default order of `Sequential', which means that the
! g- ^4 _& G0 |+ a# x# c // order of iteration through the `heatbugList' will be( E8 x0 B4 x7 J1 R1 l6 y! J
// identical (assuming the list order is not changed& o I; q( E5 U; q( K. z9 q: e
// indirectly by some other process).
$ O3 u+ O/ B' @ U
* k; @7 ]) `: Q modelActions = new ActionGroupImpl (getZone ());9 n }6 H2 ^, q' N6 [. a% m
$ C8 F1 x8 A* A2 t2 w9 S
try {" P! i: K4 c/ _) x( e0 \+ k5 J+ _
modelActions.createActionTo$message4 g0 d, w4 j5 d' }, l! ^- D0 L
(heat, new Selector (heat.getClass (), "stepRule", false));
) V7 S0 U% c' ]# b, o# w" f& w2 y } catch (Exception e) {( P3 n" V: a) ?" C; x' ^' C
System.err.println ("Exception stepRule: " + e.getMessage ());
; h1 J3 S$ B; e( n: S4 ~" D }
* O( R8 N, @+ N5 y/ g( a% x% M
A0 L4 }' h L6 Z5 w try {
+ d2 u( K3 I. {, a3 X Heatbug proto = (Heatbug) heatbugList.get (0);" V' S$ q% _/ s6 e, x
Selector sel =
' y* I1 l7 k( c" G# j* W new Selector (proto.getClass (), "heatbugStep", false);. }: y/ m7 }6 R5 _: ?) v
actionForEach =- }2 h3 r0 D. a* r6 G
modelActions.createFActionForEachHomogeneous$call
0 ?+ V D9 W) T8 ^3 C; R, \ (heatbugList,
8 c: h( c0 f ~: m1 l1 I new FCallImpl (this, proto, sel,
3 L" ^0 W6 H9 V ~, P( n! ~" s new FArgumentsImpl (this, sel)));
" _5 w* [+ q9 h Y } catch (Exception e) {) z$ M( Y, z/ R9 _) R' ?- B
e.printStackTrace (System.err);
: h4 s+ v8 W" C6 }- F ^$ c }
& t5 A& l! L# x m6 h f % r/ v* s6 ]5 M3 W* \
syncUpdateOrder ();
1 k, Q0 N5 g$ ?6 W, x1 \! X4 y7 b* b0 O$ \: }/ T4 r
try {- Z: i: w$ f: i2 e# v
modelActions.createActionTo$message
$ B8 X0 z y: W) G/ C$ s' A9 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
' W i5 o- Z2 o$ F3 D } catch (Exception e) {
2 }7 n% [- I2 p$ ` System.err.println("Exception updateLattice: " + e.getMessage ());5 s+ u5 f$ S* [4 n8 p3 s; R
}6 t. K, P# L" r- G) R- d
4 i$ D3 K; _# k0 J$ F
// Then we create a schedule that executes the& x- K" ^& C5 n; V; Q% ~
// modelActions. modelActions is an ActionGroup, by itself it
0 F; f6 I1 Z+ N, J2 { // has no notion of time. In order to have it executed in
. _" o$ b8 n# E // time, we create a Schedule that says to use the
$ ?. @0 X3 K4 E2 t( v) W: t" h( e // modelActions ActionGroup at particular times. This" R4 Y/ V+ U9 o; P+ k& F1 N" h2 \
// schedule has a repeat interval of 1, it will loop every/ L6 {9 s7 A& v$ ]; ]; b: a
// time step. The action is executed at time 0 relative to3 u p0 q! u( }' D
// the beginning of the loop.* ?$ \, _. V% \/ C- X: `
* v" ?4 r( M" n; z& o4 F/ _
// This is a simple schedule, with only one action that is
1 Z* a J& D* `( L! w6 F // just repeated every time. See jmousetrap for more4 C g9 y. `0 {# Q
// complicated schedules.$ J2 e( Y; ?" y6 A
2 b( U. u0 Z0 w3 x+ K& ?' C modelSchedule = new ScheduleImpl (getZone (), 1);
5 Y6 Y1 B8 Z9 p5 }2 s/ h. C modelSchedule.at$createAction (0, modelActions);5 }1 I' R" n: C- E9 ^1 M
( [# {7 c1 N* {5 U6 J' n9 k return this;
' |- f) D% I& o6 ^2 d; b } |