HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 `. B h7 p5 C( e/ z+ Z
" j, V; D% ~' K+ p5 O public Object buildActions () {
, N& u8 K: } n+ e" p( `: o, @ super.buildActions();; v# d$ O- K6 J. m: N6 s
, m: q: ?: Y3 U4 }9 s }% V# o( z( n
// Create the list of simulation actions. We put these in
8 M% y! y6 o- @2 p4 ] // an action group, because we want these actions to be# S- Z' B, l5 A0 d6 F
// executed in a specific order, but these steps should
1 s" `! n6 [: x `' z* l4 ^6 V // take no (simulated) time. The M(foo) means "The message
6 ^: w; L' f4 ]. M4 D9 j1 i- s; O // called <foo>". You can send a message To a particular
4 Q; S6 p; g) l3 x7 k // object, or ForEach object in a collection.
: J( ~' U4 v8 e0 Q! y: ]0 t / C' I- [$ c+ f5 o; R O! Z" W
// Note we update the heatspace in two phases: first run
7 K+ x; \0 S3 N$ r& G# x E // diffusion, then run "updateWorld" to actually enact the
3 d9 U( i" _' T // changes the heatbugs have made. The ordering here is
6 ?- K" V9 D/ @$ s( V // significant!
( Z+ ?# Z& Q# b" j
8 u8 C3 Y" x/ `1 E( @5 z // Note also, that with the additional8 H0 C( R+ E+ l% `* i. e v: Z# a
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 |) L, Q: `2 j
// randomize the order in which the bugs actually run9 n0 L8 ~* N5 V" ~8 f/ ~# H
// their step rule. This has the effect of removing any+ x- ~9 ]5 F( |! y
// systematic bias in the iteration throught the heatbug5 ]' L$ @# m; E* [( |' H A. n- s
// list from timestep to timestep
7 ~3 H. U* U j( K7 {8 B ; _! y; x( m) E9 @
// By default, all `createActionForEach' modelActions have/ A: [/ C$ d; p) h+ W, O- X5 R% m
// a default order of `Sequential', which means that the- S* p0 H3 a/ W
// order of iteration through the `heatbugList' will be
% a2 N1 ]' ~3 r4 x; U0 s% N // identical (assuming the list order is not changed. t2 a1 @( x- i
// indirectly by some other process).
' B1 a1 c- A, I' o. \4 r7 y, w
; i) P, f0 r# O modelActions = new ActionGroupImpl (getZone ());% a b" B0 @- N4 z& g' n
; E5 _( d `& G7 F# ?8 ]& U' t try {
+ P E: o& C0 q& D1 k modelActions.createActionTo$message5 M4 @' @* L4 @; M8 G
(heat, new Selector (heat.getClass (), "stepRule", false));; a7 B! P. G7 m: i. z5 x) o
} catch (Exception e) {
& |+ E2 n f; j0 p% J& Z/ v System.err.println ("Exception stepRule: " + e.getMessage ());" X# Z$ A9 R, o2 B7 f
}4 |" W# O9 F" S! `
' H2 ]: ~" n% T. {6 }9 t# b) Q9 w try {
( e+ m' Y J( q Q1 l$ h Heatbug proto = (Heatbug) heatbugList.get (0);! H0 b5 ^6 H8 h X5 G- L
Selector sel =
& e9 C& O5 e+ `) q7 h8 A new Selector (proto.getClass (), "heatbugStep", false);
0 [6 R, c }1 l8 m5 p# f actionForEach =: Q* y, Y9 Y2 T) K2 S2 O, G( h
modelActions.createFActionForEachHomogeneous$call
a' U P5 K/ ~4 ? (heatbugList,
! W7 p) i. K' G* d' T/ e: n new FCallImpl (this, proto, sel,
: U6 D- t# e9 B! F+ @7 M new FArgumentsImpl (this, sel)));! i* @* y9 U' g& l; i" p( ~
} catch (Exception e) {
8 s( |- f: H; Y/ w) s! @) Q e.printStackTrace (System.err);
4 t9 s, \' o% ?+ f }( V7 {9 m0 f2 C. j. q% d6 n, ^/ T
. k% s% Z m" E* N. h5 { syncUpdateOrder ();# `9 L- F0 f% Z, m8 }
0 C3 C* C' k. }% J) }* M% F1 W
try {
- J! P5 D, I8 |, y/ x( r9 | modelActions.createActionTo$message ; ?0 H( u; i Z
(heat, new Selector (heat.getClass (), "updateLattice", false));& T3 S' G3 j0 m) P- K P; j
} catch (Exception e) {
5 U$ L9 V" S( w* b6 G3 D# y System.err.println("Exception updateLattice: " + e.getMessage ());
5 h- K5 m" O4 \5 ~ }5 p2 F# U) x, s( W
* o0 q; ~: \! | // Then we create a schedule that executes the
& w5 P; b2 C* u y // modelActions. modelActions is an ActionGroup, by itself it
' D8 P/ M3 V9 f* ]) K0 a // has no notion of time. In order to have it executed in
" @% n/ N! [1 j8 s6 H: g# v2 S // time, we create a Schedule that says to use the
+ Z' c9 I0 x4 _/ K& t8 c6 u // modelActions ActionGroup at particular times. This$ l$ N9 ^ n8 t% P$ G0 K
// schedule has a repeat interval of 1, it will loop every
( ?( i l4 l* z# I1 k% k) w // time step. The action is executed at time 0 relative to% P7 N' ~ e+ d8 X' \9 z; W
// the beginning of the loop.9 I) s8 p" f# U0 t% ]7 j
* j8 z' |' U j // This is a simple schedule, with only one action that is. ]5 P2 a' w6 S$ w+ @& w3 S7 E
// just repeated every time. See jmousetrap for more
8 ~* T/ ^9 C- ]0 E // complicated schedules.
( _. {5 y+ ~; f- w W6 [* W
A. W( T6 v. W modelSchedule = new ScheduleImpl (getZone (), 1);* P7 ^( P5 l0 P# B# Q+ }- Y
modelSchedule.at$createAction (0, modelActions);
. ]3 D N" U4 h# L
! [. ]+ {2 P; z4 {3 } return this;
' r7 w9 o+ g: Z: _. D } |