HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 E2 A1 S# f( _ }$ J1 e2 S7 w
! V3 L: I& {% \# m; m. j! x
public Object buildActions () {
( ~: e; \- a6 t9 S2 ]6 b0 u0 r1 T super.buildActions();
* \3 y/ l, P2 M. z# ^! c 6 {4 G5 R+ Q% ]- ]" P; J$ ~! F
// Create the list of simulation actions. We put these in
3 f& r% n$ R, ` // an action group, because we want these actions to be
. [3 {$ t* W1 k6 ?8 [: H# f ^ // executed in a specific order, but these steps should
# l+ _, Z& b' N! q* v* F! u- _ // take no (simulated) time. The M(foo) means "The message
! r9 w* K/ b9 E8 }* X // called <foo>". You can send a message To a particular2 B. x( S0 _; y( g) E
// object, or ForEach object in a collection.
! F9 q& S: \) o3 |+ N' h2 O
) R* `, q! U% x4 b8 Y( r // Note we update the heatspace in two phases: first run& L; H( t& l1 V
// diffusion, then run "updateWorld" to actually enact the
; b2 r& @* n3 B // changes the heatbugs have made. The ordering here is
* Q+ V5 b5 d, [4 [7 X4 C/ o6 k8 p // significant!
1 H6 K; h0 t; b* j$ h. ]
" _4 ~5 ~: w" u // Note also, that with the additional# e x' m4 T. y! H1 Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. h4 `% w; c' R' g" I1 N // randomize the order in which the bugs actually run7 T4 _, z2 L- K5 X- N% g
// their step rule. This has the effect of removing any
! p+ |$ t7 o" @, Z // systematic bias in the iteration throught the heatbug
1 k: O8 U4 @& I# X. o' v8 c4 Z // list from timestep to timestep
/ K* l& @9 c8 a6 [ 7 z! W$ w8 V8 q# |/ v+ d2 p
// By default, all `createActionForEach' modelActions have
- A8 ], O k# h% a# I // a default order of `Sequential', which means that the! }; P$ X, ]; }- I
// order of iteration through the `heatbugList' will be+ |* ]$ l4 N5 S, @! m# }
// identical (assuming the list order is not changed( y. K/ n/ ?) ^9 a
// indirectly by some other process).& C1 Z% {4 k3 r) @9 ~8 a; C8 d% g) A
; W9 t6 C5 P" n, L modelActions = new ActionGroupImpl (getZone ());9 @2 q/ P+ D8 M
0 j- x1 ]. o( q2 B# S- O- T
try {
" t; B1 Q ^7 Y modelActions.createActionTo$message7 B$ }$ B6 @2 e3 }& }( d, S$ H- R
(heat, new Selector (heat.getClass (), "stepRule", false));* b# x0 Y% ~7 P# h3 w9 S
} catch (Exception e) {
0 v+ v& Q* A: [+ X System.err.println ("Exception stepRule: " + e.getMessage ());+ i6 l* S. ^6 P3 ^; g- i; n
}6 g! A1 f' }* Z9 T7 ?. |
* Q A! f$ D' e9 C5 M try {, |7 a3 |+ }5 |0 [: z4 B, A
Heatbug proto = (Heatbug) heatbugList.get (0);$ d' l: d' V b# t
Selector sel =
# [8 Z; F1 l# X+ T new Selector (proto.getClass (), "heatbugStep", false);
- m/ `3 v: P4 K1 ]" E# v" s: f6 e actionForEach =" Z4 i' ^& ~: ?6 Q; ?
modelActions.createFActionForEachHomogeneous$call! o% ?$ `; Y- `
(heatbugList,
. H. X3 S) P) Y- M) f: L new FCallImpl (this, proto, sel," n2 I- m; L5 g5 d( v
new FArgumentsImpl (this, sel)));
- N6 O+ M, e$ y5 r. L: } } catch (Exception e) {9 p- |! {& e5 D; G3 R( ]# J4 V1 P `! Y
e.printStackTrace (System.err);0 n7 c/ f9 a$ X8 j% Q
}
# P0 X x8 `; q7 h$ n9 p+ M: P * W2 I6 O9 G- L; L% x# c* q
syncUpdateOrder ();
6 D% e7 R9 g8 d; J
* H& j7 [. F6 t; C try {
! a9 E" e/ }, _, ^! r) |" l* R8 m modelActions.createActionTo$message * S0 N+ g' r( X5 }
(heat, new Selector (heat.getClass (), "updateLattice", false));0 \! w4 B# Q- U
} catch (Exception e) {1 R @ ` |- q+ n) v) U- S5 v& B
System.err.println("Exception updateLattice: " + e.getMessage ());
/ i4 J+ L( B" O }
* ^: j1 p [" n) i
4 F& h; i4 `- f1 M // Then we create a schedule that executes the+ b3 B1 y$ n3 {% J9 x, ]
// modelActions. modelActions is an ActionGroup, by itself it
3 T$ g, _ y# A4 l0 e, t% Q% [ S // has no notion of time. In order to have it executed in
! v; u# _" `' ]; x" v // time, we create a Schedule that says to use the7 E3 {) b. }& @4 I E
// modelActions ActionGroup at particular times. This
2 y$ J* @- n5 }( ~0 R6 L, K" O // schedule has a repeat interval of 1, it will loop every: m: g! k6 U) h; p1 s8 I
// time step. The action is executed at time 0 relative to8 `6 ~5 }; M; x
// the beginning of the loop.8 f0 K0 ]) Z" k- F% S4 H
# i- B7 h5 j2 o0 Q; `: D) R3 r
// This is a simple schedule, with only one action that is
3 h# X' x8 m9 c" W // just repeated every time. See jmousetrap for more
7 |/ M; D: [+ I$ c // complicated schedules.
8 B1 [8 W" g2 z" O0 ~0 t ; i6 d, a4 e- k. w8 U# E
modelSchedule = new ScheduleImpl (getZone (), 1);2 p8 N+ n. P4 p9 }8 R
modelSchedule.at$createAction (0, modelActions);
& w5 S# }. n1 {% _- K6 [! ` 7 o8 D* T: l" {8 L
return this;% b) w$ g: H& J& \/ ]
} |