HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( K4 N8 S5 X* E, Q
# N( B0 t4 ?$ E' q5 h3 A public Object buildActions () {
4 \% h7 K c' | super.buildActions();4 g. X# v& \/ a( D# U
0 j- I( ~9 N; P( {- J: ~
// Create the list of simulation actions. We put these in
- l4 ^5 l! [2 z! e8 {/ }2 o // an action group, because we want these actions to be
& M: J4 h6 R8 V1 I& H# {+ {. X- _ // executed in a specific order, but these steps should
: z- {7 e0 u( H4 I' c' H // take no (simulated) time. The M(foo) means "The message
7 s8 C& V! Z! c( O2 p, a // called <foo>". You can send a message To a particular
% j$ f* j2 \! `; \+ i0 v // object, or ForEach object in a collection.$ d2 c2 n# F9 b) l9 B" z1 M6 ?( g; B
8 s% I, s- x/ b( M
// Note we update the heatspace in two phases: first run* O. ?* C6 Z* i0 T4 S9 g( F; x. m8 ^* g) Y
// diffusion, then run "updateWorld" to actually enact the- F1 x2 w3 O+ ?" u& l8 y( l- i; M
// changes the heatbugs have made. The ordering here is
( R' S6 ~) x1 j! | // significant!
! s$ k% O U9 i
' f, N5 v( Z$ B2 i8 Y // Note also, that with the additional
) w" D0 h, U0 P! h; Q6 [8 H% X // `randomizeHeatbugUpdateOrder' Boolean flag we can. W1 j& e3 H9 J0 ^0 U* O
// randomize the order in which the bugs actually run
' K1 |( f C8 H // their step rule. This has the effect of removing any4 k0 C- D: ^3 R
// systematic bias in the iteration throught the heatbug
3 b+ V: \, s8 J1 { // list from timestep to timestep
N! H- Q. W$ _5 o - S0 H f- U1 ~
// By default, all `createActionForEach' modelActions have! n$ Z X: W0 d- L/ B( N3 k
// a default order of `Sequential', which means that the
0 h* ?4 W5 t0 B6 B8 m8 u // order of iteration through the `heatbugList' will be9 |5 ]+ X" `( u# ]# W4 W; d4 w
// identical (assuming the list order is not changed' q6 Z O* \8 }* a1 n
// indirectly by some other process).0 s ^" |) J$ K/ H+ A" q- L: {" C: c
7 q0 J0 N* W, S- | l5 S( {) j/ A modelActions = new ActionGroupImpl (getZone ());
2 l6 I$ S& X7 Y+ W m6 e8 W9 Y: ` f: k+ G: ?
try {
& G: p) v$ M2 l modelActions.createActionTo$message
, C3 \2 B" k4 t2 { (heat, new Selector (heat.getClass (), "stepRule", false));$ X0 Z9 R' R4 y9 B
} catch (Exception e) {% _8 `; m& \0 D8 ~. [/ a8 }+ {
System.err.println ("Exception stepRule: " + e.getMessage ());
7 j0 @3 T+ `5 h+ c# C9 f6 z% ~7 ` }6 x2 Q+ v2 [. o! e# @# I
6 H+ {- _) b8 y# s% ~/ g+ S
try {0 L6 |& e. M# z/ B- L" n3 O# e
Heatbug proto = (Heatbug) heatbugList.get (0);' Y( F7 C! @! c o3 h
Selector sel =
3 _: R" B% }" y# }; ~! q new Selector (proto.getClass (), "heatbugStep", false);
2 f6 l* j# p( r# r. V actionForEach =" O5 @8 @/ l) K
modelActions.createFActionForEachHomogeneous$call; Y" I" u) ?# V* ^) W7 q7 j# t6 ?
(heatbugList,6 S) J8 K5 r2 w7 ?$ @- z2 I% |
new FCallImpl (this, proto, sel,
: n2 ?8 U7 R7 Y, @4 Y6 ^ new FArgumentsImpl (this, sel)));
' v4 ? @/ Q, A/ G7 h- m3 d } catch (Exception e) {0 D7 s1 A1 r! q7 Q/ f
e.printStackTrace (System.err);- U6 m9 v& W w# } o1 B8 \
}
/ L1 x2 H; z9 x, d3 `
6 n/ w$ {: b* ~* H3 ^ syncUpdateOrder ();+ t% K5 {3 d. L7 j" F }
: Q0 [ C- R( G8 B1 ` try {$ L3 f! z! t+ M, A
modelActions.createActionTo$message # S7 q4 a- x5 e2 t: l p" J
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 L* X! }& d2 y* G4 r } catch (Exception e) {! ^/ L0 z5 t }- o4 p7 \
System.err.println("Exception updateLattice: " + e.getMessage ());
% e/ {+ T! ^: f3 [0 w5 i" \ }
" H. J3 C* g7 D- B $ h$ t9 m* Z( b- u1 W4 `+ s. F. A, y
// Then we create a schedule that executes the
% P% A0 b& }3 G6 ? // modelActions. modelActions is an ActionGroup, by itself it
& C$ C( e0 h. j. z g7 o // has no notion of time. In order to have it executed in, H1 M4 P$ Z* X+ i4 d* u- R$ e2 z
// time, we create a Schedule that says to use the; V0 l$ u- P1 k6 k; u$ D
// modelActions ActionGroup at particular times. This9 k7 i9 W% y+ S8 s
// schedule has a repeat interval of 1, it will loop every9 I- H+ D4 ~8 @5 K
// time step. The action is executed at time 0 relative to/ `! v( c# n d$ o! ~
// the beginning of the loop.5 [* c9 E3 i; [8 r4 g i) q4 E6 ^
# ]3 V4 w1 k. A# p
// This is a simple schedule, with only one action that is
) |: {) T4 Z: r5 i* c // just repeated every time. See jmousetrap for more' L) v: S/ A1 V6 {5 D
// complicated schedules.6 E% l4 w0 u6 h7 c: a3 A$ R
# L) ?1 J/ L/ b( w0 f- |* k* l n: [
modelSchedule = new ScheduleImpl (getZone (), 1);3 w1 L1 K/ }0 r6 Q% S; ]
modelSchedule.at$createAction (0, modelActions);
, T& I* Y8 ]1 _; P) V0 Z
* Z7 A; J% M6 @% V: `( v return this;
1 ^, {& f- J$ T7 w4 x } |