HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. S1 Q! p+ r0 {0 P" |
4 v4 v8 _+ I4 n* a+ @
public Object buildActions () {
) s& b2 z9 I8 Q/ D4 ?7 M super.buildActions();
) O$ o0 v$ K6 u5 V
* m% J1 q2 v' L. ^+ l9 y7 } // Create the list of simulation actions. We put these in
- \$ [- D. b0 P. G& T' m // an action group, because we want these actions to be
S" Q) p6 g7 c0 X5 F! \ // executed in a specific order, but these steps should6 n( O+ z" Q3 t8 z% |/ s& g5 W
// take no (simulated) time. The M(foo) means "The message/ G' Z6 V# F- t( H, p
// called <foo>". You can send a message To a particular% g9 E3 E1 ~/ e* C5 e
// object, or ForEach object in a collection.
% t8 p! w, N; F9 d9 X9 U6 [: M- B
( S; @. i1 y3 Z% @! F" Z! g! s // Note we update the heatspace in two phases: first run. Q. l! j* x- Z# k, e4 C
// diffusion, then run "updateWorld" to actually enact the
$ }9 G+ O- i- T% r // changes the heatbugs have made. The ordering here is. J1 U+ x o, S; ^
// significant!
# H* U! j7 |9 [
# z! x+ z0 C6 @8 u& l) c // Note also, that with the additional
( ?( S9 i2 Q( ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
% m" u% l/ m" N3 {: i4 B8 m // randomize the order in which the bugs actually run
! \, n+ X# u% t0 _4 i // their step rule. This has the effect of removing any/ Y" V9 z, T- [' C9 D
// systematic bias in the iteration throught the heatbug y( ` q9 N& ~3 R8 ]% N
// list from timestep to timestep( j9 c- N7 c7 @) O: ~6 B5 b9 u
* `/ ]* S" D" M. P/ y7 D% L
// By default, all `createActionForEach' modelActions have5 s/ ~; w6 U: H7 z: t8 H& z
// a default order of `Sequential', which means that the: r; O6 [$ H; R+ I; x9 t' E. O6 j1 ^" w
// order of iteration through the `heatbugList' will be% I. X$ z" P, |
// identical (assuming the list order is not changed
4 u( e7 t* q9 i // indirectly by some other process).
2 w2 r- O3 ~- Z) u. F3 }
8 B/ t+ W. E4 ^; f0 \ modelActions = new ActionGroupImpl (getZone ());
8 {. g" k2 x" v& R( I' ^' W/ Y
: f5 G2 d( y$ j! C% v try {' e- v7 p3 Z6 M) X' N
modelActions.createActionTo$message
) F( h) }; `' A: s$ K! L (heat, new Selector (heat.getClass (), "stepRule", false));6 ]$ X# K4 y! _6 z
} catch (Exception e) {+ c5 e% g6 |2 ^- ^
System.err.println ("Exception stepRule: " + e.getMessage ());
9 L7 I$ c8 k, u* q$ T1 a8 Z) b# [9 g, ?9 p }
/ T* \7 p0 r5 U! ?- f) E& M, }1 E8 z$ m1 y, g
try {* ?0 U4 {) l% p) \ r# g+ D4 ?% n0 M
Heatbug proto = (Heatbug) heatbugList.get (0);
2 F% ]1 w* ?0 i6 ]; R1 _ Selector sel = 0 \3 y7 s: w" V
new Selector (proto.getClass (), "heatbugStep", false);! }- r4 b! s7 R7 q ]
actionForEach =% Q8 U' v2 _4 ?5 H* _0 U) {$ o
modelActions.createFActionForEachHomogeneous$call
( b: B9 L) ?* O4 O. f+ D1 P (heatbugList,
l) _. p5 o' t% j new FCallImpl (this, proto, sel,% v; z2 L1 f: s7 O6 F% e& E! z
new FArgumentsImpl (this, sel)));& d5 S! f/ ]1 `- B
} catch (Exception e) {- z: R l3 C- M' u7 r( ^
e.printStackTrace (System.err);
8 S: h2 V: ~8 n C: s }7 ~( F% i) p5 f9 M
6 \; h) q$ h9 i0 G6 X+ y6 L syncUpdateOrder ();
; x) ^. m0 n3 T4 ?2 p3 b& x
2 n3 L6 i3 @" J8 M' z0 B try {
6 m( l9 Q5 s6 a) K" M( X+ |! k+ b modelActions.createActionTo$message
0 f% x2 k0 g2 @: O2 w (heat, new Selector (heat.getClass (), "updateLattice", false));* c1 X T/ p- M
} catch (Exception e) {& \, d5 l9 j: L3 e6 J1 |
System.err.println("Exception updateLattice: " + e.getMessage ());8 Q% m2 s. [/ ]8 [! S2 L4 M4 W
}
8 T/ ~$ W6 r1 S8 W) R, a4 K/ {3 s
% I+ t* z: F8 e; F8 Q2 V // Then we create a schedule that executes the
6 G# b) l+ e/ c0 C( [8 m // modelActions. modelActions is an ActionGroup, by itself it
4 i# m) {' a( W. y8 }7 a // has no notion of time. In order to have it executed in% Z3 O g/ `0 A: Y* ^/ [
// time, we create a Schedule that says to use the
6 ]. w* n+ I X5 S" { // modelActions ActionGroup at particular times. This0 {; D5 S7 X" K3 Q# V
// schedule has a repeat interval of 1, it will loop every* E* m+ l" n1 U+ n! `
// time step. The action is executed at time 0 relative to0 k$ Q3 i8 z Z3 [( B
// the beginning of the loop.7 B! k8 T4 k0 n$ @
% C1 A3 I9 U) H6 ]/ A+ c3 b' y // This is a simple schedule, with only one action that is
' ~6 ?2 W* c3 E+ t3 V // just repeated every time. See jmousetrap for more
3 ?( a2 C: S" g3 ^ // complicated schedules.
. o" F& a5 m, q 4 c5 D* h8 F$ t: z4 A; c4 q6 I- f
modelSchedule = new ScheduleImpl (getZone (), 1);* o. w9 _# W* K7 i4 ]
modelSchedule.at$createAction (0, modelActions);
( }4 g! H# p. l4 k1 n9 L* I' n6 W $ {8 c# a1 U5 H- `! _* J
return this;
4 a+ W; }3 Y+ a) _* d$ X1 u } |