HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" A' O9 U- J& O
/ Z; L9 ?: s3 u5 T% f public Object buildActions () {/ {; y8 N: ]! N0 A- A. `) R
super.buildActions();9 U3 J8 P* v% E. F1 u
+ s% |4 @" Q9 i' Q+ ^ // Create the list of simulation actions. We put these in; s0 k( ~6 y9 Y7 e0 X3 V
// an action group, because we want these actions to be1 T( ^% _ E* v0 I4 v
// executed in a specific order, but these steps should
# u9 d; P$ X4 B3 @# x) j! p, | // take no (simulated) time. The M(foo) means "The message* p" t6 @; ?! r7 g; \: L
// called <foo>". You can send a message To a particular9 c$ q- L3 U _# y
// object, or ForEach object in a collection.1 x8 Z3 C5 P# q6 v. x# O
% u0 ~' ?& f' e# d
// Note we update the heatspace in two phases: first run/ j% r+ z. l, r) s
// diffusion, then run "updateWorld" to actually enact the1 ]5 a4 B% K0 s0 d' o2 S
// changes the heatbugs have made. The ordering here is1 p. ^5 J4 V4 N% i D' k9 ]
// significant!1 m- {) s( Z) H
' v( K+ Y: c. P& ^+ Q& a- C+ @
// Note also, that with the additional
, n* C$ v# |6 ~0 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ e* C4 b) w) `+ W: _ // randomize the order in which the bugs actually run1 g2 B( ~6 P$ o4 ?2 j( X) A6 {
// their step rule. This has the effect of removing any
# }- q1 J9 u; ]1 Q // systematic bias in the iteration throught the heatbug
% W% I5 U" x0 S9 M) O // list from timestep to timestep
6 O/ |4 Y5 I6 \7 G0 U
- N7 i) g8 n+ W: y+ B // By default, all `createActionForEach' modelActions have
, k- F3 T1 o& m# \/ U7 L W // a default order of `Sequential', which means that the% n- I$ h' C$ s& M) b
// order of iteration through the `heatbugList' will be
% ]$ r% }. z$ e // identical (assuming the list order is not changed
$ F/ o- Z5 b/ e7 m* O8 X // indirectly by some other process).
& ^3 T: w# E1 K( ~" d
7 U L- ~' S9 [, A+ @ modelActions = new ActionGroupImpl (getZone ());
e8 b1 F a9 F( n& @9 g/ _% O0 }5 V* z/ F4 E- D
try {
~2 b9 V, H6 N# B- U& O: o modelActions.createActionTo$message/ r+ \3 K* Y& }7 [
(heat, new Selector (heat.getClass (), "stepRule", false));
; j6 s8 P( o8 C& Z& {& H } catch (Exception e) {
5 D! O+ O, j3 P* { ` System.err.println ("Exception stepRule: " + e.getMessage ());
9 |$ A7 A% F) I) e1 H }! ~+ G; v2 }/ R3 c: p- a8 X0 X6 C. G
: R9 e1 O( e' Y+ V) [- Y' n
try {' y! g1 [9 H2 Y" Y: s \
Heatbug proto = (Heatbug) heatbugList.get (0);2 d/ ?- K; g9 n" x9 {
Selector sel = 6 b6 E/ O! C$ q
new Selector (proto.getClass (), "heatbugStep", false); {$ T" j- {; X+ U$ N1 q
actionForEach =
) Q T1 {: @- ~, R" A* \ modelActions.createFActionForEachHomogeneous$call1 j( o3 a6 O7 d" v' l
(heatbugList,
% v$ G8 O2 K. x$ F% g new FCallImpl (this, proto, sel,
9 H2 [6 L s" L( o! Q new FArgumentsImpl (this, sel)));
! @% v" D0 Q+ K- O4 M- L } catch (Exception e) {
4 u) E6 ~* m `! a8 g3 F0 g e.printStackTrace (System.err);/ N2 G) G2 e' t& i% H; o
}
, p" o0 S/ p9 U( \" X- L- V4 v5 F
) g7 G" ~( o9 H5 v6 y4 i3 z syncUpdateOrder ();# m9 i0 ~5 Q/ H5 I4 c2 l) ?
& E u. Q+ f: V) X/ Q try {
+ r, ~$ n& i0 T$ T& V modelActions.createActionTo$message
, [& y4 a6 k A' P (heat, new Selector (heat.getClass (), "updateLattice", false));
+ n% z" o9 H) Z$ \6 |9 a } catch (Exception e) {
( f0 {" c' c. N- D* B6 ]% ~ System.err.println("Exception updateLattice: " + e.getMessage ());
4 p8 v$ g# A" `- O8 @3 { S( C }
7 M" j( W) u/ n2 |6 C& v s " E R; R& I0 b
// Then we create a schedule that executes the+ a! J" x2 x6 Z6 T. |9 {
// modelActions. modelActions is an ActionGroup, by itself it6 H6 n! K+ X1 R% |& j5 p
// has no notion of time. In order to have it executed in
# A: |" u7 R: F" q0 ^0 h // time, we create a Schedule that says to use the% ^2 U& X% H- r8 Y' l
// modelActions ActionGroup at particular times. This
6 n5 Q" Y; F; t& B, `2 L! M // schedule has a repeat interval of 1, it will loop every+ J8 r6 p R8 O3 H* [! W2 T
// time step. The action is executed at time 0 relative to0 s4 k& J1 Y) m
// the beginning of the loop.
( i! i( B- F; x$ O7 ~, d4 e/ h4 W I
// This is a simple schedule, with only one action that is
: U- U- J3 X/ O/ ` // just repeated every time. See jmousetrap for more
3 f2 t& n/ `7 d! q" e // complicated schedules.
: c4 h1 |7 b7 K- U* O" g 4 |( j6 |' o( ~& `# O5 L
modelSchedule = new ScheduleImpl (getZone (), 1);
; ?+ m/ ?( P8 I; S, \ modelSchedule.at$createAction (0, modelActions);1 ?2 b9 z9 e4 ^. H
5 \6 J) \3 n# G5 {' ?/ L* n) T
return this;% ?; c/ ?+ K4 N3 X& r
} |