HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ b0 e! Y2 A5 j! `1 {" h( q1 j- R8 c7 Y: ?$ F
public Object buildActions () {: h `: L) N7 A
super.buildActions();
1 w$ n, E j! f+ w: E, g. { ! S, g7 e" L4 H$ _/ ~6 E* [
// Create the list of simulation actions. We put these in. I6 Z+ T: ^, I; E
// an action group, because we want these actions to be
) t/ c6 T" h3 O6 [/ U/ d, K' u // executed in a specific order, but these steps should
5 U( U3 |/ i* O b* ] // take no (simulated) time. The M(foo) means "The message* J. z0 w* N1 H! x
// called <foo>". You can send a message To a particular3 ~6 l' m1 Z6 G' z
// object, or ForEach object in a collection.
: ^# o& l! J# C( h F5 ?" [3 d. a
4 @- C& w$ e$ C- V4 m3 n }: M // Note we update the heatspace in two phases: first run8 e0 b* c3 x' }; {1 K
// diffusion, then run "updateWorld" to actually enact the
% J9 A' C+ U s. V7 r" t+ S // changes the heatbugs have made. The ordering here is, |8 R* A9 g. S
// significant!
$ j& Z6 {9 B, S& \( b
6 J+ @) G+ Z4 b5 R+ O8 a" d // Note also, that with the additional
# z8 |8 i1 q7 ?9 B2 K5 } // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 [: Z3 F6 l& ^+ O0 G! H- T // randomize the order in which the bugs actually run- q3 ]) k& C2 z+ k' J. v
// their step rule. This has the effect of removing any
1 u1 C4 \! x! k3 [" x // systematic bias in the iteration throught the heatbug
! r+ T$ U8 M0 O- V: S // list from timestep to timestep
6 S( ~# e* e9 ^0 |0 V1 v ; R0 m2 T9 R# E5 _. o) |" i3 c$ O8 J
// By default, all `createActionForEach' modelActions have0 n2 A/ ], N# G. N/ b
// a default order of `Sequential', which means that the
+ d) @7 i; Q- I+ ` // order of iteration through the `heatbugList' will be [: A2 P0 L# Q8 g% F$ R( ?
// identical (assuming the list order is not changed
; X0 ]! I$ k+ f$ H& F# w: K // indirectly by some other process).
; B' K* `- S( Z4 _: F7 i8 n$ ~ ! Q& m% ^* ~. i
modelActions = new ActionGroupImpl (getZone ());+ f' \. e$ k$ w/ _) R; V: W
9 w# j- ~2 h# w9 d. q0 ]& ~; V
try {
; @: p* i3 m9 g, d$ O% s modelActions.createActionTo$message
/ Y7 q; Q3 `6 ], J1 p N (heat, new Selector (heat.getClass (), "stepRule", false));5 I3 I- c( r& a4 S+ Q; D
} catch (Exception e) {
* Q/ D! A* `1 D8 ^ System.err.println ("Exception stepRule: " + e.getMessage ());/ E' x: S% l( A9 t; @
}( z, z$ N. C1 q2 A
9 u9 G3 h6 K! s& u
try {
: H# M8 t+ M( A2 s0 E Heatbug proto = (Heatbug) heatbugList.get (0);# A O, X( R" z) E# f B
Selector sel =
% o i* ?2 V0 p; P6 W new Selector (proto.getClass (), "heatbugStep", false);
6 \7 x2 F9 l! ^$ g actionForEach =
[) W% s, @8 | modelActions.createFActionForEachHomogeneous$call
4 ~" x; H- E% g+ K0 J (heatbugList,
0 Z5 x; J) S' u; P a" R new FCallImpl (this, proto, sel,2 W( _ p+ ]! k$ G5 x G
new FArgumentsImpl (this, sel)));0 X3 _' U, \; I$ S6 l
} catch (Exception e) {/ k; V2 ~5 H( u `
e.printStackTrace (System.err);" R! l5 D K Z1 z
}
+ t: V4 }9 P' c
4 e% Z/ i% t% X8 s: ^ syncUpdateOrder (); n9 M% _0 `2 f0 a/ ]; w: `4 r% n
, y" i/ e" N) Y, e' d8 A% b try {9 V+ Z: r4 G2 ]7 g5 Q4 B" `
modelActions.createActionTo$message
8 _" `. p4 a& t/ j. \) ^ (heat, new Selector (heat.getClass (), "updateLattice", false));/ k% D' T/ Z# ~3 }/ {
} catch (Exception e) {
9 i, S- S6 ?; C w; a% w m7 v System.err.println("Exception updateLattice: " + e.getMessage ());$ a/ t1 W- G" C0 H; U+ `
}, C: @% |5 }/ S& W5 K8 Y. g5 ~) }
; V6 Y6 K1 e# ], ?, @+ Y' I
// Then we create a schedule that executes the1 G; I: G" w g1 }
// modelActions. modelActions is an ActionGroup, by itself it
8 Q% L ^8 F+ @9 ^0 T+ o // has no notion of time. In order to have it executed in2 {3 z# c5 T2 ?6 F' y( F
// time, we create a Schedule that says to use the
# j! M; G6 J: @7 b1 x // modelActions ActionGroup at particular times. This
' a6 i3 S) h1 Z& S F) n3 H' K // schedule has a repeat interval of 1, it will loop every
* d" F. W2 Z% [0 Q: q* K // time step. The action is executed at time 0 relative to% c/ T; `7 M. A8 Z1 [3 @, B% t2 r
// the beginning of the loop.' }4 L$ H" q. m* |
) q" g; @' g6 E$ _
// This is a simple schedule, with only one action that is
; w9 S* d% D! S% f Z8 a // just repeated every time. See jmousetrap for more/ a$ L4 E1 K" I# G2 o( @* V
// complicated schedules.
$ u7 W- {* X; K' n7 H! k9 S1 H
% x2 r- M- w" G2 m/ f modelSchedule = new ScheduleImpl (getZone (), 1);
- ]% ]; x. B: N r modelSchedule.at$createAction (0, modelActions);
' b, {! x' K% X3 L0 d- N. Y8 |
# g! n% V" a. c6 K0 Y4 Q return this;1 ^) j# g0 ]- F$ ?4 x- }6 _9 K
} |