HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 r+ c+ |+ d: W0 L) C$ w# D# ^3 @" H% z4 ]/ C) c; ]# x# w
public Object buildActions () {
( e9 ~" h( `* m! F super.buildActions();
Q) X& W4 S5 m* R6 Z/ C2 g " u- X0 j6 H1 T
// Create the list of simulation actions. We put these in
: v. C9 ^$ ?& H$ C // an action group, because we want these actions to be5 r0 c- m- z* K/ C" e4 d# v
// executed in a specific order, but these steps should
6 }9 f( {/ Z9 K, _4 t4 }, u // take no (simulated) time. The M(foo) means "The message0 o3 T# r" R# p8 h- C4 G$ l% R
// called <foo>". You can send a message To a particular# ~( _( n0 `$ ~# y8 M* c
// object, or ForEach object in a collection.
) l: _: P; P0 O% Q9 o
* N$ F4 R9 Y( P. G2 \% D // Note we update the heatspace in two phases: first run/ S" H9 L( H& `) i6 i6 G0 J
// diffusion, then run "updateWorld" to actually enact the" ~& [9 L! v0 Z7 b
// changes the heatbugs have made. The ordering here is
2 u- V) G" k! E6 u; D& |" c // significant!5 T( z9 G3 B5 H" R5 i5 g5 s
/ I$ p6 k. ?( g M/ u" v // Note also, that with the additional3 f6 o1 L; P/ ^- V0 W2 K" I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# w0 S* `5 `; Z3 Z; I% | // randomize the order in which the bugs actually run
$ o) ^) l [4 Y; Q% d' \ // their step rule. This has the effect of removing any
* t' l1 d( @% ?2 w; w6 o // systematic bias in the iteration throught the heatbug$ n9 B m% Z3 |6 j
// list from timestep to timestep1 k7 z+ r% E' Q/ D4 c; B E
4 B) o, y/ t# v- c; H* D6 g9 l // By default, all `createActionForEach' modelActions have' S7 A5 f+ W3 K8 _' K$ o
// a default order of `Sequential', which means that the" y$ r% }/ U# Y: e: Z: j
// order of iteration through the `heatbugList' will be
) x$ ]; o# C7 j. Q- X7 Z. b0 Y // identical (assuming the list order is not changed
( | h! n3 B7 X; v' t // indirectly by some other process).) a2 u2 I1 `5 m% u& U" F
) X a- a n( z# |1 l
modelActions = new ActionGroupImpl (getZone ());
R, ~+ d |/ ?! [1 z2 _# i
1 G i2 k; B6 z try {
. M0 B( D* O* a& U modelActions.createActionTo$message
4 Z5 @& K+ E& q7 _2 {8 B (heat, new Selector (heat.getClass (), "stepRule", false));* B, d; `: B1 v; ~- V0 N
} catch (Exception e) { M+ c( F- e, V- y M
System.err.println ("Exception stepRule: " + e.getMessage ());, U% f [' |! N1 i1 ^( R
}9 z+ R- q1 a9 y
( d" r: x3 ?2 m try {
! _ J; ?, b1 w& B1 ?: y! ` Heatbug proto = (Heatbug) heatbugList.get (0);5 `" M( z! c& p: {% {6 Y( e
Selector sel = 4 |3 t# B0 _# h7 l: N. m: |
new Selector (proto.getClass (), "heatbugStep", false);/ v- U8 ~# l3 f" B0 M
actionForEach =2 d* G6 l7 ], U9 y7 h d) F( {* v* E
modelActions.createFActionForEachHomogeneous$call( f, |# c% b) S) j1 z8 T* X
(heatbugList,0 J$ f o9 j/ j/ G( o4 ]/ u4 s
new FCallImpl (this, proto, sel,
3 b1 E- `( J: v" c: z K new FArgumentsImpl (this, sel)));: b0 J7 a! T! Q M# L0 ~
} catch (Exception e) {
* Y( M% D) c O$ A9 g e.printStackTrace (System.err);
7 _$ M. Y b% ]0 _, M- n! v. p }# K0 ?% n( p: ]0 a. I; u: Y* i
5 w% _& p; _8 s+ o k/ G+ A syncUpdateOrder ();
* i3 F1 T( u( m4 h
P' M7 z3 l/ S try {
* t2 i; I% R/ [ n% E ~/ m# B modelActions.createActionTo$message i' W( t/ {; O7 X4 a
(heat, new Selector (heat.getClass (), "updateLattice", false));- p8 _: U! ^- d! y, M+ J
} catch (Exception e) {
& \: B1 Y( G. L! ^/ c System.err.println("Exception updateLattice: " + e.getMessage ());
0 C7 A( N% g* } H7 H3 B }
% d. x9 Q; G5 z8 M5 W; t
3 p5 P: k$ ?7 T7 m) \5 p3 d! w // Then we create a schedule that executes the$ `6 X# E7 X5 i z
// modelActions. modelActions is an ActionGroup, by itself it; Y' f) m2 V7 _ Z6 E8 f, c1 W
// has no notion of time. In order to have it executed in8 i# Z$ J3 W( l1 ^$ c
// time, we create a Schedule that says to use the; g3 Q G9 I; Q
// modelActions ActionGroup at particular times. This% u& H. h- f `" F) ]; A
// schedule has a repeat interval of 1, it will loop every; S" U) S( T. d' ?( S3 G
// time step. The action is executed at time 0 relative to# O3 i+ b- `# K! t1 b
// the beginning of the loop.
- k d' k ~) m* {. ?- D
0 m& \% B8 N$ v2 \ // This is a simple schedule, with only one action that is$ [5 I: `1 j9 M3 Z
// just repeated every time. See jmousetrap for more
! d M j( @) a3 ~/ [( T% {- Y // complicated schedules.
4 m, v0 C# y* m r 3 @! T7 L2 d3 A3 c: y
modelSchedule = new ScheduleImpl (getZone (), 1);3 A) z+ g2 y( R
modelSchedule.at$createAction (0, modelActions);/ L0 F7 O6 t# }
3 {( b! ^" P" s: M return this;
( h7 y3 M7 ~ t. n } |