HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 r8 V: @8 `6 k: ]1 |! |$ e5 N6 K2 |
, c8 I3 B8 o q( n0 M8 `! ^ public Object buildActions () {
, Y) _ r" w9 L* ^- V% v4 s super.buildActions();
; a& J' z2 a- w2 a! k
7 @) Y; ?( r3 U) _% R // Create the list of simulation actions. We put these in! J6 d r/ ]4 H/ F0 K/ m' K
// an action group, because we want these actions to be
. p- F. x+ }# ^" X7 v4 J5 f7 Z // executed in a specific order, but these steps should, e: u& Z# l* ?: w# K9 @0 }( _% ~
// take no (simulated) time. The M(foo) means "The message) f! J- X( X2 y+ ^1 t2 x
// called <foo>". You can send a message To a particular! x" }) g( ]/ Y, e! q- M, [7 C& i
// object, or ForEach object in a collection.' ]; c7 C1 u! h. F j7 X
: v# R; Z9 h* D // Note we update the heatspace in two phases: first run7 u" S; H! b+ C5 ?2 e0 x+ [% Z
// diffusion, then run "updateWorld" to actually enact the
- q% F, g# p1 h( U // changes the heatbugs have made. The ordering here is
7 y- r4 W1 [+ Q3 w3 U: F // significant!% @4 A. P1 ~4 M& C+ O7 f4 o
) z4 C1 j# F5 t9 R' v% k( y, K
// Note also, that with the additional
, ?, R3 d" s- o0 w% {+ r7 ~: m // `randomizeHeatbugUpdateOrder' Boolean flag we can; t: [1 l4 p4 J8 t* n
// randomize the order in which the bugs actually run8 c O o3 D! k/ b8 _
// their step rule. This has the effect of removing any) C7 |0 I. o2 l+ x. a: N' l6 ~
// systematic bias in the iteration throught the heatbug
( O5 Y' r' S2 C W2 F // list from timestep to timestep
C5 X( `/ Z: [( d) y / j5 O6 o2 u X8 a6 c) Y% S
// By default, all `createActionForEach' modelActions have
/ j# p) l9 o4 a! C J @$ c // a default order of `Sequential', which means that the) {1 E3 \& J8 Y% C, W' |
// order of iteration through the `heatbugList' will be
% w/ F& [8 C1 D( u) s$ U( ~' g // identical (assuming the list order is not changed% X8 u, T. p8 z( C2 E9 x: ?
// indirectly by some other process).$ |0 \, i4 e3 t2 I2 t: h
4 K) J; J. V9 ]1 S. [ modelActions = new ActionGroupImpl (getZone ());
; }4 `: |" J( ?0 U# c1 ~" u, l& B* s5 a. X" A6 u4 ~& d
try {0 u& d2 F6 v& k2 u
modelActions.createActionTo$message
8 R: B3 Z1 U1 p9 [ (heat, new Selector (heat.getClass (), "stepRule", false));
' Q) I @8 |3 ~8 _* B } catch (Exception e) {
& Y" [* D( S0 {) M8 ^+ z/ I2 f1 U System.err.println ("Exception stepRule: " + e.getMessage ());
# l6 j+ y6 O( p }
) |5 M7 v! B8 O% \+ ~0 B$ b. g
* ~; Q5 K o9 d try {" a% R! ~" D1 F& \$ @
Heatbug proto = (Heatbug) heatbugList.get (0);
3 g: q' {0 n, S# i: s- b% ] Selector sel = ( |2 b+ i4 M& T& n
new Selector (proto.getClass (), "heatbugStep", false);
: ]& {, [1 V/ Q3 O7 h7 X9 Z, d actionForEach =# ^- F6 M9 [% }
modelActions.createFActionForEachHomogeneous$call
; b6 L* N4 \& |" u. T (heatbugList,% G- D+ q! k* P( b* U
new FCallImpl (this, proto, sel,( r! D7 T$ c( x2 j" j; J* c
new FArgumentsImpl (this, sel)));5 `1 e+ |! A' K8 t
} catch (Exception e) {& x( O z8 Z0 C! Z
e.printStackTrace (System.err);
& S; o5 L H3 {1 C% r. V% K. N }* I- _5 N3 b- v- U3 t' L& V
6 H+ }3 ^9 m+ ]. w( g; ]
syncUpdateOrder ();# u* v/ ], C n9 Q9 R0 r7 K) {
4 l8 t( _* H+ F5 ^; b try {
" m$ J4 c( v, r modelActions.createActionTo$message
0 c) C2 A/ b. p& V. e (heat, new Selector (heat.getClass (), "updateLattice", false));
; I2 J% Z3 f! R2 j% p& | } catch (Exception e) {$ l: H1 N# u( ]
System.err.println("Exception updateLattice: " + e.getMessage ());& f$ [ u; h+ M
}
; H* |3 e. D# X, w- i( F( r
+ J. W' w! f$ G/ R, n. ?# e // Then we create a schedule that executes the( k" @5 M3 K( D2 q$ N9 M6 C# C
// modelActions. modelActions is an ActionGroup, by itself it
|* V% F+ |( Z* W( R // has no notion of time. In order to have it executed in
8 y* C4 I! g8 K R7 O3 u$ I // time, we create a Schedule that says to use the% ?, K" G7 @9 j+ }# M9 Y6 D
// modelActions ActionGroup at particular times. This/ d! _* j' l" G4 S- G( O5 b/ f
// schedule has a repeat interval of 1, it will loop every
0 p% Q: `& t* C! @9 R& ?' }: D // time step. The action is executed at time 0 relative to
3 N2 J' s( C6 {6 l // the beginning of the loop.
8 q6 H+ x% s/ _7 h) L2 N1 k w& ^' i
// This is a simple schedule, with only one action that is+ a9 }* g: s7 r0 F7 ]% l; a$ c
// just repeated every time. See jmousetrap for more) A+ u- H6 X% j
// complicated schedules.
$ e @+ [) `: X" V: Y, A
. e, y e' k% _& @! X modelSchedule = new ScheduleImpl (getZone (), 1);
( k1 r7 t" ^: |, w4 q2 M modelSchedule.at$createAction (0, modelActions);3 l$ Q# Z0 q8 i7 J# P, X
4 n( G/ d0 K" Z return this;- S8 { m6 s. `/ x5 F6 v
} |