HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" b1 Y* N; P" g3 k! [8 H3 a* R
/ b4 H+ z5 S" m0 W+ ]3 V1 s9 S
public Object buildActions () {. }4 w8 g% p+ I% [( r7 ^ u" y* |( @7 n
super.buildActions();1 E* H. B6 F! o$ M* B
# n4 m$ d$ h2 y! c
// Create the list of simulation actions. We put these in' O4 u7 O. Y) \) M
// an action group, because we want these actions to be* U8 z+ R5 ~: T
// executed in a specific order, but these steps should
& a* y( z( G" ]1 L // take no (simulated) time. The M(foo) means "The message
' Z9 d7 a; H# i // called <foo>". You can send a message To a particular
4 \& M) F4 v% |: k2 f' T // object, or ForEach object in a collection.; ^5 ~# L# a* B0 d
/ u0 P. \" ~* G' x
// Note we update the heatspace in two phases: first run
' t+ f0 f$ F0 H. R- o // diffusion, then run "updateWorld" to actually enact the& Q+ O3 l2 E' x8 D& k
// changes the heatbugs have made. The ordering here is$ }; R" m. M+ r8 |' o2 f
// significant!4 c- i' T4 ]) s% f* B$ Q
- c( |' G9 x2 P$ {9 @" L
// Note also, that with the additional/ y3 d. j1 v7 Y; V8 Q) R) X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ p" m5 u4 i: H2 \; |, v // randomize the order in which the bugs actually run
v, x+ K5 \' f9 l5 L // their step rule. This has the effect of removing any( P: S3 u6 m! n: Z
// systematic bias in the iteration throught the heatbug
( y- P- \$ n9 n. g, `. o& U/ K // list from timestep to timestep
1 ?, F7 {/ `& a7 ^ " Z! I$ H n9 L, b* @
// By default, all `createActionForEach' modelActions have
2 h8 r7 G7 b H/ H // a default order of `Sequential', which means that the
+ v1 m5 u; O( C8 i // order of iteration through the `heatbugList' will be
1 t$ d) {9 L0 b7 f: s/ o: {& W // identical (assuming the list order is not changed- o) \! o4 c0 n9 a# }2 A" S4 t
// indirectly by some other process).
Z' w% _' {6 o1 h
7 h7 _$ d3 ?( Z9 J modelActions = new ActionGroupImpl (getZone ()); G( B+ G. d- H$ y# V: b
) O8 e+ M/ E+ m. W) Y/ @$ N3 T try {
5 m7 M- V/ u/ w) h modelActions.createActionTo$message
# e' V/ p9 p$ B8 O" W (heat, new Selector (heat.getClass (), "stepRule", false));
) g) g. P6 u2 Z } catch (Exception e) {
2 x' i7 F+ ]4 }' m. v1 |2 n5 X! X System.err.println ("Exception stepRule: " + e.getMessage ());5 O# z( Y4 i) i/ f
}+ [6 A; T& J4 C. W- V
, E8 v9 n. x. F8 P7 w& E1 z h/ p7 w
try {# N- c# X& F- D6 p. x$ _
Heatbug proto = (Heatbug) heatbugList.get (0);; ^: [- O5 j4 [" x8 u
Selector sel = 1 i9 u3 M5 R5 l7 K5 m( y
new Selector (proto.getClass (), "heatbugStep", false);) U& k' O+ |& D5 |# m5 }
actionForEach =5 j7 K+ Z9 _/ \: u. B" G
modelActions.createFActionForEachHomogeneous$call
" H1 L: L$ N& \8 c1 |7 Y, g8 j( p (heatbugList,
8 e. C# O4 A% D: Q8 |% c7 a new FCallImpl (this, proto, sel,# h& t5 r e" c& `
new FArgumentsImpl (this, sel)));
2 r& R$ i' t, e' Q# Z } catch (Exception e) {
# {% C. b) T( @ e.printStackTrace (System.err);
; a5 o; F+ Z/ z1 ]- V }
1 e) v8 T& B7 P ~& g6 V 1 q; P1 H5 j- K8 {. j, e9 |
syncUpdateOrder ();+ V. ]. w. ~1 j" R' I8 j& i# O
* R3 O. G$ ]) q; @! T/ s try {$ h9 `, X3 @2 G7 _
modelActions.createActionTo$message 6 M, u3 `, h: j9 f' K! I
(heat, new Selector (heat.getClass (), "updateLattice", false));
. H$ }# d5 h- c6 D5 B( `( X- s } catch (Exception e) {
# n/ H, O! W3 l; D& k System.err.println("Exception updateLattice: " + e.getMessage ());/ S0 Q* I" W3 Y
}/ E4 }" C' i4 w8 h% `7 _% I
; F3 l; |6 [8 D, R7 t i: t
// Then we create a schedule that executes the' Q2 Z1 K" I, O; T" A2 ~0 t+ R
// modelActions. modelActions is an ActionGroup, by itself it/ I( \- z% E0 z
// has no notion of time. In order to have it executed in
, s: o' C8 b8 ?3 d // time, we create a Schedule that says to use the* x. Z% u( c' |
// modelActions ActionGroup at particular times. This4 W/ j3 P2 x# y8 ^% D2 y
// schedule has a repeat interval of 1, it will loop every
* B4 s, r, d9 A2 I7 Q+ ^$ s& ? // time step. The action is executed at time 0 relative to
, t( t5 L: Y6 K; x8 f# r // the beginning of the loop.7 d8 o! w. Q' T8 L6 u# J
. M1 n4 L0 \* l
// This is a simple schedule, with only one action that is
# } e* A) ~2 o // just repeated every time. See jmousetrap for more
5 D& \$ l0 m" `' @+ V& l( w$ s // complicated schedules.
$ E8 R+ m* k! u( A2 M) f
9 o, b9 v$ {& [( Y modelSchedule = new ScheduleImpl (getZone (), 1);
: B6 J Y. A2 o" r% ^5 p% E modelSchedule.at$createAction (0, modelActions);7 J, b9 U* n5 D$ _9 a8 U! A
& ^" \5 e0 `/ [7 m
return this;" ~8 S" ^* b' q& |% A. L; T
} |