HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 V$ N5 a. a1 j. U
2 Q0 C# R: m2 K8 ~! v2 C public Object buildActions () {
Z( q9 n2 r9 e8 p, Y super.buildActions();
1 ^" e( h3 q8 |3 z$ @
2 l' J1 f. c* C // Create the list of simulation actions. We put these in
' T0 O1 J' b! F) |" P1 c& h // an action group, because we want these actions to be2 G, [- i: c8 Y/ ?, N! k
// executed in a specific order, but these steps should
* y7 ~2 O# `8 z; b& [6 m! ]! D // take no (simulated) time. The M(foo) means "The message' \5 G! |, M+ ^
// called <foo>". You can send a message To a particular. Y* M# d+ e, t, c, p
// object, or ForEach object in a collection.
7 I+ |3 B3 k3 V) o! g
( H$ k( u+ p1 _( U5 b // Note we update the heatspace in two phases: first run' r5 Y$ B) K/ S. a: u6 d+ G
// diffusion, then run "updateWorld" to actually enact the0 K" D! v- z- D) t, Y* |
// changes the heatbugs have made. The ordering here is9 N6 I0 L1 p$ h b
// significant!& [& Y( W! V. q- f6 _+ y
. s+ B+ b+ C/ v' S // Note also, that with the additional
* L& k) z5 J1 d3 M // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 r3 }/ U# J+ X- x // randomize the order in which the bugs actually run8 o! w3 p/ c4 G, ]' y, }' i; ^
// their step rule. This has the effect of removing any1 j1 X; D: X4 W* M# j$ n e" D
// systematic bias in the iteration throught the heatbug. j" W" K- p: r8 W/ s: w( H W% |; c
// list from timestep to timestep
3 q: y* Y+ b4 K+ e S4 y( f
2 l. u9 a. }$ [! K Q+ R( M, J- n' [ // By default, all `createActionForEach' modelActions have
# p( w% r& J0 N' l/ \( C! r8 ? // a default order of `Sequential', which means that the
! R$ e' {: h) D9 q- C2 }0 a // order of iteration through the `heatbugList' will be
, T1 m( `9 d1 \7 A, O5 R // identical (assuming the list order is not changed7 x* X0 i, K1 S# o) Z& Q& c3 {$ z
// indirectly by some other process).* n N9 H8 X3 M( `; V+ D
3 e) Q. t! l) d. \. J9 `# w
modelActions = new ActionGroupImpl (getZone ());! I2 `, W; V. }8 ~
0 w5 i! Y* Y: b8 J' j9 d try {
0 e1 Z& S! Y R4 Q. L! } modelActions.createActionTo$message
3 y% _- q0 d; J, v' b. K# } (heat, new Selector (heat.getClass (), "stepRule", false));7 M5 \0 v2 J' g4 r9 ]* V" s( N
} catch (Exception e) {7 H2 G* y2 Y. Q/ O5 p" T; B2 |
System.err.println ("Exception stepRule: " + e.getMessage ());
/ O8 P/ o# r- n9 A4 B9 g }
* g( R9 k( }: ^. I2 [& J' r, g% a9 r( J
try {$ A8 s: k( M Y% d8 F+ G
Heatbug proto = (Heatbug) heatbugList.get (0);
, A" u. R% K) K Selector sel =
* i8 z7 K. R- s new Selector (proto.getClass (), "heatbugStep", false);
3 C- [- G6 A, g: P1 U$ D actionForEach =' e" O6 j/ c/ k$ P. ?
modelActions.createFActionForEachHomogeneous$call/ z% ~9 X) @2 f. \0 ^* n
(heatbugList,
8 ?- Q* M: P- T' B3 W- ]. [5 J; ` new FCallImpl (this, proto, sel,
6 r% x: z) `/ M; ]6 F new FArgumentsImpl (this, sel)));
2 a( _- o5 y* D } catch (Exception e) { i/ H0 s+ r1 n/ L3 f8 q1 S
e.printStackTrace (System.err);
# S! q6 U$ k w4 L0 \) H! W: j8 `& A }+ F" ?9 i t5 j& d
! z2 F. {! j& Z4 ^5 }8 ]9 s1 U syncUpdateOrder ();9 l* F" }1 _1 T
7 {9 \% J" F. x! r5 R5 K0 a try {
" A, O4 K. Q0 ^& W+ N modelActions.createActionTo$message ; d# I! q4 ]% u' U5 h7 y3 k0 R
(heat, new Selector (heat.getClass (), "updateLattice", false));; G( s" \4 A" v) G7 W. w2 h
} catch (Exception e) {
1 V0 _/ \ W j1 R2 g System.err.println("Exception updateLattice: " + e.getMessage ());) V# o! N6 ]! q7 r% U E% u5 E
}
, @8 _4 l7 I2 T) n j * F: I3 Z8 T* p
// Then we create a schedule that executes the
* z" d. [! A! _ // modelActions. modelActions is an ActionGroup, by itself it
1 w- O* g; @" a0 g1 s* \ // has no notion of time. In order to have it executed in
. o% |' b- h0 h" ]1 C // time, we create a Schedule that says to use the8 C; H5 P, M" U1 _' g
// modelActions ActionGroup at particular times. This
2 r' q% ]) `6 O2 ~" L // schedule has a repeat interval of 1, it will loop every8 O. a* o H3 E" a$ l: x
// time step. The action is executed at time 0 relative to
3 | I, f1 ^) A+ t: Q- k% v // the beginning of the loop.
9 y& I. Q% W' ~( t. ~; Y9 U4 H [, h+ N
// This is a simple schedule, with only one action that is# R- Z6 W; k$ B% N! d
// just repeated every time. See jmousetrap for more
! {; H j) W2 K6 X // complicated schedules.
- E+ y7 p. ]0 r
% C) r9 C# g9 q+ X$ d$ L- l7 r modelSchedule = new ScheduleImpl (getZone (), 1);- m4 w- C; B2 k: p$ o3 X( \
modelSchedule.at$createAction (0, modelActions);
. q+ I! E5 P3 U1 v9 |- K$ n/ I3 C
1 U5 K- Y/ o. c; R# o a return this;
% p+ L* |9 Y* Q2 @5 p } |