HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* {, z' @/ G9 N7 p: M5 ]9 `9 L Q3 F9 @7 e: t! B; Q% y& v
public Object buildActions () {3 R# G$ W5 A/ B
super.buildActions();
% C+ t X# ?0 M 3 S6 X5 d+ t2 e n6 r
// Create the list of simulation actions. We put these in
# {7 c, V5 D( S3 X$ X0 E1 x // an action group, because we want these actions to be
9 c B. }1 g# A) G // executed in a specific order, but these steps should
% I' k6 T! b4 w5 ^8 L1 \ // take no (simulated) time. The M(foo) means "The message
$ O, L/ T6 F: [2 Q // called <foo>". You can send a message To a particular2 ]' o# m$ ^, C1 X
// object, or ForEach object in a collection.+ ~, {+ ^3 {3 f) C- e
" }" s" B0 S; c: ^* m // Note we update the heatspace in two phases: first run- r1 l- o5 A) ~
// diffusion, then run "updateWorld" to actually enact the
' I# L2 {5 Z1 Y3 U // changes the heatbugs have made. The ordering here is4 i! ~: V) @6 T- [
// significant!
0 g8 D' e) O0 L- G+ {
5 n$ m3 {0 y' D- F // Note also, that with the additional8 H# N: d* M: i3 I: @" x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 n8 d- D% |+ K // randomize the order in which the bugs actually run2 R, l" w7 S2 F2 v; i4 n
// their step rule. This has the effect of removing any
' b7 q) v& `) D7 @/ F- ` // systematic bias in the iteration throught the heatbug
. A: ]% Y* M+ v // list from timestep to timestep
9 C5 V) V7 D5 K7 V* b+ q9 h
. X$ f" q9 _# y: W+ M+ S // By default, all `createActionForEach' modelActions have
- f$ J5 d( Z$ ^6 ? // a default order of `Sequential', which means that the
4 A+ Z& V8 g5 C! q // order of iteration through the `heatbugList' will be1 C& A s$ s: i3 y# J4 ^) Y
// identical (assuming the list order is not changed5 U" @( x( a5 B N+ \8 U3 c
// indirectly by some other process).
- f# N! W/ p* M- t8 u! a
1 ^6 L" Q8 I" |" N modelActions = new ActionGroupImpl (getZone ());4 |5 n4 [* ]! _8 a/ z
2 y; c, j' s0 g6 x
try {
) l3 X# c3 E' w% k; I" e8 O modelActions.createActionTo$message
6 Y. k+ t+ I% z- l3 @& s (heat, new Selector (heat.getClass (), "stepRule", false));
& S W. e6 s: v/ S w$ d- L( `2 g/ D0 o } catch (Exception e) {
9 @3 W+ a) p& @2 i+ | System.err.println ("Exception stepRule: " + e.getMessage ());8 A8 Z3 n8 ]+ L8 }. g5 H6 M
}
. i9 w' K5 `' [' ?, v6 ^* h
4 R1 j7 t" N: x try {
; z+ x) ~9 }3 u6 b Heatbug proto = (Heatbug) heatbugList.get (0);3 q4 g6 S; p! y3 D: t* \
Selector sel = # g% J2 V0 V* C: `3 V8 Z
new Selector (proto.getClass (), "heatbugStep", false);
% u. I. @/ C; V7 v ?" W actionForEach =
% [- A. u9 w* |: C& Z modelActions.createFActionForEachHomogeneous$call) H$ z1 P9 C$ Z2 q
(heatbugList,
) L7 i; J, u \2 T+ o9 j- F9 I new FCallImpl (this, proto, sel,/ s4 ^8 @$ K2 |' J' z) W5 W) M9 t
new FArgumentsImpl (this, sel)));
* j5 e8 u( ]3 o) a# \8 V } catch (Exception e) {: d: y& M9 a% J4 K3 T
e.printStackTrace (System.err);/ K6 N1 _4 q: G& g( P8 R9 Y5 d
}3 b! v1 q2 z: c# U
" @6 H" r# G: u% T. K' I syncUpdateOrder ();% B" N" A0 R& Y
% n& j) J& S6 P* a/ R# |
try {
: [* T" |5 P- _7 N modelActions.createActionTo$message
/ _4 B8 j l( e9 e3 y+ {; s (heat, new Selector (heat.getClass (), "updateLattice", false));
& g q4 X$ i j' Z } catch (Exception e) {
& x. o7 h5 Q' W System.err.println("Exception updateLattice: " + e.getMessage ());$ F+ C. _1 Z+ N4 w
}
! E# [; E6 `4 A# G# ^, T7 w
4 H2 x% S2 M5 z- U- b // Then we create a schedule that executes the
# d W7 h0 o5 ?* C+ w# s // modelActions. modelActions is an ActionGroup, by itself it
* E' f7 _3 M; l' m // has no notion of time. In order to have it executed in
: V8 H2 f0 \9 L7 r% X, D // time, we create a Schedule that says to use the
3 i. D3 j% ?* D7 I // modelActions ActionGroup at particular times. This! c# q6 T& `! `5 |
// schedule has a repeat interval of 1, it will loop every
; c. ^2 p C! k2 T% W // time step. The action is executed at time 0 relative to3 q: J; q( _6 h& g9 g
// the beginning of the loop.
. F5 P6 i: c* m- t# F* j& v& Z% b" u! [5 n' K: Z$ t
// This is a simple schedule, with only one action that is
/ m, V r R. k$ O z // just repeated every time. See jmousetrap for more
7 S, \; [, J l+ D$ n // complicated schedules./ _$ L$ x3 _8 `$ m9 ?
2 T7 v* z& y d1 W v
modelSchedule = new ScheduleImpl (getZone (), 1);7 ~, k: T* z6 m$ a
modelSchedule.at$createAction (0, modelActions);1 o+ ?( R* k- d0 w( C, a
* N3 ?6 r& R$ o i
return this;! b" N. ]/ i J+ Y7 d2 {
} |