HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% E* }; b7 O5 P) |: _' R$ p# S2 [% C9 \. w1 k
public Object buildActions () {
1 c& ^+ y' D2 a7 Q8 y: q0 h super.buildActions();. ?! j5 J% w4 w. F. Y; o+ J
. h$ E4 S- A# ] // Create the list of simulation actions. We put these in5 C6 t9 w: T9 w- _/ ^1 | U. o
// an action group, because we want these actions to be Y% N( M8 W, D! ]" [9 w
// executed in a specific order, but these steps should. A& B7 k5 |4 `" p' v R8 e
// take no (simulated) time. The M(foo) means "The message
6 s0 N! A0 I3 ]( Q6 i // called <foo>". You can send a message To a particular
" ?6 ^" q- G. j# M, d8 j // object, or ForEach object in a collection.
! z9 ]; T% f+ @9 O 5 O7 C b5 k2 d
// Note we update the heatspace in two phases: first run
, \" C1 Q U: o; T3 [ // diffusion, then run "updateWorld" to actually enact the
4 f! J) g$ M9 k$ u // changes the heatbugs have made. The ordering here is' O8 n+ D) S- t
// significant!
& O3 ]/ t f) ~
- u5 a( [6 q' \+ M( C, n8 X // Note also, that with the additional
3 f# L1 r; n5 R. p1 u$ Y4 V6 _ // `randomizeHeatbugUpdateOrder' Boolean flag we can1 O) c6 K2 p( x% b* E
// randomize the order in which the bugs actually run
4 z7 k) M/ K! b$ ?, m1 x // their step rule. This has the effect of removing any1 d: D% `0 _. U1 E% l0 y
// systematic bias in the iteration throught the heatbug9 g! b4 v5 |; k0 p+ i
// list from timestep to timestep; Z/ M, i7 o; I% z6 p" q6 W1 n
2 H i; N1 U1 A8 t // By default, all `createActionForEach' modelActions have
3 f: m+ H9 r3 ~; P/ K: r // a default order of `Sequential', which means that the
" D& n- f7 G- k* G g! Q // order of iteration through the `heatbugList' will be
- L c& h# A' Z8 A // identical (assuming the list order is not changed
9 P$ J* _ {1 M5 M2 }5 O; Y0 d+ q) s // indirectly by some other process).. \. R* e% M8 q" Z; L$ O$ ^
9 k I/ O$ D: f/ W$ ]) y5 Q modelActions = new ActionGroupImpl (getZone ());
) I1 K' T o7 P: w
: W7 i+ y' n }) m try {
7 `$ F6 E! v% S, G) H) D modelActions.createActionTo$message
$ U4 }. r2 k$ s# Q/ R (heat, new Selector (heat.getClass (), "stepRule", false));
! N3 M, e8 P$ y0 ]3 B } catch (Exception e) {
( P2 L. e4 T& H0 c# i System.err.println ("Exception stepRule: " + e.getMessage ());% {( ~ q( q+ h6 `% G1 J
}. a: D) C& a# Q; r8 o
& i0 R; f* K) @) O; q
try {
E( I2 I' t w+ T' S/ W Heatbug proto = (Heatbug) heatbugList.get (0);
* O A9 N2 G2 F7 u Selector sel =
x2 E2 Q. r$ I3 a new Selector (proto.getClass (), "heatbugStep", false);
% h8 r0 c% i* M9 `/ D5 X" l5 G actionForEach =6 D# f& x( Z# M0 {( T
modelActions.createFActionForEachHomogeneous$call' A* }3 B8 L1 Z
(heatbugList,
! K4 ~- P: n. s7 U' E0 }; C new FCallImpl (this, proto, sel,3 c) L7 `0 X, r& x: L, b1 ~
new FArgumentsImpl (this, sel)));& Y, @3 A. E: h5 D! S7 ^" A" N K {
} catch (Exception e) {
1 v8 x. L2 E! {! m6 S* V e.printStackTrace (System.err);
7 w; T2 @& C$ s9 L8 k$ A }
' p E$ _# f: ?3 }& o
: p0 x' |0 v/ J- \1 u, ~9 N; M syncUpdateOrder ();
9 }2 c: k6 V$ V, r( l1 k. ^- {
/ S' g; M8 i' f( b' m try {
! m: |! ?' c0 s7 z1 r modelActions.createActionTo$message
# D" l6 E. U' }2 O5 l3 f (heat, new Selector (heat.getClass (), "updateLattice", false));
w+ X# B c& b ?0 M } catch (Exception e) {' L+ R% m# k; m2 L) H
System.err.println("Exception updateLattice: " + e.getMessage ());
, K$ T# r7 n8 l; ]; S }$ p1 V0 Z% {& r5 t% b2 v+ Y- E' n
$ y. X/ v G$ k6 R! _) c+ k // Then we create a schedule that executes the* P5 |2 }% A& D! g
// modelActions. modelActions is an ActionGroup, by itself it( I9 w: }& L! S {1 v2 k: z$ u
// has no notion of time. In order to have it executed in" [/ R+ H* h# d2 n, {8 Y
// time, we create a Schedule that says to use the2 m& q4 M8 ]; F2 n' L7 w6 ^7 G8 U) `
// modelActions ActionGroup at particular times. This
2 Z8 v& E7 r$ ] // schedule has a repeat interval of 1, it will loop every2 J4 ]) u0 w5 T& z4 a
// time step. The action is executed at time 0 relative to
6 u* ^8 G$ ]' ^4 U8 [' } // the beginning of the loop.( y8 ~# j3 u, f; m! a
. C1 z* r& P) ], u Z { // This is a simple schedule, with only one action that is
3 r/ ~! u1 u! t // just repeated every time. See jmousetrap for more
% l# g0 G7 D5 F& N( J3 m // complicated schedules.6 d) ?. e8 |+ G; L8 y# M A
( e+ B. E1 B @+ x* w modelSchedule = new ScheduleImpl (getZone (), 1);& g, G3 w! p) [* V
modelSchedule.at$createAction (0, modelActions);
; V' t, `) d, V1 f
" J6 p) S$ `+ m. K return this;6 V9 G" F# @& `+ M+ \* ]
} |