HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 k0 j4 _7 B0 `( n0 l! c) `" O6 w7 p1 f' ?. _: G" e
public Object buildActions () {/ F' z: v5 v9 Y( i K: q% M4 e
super.buildActions();
: M; ~6 g' _- j0 G. }
8 J8 G: M( l$ M. L G6 i // Create the list of simulation actions. We put these in7 b2 u# g* C) P) w# p
// an action group, because we want these actions to be3 w3 W( m% @5 H# y" k& K2 y
// executed in a specific order, but these steps should
o3 e( B! u1 h$ p. T // take no (simulated) time. The M(foo) means "The message) N% z" m# X# R8 G' I
// called <foo>". You can send a message To a particular
" _4 [. d9 z( h0 F // object, or ForEach object in a collection.
8 A( z* P# q. ]+ i% Q+ T
, b, H" w: e; [ // Note we update the heatspace in two phases: first run
+ v* t* V! v, a7 N3 ` // diffusion, then run "updateWorld" to actually enact the
1 L; Y6 w+ R6 h- B' ~; I // changes the heatbugs have made. The ordering here is9 m+ q. a% b( G9 g& K" I0 w. P# T! A% I
// significant!
% F( D3 ?) x( e0 A% @
; T; c7 x) G, M m a7 j; U; |6 ` // Note also, that with the additional
$ ` {, A2 d' \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
& E$ `. K0 n% c: w // randomize the order in which the bugs actually run
2 h U; f) F/ l // their step rule. This has the effect of removing any9 P2 C# k% J7 C! |' @% g% s5 L' ~! c u
// systematic bias in the iteration throught the heatbug* p! \/ [4 l+ t& d- V+ F8 `& x
// list from timestep to timestep
6 R( Q) E" @) j
1 m: w9 m" n- e" Y6 X // By default, all `createActionForEach' modelActions have
& I2 L" ^+ M0 e // a default order of `Sequential', which means that the8 _. c; J% |# F* i) {
// order of iteration through the `heatbugList' will be( Q6 D; a3 x: ^' h
// identical (assuming the list order is not changed
5 y5 c8 {; `+ O+ Q' U- z // indirectly by some other process).
( A2 [; N: X4 D
: B( u- _8 |! M. f! P# J' P" S modelActions = new ActionGroupImpl (getZone ());
8 Z! L" @" q- B3 r0 E5 l# H6 I1 Z# n2 Q' {+ O' V
try {
: ^% R5 o! ~ ?( a modelActions.createActionTo$message
7 h3 h$ w6 V3 H; | (heat, new Selector (heat.getClass (), "stepRule", false));
! F L$ {( e4 d3 H' q } catch (Exception e) {
) \' p- F" Y* D System.err.println ("Exception stepRule: " + e.getMessage ());
; I8 J. [# j# ~/ \+ d# V& f }
* H+ O) A5 M( U, C; B; T1 d% J9 r' V
- f5 J! b7 D% a7 T try {
6 z$ p2 M" I* ^' ^ Heatbug proto = (Heatbug) heatbugList.get (0);% t/ H; S u* f% j' H9 u: \
Selector sel =
5 \ {& Z6 j% |: Y new Selector (proto.getClass (), "heatbugStep", false);
; @0 }0 z! S& J) f( L actionForEach =
( A% N" }2 D/ _* l I modelActions.createFActionForEachHomogeneous$call
s9 E; D- F+ W) V% M' M' D4 G' e (heatbugList,
4 Q2 H7 g6 H* v new FCallImpl (this, proto, sel,
* p$ @1 j# l" d$ S0 t new FArgumentsImpl (this, sel)));/ x' m! g0 u# q: D, y5 U
} catch (Exception e) {2 i6 T( w6 e& h
e.printStackTrace (System.err);
2 f' y5 L {' J) I$ U& H }, }2 j, z; Y6 ], C/ n5 U8 \
0 D8 o" c5 h! Z5 m5 `8 }( t; _ syncUpdateOrder ();8 G3 N4 ], c0 q8 z
x$ y" r2 g5 \4 i
try {# t0 \! y) L4 l# m# V2 s7 c1 a
modelActions.createActionTo$message , S" _! X2 A$ l1 [4 W7 ^3 h
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 ]: c. P+ Z9 `3 \0 D4 H& { } catch (Exception e) {6 }- t1 k2 I, i" k
System.err.println("Exception updateLattice: " + e.getMessage ());/ U4 c6 D1 |4 H4 {# B
}
! H, }+ G- a, ~! l; g
, s# ?; ?7 z! |5 i B6 }; [' g // Then we create a schedule that executes the
0 r5 @# o1 O( i9 X // modelActions. modelActions is an ActionGroup, by itself it6 b* l8 `% J P6 [2 z* l
// has no notion of time. In order to have it executed in
. ^' ?# k7 l8 g8 `' ?4 |- q+ S // time, we create a Schedule that says to use the
: D- o5 O5 ^% a# e U3 b // modelActions ActionGroup at particular times. This
: y% ], Y! P* p8 N$ f0 T8 J // schedule has a repeat interval of 1, it will loop every0 V% f% o1 _& _8 @: t0 M
// time step. The action is executed at time 0 relative to, {/ R$ c8 k H, V% S( U
// the beginning of the loop.
# ?7 T# j! `, s1 ~" i4 s* \$ {7 s
+ t: f# H8 p0 j // This is a simple schedule, with only one action that is
1 Z! o, I2 A6 w9 ]1 ]0 T) ^3 L // just repeated every time. See jmousetrap for more
. H3 i v# b! w1 q7 E; ] // complicated schedules.
9 c$ ^1 `2 s- ]2 a- E' G- `) t ( [. k' y! ~. k1 v
modelSchedule = new ScheduleImpl (getZone (), 1);
( k3 v4 I/ |" G s modelSchedule.at$createAction (0, modelActions);* T4 [- l" C. ?3 D, \) Z
- Y# e! [0 d8 N( A$ P; s. b return this;
" G& [8 V" l5 V' s2 R: t' F } |