HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
F" ^, k5 D! U: I' I. W3 D! C+ ^) v2 e" S3 `, l3 f
public Object buildActions () {
4 ]" M' U7 `5 `5 R$ T) R# `4 y4 j+ M super.buildActions();1 k. f9 ^9 |8 U1 k# {/ n
* {* I% h7 \" p: b
// Create the list of simulation actions. We put these in
4 Z; S: G* q% Q/ e8 x+ J // an action group, because we want these actions to be( V9 v0 N+ A3 A5 F
// executed in a specific order, but these steps should
- E3 T X& i4 @% W6 A // take no (simulated) time. The M(foo) means "The message
7 f0 d/ D/ y% K* [3 R% Z$ f4 ` // called <foo>". You can send a message To a particular
+ \. H1 j1 V! @+ S5 |9 q // object, or ForEach object in a collection.
h! d7 h. | W; t, Z& Y! { 6 U' o% B I0 t$ ~+ J
// Note we update the heatspace in two phases: first run
6 h( E" l) g7 c // diffusion, then run "updateWorld" to actually enact the3 e6 U) t' H9 H7 O' y; a
// changes the heatbugs have made. The ordering here is
6 r" z- w! u. \6 U) {. y // significant!/ J5 I7 b; f' r- ?' F
9 ]% L, A& b! x# [& o7 ]7 E1 w // Note also, that with the additional
4 ~/ q2 W* i7 {- t // `randomizeHeatbugUpdateOrder' Boolean flag we can4 d$ `7 j. L5 j5 h
// randomize the order in which the bugs actually run
! @ _& O3 u7 g b4 u# W1 l // their step rule. This has the effect of removing any" U- B9 ?( F+ r V0 Q# y
// systematic bias in the iteration throught the heatbug- d5 ?/ C9 d. t9 W
// list from timestep to timestep
" i' S" J: N2 y7 D+ q * V( H- e! |7 C+ Z# {
// By default, all `createActionForEach' modelActions have
/ U8 a v3 I x( _$ j // a default order of `Sequential', which means that the- N3 s0 Y; j' |/ E
// order of iteration through the `heatbugList' will be
' M8 V9 d8 \- r' e/ B2 s3 h* g // identical (assuming the list order is not changed
! j7 S G0 O8 E ?/ B // indirectly by some other process).( R) p! H7 Z/ R, p
' R- R& C. T2 N; I modelActions = new ActionGroupImpl (getZone ());: _/ _8 X) [7 z2 u, }3 `* i
- |" R; v6 }% l/ ^: U9 O
try {. b* v! x1 h- F1 d E
modelActions.createActionTo$message/ F" x W8 W2 r# R
(heat, new Selector (heat.getClass (), "stepRule", false));4 e3 I# b% z; u2 M7 K2 [
} catch (Exception e) {
) y" E" e- p. a X8 _# B5 ]: i1 T$ s0 B System.err.println ("Exception stepRule: " + e.getMessage ());
% F3 W$ ]% f- S) g }
0 f; W* p) { l1 g6 d
7 O/ Y( i% c4 h# G( j. u | try {$ X. K4 g8 `- b
Heatbug proto = (Heatbug) heatbugList.get (0);
9 V8 \- ?9 g2 N4 R Selector sel =
( b" C$ U9 _ T. o: J new Selector (proto.getClass (), "heatbugStep", false);
9 i. K; A5 x" N' s actionForEach =
4 e6 y, ^1 f* f! r( _# `* a modelActions.createFActionForEachHomogeneous$call+ a; r5 d3 D+ o/ y+ f
(heatbugList,8 U% i; V* Z8 L/ X* X
new FCallImpl (this, proto, sel,
+ E: e/ F0 H5 n5 G. N new FArgumentsImpl (this, sel)));
9 Q% q7 }9 H9 O1 f5 c6 o3 u; \ } catch (Exception e) {6 G+ b7 V7 P% g. U& m3 l
e.printStackTrace (System.err);6 Q+ w! `3 H8 ]* B/ P
}
f1 |8 d3 G- K2 A+ p1 l; l
) e5 v: k8 g+ E1 Y0 r5 [: W syncUpdateOrder ();7 R0 ~" }( n1 U( S
( j# W3 h+ H8 N/ c try {3 F/ C1 Y) l8 F( I1 H: Q( f0 Q
modelActions.createActionTo$message
( W% x* E) E. g+ Q4 G (heat, new Selector (heat.getClass (), "updateLattice", false));
& P6 n, B* l1 N% m } catch (Exception e) {4 F/ Q+ g7 X# A9 k% n) ^, D
System.err.println("Exception updateLattice: " + e.getMessage ());7 X4 e! u- u/ c1 E
}
8 B: w9 A1 W! | " ?3 v( v8 a0 u5 R
// Then we create a schedule that executes the
, V8 T% C4 o+ @4 R // modelActions. modelActions is an ActionGroup, by itself it
# @- O& E, i/ v9 K# m6 T# C( r // has no notion of time. In order to have it executed in
9 w# L1 P! u9 ~ // time, we create a Schedule that says to use the. c3 Y* A' s2 I5 z" F
// modelActions ActionGroup at particular times. This
" {5 w& X+ g( u2 ?0 Y! A // schedule has a repeat interval of 1, it will loop every
# h. B$ h2 I, A \ // time step. The action is executed at time 0 relative to* ~; G% j& Q: J
// the beginning of the loop.
# i7 S6 N* ^+ m
- H7 k; B+ W* r d/ v v9 }+ }0 g, b // This is a simple schedule, with only one action that is
8 v3 k4 o9 T7 Z1 i // just repeated every time. See jmousetrap for more' u+ K5 F( o, @' `! q: _
// complicated schedules.
* n5 J9 N0 j5 m( v
8 t1 v2 w0 A1 q3 w8 X% t modelSchedule = new ScheduleImpl (getZone (), 1);
7 p0 k9 }. X" R* n8 ^; O% i modelSchedule.at$createAction (0, modelActions);3 d. ~0 L) D; _- J
" Q' _8 z9 z9 \% z+ y
return this;
$ j1 H7 m( l0 f } |