HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 e7 l9 j+ w) M" Z7 {# z6 E8 Y" o( ]. \6 O: G
public Object buildActions () {
6 P, p& ~& S% q t7 J$ x/ } super.buildActions();+ J4 k/ m5 B9 T0 k4 Z/ ?4 ~
, B4 S* I* k& A; d3 x3 N M5 l // Create the list of simulation actions. We put these in
3 D) G7 `. I; E. G1 c; v6 d7 d // an action group, because we want these actions to be
& `; F6 N& ?, r* u4 A // executed in a specific order, but these steps should6 S% ^- B" X" F" C
// take no (simulated) time. The M(foo) means "The message P+ @8 J% y/ Q7 z- J% I
// called <foo>". You can send a message To a particular
0 [2 P7 A4 T ^' i$ G1 h // object, or ForEach object in a collection.% V0 \0 R7 X1 i- P# ]6 c
2 ?# g: h _6 l9 j E
// Note we update the heatspace in two phases: first run
& Z a+ I6 n% ]% H2 t' d // diffusion, then run "updateWorld" to actually enact the
* j" y1 a; A/ Y3 P( ^# G0 N // changes the heatbugs have made. The ordering here is
. I, q3 t2 M9 l6 ]7 \$ P( p // significant!
- \5 x% n$ }6 _) |8 u , B& H, {' ]- k' G1 S7 \
// Note also, that with the additional
$ u9 V" i# O# n; I+ _7 Z3 ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
. F+ N8 u) M( _6 }4 @* I. g' ~ // randomize the order in which the bugs actually run- d7 _' i/ N- o. R
// their step rule. This has the effect of removing any) v( @ d: Z7 j/ X
// systematic bias in the iteration throught the heatbug7 B4 V% Y+ k9 B; G! M8 \/ E
// list from timestep to timestep
: j& Z" ^2 |+ ^) H% M( d / j, r# O' N$ ?' V% b @& T/ c
// By default, all `createActionForEach' modelActions have% N6 ~ d* M2 H5 k( Y, K
// a default order of `Sequential', which means that the
# ?+ P* C( Z& b3 ] // order of iteration through the `heatbugList' will be' {) }# K! k; p
// identical (assuming the list order is not changed8 Y) Z3 F3 t1 J! a( p* T- [
// indirectly by some other process).: q5 B6 S# o+ k. [' u
: W$ e0 w- d; S( C! {2 ] modelActions = new ActionGroupImpl (getZone ());
4 ~/ o# ^9 X& |0 t" {! o; J% o7 F5 ^
: v) a6 H* R# o" j( V0 Y% v try {
. {' J' k0 k+ ? k k- D L0 p modelActions.createActionTo$message
! R; A6 Z# C# @! T7 u: V (heat, new Selector (heat.getClass (), "stepRule", false));! q4 w, A9 W8 |% z
} catch (Exception e) {
3 D5 f6 p+ D1 i2 F4 V5 M) M System.err.println ("Exception stepRule: " + e.getMessage ());7 q6 x. ?. Y, n% K! [
}7 ]! E' l5 W# D/ P1 |/ Y
1 u' ^+ g2 e6 @$ Q) F4 \ try {" ? \; i* k" e8 u0 {! k6 Y
Heatbug proto = (Heatbug) heatbugList.get (0);" j1 A4 o* S) o# ~ f
Selector sel = 3 F/ Y. ^/ S! I: x
new Selector (proto.getClass (), "heatbugStep", false);0 b/ ~- c; |6 G: Z/ G4 S. s
actionForEach =7 p/ ?$ }0 @1 C/ N- Q: S0 M
modelActions.createFActionForEachHomogeneous$call
- D: v. L1 ]+ q: a' F9 s (heatbugList,
; m$ ^/ o- N6 d) M; O6 N0 I new FCallImpl (this, proto, sel,# g5 j n6 v; n! x
new FArgumentsImpl (this, sel)));
% r4 A- s* Q: h0 X m, G } catch (Exception e) {
5 _* N6 ~0 T: K* g3 N! J5 [$ G e.printStackTrace (System.err);8 A$ }1 g( j5 I- U4 ?) \' ?
}
8 j) \2 p. \8 V' ?9 D {. l
$ I1 g: W( ]$ a syncUpdateOrder ();7 @1 y! c( a. U2 x5 F
4 [ S% d/ P0 S3 U
try {1 i- l4 |* q4 [3 w
modelActions.createActionTo$message
" ?, J7 G S1 f' V- p (heat, new Selector (heat.getClass (), "updateLattice", false));
, d/ P6 b- B8 y } catch (Exception e) {. H- c. @* k1 W. N1 S
System.err.println("Exception updateLattice: " + e.getMessage ());
! y, N1 q( l9 b }
1 p) v) m* ]2 u) F 5 D* Z, R( {1 J5 z0 @
// Then we create a schedule that executes the) }8 j' H/ L0 G
// modelActions. modelActions is an ActionGroup, by itself it, X2 |# I$ \2 u# ]0 h) u p
// has no notion of time. In order to have it executed in( L R4 e9 {( C7 |' q
// time, we create a Schedule that says to use the. a, g7 A% z3 q9 v
// modelActions ActionGroup at particular times. This
$ g" U5 g1 T1 h // schedule has a repeat interval of 1, it will loop every) N) p; j% j- M4 T. l
// time step. The action is executed at time 0 relative to
* P4 o. I: e# X8 c1 v5 V- s: M // the beginning of the loop.
2 H5 v3 O3 J- }+ O W3 g( k( J+ J# `$ G8 ^* b' R3 Z
// This is a simple schedule, with only one action that is) ^* N$ L* v: v! ?
// just repeated every time. See jmousetrap for more9 q4 P$ u5 R4 J
// complicated schedules.. Q2 `$ I- K! M& e
' {: n2 v5 a( S9 f2 G, C modelSchedule = new ScheduleImpl (getZone (), 1);
6 b6 h" X3 U+ I& I: i- k# i modelSchedule.at$createAction (0, modelActions);
9 t# [5 n! i! [: D * @ w+ h8 y" w9 K0 g! U
return this;
, q9 j- D/ t) c; c } |