HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* M: D; e# U; b x
* `9 n8 G- q$ p2 n public Object buildActions () {
% f: Q9 n9 z/ }! A9 H super.buildActions();6 M. c: J( j1 Z8 L
6 M6 `5 T% i9 z2 W$ J7 d // Create the list of simulation actions. We put these in
9 f4 k+ c' e' ]5 G // an action group, because we want these actions to be5 B# I+ t' W! ]" `5 E7 M! q
// executed in a specific order, but these steps should& a1 b; ~6 e: u4 z1 H. O* p
// take no (simulated) time. The M(foo) means "The message
( G- @! V/ Z6 U // called <foo>". You can send a message To a particular
1 ]: N9 a- H3 x, E" B/ c: N, [ // object, or ForEach object in a collection.
' e5 H, @1 x7 u
( b8 c% ?+ l- `& U# U+ C( C // Note we update the heatspace in two phases: first run
, c# S2 ~! K" @0 u) `$ H // diffusion, then run "updateWorld" to actually enact the+ F) {! }$ R, A7 Z
// changes the heatbugs have made. The ordering here is
" I3 H1 P A/ E // significant!
; k4 E5 `4 Q1 Q 8 f- Q0 @7 ]6 ~7 v
// Note also, that with the additional
6 ]( A2 R' S: I- [, X: b9 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
Z4 a9 Q1 i' }7 V5 L" B. n* v9 z // randomize the order in which the bugs actually run
( y6 s( o' X( f6 x // their step rule. This has the effect of removing any
2 h& I* ?3 x9 l" U* i3 X& ^. ] // systematic bias in the iteration throught the heatbug
3 B1 u- T+ J( G0 y! f // list from timestep to timestep
3 ~6 q# O$ w! a4 R5 s2 J6 r+ G( V: r 3 N2 G& s/ g' q! n, H6 a
// By default, all `createActionForEach' modelActions have0 l' K+ }5 [) Y+ N% B1 g
// a default order of `Sequential', which means that the! Y2 [% [+ I6 h3 g
// order of iteration through the `heatbugList' will be
% T6 T# i- E7 d; z% u% }5 {" H // identical (assuming the list order is not changed
# v. L+ K# a9 w; M4 k2 Y // indirectly by some other process).
3 h& D b5 `% v. D, C
i3 b# E p3 a modelActions = new ActionGroupImpl (getZone ());" q3 F2 L4 S& q7 |4 P1 t, M
2 W# x5 G# `- H5 u7 i0 K' w6 B6 X# } try {
+ `: G3 X. ?8 l ]6 r$ [6 U3 N: _ modelActions.createActionTo$message
9 B; k" _, [# r (heat, new Selector (heat.getClass (), "stepRule", false));
% N5 d6 t7 E8 K" |% E; t } catch (Exception e) {
3 _/ `/ j# {6 V System.err.println ("Exception stepRule: " + e.getMessage ());
! o/ K3 Q5 R, V }
# S& F4 l9 V# @. M
5 p4 U, S \/ {/ m& k try {' S( R' x: `9 Z, _2 M `
Heatbug proto = (Heatbug) heatbugList.get (0);- ~1 w+ }, U/ E1 R" q
Selector sel = 8 [, w' p6 ]8 F3 e. l; t5 U: o
new Selector (proto.getClass (), "heatbugStep", false);
; }1 t$ a" k; B/ ?, |2 i7 J, p. f actionForEach =
, T1 e6 h( ^+ b" X8 q modelActions.createFActionForEachHomogeneous$call$ H* u: s6 L9 R5 n" }1 u" c# ]! N7 z
(heatbugList,
2 l9 q- e/ z/ d* c/ |2 ?1 ? W new FCallImpl (this, proto, sel,# ?1 Q: _' @! p. d; S1 V, X4 s0 g
new FArgumentsImpl (this, sel)));
1 g, ^, h$ o( r } catch (Exception e) {
" F% o$ ~# b3 d, A6 C- @: x/ g e.printStackTrace (System.err);; o) W3 w6 B! W; N
}
. C3 f/ E. _# P' V , T# m6 j! S$ {% z. _
syncUpdateOrder ();
& F& q9 r5 \ _1 @2 a0 i+ u9 U4 w! P
try {1 |# G6 A6 A* y6 v9 J
modelActions.createActionTo$message - }. k8 k' g- \6 z! s+ o
(heat, new Selector (heat.getClass (), "updateLattice", false));6 P5 W" M: V0 e8 b- z7 h. ?; U
} catch (Exception e) {2 c3 X( i. J5 H! Z8 A
System.err.println("Exception updateLattice: " + e.getMessage ());
% w6 U4 Y O7 Z# ~ }" O0 M8 i$ s& F' U5 W
, a# {/ z2 E9 D3 s, I/ b // Then we create a schedule that executes the
& p9 p3 R+ l" J; Q // modelActions. modelActions is an ActionGroup, by itself it
% \) O9 Z1 r5 g# @ // has no notion of time. In order to have it executed in5 u) v" M: u8 _6 }! ?! J+ L7 d
// time, we create a Schedule that says to use the
, f- d& y0 i5 z4 U: h7 j( U // modelActions ActionGroup at particular times. This9 w( q1 m8 H8 Y( W/ R$ [8 Q
// schedule has a repeat interval of 1, it will loop every: g% m$ ]# l! T# I
// time step. The action is executed at time 0 relative to) \8 T3 `8 g# H
// the beginning of the loop.
& g( Z! n% E' i7 g7 E8 N# a4 _' o; z0 s5 z" A5 |2 J9 y+ U
// This is a simple schedule, with only one action that is- [: Z2 b- g7 d7 W$ o
// just repeated every time. See jmousetrap for more5 X& f$ V5 \& g
// complicated schedules.
# ]( M6 D6 @) x) W* g) A; ~
7 G: h# Y: s2 | modelSchedule = new ScheduleImpl (getZone (), 1);
$ l* m5 W+ u* L modelSchedule.at$createAction (0, modelActions);
0 i. G1 x( ]! K5 E7 i9 l! O. Z
8 _# S" U/ x, p3 u V return this;
! y. k8 d# R7 l( r } |