HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 A5 ~$ ~1 ?, c7 L( l' ]: G
9 k' V' u3 n* _6 N
public Object buildActions () {% b! y8 C+ ~) y) b, F, }
super.buildActions(); {, f! d; V( v! o4 P0 C, |
- G3 d3 O3 H" S1 r+ g // Create the list of simulation actions. We put these in1 q8 `$ X8 e7 Y$ N$ A, T# d( M7 a
// an action group, because we want these actions to be
: @. _1 Y& c$ |5 r* M5 l // executed in a specific order, but these steps should
4 m- F6 L: X4 d1 w1 g // take no (simulated) time. The M(foo) means "The message/ T& l* G2 w1 C$ b+ v
// called <foo>". You can send a message To a particular0 h# `8 K& Q( U6 i. {- s
// object, or ForEach object in a collection.: m8 F. Z7 T1 P6 _/ b: \: ]; D
! h, V H9 f' i // Note we update the heatspace in two phases: first run; ^. h. f3 B1 Z0 X
// diffusion, then run "updateWorld" to actually enact the
/ E! F& H' W3 |% {. Z- [' E2 m" Z0 R // changes the heatbugs have made. The ordering here is" [1 |$ k& |4 R7 y7 A0 U; B7 T% `8 U
// significant!
5 q3 m1 g# [, m- ?
8 Q6 k( a+ y( v3 {: J // Note also, that with the additional
+ T/ I1 F3 s" u. s2 K // `randomizeHeatbugUpdateOrder' Boolean flag we can
' Q$ \4 V2 g/ V% o- O T // randomize the order in which the bugs actually run! G% n/ p8 d G' s6 X" m
// their step rule. This has the effect of removing any8 c: I/ k9 _! W
// systematic bias in the iteration throught the heatbug7 l ]& S* Y; c( o5 N) q2 e% o
// list from timestep to timestep2 A' i3 V; @+ u2 C. \
2 t' ?4 |: m2 g8 j
// By default, all `createActionForEach' modelActions have7 ^5 h U4 v0 v; {3 \
// a default order of `Sequential', which means that the
% a; `( }5 _. ^8 [# @, D // order of iteration through the `heatbugList' will be: J9 L! j, G2 X* p+ s: Y
// identical (assuming the list order is not changed
" {$ J6 p2 e9 G, i! A5 j // indirectly by some other process).
, e% {' k6 C0 W2 P$ K2 J- @ 8 @9 v( \/ Q' F% y, d4 W4 c
modelActions = new ActionGroupImpl (getZone ());7 ?& I$ O2 m1 f
& q- t N/ B2 W2 X
try {1 P4 E7 F) k* A) N- _
modelActions.createActionTo$message: [2 x: a% _! v) a j& _6 K5 [
(heat, new Selector (heat.getClass (), "stepRule", false));( ~# I+ R$ Z4 I# U+ _9 E% ~. E; t4 X7 A
} catch (Exception e) {; p0 L" p! V3 V4 V' d+ Z& }8 Y
System.err.println ("Exception stepRule: " + e.getMessage ());
7 W7 u* r( [* a u }! B" i6 |, _+ u; z1 A9 Z6 K
* v8 r! l. V4 o. L try {
! L& o# Q, B, f5 g( }3 D7 r/ S- q: g Heatbug proto = (Heatbug) heatbugList.get (0);
) P+ u! P3 x: L( h Selector sel =
X7 m' }$ \/ ]" \ new Selector (proto.getClass (), "heatbugStep", false);. j$ g/ p! l& A+ u4 b
actionForEach =8 k$ ]2 C1 F1 M- ?5 p. C+ J
modelActions.createFActionForEachHomogeneous$call
4 q" M: e/ g+ i( d* b (heatbugList,2 s* n0 q* p: X- ~* ?( f7 h4 G
new FCallImpl (this, proto, sel,
6 H+ x( S$ T+ A/ U& v* U new FArgumentsImpl (this, sel)));1 l# D" f; R/ y, y# L$ O
} catch (Exception e) { c, u/ E5 ~" R5 v
e.printStackTrace (System.err); {- C3 u3 I" y6 W0 b
}& Z6 F! q' t5 R' p+ g, }/ A2 n& t
$ g, ~7 |4 E/ V. X' z+ |* S syncUpdateOrder ();
" v' f- y7 D8 b& h8 s: w& d8 D
# i6 f+ Z0 U5 L0 _ try {! p8 |0 a6 v6 s Z+ u' J3 N: N" j' ]
modelActions.createActionTo$message $ C4 M3 K) y7 I( F2 R h4 D0 u
(heat, new Selector (heat.getClass (), "updateLattice", false));
I" \: E6 t- Y" T- O; L3 @ b6 [ } catch (Exception e) {1 X; {( q4 q% Z
System.err.println("Exception updateLattice: " + e.getMessage ());+ q9 o3 \3 A! H
}
' ]& i- y- w" K# v2 r; Q" h& v9 j
( r: y: Z9 @. m& M4 ~ // Then we create a schedule that executes the
6 g$ T* N! c) I2 Y* @8 Q. ? // modelActions. modelActions is an ActionGroup, by itself it G$ t* O+ c1 \7 G5 l
// has no notion of time. In order to have it executed in
! g- B' Q$ E. P6 k // time, we create a Schedule that says to use the8 T( P$ b0 ?- @# o
// modelActions ActionGroup at particular times. This5 n' v- s# X+ K& y3 ^* O! c- ~6 O
// schedule has a repeat interval of 1, it will loop every& S# n, Z' \+ |& K
// time step. The action is executed at time 0 relative to
/ Z+ X4 T$ |. M8 C1 l // the beginning of the loop.
9 b% z+ n) C- P% k! l7 @# `2 h! n8 M4 t: U& `% X8 C; r
// This is a simple schedule, with only one action that is
; ?) s T& B( Q/ T+ O // just repeated every time. See jmousetrap for more( v+ m" M! y% F
// complicated schedules.
3 h8 S L* D: g7 Q8 B5 W, X8 A 8 C- t: s- ^/ ` K$ J
modelSchedule = new ScheduleImpl (getZone (), 1);
& \, }7 X9 \( S; b: } modelSchedule.at$createAction (0, modelActions);
! B; A [6 ]3 E; s" x# p
# C" N- F0 F- V9 q return this;
; I' H, \0 u" B0 E* \9 u B# R } |