HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 ~( j1 {. n) S" N% n& B# z& b* P: E. K5 ^7 d
public Object buildActions () {
. H5 ?: L; h3 D- o$ ^3 c3 D% P+ { super.buildActions();
% p, X) N- ?) N3 C$ c! s: J I3 ]
& B- E' K; E3 z* x$ f4 Q3 ~9 C // Create the list of simulation actions. We put these in1 E( H: y, y! [1 i/ }! y
// an action group, because we want these actions to be
/ y8 }; \ w7 ^) T // executed in a specific order, but these steps should
D4 W/ k3 f) i1 C3 G1 ^/ i- [ // take no (simulated) time. The M(foo) means "The message
. F" N" [% W$ S4 [ // called <foo>". You can send a message To a particular- K1 T. K6 C. K ~) Q. v7 P
// object, or ForEach object in a collection.& w# m( R R; Z f
% x1 G, Z/ O; y" k7 I/ l$ f0 [ // Note we update the heatspace in two phases: first run6 ~* t" O" x- c; [0 C$ a
// diffusion, then run "updateWorld" to actually enact the
' I, n. t( p O // changes the heatbugs have made. The ordering here is
8 ]$ I9 @" r# t // significant!
& _( c4 U, e4 X9 o% R
) Q# I- Y- y" L q // Note also, that with the additional6 |! s4 F3 l' M: R4 y) o. H
// `randomizeHeatbugUpdateOrder' Boolean flag we can, B+ h9 y3 l, K: X: ?& L3 `+ C
// randomize the order in which the bugs actually run7 L/ t- ~ `9 N9 `+ |
// their step rule. This has the effect of removing any7 Z3 x3 d! I4 t$ f4 \* b
// systematic bias in the iteration throught the heatbug
% k) M' L9 X$ e' E1 A+ Z+ M // list from timestep to timestep- N4 c6 q0 x8 ~2 K% n
! k7 i7 N$ y( }9 t' D, {. } // By default, all `createActionForEach' modelActions have( t: [" A" S5 G8 j8 q* i1 D
// a default order of `Sequential', which means that the8 N5 u7 j+ `" t: v
// order of iteration through the `heatbugList' will be
: O% }, P3 Y" l0 B3 } W4 p& c& a // identical (assuming the list order is not changed* ]: K$ H; u$ [4 s1 O' k' J, L: T1 y! h
// indirectly by some other process).
|+ C* Y" h- R, a( S8 a0 @ 3 f! t, x( m( E7 f* Y0 O) Z
modelActions = new ActionGroupImpl (getZone ());
! Z1 V& M6 E8 ]/ C1 R% u, O" s9 I, }, h6 ^" M( s
try {
, L' t9 I/ t7 \1 n: K modelActions.createActionTo$message/ T$ q! C6 u9 M }; p% S I* d
(heat, new Selector (heat.getClass (), "stepRule", false));, `$ g. V) q/ O
} catch (Exception e) {7 ~2 J! B2 Q1 S6 V1 x x' f# a2 [
System.err.println ("Exception stepRule: " + e.getMessage ());% y7 J4 M4 Q% r* J/ ^
}
- @2 W5 O& l) T x: ~! Q" e4 T% X$ ]% r
try {
+ [: @3 V" O- b/ t _+ m5 w$ q& C! { Heatbug proto = (Heatbug) heatbugList.get (0);
3 `* w* f: [: g5 W" ?* c, v Selector sel = + ^8 ~. v, a/ d3 A& N
new Selector (proto.getClass (), "heatbugStep", false);, h) d! h3 ^! W& E, n5 a$ A
actionForEach =. E' K0 O7 c9 V4 K
modelActions.createFActionForEachHomogeneous$call
/ C1 M _3 r' l (heatbugList,
& A" c4 D" E$ `+ i new FCallImpl (this, proto, sel,/ m" Y. C7 E0 L: {: g- E
new FArgumentsImpl (this, sel)));
8 Y5 J! D! W) G4 K5 u9 X( ^ } catch (Exception e) {+ L8 Y% P% ^# u& m
e.printStackTrace (System.err);
+ ^* B: f. P3 I6 s+ r# } }
, t) p6 u2 \1 H) I1 K $ `4 W) V/ @( t# v
syncUpdateOrder ();
, {! m, x: `5 W! y+ M# P$ p' e
$ g: K0 C% ^" J try {
* E5 ~" ]# W; ~9 P, `: Q6 j modelActions.createActionTo$message * X1 p7 i* D2 |- _2 d5 f5 a
(heat, new Selector (heat.getClass (), "updateLattice", false));+ E5 |/ j) Y( X
} catch (Exception e) {
! A S4 |9 F; y8 o$ h4 S System.err.println("Exception updateLattice: " + e.getMessage ());/ E4 \1 w+ X5 h, V
}- t& t7 t& f5 M7 Q" ]0 d# n6 ^
0 d9 o+ k; d4 I- X( `5 i' U. N7 Y6 t // Then we create a schedule that executes the
: \' P, c1 q$ P& m# o( v // modelActions. modelActions is an ActionGroup, by itself it5 C# P2 ?: k" \* Z/ @4 T) K& W5 m
// has no notion of time. In order to have it executed in
5 C1 K! }- c; ?- y) h0 L // time, we create a Schedule that says to use the% z- r. L' g+ I6 x
// modelActions ActionGroup at particular times. This
/ X1 V/ G8 b( q' O' M$ e( g // schedule has a repeat interval of 1, it will loop every/ p0 { K3 w Y$ O$ S
// time step. The action is executed at time 0 relative to
7 b3 s# Q/ L4 O) L // the beginning of the loop.3 O1 x0 Z! e( c! T% h6 H; M
8 u. P* Y& P( r Y: G$ h
// This is a simple schedule, with only one action that is
" H, M7 F1 `: M, @) Y+ i // just repeated every time. See jmousetrap for more
: o+ P/ S5 q# N( m+ `' w& J1 C& w // complicated schedules.
. O( T" y: M, f: ]# r$ ?% E
3 O7 y! v' s q/ H6 N9 ~5 x# T1 e modelSchedule = new ScheduleImpl (getZone (), 1);
$ X* P% X% ?: U' T* B modelSchedule.at$createAction (0, modelActions);7 j4 |5 f6 [* e$ N* y
9 c# X- T- L6 H. U) v y return this;7 _6 I2 l& ~9 C5 U4 \7 c
} |