HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# H& D9 P; B# y3 O! g5 v" `2 i8 m8 T3 m4 i. w! Y% R/ V
public Object buildActions () {; z- }5 @% g: h2 `# B% I
super.buildActions();+ j' p2 l6 D. S
8 m6 |" ?( s0 h& e9 Y
// Create the list of simulation actions. We put these in1 T! P- a+ J0 _$ H' V& F+ r: p
// an action group, because we want these actions to be7 ~. S: ~5 v* |! R* r) l; l2 x
// executed in a specific order, but these steps should' s4 r7 i* T" p" E+ p' \
// take no (simulated) time. The M(foo) means "The message
+ Z4 M7 w7 O, Y6 x5 F; C3 V9 \ // called <foo>". You can send a message To a particular
0 W; @3 I' D0 ^ // object, or ForEach object in a collection.: F9 u W% E+ k& j. X0 J/ S" A
( m) j ~8 v" R) I' I // Note we update the heatspace in two phases: first run
z, ]- i N, s0 O* }3 B% c% p0 _3 q3 Z // diffusion, then run "updateWorld" to actually enact the
7 D) D- M1 B T // changes the heatbugs have made. The ordering here is
. b0 s/ ~! F6 s // significant!( l1 R0 {/ q6 Z- ]' ]% O
' \5 B9 a' J9 T. g // Note also, that with the additional7 C% @# Z" L8 U% m8 q4 B3 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 T: C' ?" I( O) t& X! ] // randomize the order in which the bugs actually run* C! r2 K7 D0 Z+ j; o! x
// their step rule. This has the effect of removing any
2 W9 Y6 A' q- {2 n& \- _1 A% s // systematic bias in the iteration throught the heatbug
* g$ j4 A3 X7 R$ A4 l2 I! k- c* Z6 ?& q // list from timestep to timestep7 u2 K! z6 ~& i9 B- K9 z' f
. t V5 z5 ?7 r
// By default, all `createActionForEach' modelActions have
5 k3 p3 ]! X1 p // a default order of `Sequential', which means that the
; D' ~1 s) Z. q4 h) s$ \ // order of iteration through the `heatbugList' will be1 c; S) f4 B9 C& z, a' a
// identical (assuming the list order is not changed
4 D1 O# G" E* O6 w // indirectly by some other process).1 ~1 T) Q% g4 q# H5 Z- \
+ ?* N0 b3 A: O- a6 a% x0 ]# K( Y) x% q: Q modelActions = new ActionGroupImpl (getZone ());
% D" h" U6 u6 j9 n8 `7 r; W" s( }" ?3 s
try {
- w1 @. R1 [. x) D( n; b. S( x modelActions.createActionTo$message
( r+ }: [; w( L5 S (heat, new Selector (heat.getClass (), "stepRule", false));
+ Q/ I1 D2 N/ z+ J2 Z } catch (Exception e) {
@; o" J# a' _* E0 e) _% y System.err.println ("Exception stepRule: " + e.getMessage ());9 D8 K% @; Q9 ^ e; l
}
, o3 x: @: O0 }7 L0 m# Y+ z; D! P, B: e) L9 ]
try {
4 x# Y+ a- W; @ ^$ k+ {* e Heatbug proto = (Heatbug) heatbugList.get (0);5 {) l8 x3 K/ U1 {& @8 E* ]7 G
Selector sel = 0 W Q9 {4 s! h3 @9 H
new Selector (proto.getClass (), "heatbugStep", false);
" w0 x( W/ J4 W actionForEach =% @# e: j9 ~2 `3 J( h! D
modelActions.createFActionForEachHomogeneous$call
: N+ P; B6 ]& m! L% l- v5 ~ (heatbugList,, l7 C: f. y, A7 e' H
new FCallImpl (this, proto, sel,) N. _) `, g4 Z. g9 `& K
new FArgumentsImpl (this, sel)));4 t0 \& a$ [5 z) i5 |( D% _
} catch (Exception e) {
2 M+ L; Z& q# Z# ~2 \# i* F1 Z e.printStackTrace (System.err);
8 f+ J! Z6 [) r X1 M3 ~ }
7 ?* x \* U8 N- ^8 C7 ~
0 p& u$ G+ `; {7 Z9 v syncUpdateOrder ();6 A [. |$ i% A3 q( y! l4 K
' [9 p& k# a# M
try {% {" T* R; g- X/ I* f
modelActions.createActionTo$message ( C( b/ G; f# E" o: M( b1 r
(heat, new Selector (heat.getClass (), "updateLattice", false));
' L* X" B5 W) y8 V } catch (Exception e) {( h, J1 |. z0 L4 e- ]3 F. ~
System.err.println("Exception updateLattice: " + e.getMessage ());* f' Q( a6 Y ]7 ?
}, Z7 u2 ?- M) _* ^3 i1 s
/ q1 V: \4 |0 @$ T; Q) h
// Then we create a schedule that executes the
8 p5 M+ E# F, n2 e o! Q1 D2 F // modelActions. modelActions is an ActionGroup, by itself it% [7 }0 J$ R" l) p$ y; A2 @
// has no notion of time. In order to have it executed in0 d; t0 D3 E3 a t. I/ z4 E7 F. e% l; g" F
// time, we create a Schedule that says to use the
% X& |: {9 P4 G! ~3 L // modelActions ActionGroup at particular times. This# I9 Q: i& ?5 b H9 {$ p! _, Z0 B: [; }
// schedule has a repeat interval of 1, it will loop every2 u3 Y7 L% y1 a- h6 h
// time step. The action is executed at time 0 relative to
4 C% a' w2 K3 j& h7 O7 y // the beginning of the loop.4 j8 i% q! Y; h0 ]( n' R
# @. u6 r1 L3 e+ u8 _1 x1 z // This is a simple schedule, with only one action that is
( X1 X0 h! ^' s. E // just repeated every time. See jmousetrap for more( ~4 T3 @& i" `6 J4 {6 c
// complicated schedules.
7 ^6 _/ i7 p6 T U1 E. i: Y ) P2 n0 r( L; u! u v; ]
modelSchedule = new ScheduleImpl (getZone (), 1);! W& H& E, G- N2 S `& t
modelSchedule.at$createAction (0, modelActions);
3 {( x6 z6 q4 \/ f" v
/ Y: V# W' x/ F4 q2 d5 D' R8 i return this;
. ?5 o9 E+ b, h3 L } |