HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 w) I" i# L8 k) T: t! l4 |* o) e6 W( ^& ]2 A
public Object buildActions () {
2 F8 s4 k) F3 f8 w, l% f9 b super.buildActions();
, X7 |9 c0 ~- j! a ; d. a6 w! E; f, l# c+ S2 l% s5 m
// Create the list of simulation actions. We put these in
- c% w$ t' M1 t2 ]" C1 { // an action group, because we want these actions to be
: _1 j+ J/ K1 {) L& s2 t9 F // executed in a specific order, but these steps should/ M6 X' {" W# ^: H" [0 \
// take no (simulated) time. The M(foo) means "The message! c. O, E% F' P9 X% X) h7 ~
// called <foo>". You can send a message To a particular b; _- b0 ?7 r9 X! k) _
// object, or ForEach object in a collection.( O" k3 ^% @! t$ V0 Y
# O& {- e) {3 c( N& c0 A; c // Note we update the heatspace in two phases: first run2 b6 T2 b$ [: R* `8 s
// diffusion, then run "updateWorld" to actually enact the" Q1 J# E" P/ x, B( n- z
// changes the heatbugs have made. The ordering here is
! ] B0 \ q) ~, F9 R/ S" r // significant!
+ K) _) `9 N0 {+ ]3 O% K& y5 Z, H , u+ Y( E# W! ]6 ]# `3 G
// Note also, that with the additional2 M' b/ v" @% X/ Y9 q) ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can; a% }* D. ?3 f
// randomize the order in which the bugs actually run: ` \. u7 c1 u+ { `
// their step rule. This has the effect of removing any
8 V2 E# j2 P" P1 I // systematic bias in the iteration throught the heatbug* k. z# }' n. R; s
// list from timestep to timestep. K" [! W5 Y6 \. [: [2 A
- N3 V; Q7 \2 c7 u: i; q1 q // By default, all `createActionForEach' modelActions have
: W: X3 A8 q6 W& n; t& P5 ?$ D6 F // a default order of `Sequential', which means that the
; Y+ M8 W; w3 `5 |9 |2 ] // order of iteration through the `heatbugList' will be7 S, `; Q3 s& P! X( {/ g
// identical (assuming the list order is not changed
: D5 b- O% ^7 z0 Y) G' G* i' q5 F // indirectly by some other process).9 n& _4 p( X. i, Y
; C# K4 h: N" ^; v1 S! }& w modelActions = new ActionGroupImpl (getZone ());2 h5 U6 x1 ?2 l6 @# o0 I
6 a4 ~& ?4 A6 v1 v/ q try {# e; j2 F R) h4 Q& H
modelActions.createActionTo$message5 T; y4 M* B* S' A$ W3 O7 k: W q
(heat, new Selector (heat.getClass (), "stepRule", false));
2 A( Y. V+ K0 l9 ~ } catch (Exception e) {: Z. }3 w! _3 C, r! Y Q& S* f) v% R
System.err.println ("Exception stepRule: " + e.getMessage ());* Q7 V& p0 |* X) I! o
}
3 X" X: B% u- i* m/ H7 S/ ^7 b
$ @' T: d3 B* `- B try {
$ }2 L, u1 u+ o I0 k) N/ q. J8 u Heatbug proto = (Heatbug) heatbugList.get (0);
8 D- X4 I9 K1 l Selector sel = 1 t) Q2 j% S+ i% {" c- T
new Selector (proto.getClass (), "heatbugStep", false);- n& l+ C8 J( d6 z# X* e
actionForEach =
) U0 S6 `* R& j modelActions.createFActionForEachHomogeneous$call
) p( _; w0 m1 }$ Q. Z; {' f (heatbugList," B0 _& i( s$ T* ~9 y
new FCallImpl (this, proto, sel,# a8 ~6 D1 x3 \$ _$ ^" B, {! L
new FArgumentsImpl (this, sel)));
% y8 k4 i+ c$ u! G; R" r& t } catch (Exception e) {
2 j- i+ e7 i# Z e.printStackTrace (System.err);
+ D. f8 O$ L0 O5 d E% Z& E }
% d1 m4 {" c8 u9 r % ^) g* S9 g6 i
syncUpdateOrder ();
z# {( v# f# E- k5 W& |8 t3 v6 C* u+ |
try {
+ K: ?6 o6 X4 A" i modelActions.createActionTo$message
0 Q0 O" G# l' F7 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));' X" d0 d: V6 j
} catch (Exception e) {
2 g& F8 t6 M6 S4 Y7 ~3 y System.err.println("Exception updateLattice: " + e.getMessage ());1 q9 D& Q! v1 k8 o3 W% B& k
}
& }; c# Y- a( H, W( |' P 9 c/ L* t L' p6 x& F- t
// Then we create a schedule that executes the
8 Y* |* y: o* Y) f* y6 l // modelActions. modelActions is an ActionGroup, by itself it
6 X' @/ O! d1 q2 w; z: S- [# @$ e // has no notion of time. In order to have it executed in
& r# N8 {5 s+ j- [4 i // time, we create a Schedule that says to use the
8 i$ C/ O/ X; H, G2 p8 J // modelActions ActionGroup at particular times. This1 h9 C" A2 Y8 X
// schedule has a repeat interval of 1, it will loop every- M+ P: c+ z6 @" {
// time step. The action is executed at time 0 relative to1 b1 I/ q& b! k/ \
// the beginning of the loop.
7 p0 S& F, g: N/ a+ {9 T2 e0 f5 r# h4 B; n' ^. v
// This is a simple schedule, with only one action that is
( M4 R+ ^8 w& t& x0 `3 T& \" v // just repeated every time. See jmousetrap for more$ R7 G4 @: s% J
// complicated schedules.
% m& _( P3 w8 c2 | ) x- t- f8 L- n% v
modelSchedule = new ScheduleImpl (getZone (), 1);
- `, t7 [% u. p( _& U5 { modelSchedule.at$createAction (0, modelActions);! S1 U+ a% H2 m1 V5 u! b# q
& O$ T7 o. V/ R+ e) P( _ return this;+ `, l- [; a8 S/ _1 `
} |