HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# d2 z" r6 r$ @' V3 W% s/ C4 K$ `: Y) d# }2 q
public Object buildActions () {0 K* B! t; G8 y' `1 m- b- C
super.buildActions();
( v, {# K! {) g% h% q
2 `' j: n! d% z' t // Create the list of simulation actions. We put these in
8 f9 D( n6 s( ]4 o/ i7 g( g; e // an action group, because we want these actions to be
5 M) B, I2 h7 Y6 d& E // executed in a specific order, but these steps should
- x9 R2 ]+ w7 s) M, v // take no (simulated) time. The M(foo) means "The message" s% a& @! i; @
// called <foo>". You can send a message To a particular! ^! j3 j4 S2 m* H# @
// object, or ForEach object in a collection.0 k' a0 A6 d* X* D1 U' s) ~+ L
7 \$ S8 A( O% I( i7 Z
// Note we update the heatspace in two phases: first run
/ T+ P' }' _# \( y' o# J4 l! C' i0 V // diffusion, then run "updateWorld" to actually enact the
8 [0 G3 V& O3 }/ X1 Q J // changes the heatbugs have made. The ordering here is
/ o( Z' T% }2 T" s2 \ // significant!/ N. ^" f# S3 C0 S+ Q
. P, {6 s5 Q( m& Z3 u4 P [
// Note also, that with the additional
7 { p; I( m# ]" L/ Q // `randomizeHeatbugUpdateOrder' Boolean flag we can$ I; [- z7 M" d% a, L. I: @5 {- z
// randomize the order in which the bugs actually run
1 B8 \5 z; F0 ?6 T // their step rule. This has the effect of removing any: O6 r3 x7 z# m8 o8 d* [% q
// systematic bias in the iteration throught the heatbug
( d' \2 Q# f% G# ]4 | // list from timestep to timestep+ [+ l5 X' r6 @/ w' H: I
; j) o+ c) f6 f1 t, I, L6 F( G& `" c // By default, all `createActionForEach' modelActions have
" N7 ^& L" g: A E( w // a default order of `Sequential', which means that the
- L* A% T7 _) ?- F // order of iteration through the `heatbugList' will be @- }9 x- S* j1 ]. K0 x, q
// identical (assuming the list order is not changed
$ Y3 `& l* N) _1 X' o( R // indirectly by some other process).
) y( V0 g) k B! k( |) ~. @$ _ # J4 j; F! D+ Q9 J2 D* P
modelActions = new ActionGroupImpl (getZone ());
; I% x, ^& k" {4 i& t: Z* S q! g: C/ U8 ?4 `" F" @ z# J
try {3 b& h5 ]" Z- J/ J: n
modelActions.createActionTo$message
# C5 j: ?5 }# o i# \( z: M3 p (heat, new Selector (heat.getClass (), "stepRule", false));
; o2 w& I, Z6 N/ G6 ]. P E# d } catch (Exception e) {
. ?# d( J- ^" H# E, p0 R System.err.println ("Exception stepRule: " + e.getMessage ());2 v) C. r. {8 |& o9 ^! P1 ?
}! c' Z2 J p9 O( i. \1 ^7 q
( E+ e3 _8 [0 _$ ?; k try {8 m2 l- H$ a7 C0 J1 d7 [
Heatbug proto = (Heatbug) heatbugList.get (0);7 e4 ]) T, _& j' c3 ~
Selector sel = 4 J4 J% m; T+ ]
new Selector (proto.getClass (), "heatbugStep", false);* T+ G' Q! ~3 O/ ^& ?+ K
actionForEach =
1 E, y# x3 E! Y" N3 K modelActions.createFActionForEachHomogeneous$call
" k: p# X4 k- J, v (heatbugList,
( r8 D8 X$ B/ l& g& w4 p new FCallImpl (this, proto, sel,
; v$ ]+ r5 h% j7 B, X+ _' z new FArgumentsImpl (this, sel)));! i) `: P& p& M* C: z8 `* V+ ^* j' y
} catch (Exception e) {+ H4 g4 T! k- i/ [5 `' h3 ?
e.printStackTrace (System.err);$ h7 P9 \: {* F
}
, ^. Q" |, W- } , _+ J* n- G1 _. e* ^
syncUpdateOrder ();1 O( C2 N6 N% R; w
2 w) ]5 a6 {2 ^2 z
try {9 E3 {2 M- }3 u, q! h& o/ ^. k" s
modelActions.createActionTo$message 5 R" H, S+ K8 L
(heat, new Selector (heat.getClass (), "updateLattice", false));
* v+ U* G1 B* y } catch (Exception e) {- {/ A; y' ^3 P7 Q
System.err.println("Exception updateLattice: " + e.getMessage ());- B1 W# W; X+ |9 \+ b v
}* o. {9 {+ `! z q4 c8 a. X
( H/ W3 [7 O$ M& i
// Then we create a schedule that executes the
: Z& ?3 p9 `* [8 Z // modelActions. modelActions is an ActionGroup, by itself it
, k' @) m3 W' _* C // has no notion of time. In order to have it executed in8 m+ o# x: N2 z+ C
// time, we create a Schedule that says to use the2 H, Z6 ^6 B3 {( A
// modelActions ActionGroup at particular times. This* m9 O& q w; g8 `2 K- O( c5 d( E
// schedule has a repeat interval of 1, it will loop every
7 z6 G1 p* X- A# I6 V1 ]; q a // time step. The action is executed at time 0 relative to+ }0 W3 c T l. b
// the beginning of the loop.
( d! u6 L6 J* q0 o/ x& r
( @6 }3 B7 e4 T& I. V // This is a simple schedule, with only one action that is
- t+ N t6 N3 }7 I2 ^; m // just repeated every time. See jmousetrap for more9 a" \- D- [. z3 Y" O% j. }7 d
// complicated schedules.
s K2 q( X* b
4 X( h/ ^2 w" H6 U7 Q modelSchedule = new ScheduleImpl (getZone (), 1);
8 X9 w' p, k+ i) v modelSchedule.at$createAction (0, modelActions);
" j" b3 |4 Y- |( \/ Z4 R 2 h) l3 K8 J0 m: P
return this;, Y" `7 K; z4 M6 y ]- M
} |