HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- B! Z* u" |+ d8 ^
0 E3 [3 z/ }! _ public Object buildActions () {
( N" \4 Q5 I; h7 t$ G4 O/ U super.buildActions();
. u" x) {- m7 W3 k- ]. h
, n; M0 W7 `: x5 P0 J- F1 a // Create the list of simulation actions. We put these in
+ F. @1 G* l3 M$ B // an action group, because we want these actions to be6 ]! c* I& N# R. e3 m/ U
// executed in a specific order, but these steps should
+ Z& K) m' ~, C- l! H* T // take no (simulated) time. The M(foo) means "The message3 Y% l1 Y% C- k9 @& ?- m
// called <foo>". You can send a message To a particular" U6 _- Y3 ^+ C7 O K( G; w7 C3 M
// object, or ForEach object in a collection.
2 E) V+ Z, s$ }# Q6 ?- v2 B5 L ' M/ c+ }' [7 P, d# O5 r" H2 B# @
// Note we update the heatspace in two phases: first run4 K' z7 i: }8 y' Z5 S6 C% m
// diffusion, then run "updateWorld" to actually enact the3 M$ b3 H H' ?5 J' I2 V: F2 h
// changes the heatbugs have made. The ordering here is9 x0 w# }* z# O; C. ~& P8 ?
// significant!- I% V3 N n3 J. l
6 H9 X% |$ N. j' j5 \ // Note also, that with the additional6 P0 f& v: c9 I) `) j/ V& x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' g' M) P" U3 v7 _% |+ E: O // randomize the order in which the bugs actually run
9 E3 w1 t9 ~% A) v0 \5 J9 T8 e // their step rule. This has the effect of removing any
( Y. b# y" m, m: Q0 ?) s" y. h // systematic bias in the iteration throught the heatbug
9 v. g' K0 U3 V( G% T. j `* x // list from timestep to timestep) u0 Q) \6 K0 l: }, G+ e! H- |$ F
3 i+ A8 c9 L- v+ w // By default, all `createActionForEach' modelActions have
4 W3 a. j B" `* }- O2 R% X' b // a default order of `Sequential', which means that the
3 o) f# C1 a S( d3 i2 ^7 Q // order of iteration through the `heatbugList' will be
* a5 n5 }5 j" y( ?. n" g // identical (assuming the list order is not changed
3 p! a w$ o5 e( z0 j w0 { // indirectly by some other process).
" C3 @6 z1 _5 Z9 F; h% g3 L 8 R& q4 i. }8 J9 {" Q8 `! ~
modelActions = new ActionGroupImpl (getZone ());& ~- k' q! U ?, y: V
4 O" f, l" q+ n; C2 N7 ]" }
try {4 @* t( T; Z' B# _* |
modelActions.createActionTo$message
# [2 o9 T. V0 P! [1 j* @" f+ f2 q (heat, new Selector (heat.getClass (), "stepRule", false));0 c* Y7 o+ U5 c- K# b
} catch (Exception e) {
1 ?+ H. s1 L+ K7 b System.err.println ("Exception stepRule: " + e.getMessage ());
: q" A4 q0 `$ x! W& k9 w( O/ n# x5 `: v }
+ i- z* b/ f9 S V( K4 n6 J; E- r3 O9 h* d( ]0 O* K
try {
8 o/ A$ m' J5 }/ q- t9 D Heatbug proto = (Heatbug) heatbugList.get (0);
; c/ }$ |1 O. M4 R+ A Selector sel =
- l; t9 n6 H' {8 @, J new Selector (proto.getClass (), "heatbugStep", false);( I# |$ }, F9 E; g- E
actionForEach =
! |6 S: S1 X1 Z. j% V% s$ f2 _ modelActions.createFActionForEachHomogeneous$call
* m9 _7 l6 L" C: r6 N6 a3 K c (heatbugList,
2 T9 e* z$ H! c- P" a6 T new FCallImpl (this, proto, sel,
! T+ H# B% N" S J1 G) m+ t5 Y new FArgumentsImpl (this, sel)));
% O$ m( ~3 I: M4 P8 T } catch (Exception e) {# s/ @0 A7 {1 C- Q6 f
e.printStackTrace (System.err);; n. q) [; J5 `. N6 M
}
6 \- K6 E5 y# E4 C9 p9 l5 z
5 c" d L6 h( P+ b( f9 ?* _ syncUpdateOrder ();# s! V/ {; i" ^8 Q1 Z z
0 R0 Q7 `+ k3 k1 w3 j5 H try {& f0 b# d' e* F
modelActions.createActionTo$message \; e/ H( C! r; o
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 T/ |2 j0 z3 \0 u } catch (Exception e) { C8 H( ]1 F3 i$ ~4 f/ ~4 n
System.err.println("Exception updateLattice: " + e.getMessage ());
3 S8 Z, v$ {8 U \ a* l) T P }
0 `" N' S+ x9 J
' c! x1 Y7 g3 ~5 }( c3 \+ |" i // Then we create a schedule that executes the% E/ e/ g$ q$ w0 t6 O
// modelActions. modelActions is an ActionGroup, by itself it
7 \' t# B* E/ H3 ~% v* F8 g // has no notion of time. In order to have it executed in
" L( ]9 U+ n0 ]7 t // time, we create a Schedule that says to use the
! `2 u7 T/ p- @% e // modelActions ActionGroup at particular times. This
4 D( Y& H/ x/ j7 D. V. x# l // schedule has a repeat interval of 1, it will loop every, S! ^, ~9 Z% M4 S* V' E
// time step. The action is executed at time 0 relative to. A4 W& K! [" r$ p5 W" ]
// the beginning of the loop.$ o. p( u, K+ J: R! G4 ^+ [ r
$ }7 p, n6 Q2 L* ^0 U- C
// This is a simple schedule, with only one action that is4 v6 w* v4 {' S/ I. |/ L2 [; v
// just repeated every time. See jmousetrap for more
) I9 u! O+ u7 d1 Y- x; r9 a // complicated schedules.
- B" Y, t- J" W5 G9 b; Z ( N- Q1 |. z" o! e: j, I
modelSchedule = new ScheduleImpl (getZone (), 1);
7 r8 Z0 g2 C, x8 ]" @7 M modelSchedule.at$createAction (0, modelActions);
4 ~- W* `! H# R" i$ [; r# H
' F5 N* w I, R. w0 u, u return this;
; w @4 D- ^ ?5 [. n5 ~ } |