HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ d. Q/ X( K8 i9 o
3 d0 @; M e, x5 s% J
public Object buildActions () {& }1 ?' ^8 q! `/ f# z
super.buildActions();
8 }7 _0 I& v; y; R. M5 U* u
: W4 q5 p- [7 b. c2 }& Q' M0 Y. \ // Create the list of simulation actions. We put these in9 M; ^1 P; x3 [
// an action group, because we want these actions to be
! i( o$ H) t U; J F // executed in a specific order, but these steps should( T9 M. \; p3 T" S' S
// take no (simulated) time. The M(foo) means "The message
" X; i: V+ }5 h% y! m // called <foo>". You can send a message To a particular
5 \6 v) W3 l9 c0 z8 H* ~/ x, { // object, or ForEach object in a collection." Y1 H4 |& e5 Y
5 M- d2 V/ w; Y0 P* X( A. P: X // Note we update the heatspace in two phases: first run
( L; A) R: c* e" q. t `2 U& ^ // diffusion, then run "updateWorld" to actually enact the9 B% i# D: Q9 X$ J
// changes the heatbugs have made. The ordering here is
! j# Y P$ A& ^0 H; p$ @5 F // significant!
, v. l( R7 L5 H# P
1 V* E3 w* X' v2 d // Note also, that with the additional
8 z/ C/ M7 Y- M% k# x // `randomizeHeatbugUpdateOrder' Boolean flag we can
( V% x( I, Z5 _ // randomize the order in which the bugs actually run) W9 w' Z8 d# F! b0 U3 h8 h/ f5 O
// their step rule. This has the effect of removing any# |" j& B2 F% F$ r
// systematic bias in the iteration throught the heatbug1 w6 W( K5 A% C, j2 C* A
// list from timestep to timestep' t6 ?; \( b3 C
1 N) Y# {1 d( k2 {
// By default, all `createActionForEach' modelActions have+ [1 ~, B2 g6 h" i8 G6 B, O
// a default order of `Sequential', which means that the
5 b- V) [2 T% l // order of iteration through the `heatbugList' will be
* h- n. p9 C% X M [, i // identical (assuming the list order is not changed9 i! W1 m; ?+ V' I, [, u
// indirectly by some other process).
$ v& C+ Q" ?; ]0 v) Y8 D! Y
7 D/ T9 g' h# ~ v2 a$ j modelActions = new ActionGroupImpl (getZone ());# L' H3 H5 w5 ~2 f! k% ^( J: R
- g% {6 `2 v0 I7 Y9 M try {
6 m; m6 m' y6 `$ G/ m( a modelActions.createActionTo$message; B C- `0 y* W$ S
(heat, new Selector (heat.getClass (), "stepRule", false));
7 L8 K5 u, ]3 T/ W } catch (Exception e) {; ?. t) H9 g/ B! m9 ~$ R
System.err.println ("Exception stepRule: " + e.getMessage ());
9 `* a6 }0 Y% {% s }
( B Z* s! g2 _ q4 }0 s1 W) m) R: f& W, H/ [! C1 D; b
try {. i8 h) V8 n, B$ y0 B' q5 Y
Heatbug proto = (Heatbug) heatbugList.get (0);
: J7 U- \' Q/ Z; v# u2 { Selector sel =
! `7 S7 \! \# f% g6 c new Selector (proto.getClass (), "heatbugStep", false);9 _( A) s5 [" `7 P9 }3 x" o
actionForEach =4 n+ K" s0 Y% [/ J/ e( j
modelActions.createFActionForEachHomogeneous$call% p1 h" Z" h9 v: r' r/ N
(heatbugList,
: \" d* _( A/ `! @, G new FCallImpl (this, proto, sel,
! r! i9 ?! s8 q4 D$ D new FArgumentsImpl (this, sel)));1 x7 K4 Q1 M/ j8 D
} catch (Exception e) {
, z& O. Q+ J( b' }% n" N* }, u e.printStackTrace (System.err);$ w9 T3 E0 X; H5 s {
}# ^; x1 z! J* a2 ^1 R) k) @6 `
, p' w ]- ?- ^
syncUpdateOrder ();% C" C4 d M1 m" X: A: e4 J g! q
. A0 t2 d0 l% C0 X% f! M. v7 e try {+ i% c. X* P9 L) O- R* U/ Y
modelActions.createActionTo$message
2 }3 V$ W: g$ I! s- g3 j* w (heat, new Selector (heat.getClass (), "updateLattice", false));$ o0 D4 o y- ?* H* ~
} catch (Exception e) {1 ^, V$ U+ [: S9 s" f, l4 J9 h% i
System.err.println("Exception updateLattice: " + e.getMessage ());# J! D2 `+ _! Y2 ?" D4 e: T
}0 L8 c/ r* P+ _. }
) V4 m" e- c+ C/ n( P* b: o* b
// Then we create a schedule that executes the
2 }& r& O% U' W, Q/ t0 }$ N // modelActions. modelActions is an ActionGroup, by itself it8 d' u. W6 t) d# s1 m
// has no notion of time. In order to have it executed in# o/ c( q5 V# U0 ]
// time, we create a Schedule that says to use the
# f* f P) \2 f3 O+ S/ i; _ // modelActions ActionGroup at particular times. This9 _( D1 G6 y' n' l/ q1 I1 S
// schedule has a repeat interval of 1, it will loop every
j" S3 J- l. I5 t" @! u8 T7 n // time step. The action is executed at time 0 relative to
: p& {+ A, S, W# ^4 J- D" R; M // the beginning of the loop.
0 U$ D: ~3 g4 i0 v$ Z- ]! E
; I7 i7 C$ x) q, y // This is a simple schedule, with only one action that is
: J8 o; r: S- t( B) r! e // just repeated every time. See jmousetrap for more
; X, V2 i. e# v- @: L // complicated schedules.
3 W! X* j! C0 U# a . h7 e7 y' M1 d# ?6 G6 I& Q3 Y$ }
modelSchedule = new ScheduleImpl (getZone (), 1);
) }- U* h2 [6 }7 S9 G3 g; D modelSchedule.at$createAction (0, modelActions);; F+ h" `% G( }( N; n& }4 B5 q
( h. y R! @+ D: K
return this;
+ T- l( D" D2 R8 x# v } |