HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ u5 t: ~+ o) e, n4 v& v/ Q
* @- w* m5 a4 D( a+ m public Object buildActions () {
+ {- A1 G1 ]; ]; s super.buildActions();
* e4 P4 E0 y& t6 t% a& H
9 @. V' o# O! B" L s7 E // Create the list of simulation actions. We put these in
- m! E, a8 }- F! U/ r // an action group, because we want these actions to be* D1 ?( q1 m9 d6 e3 H
// executed in a specific order, but these steps should
$ D" k$ _! T5 o- n // take no (simulated) time. The M(foo) means "The message" _/ ^( o5 j1 }# s7 H
// called <foo>". You can send a message To a particular
' S, n2 |" L5 l$ v* @ // object, or ForEach object in a collection.7 O; }. F/ {; f/ B" ?+ O
@; a% t3 }! Q& s // Note we update the heatspace in two phases: first run$ _8 D+ z5 o3 Z7 U6 w9 k3 {
// diffusion, then run "updateWorld" to actually enact the6 b( ?7 W! ?, ?) X0 w
// changes the heatbugs have made. The ordering here is) \: V' t; F( p( q0 c
// significant!2 Z. V' O: N$ l! n% t W+ L
- ~2 R$ h" g$ f* Z/ S! n! \ // Note also, that with the additional
' L: y) [( Q p7 w4 `% K // `randomizeHeatbugUpdateOrder' Boolean flag we can
* @3 o Y/ }7 s2 r2 W // randomize the order in which the bugs actually run1 I$ W" F) c6 _2 x& k' I
// their step rule. This has the effect of removing any
$ B. J) ?$ _6 V j" @: ? // systematic bias in the iteration throught the heatbug
4 {- n+ j$ X. Q1 v% c // list from timestep to timestep, J8 L& @" N6 Y7 l; L0 c! q
, {' ?$ e& K) P; X8 e0 X // By default, all `createActionForEach' modelActions have* ^( w! p: H; o" ^. e& u0 [3 ~) @
// a default order of `Sequential', which means that the9 O" x) E# U8 L3 r6 E
// order of iteration through the `heatbugList' will be+ ? Y0 O9 J% J0 t& ?
// identical (assuming the list order is not changed
5 m7 L( `7 W, o* g) b- b // indirectly by some other process).
1 ^3 ?$ D/ H! q) h9 r8 n0 v
" I% G! o9 l7 e- K# I, ^) L! { modelActions = new ActionGroupImpl (getZone ());7 q( G/ h/ L. u Q7 P
. b. W9 `. `: U! g, o9 W1 @ try {6 p' Y3 q( W: o; m3 v- O) E
modelActions.createActionTo$message; g( ` t2 z3 ~% w7 r
(heat, new Selector (heat.getClass (), "stepRule", false));. n% p* S5 z9 b+ H0 {
} catch (Exception e) {
8 c" Q* E9 Q& H* _9 n8 p System.err.println ("Exception stepRule: " + e.getMessage ());
( @& O# P5 O$ \/ r9 E }$ W5 Y6 T3 o/ h4 @: t
* o' m* s+ Z+ N" g
try {
2 L; T+ J. E' L2 o Heatbug proto = (Heatbug) heatbugList.get (0);/ W5 T$ k* y6 i- `* U) m+ O
Selector sel =
! G& s. a+ {" H& f+ k new Selector (proto.getClass (), "heatbugStep", false);, E) [& d8 t. d' E3 P
actionForEach =, z9 Y s i i8 [2 l5 u
modelActions.createFActionForEachHomogeneous$call' o, X. l9 ^* b0 }7 J* o, w$ K9 Y
(heatbugList,. Z: F/ q/ j. g& { m! `
new FCallImpl (this, proto, sel,- U% i8 X$ B% |% f0 g( T2 _$ e9 N
new FArgumentsImpl (this, sel)));
2 o' P6 \3 p. s9 u" N+ ]9 ^ } catch (Exception e) {; l6 s$ d9 C% V9 o6 w
e.printStackTrace (System.err);0 e, w, _: @/ P6 w
}" L7 B2 N$ k8 q8 @0 G1 ^* S
/ p( o; c6 G! W# G syncUpdateOrder ();7 S# Y1 u; C7 l9 X V# L9 y
' x; R$ _' E/ v6 y! C try {% P. \( }3 r/ d( B) G
modelActions.createActionTo$message 2 b w( m# P! s$ J
(heat, new Selector (heat.getClass (), "updateLattice", false));
: D$ t! ^/ b5 w6 _$ P& v* G% } } catch (Exception e) {
# y: h) |! s) c1 m% G System.err.println("Exception updateLattice: " + e.getMessage ());& i0 e3 J- w5 d Z5 G& S
}
1 U8 N9 \; C N8 H a: r, E
' `2 U$ D; i9 D // Then we create a schedule that executes the
1 S0 @ s6 t0 x' P* _7 F5 s, F // modelActions. modelActions is an ActionGroup, by itself it7 ^/ F( c X: w. {7 S' D8 a0 v
// has no notion of time. In order to have it executed in
, C1 y7 j! c$ k. ^: ]- n // time, we create a Schedule that says to use the
2 L+ n% e6 P" m* K9 Q$ U; _ // modelActions ActionGroup at particular times. This5 H2 m6 c% P8 {9 e$ v
// schedule has a repeat interval of 1, it will loop every! l- `+ ~+ g* |; b
// time step. The action is executed at time 0 relative to
, `. X2 Y' P& \: _ // the beginning of the loop.6 ~- d/ e' h6 V" E4 `
* @6 n, v; R* Q. n% c5 k // This is a simple schedule, with only one action that is
7 H& R+ p; x5 q // just repeated every time. See jmousetrap for more" [: C0 I1 y. y9 ^/ ~
// complicated schedules.+ o R( [: b+ J& H( L3 W4 Y: s
! w0 Y5 m* \! @' p) f# \7 y modelSchedule = new ScheduleImpl (getZone (), 1);
! v+ n9 N+ M$ Q! @& `- S( d modelSchedule.at$createAction (0, modelActions);; S( \; T. H- q# R6 p
7 u5 U6 [( Q8 ~" M3 F1 j* C6 b return this;8 I1 V8 l& M" N2 q+ U
} |