HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 M( x# \* o8 @& J' \
+ K* a# M: l" Q) [
public Object buildActions () {
7 P5 y( z6 g/ X* F" E! k, S super.buildActions();
8 D3 N+ j' m. j5 m2 Q
* D; v8 e& B, p$ x1 f+ X // Create the list of simulation actions. We put these in4 ]) X5 J6 A' D
// an action group, because we want these actions to be( L3 I9 z$ `8 J, g1 a
// executed in a specific order, but these steps should
0 b k1 _' j2 E" C0 ^; o { // take no (simulated) time. The M(foo) means "The message/ m6 J3 H$ Z: w! Q5 s: B- }
// called <foo>". You can send a message To a particular
- D# K' R/ |5 [& O' |! r // object, or ForEach object in a collection.! H; [* Z2 i7 e7 I8 g' E
: p7 T- ?$ k9 {2 z0 P& s6 W
// Note we update the heatspace in two phases: first run# ]5 e# [/ r, `5 W4 \
// diffusion, then run "updateWorld" to actually enact the" S* O2 p% n6 ~3 y: X. g6 w; n
// changes the heatbugs have made. The ordering here is
* V9 c, u) P: l2 Y1 U4 ] // significant!
. W. W" P# b+ n# Y7 h: b
# R$ s- Z" t d // Note also, that with the additional- j, G3 j$ M: p, r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- {8 f2 G, q6 n+ ~ // randomize the order in which the bugs actually run( t: J4 y* O6 z; p2 c
// their step rule. This has the effect of removing any, s8 l2 T2 l( K4 _% w, ]: E5 E
// systematic bias in the iteration throught the heatbug
- k% g1 j$ Q- x1 x // list from timestep to timestep
) L) N5 m _& T1 O$ |# J3 l; @$ V
; A& p$ w' i+ n% J& D$ B // By default, all `createActionForEach' modelActions have
8 i- W) o/ b7 j. E- Z // a default order of `Sequential', which means that the; y' W( z% m7 M2 ~8 r$ M
// order of iteration through the `heatbugList' will be7 S5 [( t3 u5 f/ k0 J% Q% m
// identical (assuming the list order is not changed4 ~+ b0 Z! [( E; R
// indirectly by some other process).
/ Z2 B4 c" {, z% M; n) t6 L8 W ; w% q3 l/ V% L4 H. s' ~
modelActions = new ActionGroupImpl (getZone ());
0 Q1 W( _. v+ `, c6 a& ^; U1 R( C u- g( R" Z4 S/ @# y
try {# ?7 Y% v" y3 H
modelActions.createActionTo$message) o( S6 G' {% p4 R3 h/ F, c1 h9 u
(heat, new Selector (heat.getClass (), "stepRule", false));
$ N4 F/ v, C6 ?( l } catch (Exception e) {& w( X& K$ K4 Y; a# I4 G) C
System.err.println ("Exception stepRule: " + e.getMessage ());( I+ m1 B3 ?# T. I2 l( i0 |
}+ F* I2 D# M8 H7 |8 w
% B7 o. ?# T$ p6 v7 s- [6 A
try {
8 R& r- i7 T2 O" l: R Heatbug proto = (Heatbug) heatbugList.get (0);
- V" s2 [$ l! K3 c Selector sel =
; U( m) E9 n1 [6 s$ b new Selector (proto.getClass (), "heatbugStep", false);
/ n5 G; {' L# S6 \0 i actionForEach =/ c$ |3 x, _4 N
modelActions.createFActionForEachHomogeneous$call
a' G+ Q+ U! h( R; V& ] (heatbugList,
) Y4 | J& w9 c( o. ^ new FCallImpl (this, proto, sel,
4 o! P/ j" h; H7 O5 P s# F new FArgumentsImpl (this, sel)));
A$ {& {& K& T, W1 r' d } catch (Exception e) {
# O. V/ q j( m* h' } e.printStackTrace (System.err);2 r7 b# V2 H8 T
} Y0 w4 ~% c: y' N
% s. a7 o, m3 N
syncUpdateOrder ();
3 V# {+ }3 R8 r0 d8 o+ E. [2 y' Z3 c* G# C
try {$ B; f T) P, u+ I. P; o; S& y
modelActions.createActionTo$message
8 g3 Q |* ~7 X9 O7 _% g" { (heat, new Selector (heat.getClass (), "updateLattice", false)); ~5 a1 `3 i" i( P& r9 S
} catch (Exception e) {
3 W( j4 q& ?' M& T* @: W! p) U6 _ System.err.println("Exception updateLattice: " + e.getMessage ());
' y# a, j" }: B }; T* P, {( a! K# P' r& F$ a
! e$ t0 ~' y. a
// Then we create a schedule that executes the, ~- @( k. d7 H' f! A
// modelActions. modelActions is an ActionGroup, by itself it
! Q/ R" t: ]' K" }7 F // has no notion of time. In order to have it executed in
9 b0 ^$ J3 u% i% e& O // time, we create a Schedule that says to use the) U3 ^+ ~. l$ g* d
// modelActions ActionGroup at particular times. This7 O/ [# t3 ?4 R) h
// schedule has a repeat interval of 1, it will loop every
( n1 }9 O7 g- C' s // time step. The action is executed at time 0 relative to
0 u3 I" p+ q2 y0 f6 j$ r // the beginning of the loop.2 l z. W# ^/ r
0 x# A& P* r+ e. Z // This is a simple schedule, with only one action that is
5 `# M: U3 J; i# J2 Z3 [6 B // just repeated every time. See jmousetrap for more
- g! ~; {+ C. u% c: k8 } e, t' z // complicated schedules.1 N8 j" x1 F2 [# U0 [+ ^
8 r( _) ^. ^9 Y; s, V. Q- f
modelSchedule = new ScheduleImpl (getZone (), 1); w9 h- b8 d/ i7 C
modelSchedule.at$createAction (0, modelActions);- ]! |9 J! G+ q+ a% D3 d
5 ?: N; R: g g
return this;
% A* e+ U4 D% R* Q/ L! B( t9 m } |