HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 ? s9 O7 L7 _3 m$ K8 `6 @3 `$ J
A' H m1 ~4 L; D" j9 o
public Object buildActions () {
5 Z& Y ~; F) n* x" S! N/ |8 z* I super.buildActions();% N) H% c) x2 K8 O
" o. b# t) ~- u% z& `7 l. v
// Create the list of simulation actions. We put these in: r; V5 m) u0 r
// an action group, because we want these actions to be1 l) x3 X6 p `3 t
// executed in a specific order, but these steps should$ o1 n$ `, S9 c- B" A
// take no (simulated) time. The M(foo) means "The message/ Y8 n5 Y* i( r9 Q- I# H& @8 \# Z
// called <foo>". You can send a message To a particular
# Q" r9 b: k* L9 e5 a- | h // object, or ForEach object in a collection.3 T5 J9 b# n# W
9 \" A5 Z2 T8 W3 A% b
// Note we update the heatspace in two phases: first run
" d7 l3 }8 p- b7 {3 m D // diffusion, then run "updateWorld" to actually enact the
; E! C. b; ]! c, ]- ~ // changes the heatbugs have made. The ordering here is0 k$ j9 t/ @5 J7 W* L$ y! W2 h
// significant! A9 G! B) n& N- u- V
6 ?& l( ?1 y: _/ ^6 ~6 q, [/ k" ~
// Note also, that with the additional v$ |; r$ }0 ~+ o# s# i
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 J+ a% `( d0 x/ p // randomize the order in which the bugs actually run0 G- k" {! M# S5 O
// their step rule. This has the effect of removing any
. ?; J, P E& P2 K8 v3 l // systematic bias in the iteration throught the heatbug
, K$ _% H% {- y' D4 v // list from timestep to timestep
% A! Y ]. v, V% Z" b% {3 @
) K E" Z1 G, ^# L& Q! ? // By default, all `createActionForEach' modelActions have
0 p4 C( T+ M% K0 C% Z$ y: l" s$ L( A // a default order of `Sequential', which means that the
# m1 U( A* Q1 h" `& D; h1 d // order of iteration through the `heatbugList' will be6 y! K- \$ v6 X
// identical (assuming the list order is not changed1 o5 `( d% q8 _$ ~4 |
// indirectly by some other process).
4 \+ F1 c- Y( N2 l( I7 v, B3 K1 m
" Z$ f! l: l8 c' a2 [/ f( C modelActions = new ActionGroupImpl (getZone ());
! c! f( b( |' m; {; N
$ h( P, F6 W" t& a. a try {
: h) T; P- h# ^+ D, X7 m4 G) R modelActions.createActionTo$message
& O0 @& i# o. r, O0 E) s" v9 Z& q! A (heat, new Selector (heat.getClass (), "stepRule", false));
/ ~6 D, C2 V" y# D$ t7 ~* ? } catch (Exception e) {
4 G+ @5 |1 I1 Z+ O* g System.err.println ("Exception stepRule: " + e.getMessage ());/ H, @: ~* B+ }8 C- m9 {( x
}" ?& V, W; f; Z- Q9 k
7 Y( ^7 S( T0 z$ u6 i3 ] try {
; l" H8 E2 G1 D/ A# M# ~- A$ N Heatbug proto = (Heatbug) heatbugList.get (0);2 W2 ?6 I6 p/ m& U
Selector sel = 6 M: F8 w% c* T* f/ v# U% x+ G
new Selector (proto.getClass (), "heatbugStep", false);
+ K; N7 i+ d& Q- z actionForEach =! p1 y+ `' {# G& X
modelActions.createFActionForEachHomogeneous$call1 T2 r; [7 o4 J9 V* R. S( ?
(heatbugList,
" G% @) x; g# G new FCallImpl (this, proto, sel,( x9 ?! G: H% [/ r6 L: K0 q9 T& }
new FArgumentsImpl (this, sel)));6 B" E9 Y5 A; G, P
} catch (Exception e) {
5 F# v4 a( A$ H9 P! j' N+ }/ R# |# E! z e.printStackTrace (System.err);' R* K( g4 m% O0 C h
}/ ~% ^; |" ^# ?* W% B- x% }/ P7 O
# K) U! J: b0 S syncUpdateOrder ();
& ^) K! |$ Z: h2 w$ Y8 l, s8 C* ~$ C; c
try {
3 b% \: y( ?- W( w- k- z$ @+ t6 t modelActions.createActionTo$message
$ @" J2 p" p0 n$ f (heat, new Selector (heat.getClass (), "updateLattice", false));5 N# N5 i! J5 ^. O" u
} catch (Exception e) {4 y1 S: ~; E$ i, o- O3 e
System.err.println("Exception updateLattice: " + e.getMessage ());6 w. X7 P6 R6 D2 z: r
}+ E* u% a7 w- M( c( G& W7 f
0 k; x5 D% I M/ [ // Then we create a schedule that executes the
7 l4 }. O6 X7 A: T // modelActions. modelActions is an ActionGroup, by itself it
' K! N4 C8 c( W# I) D // has no notion of time. In order to have it executed in$ n: O& I4 X" v, W
// time, we create a Schedule that says to use the
$ u4 f0 Z2 p6 k- p" ^; O7 p // modelActions ActionGroup at particular times. This% i& B" J9 s( E8 A5 u+ D
// schedule has a repeat interval of 1, it will loop every. M$ Q' f/ i$ |) e8 r9 v. Q
// time step. The action is executed at time 0 relative to
; a/ K& d1 t4 t5 P // the beginning of the loop.
; J' w( A7 Y6 k. I, m- `. d8 p3 A( c2 c) k0 s+ a1 c$ K: \% r# K
// This is a simple schedule, with only one action that is
' g5 E ~. W6 v ^2 C; p" w% Y( N. @# Q // just repeated every time. See jmousetrap for more+ b% k) Q2 u' d/ Q" {
// complicated schedules.9 V5 l: Y. b8 ?# l
+ N H) D& K5 j; k. U/ V6 y modelSchedule = new ScheduleImpl (getZone (), 1);( r6 y- }$ n0 B* Z
modelSchedule.at$createAction (0, modelActions);
9 ~2 a" m/ e0 W% m5 C - |: ]0 x+ f9 o4 P9 r, g+ U
return this;
1 O, J; P. R4 T+ c8 [- ~ } |