HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, T2 N2 m6 ?8 e9 ~7 \* ] u1 C
0 J. s9 v( i ^; |. Z
public Object buildActions () {
. R. x% o% k9 v. o3 G super.buildActions();8 I. }9 J! y0 o8 e! F" T
$ m a0 z! q( n3 [
// Create the list of simulation actions. We put these in+ _% Y" g) R2 |! S" o0 G/ ~
// an action group, because we want these actions to be
8 x9 ^7 h, n/ H0 ?' y; X/ Y# f) p // executed in a specific order, but these steps should
2 f( U! R# Z4 V7 f: a // take no (simulated) time. The M(foo) means "The message8 i b& ~! {# ^2 W% P. v4 R
// called <foo>". You can send a message To a particular
1 P$ K) ^5 D" l$ R // object, or ForEach object in a collection.. s9 d1 P1 d( t0 n$ c
5 l0 F% K4 x; z" s5 _
// Note we update the heatspace in two phases: first run3 r& ~; I" C. @
// diffusion, then run "updateWorld" to actually enact the
- J0 r: ?3 a a7 m# w* r8 `3 l9 z // changes the heatbugs have made. The ordering here is
0 @- z4 J0 ~0 k) J // significant!# a$ d6 F& s' B0 _9 H
% @1 Q1 k& i( x# A7 e4 `$ L) f" Y/ v. ` // Note also, that with the additional0 G: x% a" W$ H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
! K v( @- p# v# t // randomize the order in which the bugs actually run
- w9 S1 M% `1 M+ L& g/ h E! f // their step rule. This has the effect of removing any
0 I. m6 f, ?, x5 J4 O; W // systematic bias in the iteration throught the heatbug- V! _, n. d% l9 ^8 u, f
// list from timestep to timestep" e% |) K" r# W3 a5 v
( S4 z: R7 A! V: b" M: d8 C // By default, all `createActionForEach' modelActions have5 w5 h3 F* ?2 q1 v7 d* Q# k
// a default order of `Sequential', which means that the
$ ?7 o% `/ K- m* i // order of iteration through the `heatbugList' will be+ }. l6 K& M2 p0 q8 s
// identical (assuming the list order is not changed
0 g7 j; y9 x0 w& p4 b, G8 E4 ^! L // indirectly by some other process).
8 n5 E3 d8 q+ I3 v
& r1 t/ f# O6 H# { modelActions = new ActionGroupImpl (getZone ());
) _# v, f1 b% i/ r1 f
" F g; W1 _; B6 r; S, J+ h- n try {
4 C4 B4 Q+ ^, s- N: L) ]4 S0 T modelActions.createActionTo$message7 e# s' m3 |+ a
(heat, new Selector (heat.getClass (), "stepRule", false));
+ ?* K7 o% W' j3 W; w: q } catch (Exception e) {
( k: d& s, O% e3 H: E System.err.println ("Exception stepRule: " + e.getMessage ());! l5 u; }8 b' O2 t7 b
}
+ |1 T7 ?- _! I+ {: Q* J. \& {5 }3 R' D% Q* w
try {
! B% {0 [3 t; r3 j: H: d! R Heatbug proto = (Heatbug) heatbugList.get (0);% b* Q6 k! h8 N, _ ?- \
Selector sel = 5 {9 U; w+ i" @. k+ M% `
new Selector (proto.getClass (), "heatbugStep", false);; A" x# ]# x) H
actionForEach =! y" t+ Y) T7 a! N$ y/ \/ g
modelActions.createFActionForEachHomogeneous$call j0 z- E4 D$ T5 D
(heatbugList,; z* _# t9 v3 v5 S8 K
new FCallImpl (this, proto, sel,
4 A0 w; g1 e8 b9 F7 g1 C new FArgumentsImpl (this, sel)));0 h; i. Y( {8 B/ e
} catch (Exception e) {
; j! D5 _) s9 t7 X$ E2 f e.printStackTrace (System.err); k* B8 M! Y! V! T9 f) L
}
0 d- J1 K- l: q( ]. I3 a
1 M8 Q; E1 n' y7 ^' V* n: S& r$ F syncUpdateOrder ();
. X1 ?5 L9 D- p/ _+ R4 \
2 [! Z3 R1 S* r7 W& u try {
8 a' H: J8 E3 u modelActions.createActionTo$message
' m9 D1 @% h+ n7 m (heat, new Selector (heat.getClass (), "updateLattice", false));1 O" d7 u# ] R, e' ?# C6 n- A% p
} catch (Exception e) {
7 k8 x8 f, d' Z7 }9 ~5 T# A2 i; c System.err.println("Exception updateLattice: " + e.getMessage ());
& ^6 U0 e' U* |4 W, P; x }
; _; {6 L0 g. a( B* t 4 m) ?8 h) R0 m% X$ ~
// Then we create a schedule that executes the/ g; J4 z. Z* g( f8 T9 ^) P2 T
// modelActions. modelActions is an ActionGroup, by itself it
. }! f4 T2 Z4 h4 j$ R0 B // has no notion of time. In order to have it executed in! E, r# p) Q4 H9 F) ?
// time, we create a Schedule that says to use the: I0 o& G. ^4 @% S: k
// modelActions ActionGroup at particular times. This
: p+ E4 j$ P, P0 S2 O: R+ I // schedule has a repeat interval of 1, it will loop every0 ?2 x# h3 L1 c% d. U$ y H
// time step. The action is executed at time 0 relative to
" g% U% q; w9 v$ O9 Y. J+ W( W // the beginning of the loop.$ D. X7 A5 v4 ]& T
3 |7 i7 v# F! D8 f+ F
// This is a simple schedule, with only one action that is
( f6 m u% \4 s% d; Q9 X // just repeated every time. See jmousetrap for more' g1 ~& v. O0 R" H- E( V8 _
// complicated schedules.
9 Z" y6 I5 V/ c* t* E ( P4 Y* W0 |4 I/ v5 M
modelSchedule = new ScheduleImpl (getZone (), 1);
. j9 z L( n( w1 p3 g* x: `% C modelSchedule.at$createAction (0, modelActions);$ @3 J" P' A" G- ~2 U! p
1 W. }) q4 h( [# Y9 a" ] return this;
; @# i: B/ l: Y1 b, [- E* ? } |