HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' s& v2 C) e c5 r/ @. P U
/ {% h7 ^4 M% i& W5 y$ r' T/ P { public Object buildActions () {
c5 M1 d+ L7 a7 o% m) u/ n super.buildActions();; {6 g; Z) |. L" ?$ v( G2 g
9 y) U0 d* p! H4 s; R/ v; L+ T // Create the list of simulation actions. We put these in( w' P4 d# R! N4 S/ j, K" h
// an action group, because we want these actions to be
( {& @! Y' j& c3 _3 | // executed in a specific order, but these steps should
b2 Z2 e* E# U: N0 L% v* [ // take no (simulated) time. The M(foo) means "The message7 P C- c( ]0 R. I3 r# }5 Z2 q
// called <foo>". You can send a message To a particular
1 }9 \; L8 |! G4 S; a% L* ? // object, or ForEach object in a collection.
9 {" k" i y6 s& A! k, ]8 c5 w , N8 `; s% b! k+ Y, a3 t o& q9 }
// Note we update the heatspace in two phases: first run4 ]3 M: X4 A. i& I0 v, M% k
// diffusion, then run "updateWorld" to actually enact the
T) `" b. h* |( E // changes the heatbugs have made. The ordering here is* A. K) W- v" `# K F: J
// significant!
" z; F3 v5 Z6 f3 z7 C
+ q% t ]' g( ^0 e // Note also, that with the additional% N$ R$ x7 F0 D) J3 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can& A" M, P" |, L+ A3 E- d9 I
// randomize the order in which the bugs actually run
+ B! _1 `" j. L( q, N& D$ u // their step rule. This has the effect of removing any1 }6 u; b. W0 ^ q- t
// systematic bias in the iteration throught the heatbug s2 b$ Q; j: k, D
// list from timestep to timestep! w5 j- V, s, a
6 U1 B8 j3 { ]$ w9 L // By default, all `createActionForEach' modelActions have6 J4 m1 ] R. t. t4 Y& u! k$ ~
// a default order of `Sequential', which means that the: n/ \! q' o$ d4 L
// order of iteration through the `heatbugList' will be& t0 Y+ \9 u( v5 p& }9 D' M3 i
// identical (assuming the list order is not changed
0 @' J/ Q2 `, c6 N6 q6 o3 p // indirectly by some other process).
2 C1 L4 O* n) L; q+ m
- ~* J: t" e2 K6 b# A/ b2 c7 k, B modelActions = new ActionGroupImpl (getZone ());
% q2 h; O/ Q( \' Y+ T& M# O) ^2 ] @% V6 i# p
try {% O# M5 M* t( G$ J) M% @: C7 I. W
modelActions.createActionTo$message1 i/ Q- c' ^$ y9 q0 a7 w! A( N
(heat, new Selector (heat.getClass (), "stepRule", false)); b- ?) A4 V' t2 L- X
} catch (Exception e) {, u3 m# O/ j( V
System.err.println ("Exception stepRule: " + e.getMessage ());
% i. `& Q) l6 D6 r2 J }
& W+ r1 e% R @! w2 z# @, j3 o! V
try {9 b6 y2 I5 Y( q/ z) ]1 V$ g
Heatbug proto = (Heatbug) heatbugList.get (0);
" i7 T, q) h, E% O1 E- E. J& N6 R Selector sel = , Z% Q3 Q/ Z i# [+ a) f
new Selector (proto.getClass (), "heatbugStep", false);
9 d; s: ^% i8 e4 g" C actionForEach =
, O. z+ C7 |) P) W, m modelActions.createFActionForEachHomogeneous$call* F7 K0 o1 K0 M' T8 r
(heatbugList,
5 [" p. Y6 q9 n% Y* E0 x8 C" u. r new FCallImpl (this, proto, sel,
2 h/ j8 G1 Q9 ] T& d K new FArgumentsImpl (this, sel)));
n6 ?- ]( U t/ u( x. ~- o } catch (Exception e) {) s; Q& G L2 x1 T/ x& H- F
e.printStackTrace (System.err);
# G- p U5 J4 y1 b! g2 ? }
" Y" E, i+ i( [5 [, d( ~) @: m
9 F! e8 `8 E: P- Z$ ?9 v syncUpdateOrder ();
1 ~! I: ~9 c' B/ y! ?6 X4 k9 i
4 W8 z) L1 ]5 g8 F% W try {! b( R* t9 [/ h# p; ~( t: U. [
modelActions.createActionTo$message
2 Z6 Q* M$ O6 ] b (heat, new Selector (heat.getClass (), "updateLattice", false));5 ?& q' j0 N- ~
} catch (Exception e) {- d# V, U- p; g1 a
System.err.println("Exception updateLattice: " + e.getMessage ());2 i4 ?! x' R/ B! K+ ?5 o/ k
}
9 E5 Z! M9 V5 |) V# b4 ` ; L+ {! x9 a% j" m' q7 T) o
// Then we create a schedule that executes the
0 p8 `# `. n, z$ ?# u: [; z. r/ R // modelActions. modelActions is an ActionGroup, by itself it
/ @/ M R# |# y+ ~0 t, \1 _ // has no notion of time. In order to have it executed in
+ ~( S9 x& E% t0 J9 ?. M& S // time, we create a Schedule that says to use the
/ a* w: p* x1 h/ \, }& k // modelActions ActionGroup at particular times. This
. A8 r/ h; d$ _- j _6 b, u$ r // schedule has a repeat interval of 1, it will loop every
2 Y8 o3 t0 G9 x6 `% f5 P3 Y // time step. The action is executed at time 0 relative to4 r' c' J6 U0 @
// the beginning of the loop.9 _( U; p$ S- M& {
2 {% B( D H+ ]* A) j // This is a simple schedule, with only one action that is
+ F# ]' j6 b2 Y! Z // just repeated every time. See jmousetrap for more$ m9 L- s( j7 U6 `& B2 `
// complicated schedules.
( y- ~" @- g0 d5 j+ F+ L, q/ m. A
* ?. q9 p5 S1 e# T' c. |: c modelSchedule = new ScheduleImpl (getZone (), 1);
' w# w* g! J: P3 Z: m* m modelSchedule.at$createAction (0, modelActions);& N6 ~8 j8 o2 D, @) H9 L5 Z
4 c3 j6 Q0 M3 d7 z return this;- A, W$ e- l$ X- q& Q
} |