HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
F) `6 X, i, l. I& `
5 r" \9 |, @3 m5 a$ {* I public Object buildActions () {
, y) d2 P/ K* L* f4 a; ~) H1 ]. m super.buildActions();% y; R7 S+ y, ?7 T u5 {6 T8 C* D
) m& A9 b- l- \% I e9 b/ Q& s // Create the list of simulation actions. We put these in
~% |! M0 H; {4 B0 r m- z // an action group, because we want these actions to be G( i! \& \' e" C
// executed in a specific order, but these steps should0 k; q6 |1 V8 A8 t
// take no (simulated) time. The M(foo) means "The message; F8 @% T* W9 n9 F9 H7 v% Q9 d
// called <foo>". You can send a message To a particular
- _- W& }" F# X) T // object, or ForEach object in a collection.
# [8 v$ }# J$ H" n+ f+ n
* X* R% g: x9 ^/ h/ D& y // Note we update the heatspace in two phases: first run
$ h1 P' Z& z$ P/ o" u* n) C; l) o7 \ // diffusion, then run "updateWorld" to actually enact the
8 r6 [% G( v) `2 g7 T' O // changes the heatbugs have made. The ordering here is/ }7 Z* w3 ?+ T2 J' u7 V
// significant!
/ L z' P w; u7 t# O7 K / _/ j9 @' Y" Z. l6 r# r4 d
// Note also, that with the additional
( E W x: \; U0 m' s0 V/ G // `randomizeHeatbugUpdateOrder' Boolean flag we can& P8 U4 K8 X# S; f9 ~0 l4 W7 |
// randomize the order in which the bugs actually run" C6 z5 N8 P2 s) i; G5 d
// their step rule. This has the effect of removing any" D0 J3 o* F0 B! A& S0 A; I4 h
// systematic bias in the iteration throught the heatbug' Y2 W4 ?1 Q8 M2 @4 w8 X! k
// list from timestep to timestep
+ _" Y& o6 k( k& Q
$ [' e6 Q6 |( X% u1 @, l/ o // By default, all `createActionForEach' modelActions have
! w/ R$ t2 a; C- _) g; } // a default order of `Sequential', which means that the
' l" R) J5 D8 R$ x+ w1 ~ // order of iteration through the `heatbugList' will be4 h5 H3 v4 a4 @+ @9 P3 C: A
// identical (assuming the list order is not changed2 K" e" G& s) E, y7 q7 a9 I& d% b; R
// indirectly by some other process).; u: c: n) W$ Q
3 Y# O: O( i2 s/ |$ t modelActions = new ActionGroupImpl (getZone ());9 M1 F1 E2 d: B$ k. A
! }, Q- k# x9 i0 P: m try {
7 b- h6 ~ w6 g7 E+ D/ M modelActions.createActionTo$message
- F- q6 B3 X$ |4 w (heat, new Selector (heat.getClass (), "stepRule", false));
$ D4 i) S8 |& E } catch (Exception e) {( k" Z7 z& J; F, L4 f! g
System.err.println ("Exception stepRule: " + e.getMessage ());
6 \4 ~( b* d8 d+ V% O: E }
5 W+ H0 |; K p I- D+ @3 b6 e8 g1 t1 e- q2 X4 W% |" A0 r
try {4 s3 W4 B; ] u% L& B/ O4 e" M# M
Heatbug proto = (Heatbug) heatbugList.get (0);
' m0 z- l3 z7 Q. K Selector sel =
G# I& J* _: j" d9 t6 p, Z- T new Selector (proto.getClass (), "heatbugStep", false);0 Z. V* w8 X& v! h a
actionForEach =" q. U" ~6 C z# Z" Y- a
modelActions.createFActionForEachHomogeneous$call$ q6 `( ` f6 b5 W: k# j2 p" A, I
(heatbugList,# Y+ W9 f1 _8 b8 H! L
new FCallImpl (this, proto, sel,7 @* c, ?1 {6 \4 W1 c$ j
new FArgumentsImpl (this, sel)));
' z5 o) Y) `" w7 q' O7 o } catch (Exception e) {
$ ]2 c. b! @$ I0 ^5 ]0 r9 m" P$ x e.printStackTrace (System.err);
; Y" H v7 P2 }: P+ h }9 J% q. |* F& n% _) m' r
# K, q/ O( t. t+ l
syncUpdateOrder ();; V; W2 n0 d/ F* e t; @2 _
/ O4 N! d+ ?1 \. C3 \5 o' l/ q try {" q8 i+ K# y. c, l. Z
modelActions.createActionTo$message
5 f1 }+ Y& G: N) U; }# E, ] (heat, new Selector (heat.getClass (), "updateLattice", false));8 ]/ p5 C; h5 K5 A0 n% }$ \. l6 ?
} catch (Exception e) { t7 Z; O! y2 k# U1 c F
System.err.println("Exception updateLattice: " + e.getMessage ());+ b8 Q7 P: V- B z" M# v3 b3 c
}0 j( v5 ~ y4 a
* l8 z! p: W7 A1 d! R* r // Then we create a schedule that executes the3 j8 w: F7 h6 a
// modelActions. modelActions is an ActionGroup, by itself it2 _+ `2 B) G# \' a2 m! h
// has no notion of time. In order to have it executed in. b' c6 A; u- q0 S- F5 @8 X
// time, we create a Schedule that says to use the
" L% J. Q* r7 k // modelActions ActionGroup at particular times. This
: J) k3 m2 v, v6 l: x1 W // schedule has a repeat interval of 1, it will loop every
9 L% O9 M9 M( @6 x8 r8 g7 B // time step. The action is executed at time 0 relative to6 A4 ~4 f6 _& {5 v+ ?: E9 U: j
// the beginning of the loop.
% t( o* j- s( E
6 F3 x' k5 i6 @6 `9 C' Y0 m7 |8 [2 w // This is a simple schedule, with only one action that is
7 f7 d. L3 k) M# ^ // just repeated every time. See jmousetrap for more
. Y- Z$ Y$ }* w8 L! S* J: ] // complicated schedules.
/ _* f% A- V5 s+ c* \
9 e9 E3 b; |$ l% d" U5 C, j modelSchedule = new ScheduleImpl (getZone (), 1);5 I4 X$ g2 [" b6 _$ ` {; B( u# I
modelSchedule.at$createAction (0, modelActions);
+ o+ f, ^0 o% U! b
: U( V, ~. u( E2 ]- O/ \2 { return this;
5 t9 Z F6 N' B) [/ E! p } |