HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 U/ B. t d* o
6 F# A9 c$ F1 A public Object buildActions () {& K1 l" V. u8 o+ T
super.buildActions();5 ~$ X" o" M! Y8 E3 v
0 J) C7 ?. h) q& ]! l" ~# B // Create the list of simulation actions. We put these in1 a# D2 u2 h1 d( S7 h, _3 |
// an action group, because we want these actions to be
1 t4 A6 h- t M! J0 S // executed in a specific order, but these steps should
' l/ r L; D$ F. B+ p5 H$ g3 U // take no (simulated) time. The M(foo) means "The message
9 x) R+ l. B0 a+ L/ ~, H# P // called <foo>". You can send a message To a particular; A" }/ b) _$ L0 S& G
// object, or ForEach object in a collection.
1 u0 i4 c4 a' z1 @$ e6 h7 a) u9 L5 o
; ?2 ]& J& d6 w' d+ s, R) A% y' m. e( s // Note we update the heatspace in two phases: first run0 o# e; ~& {" C/ y" J d( L# x
// diffusion, then run "updateWorld" to actually enact the
/ K2 @! ?- B9 ~' s // changes the heatbugs have made. The ordering here is
- `- S' x, ~& h: n // significant!8 K8 p$ M3 X; o1 E9 |' _
" t1 _0 s! @5 r8 k& ~4 A // Note also, that with the additional
2 b* }* Q' L( k1 H0 O1 ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
) N( M4 A7 G9 E x // randomize the order in which the bugs actually run
" R0 R9 ~% l- y) `% K% @( v: F8 A // their step rule. This has the effect of removing any; }! e& l1 E4 `3 q6 N. e
// systematic bias in the iteration throught the heatbug& Q7 y a* c: P0 G. B
// list from timestep to timestep% W0 [) {+ u. L* B: W) j
# A: r; x' y8 r! f& q
// By default, all `createActionForEach' modelActions have& V( }6 B1 P% V @$ G; F) z
// a default order of `Sequential', which means that the' ^! k( I2 e% e8 i/ v" ?9 \
// order of iteration through the `heatbugList' will be2 ?7 ^; S" k% y4 E! J
// identical (assuming the list order is not changed; B0 x: {" K4 T
// indirectly by some other process)./ X- G9 f0 s$ w$ N# {7 A
% V o Z; z& l7 x0 z( Z$ ~ modelActions = new ActionGroupImpl (getZone ());: T8 I# {2 @3 e) @
+ [+ E! p) h! D, j try {
; t5 _, n; u# k: S% m, f modelActions.createActionTo$message
, n0 g( C* n: T3 N0 V- }/ P (heat, new Selector (heat.getClass (), "stepRule", false));
& G2 K4 _+ c- L } catch (Exception e) {
, }2 [8 z( Z% b3 M* T System.err.println ("Exception stepRule: " + e.getMessage ());" G: ~ U- G" y8 F% R" s4 H
}0 ^2 A: j- P, c0 A! a
( O2 H$ F) f# r& W: u# F; g try {
* q0 d# Z" M6 V( t: y) W6 @! } Heatbug proto = (Heatbug) heatbugList.get (0);
& o3 b( d/ Y( w( B3 P Selector sel =
, s3 T) C! j, W. {3 I1 U new Selector (proto.getClass (), "heatbugStep", false);
6 h4 _/ s& x; k8 V! w actionForEach =
U4 f+ `7 _' Y5 ~- K a modelActions.createFActionForEachHomogeneous$call# y/ r( e& i/ }) R1 W) G6 s3 u
(heatbugList,8 n# `6 [/ [* a' b9 O+ D
new FCallImpl (this, proto, sel,
4 A: ]# c6 S% L& t3 x& o% O new FArgumentsImpl (this, sel)));8 p" r2 X) G$ H5 G
} catch (Exception e) { B7 `' `$ j8 j/ m2 o
e.printStackTrace (System.err); T- z$ {* `( `( S
}/ \; c% s; c3 r5 G: E
' r: \# u2 F5 X. y syncUpdateOrder ();
# `! B- @7 a; I/ t
' [ U$ B T8 A1 ]" m1 e try {% ^$ {5 c, i& x( \( o: V" E
modelActions.createActionTo$message
/ m# Q" I" x' Z# C) X I# E (heat, new Selector (heat.getClass (), "updateLattice", false));- m7 y8 k$ @. T+ w
} catch (Exception e) {
2 I8 {) l7 b# |! S0 H# v System.err.println("Exception updateLattice: " + e.getMessage ());# I+ f3 |- Z# A. S7 ]% S/ O$ q
}1 b- }1 W' e J. o
$ }" K" I; b0 Q. P4 o2 ^! T // Then we create a schedule that executes the! }) p- q( }+ v/ v0 J! M' e' S
// modelActions. modelActions is an ActionGroup, by itself it
& b3 s H' Y. y# v! g8 L8 g // has no notion of time. In order to have it executed in2 \2 {& K* ]& k% Y: A
// time, we create a Schedule that says to use the! s; |, V$ [& e1 p
// modelActions ActionGroup at particular times. This& z6 u! w# E& ?/ y3 j
// schedule has a repeat interval of 1, it will loop every
. B6 H' Y7 ^3 Q1 V3 [( B // time step. The action is executed at time 0 relative to% Y' b* X) g8 K9 s: e; r
// the beginning of the loop.
- A8 z' p3 K% j8 ~7 `3 {" {) x1 t) V9 L
// This is a simple schedule, with only one action that is
7 R0 r$ w" B% m3 H- H/ e // just repeated every time. See jmousetrap for more
8 k# A1 X- w6 r9 B // complicated schedules.
) U7 {/ @9 l3 K" v 9 S7 v# U. F e- B1 i
modelSchedule = new ScheduleImpl (getZone (), 1);
8 h3 q2 U- f$ X+ L% F6 I. o( N6 W$ w modelSchedule.at$createAction (0, modelActions);7 U4 b7 ]; w, u( `9 m
3 l. f Y9 ]& t+ {: E
return this;
7 w% t3 z+ C& T! [ } |