HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. C( D4 X" a- F7 k5 j3 ?3 d
C4 L) X$ s5 D* C9 n; H! s public Object buildActions () {
3 Y( e% o7 k. X* N! m" p3 _5 y& E super.buildActions();; F3 s1 u8 l9 [. M
+ f) Z; Q9 l3 @% @: K8 q // Create the list of simulation actions. We put these in& _; i# H6 ?+ ~( F8 d
// an action group, because we want these actions to be, f* k7 k# V7 F& h# s1 ?/ f
// executed in a specific order, but these steps should
; V& O6 \' { I' S& ]+ Z* a // take no (simulated) time. The M(foo) means "The message
5 I& I, p9 g( L; B3 [ // called <foo>". You can send a message To a particular
! D, \: M3 d, W" b" t( } // object, or ForEach object in a collection.! t: h( w$ W- [& O/ u5 @9 Q
. A1 ^4 }3 K* @1 B5 Z! v // Note we update the heatspace in two phases: first run4 T, u! i- v O. o, {8 @& S
// diffusion, then run "updateWorld" to actually enact the, E- A1 q0 C6 ^
// changes the heatbugs have made. The ordering here is7 y0 u6 _9 y5 D; [
// significant!
3 E1 V& I# \2 P. m $ w1 ^( A" @0 e. S+ r
// Note also, that with the additional+ x: i; x7 i. U1 j
// `randomizeHeatbugUpdateOrder' Boolean flag we can( {) N6 ]+ c+ a
// randomize the order in which the bugs actually run/ G7 _4 ?% h5 ~3 h, d8 i* }- ]7 f' V
// their step rule. This has the effect of removing any8 a F" f, b6 M7 `7 @( v A1 m0 F+ s
// systematic bias in the iteration throught the heatbug' S" Q" b! ^6 e1 Z$ N, }" @
// list from timestep to timestep
8 \% Z( b" [( }1 K. G! N, r6 b2 j ) E+ y" a$ L$ F4 j& o, _3 d, w
// By default, all `createActionForEach' modelActions have
2 P3 c' ^; ?9 |+ S // a default order of `Sequential', which means that the
2 X. d8 R/ n. V // order of iteration through the `heatbugList' will be+ M& @) t. {$ p4 O
// identical (assuming the list order is not changed3 \9 G4 Z5 M0 |; s2 Y
// indirectly by some other process).
# J' J8 ]! c2 u, n; l" J$ n2 r+ c ( V" }/ u$ @3 ^* k
modelActions = new ActionGroupImpl (getZone ());! r. K! X: R6 x4 S+ q
! O- P% \7 T _" m* d try {2 m. L! d3 V. r# g& v6 p
modelActions.createActionTo$message
8 E7 z9 J* v" A% M1 q (heat, new Selector (heat.getClass (), "stepRule", false));7 E$ m+ P' J5 d& M' q m3 v, u/ t% V
} catch (Exception e) {
9 s m7 J4 ]% T9 \ System.err.println ("Exception stepRule: " + e.getMessage ());+ Y* L3 B7 b ]( S
}
O' Y$ i! d8 x0 P
% H( }2 Q6 F% a* _4 p* f) ` try {
1 Q' `& [0 g3 t# O' d) q: o Heatbug proto = (Heatbug) heatbugList.get (0);9 M9 ?$ Q3 n2 x8 M y
Selector sel = # E0 W! _) _/ ~9 e: Q# M
new Selector (proto.getClass (), "heatbugStep", false);
: p. c. E5 b8 a2 k7 y f actionForEach =
+ f3 y6 N" l4 l6 V' }+ l3 O0 ?, e modelActions.createFActionForEachHomogeneous$call
* P5 c2 n: Q& r% R0 _0 X% d (heatbugList,; n8 h( l: O" z: d6 b
new FCallImpl (this, proto, sel,: K/ s: l0 v' @6 c8 V) n
new FArgumentsImpl (this, sel)));' [6 o+ Y4 Q5 C2 |9 N0 H( j" H
} catch (Exception e) { g' l9 P4 N4 F c Y; Y/ \
e.printStackTrace (System.err);
% f* g, n* X) e. ?1 @# }0 N }
$ T1 D1 H5 Y' N3 c: B 9 L- D" Q. [ A3 Z
syncUpdateOrder ();! Y/ N8 R* ~2 A9 u5 S
7 ~3 _. B: O; j- W4 i
try {7 X% E7 R- b5 Q& ~# T
modelActions.createActionTo$message 0 e1 Z$ m* `, d4 [- L/ t4 \% E$ y
(heat, new Selector (heat.getClass (), "updateLattice", false));* K/ e/ {' E. n# ^/ Q
} catch (Exception e) {0 q/ [) r4 K" i1 a# j* p
System.err.println("Exception updateLattice: " + e.getMessage ());
# d0 U1 [6 G8 `& {+ ]8 A }' ]; c$ K: @3 Z3 g6 e
% Q: T5 I8 Q& n* {2 M // Then we create a schedule that executes the
8 B4 ~' y9 b) i2 m // modelActions. modelActions is an ActionGroup, by itself it `4 L1 q& G/ }8 ?
// has no notion of time. In order to have it executed in
- F' V$ i# Z- N+ _; W# ~1 N8 P6 d // time, we create a Schedule that says to use the
/ J1 O3 C5 y# }4 t; [8 Y3 W // modelActions ActionGroup at particular times. This
( S. l8 g0 w( P/ a9 c% W0 s // schedule has a repeat interval of 1, it will loop every) t7 X& u, @% V2 R, a8 Z% X+ B
// time step. The action is executed at time 0 relative to
- R0 H. b/ l2 ~: j8 b' p" J9 }/ n1 V // the beginning of the loop.
- C- S' p$ N' d5 d' C# K7 o& T$ u* B! M/ j( t
// This is a simple schedule, with only one action that is
7 C) g$ S- h" g. }4 c! j9 m // just repeated every time. See jmousetrap for more
8 V, g( M9 X/ C3 m. ?3 ]- \, b // complicated schedules.
% t1 l4 `, ]$ G t4 h1 v ; N% Z- @3 p P# n* Y. f/ r1 W
modelSchedule = new ScheduleImpl (getZone (), 1);
& k K- P0 b t% i modelSchedule.at$createAction (0, modelActions);
U R2 i( {$ s8 s ( L3 ?$ ~, [, f5 E) C. }/ U$ T, P
return this;
+ A9 Z; A5 z2 _' O! k; a7 e } |