HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ k( j0 M9 ?9 `# f: [- {! X( F& X0 e- d r1 U
public Object buildActions () {
9 o' T2 Z1 q0 O/ f' n1 N4 f, R super.buildActions();
' L d; h: b7 Q+ s & A' ?" O3 v" R8 ?
// Create the list of simulation actions. We put these in
1 Y4 G, C" [& v7 \ // an action group, because we want these actions to be7 x% ]5 l9 m7 p& I1 q
// executed in a specific order, but these steps should, V# |( Q/ @: m
// take no (simulated) time. The M(foo) means "The message
5 Q& d& ^. ?: z2 Y // called <foo>". You can send a message To a particular/ D: h" Y& e+ `! n. K0 `
// object, or ForEach object in a collection.
) P/ p8 P/ `: j$ H; \ , d8 B- w- T9 r+ H1 s4 u
// Note we update the heatspace in two phases: first run
/ k: g& `8 d6 ]' o // diffusion, then run "updateWorld" to actually enact the7 P8 R" i, B" i/ r3 x% G; c K; e
// changes the heatbugs have made. The ordering here is
3 ]% @! n6 d# {! @3 X$ [1 |* X // significant!- q! b, T; n, U4 H, c" M- X6 m
3 K- B8 o- h' {- q5 s R; ^
// Note also, that with the additional
6 a: e6 ~7 E' |. I; ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can3 h. {* ~4 T3 {+ V/ p
// randomize the order in which the bugs actually run
+ y& d `- O5 Y( m5 S! N( @; A5 K5 o // their step rule. This has the effect of removing any
$ ]- f/ F6 q4 j* b# U // systematic bias in the iteration throught the heatbug
6 J4 [' F: s- S6 x2 h // list from timestep to timestep
) l2 @: M P) V' j# ]" i 2 A8 |- k; J# P9 l
// By default, all `createActionForEach' modelActions have
% W( N8 }3 N; F2 e, x5 v& ~: }1 J: I // a default order of `Sequential', which means that the
! c; U2 I% J8 g) ]% z, d$ _3 ? // order of iteration through the `heatbugList' will be( `1 N- x O. j5 q8 a7 @1 _
// identical (assuming the list order is not changed1 s7 C& ]$ T9 J4 A4 p
// indirectly by some other process).
8 J7 u+ O5 C/ J1 {, u s
, r; O" ?: W! J% W modelActions = new ActionGroupImpl (getZone ());
6 ]7 _( Y0 |1 W' @, @2 ?1 `
6 f t& F. g. X) e: p: y/ Z+ z try {* {6 B+ b0 ` {, N: s! @' _+ b: W. Z
modelActions.createActionTo$message3 s0 |3 J7 C/ ^- P! l2 k
(heat, new Selector (heat.getClass (), "stepRule", false));; \4 X9 ~! ?% R6 [9 v; f
} catch (Exception e) {4 Z" Y6 j' W% e8 U$ X( f
System.err.println ("Exception stepRule: " + e.getMessage ());
! o: X+ s8 U3 n6 s+ Z- x. ] }
" C) J/ l2 i) \' z7 x, ~8 v6 Y! r1 Y/ Y
try {1 N0 T0 c0 m6 R, N
Heatbug proto = (Heatbug) heatbugList.get (0);
: S( c6 t$ X* U! A" k* l) J Selector sel =
0 V+ {/ x- ]; C! v' p4 Y& a: _ new Selector (proto.getClass (), "heatbugStep", false);
( ^9 Z d" P" M, o* p3 J6 ^ actionForEach = @ S3 ?0 D- W/ o) v0 P
modelActions.createFActionForEachHomogeneous$call
& U! I! @5 K9 B$ _& G, k (heatbugList," M5 ?. F" G6 c5 O+ U, Y; B
new FCallImpl (this, proto, sel,$ `$ S) p# b; J2 Z1 S+ h
new FArgumentsImpl (this, sel)));1 l- h: x% V' ^$ I" I9 g% K) C
} catch (Exception e) {
7 B# [7 k! L2 L8 D3 R6 B e.printStackTrace (System.err);
$ B( L; d% o: B% F' @& ^9 ?1 e }
2 l/ }0 K- Q. c, {' m L3 w% p, y: t ! r4 g7 W0 @# S; u/ F# R
syncUpdateOrder ();
& H- j( e1 T. b8 t# N2 u+ ?+ W7 u$ [$ g
try {
, l' E+ e, U; r modelActions.createActionTo$message - z* A5 C9 Z K" o
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ C$ G, Z+ D5 k# \) ~ } catch (Exception e) {' w& t- M2 j+ P+ y6 Y/ \5 d2 V
System.err.println("Exception updateLattice: " + e.getMessage ());. ?4 J% Q" ?" o1 }& d
}
: h; O- j, @ S 7 i1 h. v- i# m1 u
// Then we create a schedule that executes the
- q2 n% b7 t; {' z. y) F" q8 M // modelActions. modelActions is an ActionGroup, by itself it- w' J1 [9 }$ N
// has no notion of time. In order to have it executed in* v8 P( h ?2 \6 }* p/ B
// time, we create a Schedule that says to use the' n* J. c4 ?- C- c
// modelActions ActionGroup at particular times. This
% p) x; w1 |) h2 f5 e9 ]# ^3 W // schedule has a repeat interval of 1, it will loop every; d, s9 z$ a; `
// time step. The action is executed at time 0 relative to
8 D: g$ l1 O$ D, G // the beginning of the loop.. H/ Q' s0 P$ I7 z8 M
3 b& x: U) ]2 S' Q G; l6 U/ {, g
// This is a simple schedule, with only one action that is
6 v; @( [$ G# W K8 o! G+ ~" ? // just repeated every time. See jmousetrap for more
) r7 D. L" ~% H // complicated schedules.
4 m1 E: E6 X& E N
# I, i( [7 [' m: @! { modelSchedule = new ScheduleImpl (getZone (), 1);: n' J+ P$ O" P* J4 D0 o" F) H) [
modelSchedule.at$createAction (0, modelActions);. ^0 q k. `. o5 d. i
) i1 _# y0 |, \2 y+ X/ O return this;
; @5 n! D# A5 {- A" }$ F } |