HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: {! }( B2 s; V' o1 L5 u( R Y! X0 d
! n: C7 a8 P/ S4 e
public Object buildActions () {
% j: d$ _8 @5 n& _- p5 y& K super.buildActions();
% }) c& ^( r9 u; w, D; W
, D1 p, Q" F' [1 f; t7 R // Create the list of simulation actions. We put these in
) n' C* n/ c( W5 l l# J3 r- t) H- Q // an action group, because we want these actions to be
: m& B$ ^5 v% |& w- E; x- p, f // executed in a specific order, but these steps should- T% }8 o0 ^: ?1 U
// take no (simulated) time. The M(foo) means "The message3 V7 L: H: z2 ^# [9 Z& O
// called <foo>". You can send a message To a particular
- e) U. N' l& i# p // object, or ForEach object in a collection.
5 t' H/ L9 {8 l9 s) k, [. |
1 v8 G# a4 }3 S( o1 s6 ~2 f // Note we update the heatspace in two phases: first run
/ Q! V. n: E6 r3 k: d# f // diffusion, then run "updateWorld" to actually enact the7 i% S n; z% o; y3 r
// changes the heatbugs have made. The ordering here is9 }3 u, u5 d. N7 q$ F2 w
// significant!- E, f, l: X; H* A" g
4 @& r! U& P. d% J* h! t // Note also, that with the additional
" p4 U3 |- Z9 N1 E& w // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 y* Z7 K1 H0 J( z // randomize the order in which the bugs actually run) [4 Z$ i7 _ ?* r8 r
// their step rule. This has the effect of removing any' w" I" x5 o4 O
// systematic bias in the iteration throught the heatbug
. E0 v- p9 f5 e( h, J3 [. M // list from timestep to timestep/ t. R( b+ |! f j* E2 n! u9 _- i" d6 i
+ B) I! I/ [0 B5 f4 o // By default, all `createActionForEach' modelActions have1 W) `0 K/ T4 f( {) f
// a default order of `Sequential', which means that the
6 Z; y; Z! S x( F // order of iteration through the `heatbugList' will be m) X( h( W! x6 A
// identical (assuming the list order is not changed5 m, c7 i# z* T' ^; m
// indirectly by some other process).1 P/ V$ Y# ?7 u/ T) W) f
1 m0 b& R/ `' Q( c$ C
modelActions = new ActionGroupImpl (getZone ());
* f% ]- @1 J* x9 z
- k9 Q5 F1 p7 a3 o- B try {
/ h% i9 G" h. h. E7 v modelActions.createActionTo$message4 V9 D8 k7 B# y- s! J$ Q. M
(heat, new Selector (heat.getClass (), "stepRule", false));
1 e9 V, J0 d9 r8 C# V- ~# ~ } catch (Exception e) {
9 }; m& @* w) V. ^; s/ }5 f3 G System.err.println ("Exception stepRule: " + e.getMessage ());
$ |# O0 H' \, D$ v' t }
3 I' o% f9 k$ Y c% @; P. E( s; d( l/ M% D0 |; F, h
try {
; v {9 x( B* t# T9 y% }7 z Heatbug proto = (Heatbug) heatbugList.get (0);
# s3 L- r u) Q, {; |1 A8 r* L* N% y Selector sel =
3 q6 B8 N0 x8 ]0 W' j/ d new Selector (proto.getClass (), "heatbugStep", false);
6 G9 D+ t* c# }6 H1 L/ w actionForEach =
C; \' Z& v4 Y0 t6 C' h% p5 Y modelActions.createFActionForEachHomogeneous$call
% t7 j0 l$ d! D- B+ } (heatbugList,- e. {% y# b# B0 Y3 V
new FCallImpl (this, proto, sel,
9 M9 i! l* U) ?. k" j+ r new FArgumentsImpl (this, sel)));6 R4 n/ `; R0 U4 t; E5 n) T6 [6 q
} catch (Exception e) {
% z, m: x" |! _& a7 b1 t* \: E e.printStackTrace (System.err);0 \* |6 T0 @2 A4 D o
}: L) @, u$ t7 a3 o1 s! T
* e% \& L' h+ ^" B. B" | syncUpdateOrder ();' M5 E; R, D% q! r4 I
& S1 f& [5 g! P& Z* J* U& G& W
try {+ V( A& U/ K5 B; _2 Q- j/ e
modelActions.createActionTo$message - c8 \( @$ z+ F
(heat, new Selector (heat.getClass (), "updateLattice", false));' n8 W! w/ g5 u/ Q
} catch (Exception e) {* r9 y( E* Q( I+ e1 q
System.err.println("Exception updateLattice: " + e.getMessage ());
/ n) s) t5 s4 o% I( Z7 | }3 Y3 q4 @7 M& ^0 T- }- S7 D
- P. S$ R4 F$ p k7 L
// Then we create a schedule that executes the
; e9 R! F" D+ @ // modelActions. modelActions is an ActionGroup, by itself it
% Q4 [# O1 ]& s6 g$ ? // has no notion of time. In order to have it executed in/ e$ x% ^ |' j9 o7 x) k
// time, we create a Schedule that says to use the( \7 `5 c7 O# f# d) F) P2 H2 Y, I
// modelActions ActionGroup at particular times. This
. r% Y/ ?% p, s# S // schedule has a repeat interval of 1, it will loop every
, ^0 q5 P; F9 ]! L // time step. The action is executed at time 0 relative to* N7 o& G$ Y! { _/ j
// the beginning of the loop.; i w9 |; D; q+ G
& b2 @7 g* p4 |& i+ `& w* X: {; u
// This is a simple schedule, with only one action that is
+ `% h" C6 u: E // just repeated every time. See jmousetrap for more+ D. ?- v9 x5 |8 j$ l$ O9 _" Z
// complicated schedules.) {$ ~' Y9 g: t; E# r
, k4 g; x" y+ M9 ~7 A. w; K& N9 N
modelSchedule = new ScheduleImpl (getZone (), 1);/ E9 I7 w* x8 C
modelSchedule.at$createAction (0, modelActions);) Y$ N% h# H" I2 _9 J- m8 [( u
/ r7 s$ f5 j& c) A9 U* }) y
return this;0 @0 |1 ]2 @2 o! h h7 {6 M
} |