HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 J/ q. ~) O2 s2 X1 E& F
; ^/ A/ G. _) K; y% H
public Object buildActions () {
& I/ C& P* Y5 B% D! ^+ ? super.buildActions();# |$ Q; c! t# f% V0 B# E# n
8 O3 T( u" i- Z1 J% A // Create the list of simulation actions. We put these in
. x* l) R& x+ l // an action group, because we want these actions to be
6 P6 W5 V Y- W% B9 r // executed in a specific order, but these steps should
% S& {2 w' a7 l' R8 W // take no (simulated) time. The M(foo) means "The message( F$ A. X" y( `$ ^+ y8 I8 q/ ^
// called <foo>". You can send a message To a particular
3 {3 _+ D# k- [ // object, or ForEach object in a collection.: T/ n6 h: T; b$ j/ c
) G+ |/ b; G9 Q* Q7 c6 g
// Note we update the heatspace in two phases: first run
& e4 | h2 U0 ^. S7 z& Y8 ?) S* v // diffusion, then run "updateWorld" to actually enact the
% n" B) ^* e. o3 A% o // changes the heatbugs have made. The ordering here is$ H _: r% Y) p: l0 A4 C( T$ c
// significant!
2 o! y3 l- U% |, G* B& w3 W $ b" c% {2 W+ f& y3 i. J q
// Note also, that with the additional
# l/ [% ]0 s( X+ P% M) u // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 b, U& S% E. f) r- { // randomize the order in which the bugs actually run6 W2 U! y- m- g5 x, N- r7 `
// their step rule. This has the effect of removing any
4 P& Y. x2 z" I2 d // systematic bias in the iteration throught the heatbug
$ N9 z2 l; ?, K6 j6 Q- \ // list from timestep to timestep
/ X F' |/ h1 g, X) Q. j3 b a5 }9 z% B3 t' A. X# Z8 k
// By default, all `createActionForEach' modelActions have2 C h6 X0 W6 G3 p; D
// a default order of `Sequential', which means that the
" {* Y7 w0 t/ b; C // order of iteration through the `heatbugList' will be
% v* C/ S- X5 v, Y, a1 F" h+ T // identical (assuming the list order is not changed
: \8 P5 _% |( M) ]8 N6 A! _ // indirectly by some other process).
/ ]# b7 Q+ \" N. M, u
; C5 u1 N9 r( B2 A9 N3 A: H( x modelActions = new ActionGroupImpl (getZone ());3 u8 w& k# I8 T r( h- R
: G5 r" Z6 X8 E' a, b5 i% D8 s+ G try {* g9 `+ o9 E- U
modelActions.createActionTo$message" K/ B$ }. X4 I2 \, u/ l0 I! I
(heat, new Selector (heat.getClass (), "stepRule", false));6 z9 I* G4 r {% g0 ~
} catch (Exception e) {3 A1 h2 t* n' g5 D l5 n
System.err.println ("Exception stepRule: " + e.getMessage ());' M0 _& X8 l/ T
}0 p9 _1 F+ w/ A% C/ h& ~
9 c0 y6 g5 G/ Z( f try {
8 z* {2 N; q! R2 m m/ T' \4 a7 Q9 t Heatbug proto = (Heatbug) heatbugList.get (0);
. x1 c, \$ R' w, a6 _ Selector sel =
8 g" v! J% i$ e9 n. Z) X new Selector (proto.getClass (), "heatbugStep", false);
! r; f2 r2 m: b8 c1 |! F N actionForEach =
/ P: | a* ^6 {# v* i: D modelActions.createFActionForEachHomogeneous$call/ O+ p7 ^. D0 ]6 U6 P$ D9 M
(heatbugList,
6 q! o2 M. n3 Q; U& w6 l; w0 Y5 x new FCallImpl (this, proto, sel,
2 v0 l+ _' M2 g$ E! ]" T new FArgumentsImpl (this, sel)));3 x# H9 J: {# u! {5 z/ H) ~: m
} catch (Exception e) {
+ m( ]; b2 h9 d, r w- }. [ | N e.printStackTrace (System.err);
, M+ K. u, i3 J! w7 ~ }
: z! e9 V7 [, o" v* p$ |
/ S, R b9 G! }3 i+ C3 V q+ _/ ~ syncUpdateOrder ();& m- J! g: i+ Z `
5 c5 \* [0 Z& p* d2 `
try {
2 a0 W/ [$ Z/ ]/ t# i modelActions.createActionTo$message
# A8 [# i9 c# j (heat, new Selector (heat.getClass (), "updateLattice", false));. y9 F8 ]( {8 Z
} catch (Exception e) {( R! x7 Y X( ]$ g0 u1 M. ?
System.err.println("Exception updateLattice: " + e.getMessage ());7 Z6 I; J8 z# l& X
}
5 I3 a- U7 ~4 Q- @% ^ , b5 Z* a0 f; c. f8 d) ^9 l' i1 _
// Then we create a schedule that executes the
+ b; P4 O% Z. ]" }* u" f // modelActions. modelActions is an ActionGroup, by itself it% I9 l7 n M% U
// has no notion of time. In order to have it executed in: J: C% F% {2 a8 ]0 _
// time, we create a Schedule that says to use the
# N" @8 L4 j& |: K% l) k- n/ S" K // modelActions ActionGroup at particular times. This
$ k7 K' I9 U: J) u/ Y // schedule has a repeat interval of 1, it will loop every0 B" {0 j, z: A, N1 Y
// time step. The action is executed at time 0 relative to9 h; K! N( R( l; s5 ]
// the beginning of the loop.' o. Q* k" X$ ~' M9 O
6 t* b; m) |. w) Y& d- k) ]
// This is a simple schedule, with only one action that is
! R* d# w; b# X // just repeated every time. See jmousetrap for more7 w/ v# m. p; |. k2 t2 O
// complicated schedules.: v# K$ w# H* M( X
4 W0 N$ k x9 `$ Q1 `: x
modelSchedule = new ScheduleImpl (getZone (), 1);
5 a2 L$ k6 G6 O+ m3 b& w5 W O: o$ ^9 ?7 D modelSchedule.at$createAction (0, modelActions);% |1 [- e1 U9 E& R: v5 e; [. Q* s
# C+ q% A. r# Z! ^; W return this;, s" @; Y. c: j* u* ]! U9 v
} |