HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 C- |1 ~$ B- z- ]9 g' h0 r: V" {+ c* q& B
public Object buildActions () {
: o, g. C; v$ p0 ? super.buildActions();0 t2 g0 E3 e+ g$ y$ Y+ D
, V5 I! i$ Q5 n
// Create the list of simulation actions. We put these in
4 e$ O1 {: d% a9 h) \ // an action group, because we want these actions to be6 e3 F0 O/ T8 m: I) }
// executed in a specific order, but these steps should
4 i2 Y- H* d" L' [. g, q* f6 U // take no (simulated) time. The M(foo) means "The message
% y7 l( Z9 p! C7 g: w: I // called <foo>". You can send a message To a particular- L6 i3 w. `* d. ]* p: X d* m( }
// object, or ForEach object in a collection./ c% p' K; K7 F! B, F
# }% ~/ L( C r( W // Note we update the heatspace in two phases: first run
. g, ]5 g& {- X // diffusion, then run "updateWorld" to actually enact the
0 y3 S) d6 R% k8 i // changes the heatbugs have made. The ordering here is
% e2 N; j8 w9 y3 Z* V5 t( Y // significant!
/ V7 @3 {2 r9 R P" n7 a' u 8 ^$ D8 e* k5 `
// Note also, that with the additional
! R6 | b7 a; H2 x // `randomizeHeatbugUpdateOrder' Boolean flag we can
; V# g7 u4 X, q3 l a // randomize the order in which the bugs actually run: Q0 w }# ?. u0 {
// their step rule. This has the effect of removing any
5 m% s0 Y' Z( Q( ]" C3 w // systematic bias in the iteration throught the heatbug K* n8 L5 R: n% n
// list from timestep to timestep& T4 a( E( }2 D( E& m, f
5 X" [( ]/ }+ [) [$ x
// By default, all `createActionForEach' modelActions have2 Y5 i* v! F# M8 T
// a default order of `Sequential', which means that the! L- \8 E- q: w
// order of iteration through the `heatbugList' will be) L& j" L7 F) q! Z" G) \
// identical (assuming the list order is not changed u$ b; M' R# A) A' c% [
// indirectly by some other process).
5 X* O" B# n1 @! v# G
( @* S x( R0 E3 o y; z/ A modelActions = new ActionGroupImpl (getZone ()); U* x3 h8 ^! i; D- z
" E2 \1 K# s7 G* S' b3 Y try {
. k* |# [# ?3 |- K4 q" f modelActions.createActionTo$message
& Q9 P" F0 b }/ n (heat, new Selector (heat.getClass (), "stepRule", false));
; ]' k5 s9 m' p6 Q: ]0 u } catch (Exception e) {5 u4 i! h9 {1 {) r' Q
System.err.println ("Exception stepRule: " + e.getMessage ());; c5 V9 \$ V5 r5 z
} U$ q2 D* m) v4 \
1 Q8 b; q, y% w, Z4 o try {
& {8 F% k' q4 z! a Heatbug proto = (Heatbug) heatbugList.get (0);& s) {3 x- T0 F* l* ~3 t
Selector sel = 4 O- [/ t7 @4 N/ z- t0 A
new Selector (proto.getClass (), "heatbugStep", false);: E2 R* K$ Z/ b% e; ?
actionForEach =
j3 t2 |( u! L( T8 T7 k modelActions.createFActionForEachHomogeneous$call
, X/ ]3 W6 ]% B9 j (heatbugList,- g; H4 X. v+ I
new FCallImpl (this, proto, sel,+ j1 x4 ?, Y1 }$ a( ]
new FArgumentsImpl (this, sel)));
( p& o. d y- ]% B- P/ g) Z } catch (Exception e) {
# R) f; c$ h4 W* e5 A1 S e.printStackTrace (System.err);* r ?# }- N, |
}
" C( c1 N3 f' F2 b" i1 s" h& p4 D9 Y , L( j2 t8 g8 z& T" y
syncUpdateOrder ();9 n6 K$ c4 i( S3 H2 Y
: }. h! U! z2 O, L" K* ] try {" }* n: ?" T$ ^5 y* [! w- H
modelActions.createActionTo$message
9 o) L' B" z. ?; O! c U9 ^7 N4 G (heat, new Selector (heat.getClass (), "updateLattice", false));* e: g0 c7 f$ Q* Y$ X/ M, g
} catch (Exception e) {0 X2 G7 @& A" s& w& Y4 y# A
System.err.println("Exception updateLattice: " + e.getMessage ());. J9 e* }- E/ Z1 o* F4 H
}5 O* M y$ M- p5 l4 M0 U0 j
! D7 E1 Q2 Z8 V. @2 k, k$ @( z // Then we create a schedule that executes the( }* n, }7 e. A4 Z2 w( G
// modelActions. modelActions is an ActionGroup, by itself it9 k1 B) a. D0 G/ m0 I5 H, Q
// has no notion of time. In order to have it executed in
6 O' V/ X4 _( f: M, D; ] // time, we create a Schedule that says to use the) M# {" ]: i( f* }7 _
// modelActions ActionGroup at particular times. This
& a( E) |3 P1 H. x1 z' T // schedule has a repeat interval of 1, it will loop every
1 {0 D/ G, \0 m+ B( G1 g# e: F // time step. The action is executed at time 0 relative to
0 m1 ^, Z2 k9 u5 ? // the beginning of the loop.
9 @- s) }0 o5 s0 V; \( l, `2 L; k4 j# ~: h) H
// This is a simple schedule, with only one action that is
. k; t' n& p& R( U) ^ // just repeated every time. See jmousetrap for more
2 B |+ ?, W( m& z // complicated schedules./ Z2 J3 s& p3 B/ ]3 \
, ~! M/ w7 f7 ~; F* a1 n- a
modelSchedule = new ScheduleImpl (getZone (), 1); m& b+ c2 n0 a4 x
modelSchedule.at$createAction (0, modelActions);% g3 a# c' S, k j8 v* T
' R+ T. q! P; [2 r: c return this;: X: f+ c1 E# @4 m
} |