HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 {5 K0 Y2 U/ \
& f( j. p( l9 j2 F/ _5 @ public Object buildActions () {
2 Z4 K, [0 w3 H/ V# A$ ~& u' f% ]6 N super.buildActions();; w+ I6 F" [: b8 j
. E# R& A; T( R. F, k0 o. A
// Create the list of simulation actions. We put these in
* ]/ H1 A1 ?- E5 W // an action group, because we want these actions to be
: `3 t) O. ?9 K& A6 l5 o // executed in a specific order, but these steps should+ Y; k: |. b$ q
// take no (simulated) time. The M(foo) means "The message/ R: V7 s. n1 X& P1 b
// called <foo>". You can send a message To a particular
' S+ T$ c+ Q0 k. z; w" ^ // object, or ForEach object in a collection.
, d+ {5 f$ P8 ?" n
; w$ h, w/ o; C // Note we update the heatspace in two phases: first run
& h9 ]( Q: ]- f- b# K& F& {3 A // diffusion, then run "updateWorld" to actually enact the9 N+ }( ]" I4 _5 _7 [- B0 p, @7 j
// changes the heatbugs have made. The ordering here is
9 E( r. N7 D& s5 c: a$ C0 o6 d // significant!) ~" }0 G, Q/ |, b# c; L
; B% _% P0 X$ y# o- p6 Q& [& P
// Note also, that with the additional
+ e d/ M" {* T+ Y$ m // `randomizeHeatbugUpdateOrder' Boolean flag we can# g$ s( P/ O- \1 [+ H
// randomize the order in which the bugs actually run
0 t/ d6 T9 J2 i9 b; _/ x- J! H // their step rule. This has the effect of removing any
6 Y% K* p, G" T* m // systematic bias in the iteration throught the heatbug
3 d) r4 E) n7 m$ u; l% r2 ` // list from timestep to timestep2 ?2 k0 M' e3 \5 c# d, ^
1 u# z1 z% m8 |' d: C4 k
// By default, all `createActionForEach' modelActions have2 L: I3 P7 m' w- O
// a default order of `Sequential', which means that the, I8 X1 b$ {. ^6 O/ Y5 j) | N6 F; f
// order of iteration through the `heatbugList' will be
7 z9 R( j l8 K# `+ K3 r // identical (assuming the list order is not changed9 x1 u/ \5 L, p
// indirectly by some other process).
5 Y' [) p9 r+ q& O. u$ x : J! o% P5 A1 s3 W
modelActions = new ActionGroupImpl (getZone ());
6 }$ C! p* n* z. _6 f& l! q0 }+ _0 ?5 O! R# j% M
try {
9 j& g& U% q. C K7 d ?. V% L modelActions.createActionTo$message
! S) n: M5 [# D! q" O2 @$ d: m$ d4 Z (heat, new Selector (heat.getClass (), "stepRule", false));% c! U! X4 p* {5 \
} catch (Exception e) {
3 M1 | f% Y" Q) f2 n System.err.println ("Exception stepRule: " + e.getMessage ());4 Q2 D/ p. z8 `
}
3 g" n2 C) [4 ?) I0 k1 C
& z# P; e7 x/ @ n' | try {
. a0 x. Q+ l6 k+ {, E" E: K Heatbug proto = (Heatbug) heatbugList.get (0);) ?: n7 {: v- s6 X5 W/ _% g3 j% m
Selector sel = 1 O+ w& ?+ ~! j. L
new Selector (proto.getClass (), "heatbugStep", false);3 ]* I0 {- t/ r( S+ F0 H2 O) ^7 [
actionForEach =3 E7 n# u; F6 P" `
modelActions.createFActionForEachHomogeneous$call' {1 \. J6 a& a9 f" q1 [
(heatbugList,
# D% F: r: c6 P; [9 |5 f' Z new FCallImpl (this, proto, sel,4 h8 d x$ K- \: Q: q! p+ M
new FArgumentsImpl (this, sel)));6 k, P, R, }5 h& M3 G- I" p( b
} catch (Exception e) {
& A" m e) n x3 A% D e.printStackTrace (System.err);5 ]3 n; \3 v3 F" B/ m
}: x# l5 K! [4 U1 A3 b3 O" s
0 z% ~ Q$ f* U7 C# [7 ~
syncUpdateOrder ();
' w" i2 \8 l1 [
9 L; \4 Q3 ]) b& d3 y" J! P4 _ try {" ^2 L8 i7 W8 v# p+ r2 M$ V
modelActions.createActionTo$message 4 X$ M8 d5 \8 K% g: A5 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
( K# @6 U; a% D( R+ ^ } catch (Exception e) {
2 l* D( O# ?$ r System.err.println("Exception updateLattice: " + e.getMessage ());
h: { _+ x- v6 u" j* Y }
* q+ s9 |7 R- M8 n, m & F9 s; F- S1 C) b8 G! S
// Then we create a schedule that executes the5 U! ~% g! H8 V/ _, q7 b
// modelActions. modelActions is an ActionGroup, by itself it5 g# Y# N* C R1 T( O2 C8 l
// has no notion of time. In order to have it executed in
$ n# F f, b5 \+ V' l // time, we create a Schedule that says to use the" p) o# M0 N$ Z Z
// modelActions ActionGroup at particular times. This
7 w' r; K7 I b& S% o // schedule has a repeat interval of 1, it will loop every
8 U( O: s" @1 Q! ] // time step. The action is executed at time 0 relative to
w% y" {4 s! i$ @$ Q9 S8 \9 G4 D2 Y // the beginning of the loop.) Z9 m% N2 j5 R0 S( Y" f! @. y
9 E4 ~3 r- T8 A6 x1 F
// This is a simple schedule, with only one action that is
( i* X1 l( Y3 J# ~7 I1 i+ H // just repeated every time. See jmousetrap for more
5 ?+ S3 Z4 W' `4 G; l // complicated schedules.: Q- \- h8 O* w' j- L. p9 z; ^
2 U$ J/ x" y; _; o3 r+ \
modelSchedule = new ScheduleImpl (getZone (), 1);
! u* v" L' a8 x! d3 q1 W! E modelSchedule.at$createAction (0, modelActions);2 p w' |1 z* R9 g+ S! \% p) _2 B
7 u5 J" k$ ?' R* G* r: N# J" F, ?
return this;8 X' E0 e' Z$ K/ h3 }6 o" b- S
} |