HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 j Y2 n8 t2 l6 t2 E3 Z& O. q1 |3 |) l& A/ F
public Object buildActions () {
/ j( d. f3 r1 N _! z& g& r super.buildActions();
* K% j0 ^3 _2 E$ y" W# k3 F
2 H _4 j# `) }& j) e |& P // Create the list of simulation actions. We put these in
3 W0 l; q2 y. _9 v$ q // an action group, because we want these actions to be
3 S5 c. T- S+ l2 f o$ E // executed in a specific order, but these steps should7 A& k2 s: |( D' g3 U
// take no (simulated) time. The M(foo) means "The message/ o2 D0 N* {8 L
// called <foo>". You can send a message To a particular
/ ]' I& k$ n8 ]3 \/ B# U* c; ] // object, or ForEach object in a collection.
( n( s9 `# A3 P5 L: { 1 \8 {/ T3 j; x4 ]# [" ^
// Note we update the heatspace in two phases: first run3 n! l) {, D( Y! w8 K
// diffusion, then run "updateWorld" to actually enact the
/ a& I& U9 S. T // changes the heatbugs have made. The ordering here is9 S4 I" n3 n m7 V* k0 O! Q }
// significant!
# d! C- Z, d0 C+ a) t
. H4 ~/ g3 T: g' m1 B& t // Note also, that with the additional' A5 Q6 F9 Y' o. ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can; L, n/ J. I1 _0 R4 C
// randomize the order in which the bugs actually run( w2 I/ m( h9 h' | B" `
// their step rule. This has the effect of removing any8 S4 T: W8 I7 H7 P
// systematic bias in the iteration throught the heatbug$ ^* m; D6 d; L; [/ [' q. V
// list from timestep to timestep
. ]" H6 @# G" j. W0 ~& Z- b
1 e- h4 Y) e( s0 \2 n% c // By default, all `createActionForEach' modelActions have* ?" J4 x6 I a" h& i. N
// a default order of `Sequential', which means that the) ~: `; X4 T3 N
// order of iteration through the `heatbugList' will be) \# u! R& P; K( ~
// identical (assuming the list order is not changed
& d4 b/ S# O* R: @! a; ]* Y // indirectly by some other process).7 \( d: H/ M) K5 m& \0 e, i
5 g* C- I5 H3 U" ^& o modelActions = new ActionGroupImpl (getZone ());
# b4 M9 t7 q4 f t5 R( P3 F" X i: j6 D V
try {
5 I/ y% {8 g8 i5 A) g modelActions.createActionTo$message) Y* x3 [8 j# N* {
(heat, new Selector (heat.getClass (), "stepRule", false));. E; w) ?" [9 [1 W3 _/ H! X6 l
} catch (Exception e) {
+ |' }' l7 p% [" R% Z System.err.println ("Exception stepRule: " + e.getMessage ());
5 c( d B6 l1 E C }
: L1 b" p4 Y; v( _5 H; w9 x+ m
5 N$ z4 E( ^0 ?" _* a* z5 A try {
& E/ N# F, V: W0 [4 f9 J0 U Heatbug proto = (Heatbug) heatbugList.get (0);
) _* l$ ~. l; Z, G6 S Selector sel = & b4 W$ V/ n1 A: d% B
new Selector (proto.getClass (), "heatbugStep", false);" i. Y2 H# A' t2 |/ A4 u+ [1 M
actionForEach =
5 U# |4 H: [! P; C5 ~/ @6 C! `$ B modelActions.createFActionForEachHomogeneous$call9 M* g: O2 ]( k' {
(heatbugList,* ^6 C: ` W/ D: D! y- N
new FCallImpl (this, proto, sel,6 Z. ]+ z( |+ J2 F# |
new FArgumentsImpl (this, sel)));
& S' J/ t, _+ N } catch (Exception e) {* A0 B* b+ @5 J9 r4 s
e.printStackTrace (System.err);
& }* I1 w3 P; X: _ }) v9 {/ X/ L) p: F% N0 A
% F( V) [7 M: B; m. A0 ? syncUpdateOrder ();
1 o" n; @% ~( {. p7 y! a; n6 n
' y6 X- w- |* _: w/ P% ~3 { try {
E, w! W: U9 M) f4 [ modelActions.createActionTo$message - d; \7 g/ c' S( |- M
(heat, new Selector (heat.getClass (), "updateLattice", false));/ T( Y- M; }4 j! K6 d9 h
} catch (Exception e) {5 W3 ]% x/ v Y0 u
System.err.println("Exception updateLattice: " + e.getMessage ());5 C6 s: y7 {* \0 j1 A
}
+ P8 H {. F" U% l9 ]2 P; j3 S3 |; T
0 F* \: P. m; H9 @( f9 d4 v0 `! Q // Then we create a schedule that executes the
: p" p- ^0 Y0 c4 O# q) D \ // modelActions. modelActions is an ActionGroup, by itself it& ^) G1 `4 g6 F2 O$ }
// has no notion of time. In order to have it executed in" S8 c) b( C% v' s9 T% F( i' O
// time, we create a Schedule that says to use the! A# ~+ t% _$ R+ w( G# N
// modelActions ActionGroup at particular times. This
4 d% H( N+ m H5 Z+ E& _! ]: @8 ~ z // schedule has a repeat interval of 1, it will loop every
I7 n8 E/ K+ m4 q+ J# y // time step. The action is executed at time 0 relative to
' e8 u; Q: @. X% z4 g/ O( {: t" N; g4 h4 ~ // the beginning of the loop., t! E6 o2 ?8 O4 o B* d$ E
6 w1 X4 F. l" Q1 ~6 F+ j% o
// This is a simple schedule, with only one action that is- D j/ e7 a8 a2 C' r+ N1 R
// just repeated every time. See jmousetrap for more
! X3 _! y) X9 H$ R // complicated schedules.
9 l) f' V2 w% N. K5 d
' K, X2 k: z: t7 _* _ modelSchedule = new ScheduleImpl (getZone (), 1);8 l7 F; t. ]$ E& t2 r2 ^# F
modelSchedule.at$createAction (0, modelActions);
* S, L8 P! K& D+ l# j 4 {/ n4 O% J4 r' L: r' c
return this;
3 ^- B- T% O/ n+ F) i0 R. p& b } |