HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 Q- e9 _ Z# \1 y8 b
# B% X+ Y. i$ J& y' N% R8 s public Object buildActions () {
% N0 k# y' e: S1 r0 W super.buildActions();
6 x$ w: r7 Q7 T( C ! |: |+ o( @+ Z; ~+ D" Z5 R; X
// Create the list of simulation actions. We put these in
: U3 m. K. b* ]! \, X // an action group, because we want these actions to be
/ p) h9 x4 M& |. a( y: \ // executed in a specific order, but these steps should# L1 x- }2 Y0 r& }
// take no (simulated) time. The M(foo) means "The message( n# e% N0 i( U6 _& c, P" X
// called <foo>". You can send a message To a particular
, z X+ `# ~( w0 l0 R/ e // object, or ForEach object in a collection.
. w: I( m" U3 U% s8 B6 c0 b; A : l* e! a$ Y# k0 L1 |
// Note we update the heatspace in two phases: first run
" W! j1 W% R5 p ^, V% _ // diffusion, then run "updateWorld" to actually enact the+ J# |! m- @1 E" l& p1 ~# R" _( n) U
// changes the heatbugs have made. The ordering here is
f! H" O& ^! f0 m& G- O# o) s // significant!% b% }" s4 e/ X1 z+ a9 j* ~
, L: W( T0 s9 X, o // Note also, that with the additional
( O/ ]+ d4 U! I5 D // `randomizeHeatbugUpdateOrder' Boolean flag we can$ a, @3 j) ~- e1 ?+ u1 O( u1 U
// randomize the order in which the bugs actually run3 M8 [' \; H- O! j3 A
// their step rule. This has the effect of removing any: G" E9 i+ x: T7 q# H% T \
// systematic bias in the iteration throught the heatbug
7 d ]& R0 J& D8 H% L // list from timestep to timestep* l1 h+ ]* g9 x7 A# t4 F
) H2 p$ j# ]+ x6 Z! F; V' `% X* u
// By default, all `createActionForEach' modelActions have8 f9 X( l+ V' q1 v5 g: R4 i
// a default order of `Sequential', which means that the( K% Q9 _6 b( w6 O0 ]: p' x8 L. X
// order of iteration through the `heatbugList' will be
% v. l' K! c+ J2 T8 M // identical (assuming the list order is not changed" n/ l* I* i' U1 }
// indirectly by some other process).
3 |0 T) a1 e6 N8 S/ |8 A # b5 g( V# N7 B# }) v
modelActions = new ActionGroupImpl (getZone ());5 b- R9 f6 P9 @* H$ f H! [9 k; w/ z
. n( {# ]. Q# _* C/ K
try {3 v7 U. Y: q* d
modelActions.createActionTo$message
- [7 N- P. z: n1 O7 P" H. c, T" d+ \ (heat, new Selector (heat.getClass (), "stepRule", false));
9 A( i" e7 B. V } catch (Exception e) {/ w. g C) \6 K! g1 o
System.err.println ("Exception stepRule: " + e.getMessage ());
1 S: v. i" y" d& S! C }
$ C+ O/ q6 I: o' l7 Q' p
% u- Q, g Q: |; { try {) l/ z1 _; i9 V2 l$ C
Heatbug proto = (Heatbug) heatbugList.get (0);
9 U9 H& |! S& m* w/ | Selector sel = 8 p) @! n" W' R1 k0 @1 i. ]
new Selector (proto.getClass (), "heatbugStep", false);
9 ^5 I4 n6 |1 Q& o5 ]( ^ J R actionForEach =. Q5 A4 {1 |7 z: O: L3 K8 Z
modelActions.createFActionForEachHomogeneous$call
9 J6 ~/ d$ t q3 Z! z" A (heatbugList,; \+ i) \3 x: ?' x/ ] l
new FCallImpl (this, proto, sel,
1 ^- I1 Y0 }3 H/ b6 U4 v new FArgumentsImpl (this, sel)));
' r C+ F- v) X" A } catch (Exception e) {
9 g1 `$ D& X( w: a6 ^3 x/ E. J* d e.printStackTrace (System.err);0 Q: I+ u) _+ K4 s) I/ `
}+ ~& J E) m) t# D% o" @
% _* H* g5 \. F syncUpdateOrder (); U3 |& H* g* d% r9 u7 v
$ |3 E7 V! I1 u' |/ \7 w. S
try {* [8 b, `" Q; x, U q8 c
modelActions.createActionTo$message . F: v' t' q' `* ?
(heat, new Selector (heat.getClass (), "updateLattice", false));; e. n! q( i" e1 R$ r" T
} catch (Exception e) {! U3 U& {8 l/ E' h/ ~& U" ^/ J
System.err.println("Exception updateLattice: " + e.getMessage ());- a- G2 w$ ^7 K/ n
}+ P0 W, N; V' i; \
5 U( N' s2 N2 z, m2 a0 W
// Then we create a schedule that executes the+ \1 Y- l# E* `4 }: a. N& m$ h
// modelActions. modelActions is an ActionGroup, by itself it4 f3 S/ n( a. E, g: W% s
// has no notion of time. In order to have it executed in
$ T* T6 [$ B! S2 F" l, K // time, we create a Schedule that says to use the
W5 O! V, _9 u# U/ X' \+ s& D+ Z // modelActions ActionGroup at particular times. This
~) ]/ J, L( c8 x: g" U // schedule has a repeat interval of 1, it will loop every2 y2 p3 w0 z7 E ~* H
// time step. The action is executed at time 0 relative to3 E0 Q6 ^$ v/ _; l- q+ V" N
// the beginning of the loop.$ m8 B0 N5 c$ f! M( [* W* v
: V7 B7 ]3 p9 u5 ~
// This is a simple schedule, with only one action that is" x3 p0 @+ N7 ~3 @! ~8 F" p4 `8 d
// just repeated every time. See jmousetrap for more
2 Y: l$ H1 l" Q' n" r& k: L! O/ C7 `$ ^ // complicated schedules.
( T0 L' C2 q8 q; W8 T$ K
+ U& D1 |4 \$ E( w modelSchedule = new ScheduleImpl (getZone (), 1);1 [# r) w# g$ O8 K
modelSchedule.at$createAction (0, modelActions);
: V( {* c/ l4 v* G* o8 c( k: C' h
. }2 P) U. A) D1 b, D2 s f% x return this;+ Y1 n& y8 Y! f6 M$ o
} |