HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" {! |9 ]$ N3 F# d$ C6 Y7 O
) W0 v. R) z0 N. S* r public Object buildActions () {
- y8 C" ~3 y9 w, k* Q; } super.buildActions();
& W- r& t- ]) }/ O
) a" K. G; w6 w" i# I T. ]' K // Create the list of simulation actions. We put these in
" Z4 o) O" Z, Y // an action group, because we want these actions to be
1 D- m: h" ^1 C3 I+ B4 d, u( G+ g$ s // executed in a specific order, but these steps should
7 G7 [3 w/ n+ X, n) d5 a // take no (simulated) time. The M(foo) means "The message" _$ s! d7 B3 l
// called <foo>". You can send a message To a particular
, y" J, ]. m. N' ^, p: R // object, or ForEach object in a collection.
3 P" {" p! M- {" W
( S, w3 Z; H1 ^- _0 j // Note we update the heatspace in two phases: first run
$ e0 j3 j8 J0 J c // diffusion, then run "updateWorld" to actually enact the) [; y. T* k; o$ m# [+ @
// changes the heatbugs have made. The ordering here is
; Y1 K3 u6 c; {! E, P5 o# g8 h // significant!
0 Y- S1 ?% ?6 E" j . O# a0 w1 d0 w) B9 A: Z* z- h* U: |
// Note also, that with the additional
9 s% ?: {% x0 p; J3 K: \4 V7 r n3 j // `randomizeHeatbugUpdateOrder' Boolean flag we can/ }, v( T4 |; S. l
// randomize the order in which the bugs actually run" o4 K; \1 ~% E m
// their step rule. This has the effect of removing any
9 U* k; c( u4 Y: a) K2 K$ B // systematic bias in the iteration throught the heatbug
4 M1 b p! X: z% z9 {( | // list from timestep to timestep
: w$ T. W' G6 `3 N A
7 V% z6 X8 I+ }, X3 F // By default, all `createActionForEach' modelActions have
; ?: W" u( I$ W$ R // a default order of `Sequential', which means that the# E& z8 L2 U1 }4 a6 G
// order of iteration through the `heatbugList' will be
( N+ M; S+ [# @5 _ // identical (assuming the list order is not changed6 ]! T+ l5 M3 j @, {
// indirectly by some other process).
, r! I J( ?. ]$ p1 b X
& T6 U1 J( a- o0 I0 Z$ Z5 R modelActions = new ActionGroupImpl (getZone ());+ m: @" b/ e3 e: V) Z3 v# q
6 q& `! F J9 @0 s0 c& @ try {7 R+ C4 r$ v N- k1 U, Z- Q
modelActions.createActionTo$message
4 b( j/ Z3 I6 Y2 g& J (heat, new Selector (heat.getClass (), "stepRule", false));) ?( y+ b s; N8 x& }9 p. r3 p& K
} catch (Exception e) {5 `3 b& A1 ?5 G+ v4 P$ E
System.err.println ("Exception stepRule: " + e.getMessage ());
1 ? K" u& n, l( k }
8 G% h+ X3 K9 M3 ?$ l( `9 ]' i
4 X6 H9 G6 O5 F- E+ C" C- e5 P& o try { w+ g$ t9 d1 J+ I; A w7 A7 Z3 q
Heatbug proto = (Heatbug) heatbugList.get (0);$ u W) O1 C/ q) s4 y* r, [
Selector sel =
7 J' e6 u- ^5 k0 l new Selector (proto.getClass (), "heatbugStep", false);
$ S* F; j: h. a7 v/ S actionForEach =
3 b7 c6 }$ ]' B# D modelActions.createFActionForEachHomogeneous$call$ V: ~1 ^- @: s8 X" _: @
(heatbugList,4 Y4 e6 M1 q+ ?; G, @
new FCallImpl (this, proto, sel,3 A; s0 V- w+ Z4 C# l4 ?
new FArgumentsImpl (this, sel)));
& e0 B6 {$ h( Y4 l* ~4 \( S } catch (Exception e) {
# k f+ |% e( s e.printStackTrace (System.err);
+ c* F% K9 M+ V- y( v) h }( m$ l. |+ P0 d* B/ `( w( C, g
6 C# i- l" |( } [& ~8 `
syncUpdateOrder ();
) r' a& L& C% u, b- @
0 Q; a# F. v' o5 I try {5 k( ~+ W3 {1 C' u
modelActions.createActionTo$message
" B" G+ i: {* \ (heat, new Selector (heat.getClass (), "updateLattice", false));9 D# W4 K, F# U( w+ G* ]
} catch (Exception e) { k# a, Q3 q5 }, t: @9 x6 U1 p; v
System.err.println("Exception updateLattice: " + e.getMessage ());
; G/ f1 z8 s! p# @ }/ X. X! h2 [0 a
9 l2 I, x1 y- q6 V
// Then we create a schedule that executes the
a% u0 d# v z' M" e // modelActions. modelActions is an ActionGroup, by itself it, {6 m: |" ~$ T! X8 Y, n0 z- z
// has no notion of time. In order to have it executed in
$ g% n( B+ N% ~5 g* B/ l2 t // time, we create a Schedule that says to use the
) R; E) y1 }$ f& G/ {9 } // modelActions ActionGroup at particular times. This
, @1 h7 }! T2 `4 b. N& M: f; V U S // schedule has a repeat interval of 1, it will loop every
5 z: t( I# u$ |. O" C9 \3 [ // time step. The action is executed at time 0 relative to; E+ B0 t* Y, r
// the beginning of the loop.2 t& I) K' x5 w# d$ J4 E; l4 R+ S
6 [* }$ W6 N/ e. g: ?
// This is a simple schedule, with only one action that is/ H4 h0 m) ^2 q4 r7 V$ z6 Y
// just repeated every time. See jmousetrap for more
9 {+ H( B8 w$ @6 B; J& F // complicated schedules.
: ]: m* ]3 x! A: K$ \2 A8 F 1 E( S' d4 f# |
modelSchedule = new ScheduleImpl (getZone (), 1);
/ [' L7 N; ]6 i& Q& k a modelSchedule.at$createAction (0, modelActions);2 t8 e7 y: }) @& k. n6 _
: s! j, ^# q" _4 n4 k return this;
6 }- ]; e; x7 z+ ?0 i } |