HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 o: f) B; N* H+ o H n1 I& _3 r" r9 ^3 Y
public Object buildActions () {
, w2 l% [: W4 Q8 V- i5 ]8 k! s super.buildActions();- W" [* b7 Q1 E& `& E0 o
6 \ D" ]* V# k* f+ K( L // Create the list of simulation actions. We put these in
9 |2 W4 [+ d# c6 {6 a! U0 I // an action group, because we want these actions to be9 G J9 l0 N( L4 ] k- F% [7 k
// executed in a specific order, but these steps should
/ F3 F. m( x% b/ z8 F3 R+ } // take no (simulated) time. The M(foo) means "The message
) D: e5 G& S5 p4 ^7 P // called <foo>". You can send a message To a particular4 [' m" e( P4 g) p, g
// object, or ForEach object in a collection.9 t' T# y* p8 S! c+ b% I
! [4 K, A9 ]7 N( X5 L0 | J // Note we update the heatspace in two phases: first run. Y4 |2 u* j7 c$ C- s4 ^" z7 |
// diffusion, then run "updateWorld" to actually enact the
7 V9 g, n. R& S" `7 @& ~6 @ // changes the heatbugs have made. The ordering here is
4 C1 |; g) Q5 p8 i# @8 b // significant!
! B# ~3 M3 G2 j7 e& T3 u
: A" ]' W+ V" S: _( v // Note also, that with the additional2 ^6 q# V U/ v( M
// `randomizeHeatbugUpdateOrder' Boolean flag we can( i$ P' Z a5 V) X+ l& n9 i
// randomize the order in which the bugs actually run
' w$ V- V0 R& n. j4 U" ?2 B // their step rule. This has the effect of removing any
! ?5 f5 P! X; v5 t1 F( i; V# J( ^ l1 T // systematic bias in the iteration throught the heatbug9 y& H" B! O, T& t9 _
// list from timestep to timestep
' n4 F7 i# y) Y) Z7 D1 ^ ) o. j% W- u+ w6 Q1 {- X
// By default, all `createActionForEach' modelActions have* u1 d$ ?: k3 F% l1 @, E9 ^: W" j
// a default order of `Sequential', which means that the
g& n- h& v3 O // order of iteration through the `heatbugList' will be
, O9 Q! C. j |5 ~7 g) p // identical (assuming the list order is not changed
$ ]1 n- y8 l6 a9 R) t: m6 f) z // indirectly by some other process).$ ~6 X- x% e _7 z7 g+ B! ]) R
0 ?' F ], o6 v3 e" H
modelActions = new ActionGroupImpl (getZone ());& Z" \* }5 Y* j% {- I% h- l
$ l0 n/ P: ^+ B* Y' G$ X8 h
try {" z* h, v; x6 W! \! m
modelActions.createActionTo$message) A4 I+ c: A4 T2 _
(heat, new Selector (heat.getClass (), "stepRule", false));% l3 y7 B( V% J9 T5 x
} catch (Exception e) {4 U5 E9 j3 d7 s! V: y7 _8 g0 j2 \! S
System.err.println ("Exception stepRule: " + e.getMessage ()); H: _( }6 G# P- k" ~/ v
}
8 K- R: o# y @8 p* A0 {6 A) m @+ Y, _2 o& Q: `
try {' C" i2 P. \6 A! f
Heatbug proto = (Heatbug) heatbugList.get (0);! F" v p- R8 D3 X- z+ B" P
Selector sel = $ ~. r8 I3 c4 q4 h' }
new Selector (proto.getClass (), "heatbugStep", false);
" r4 U+ h6 _# G actionForEach =% ]# q- P" R9 _4 y% x6 S/ z
modelActions.createFActionForEachHomogeneous$call
2 u! [1 c/ N. l (heatbugList,9 s+ G4 W: h- L8 l& Q
new FCallImpl (this, proto, sel,
7 y! I! S4 l+ } new FArgumentsImpl (this, sel))); Y( F: l; _- |5 `1 T6 Z1 ~
} catch (Exception e) {
6 F/ ^2 N( p g e.printStackTrace (System.err);$ m1 V1 o/ ]* h, K" K j g
}9 R) M5 J& N; i: g
C! J& n* X) r+ v5 E. u' R2 F
syncUpdateOrder ();
" S4 d# ^- l( J+ N9 b2 T( n# C& e( x I# h0 D ?2 g I$ ?
try {
; j/ ^/ W9 Y0 y' ^& B @ modelActions.createActionTo$message ! p% |1 K9 r( e
(heat, new Selector (heat.getClass (), "updateLattice", false));) c3 h8 j1 j. A0 [6 i4 s
} catch (Exception e) {
% O2 F* g$ \! t+ v6 {4 L; L; \& O System.err.println("Exception updateLattice: " + e.getMessage ());
9 d' W c9 b' Q" X" y4 m, H* J2 [ }' B- a" L" ?# \- ^
9 f& A/ `# P1 T, p4 T. W
// Then we create a schedule that executes the; p9 I( U: ~, K% z7 Y5 v
// modelActions. modelActions is an ActionGroup, by itself it
' t7 n9 W) G! D3 y, D6 R2 n- d // has no notion of time. In order to have it executed in! e0 n; f4 ^5 z/ D2 l* m( U( ~; L
// time, we create a Schedule that says to use the1 c& t4 h* G, u* r8 Q' E7 n
// modelActions ActionGroup at particular times. This; R$ ^, J* K; s+ b0 |
// schedule has a repeat interval of 1, it will loop every- c& x. D& T8 Q6 R* V4 {1 O
// time step. The action is executed at time 0 relative to
9 G( _$ w. H" v4 h+ q! z // the beginning of the loop.
* y) E' @9 y0 ?; B- f
1 J5 V: }5 E9 Z; J- D // This is a simple schedule, with only one action that is/ u2 V. }# J) Z$ b8 U/ E$ N
// just repeated every time. See jmousetrap for more/ |8 u/ v; S1 z* F+ @' L" o% h
// complicated schedules.
) u0 ~. u C! \6 u8 l6 S ' L- N: R, h( T! S: Y+ x
modelSchedule = new ScheduleImpl (getZone (), 1);
1 _6 z& M! g$ F- r/ f modelSchedule.at$createAction (0, modelActions);0 C' J+ l( a( |" H2 _
$ S7 |: M x1 J$ D0 M; ?
return this;0 X5 `8 f% N7 C6 ]( R' T
} |