HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ z: P) i7 P0 {* y8 F+ X9 D5 \3 ~3 a- M7 F4 c
public Object buildActions () {
2 Q+ o, `! N3 P) ? super.buildActions();4 t: g* O/ @1 i! C, J1 g! S2 \
2 F# ^! V( R- h+ ` // Create the list of simulation actions. We put these in/ ~, i* D; B6 E
// an action group, because we want these actions to be' e* ~0 ?4 p* s& `$ Q
// executed in a specific order, but these steps should: a% T: E6 h: u3 S$ @
// take no (simulated) time. The M(foo) means "The message
_6 _6 i* V" N9 Z. ~ // called <foo>". You can send a message To a particular
0 G0 u6 f/ }3 s# g // object, or ForEach object in a collection.& I: O: u: a% R+ l t3 P/ H! z
+ l) Z1 E5 S2 r) o7 C* }% K9 w
// Note we update the heatspace in two phases: first run
% D* }: c: {4 H+ I2 l // diffusion, then run "updateWorld" to actually enact the& R* d* ^+ ^" ]7 k
// changes the heatbugs have made. The ordering here is
S$ S# V% ?+ d7 _* R H" R // significant!
5 i, A( U( E6 c, Q9 s! Q " q1 |7 P) d0 r0 S9 Y! U9 H2 ^
// Note also, that with the additional) g6 A% ~1 O) M3 ~9 h- ?+ |" c# O& W/ C
// `randomizeHeatbugUpdateOrder' Boolean flag we can* e1 X, B2 V0 {8 K! R
// randomize the order in which the bugs actually run [" _, M/ m8 x8 u/ x0 a
// their step rule. This has the effect of removing any* ^" q9 q( d2 Q8 A( x
// systematic bias in the iteration throught the heatbug1 ^9 g: V; z, m
// list from timestep to timestep
- S& z! ]9 Q& E
. c4 u _# e" P* x* y" t // By default, all `createActionForEach' modelActions have( F6 S ~1 R6 i
// a default order of `Sequential', which means that the6 j- Y2 W+ b. L: V% C
// order of iteration through the `heatbugList' will be9 D- `5 L" N5 U1 u' P# u# t1 P& r7 N
// identical (assuming the list order is not changed
6 x2 g3 V# {' Z/ y/ I // indirectly by some other process).
6 q, V# o$ S5 u3 _! Z0 P* u 5 J# G$ r0 ~, k# s7 b6 w" ]
modelActions = new ActionGroupImpl (getZone ());
* v- g; e8 \# I& f! Y7 J
; Z( A, Y, _/ a2 Q5 G/ L4 Z try {
) N$ M6 b/ c0 v1 q2 r0 w" ] modelActions.createActionTo$message+ `" [7 }$ F% q+ l5 R, P
(heat, new Selector (heat.getClass (), "stepRule", false));2 u8 s3 h, [+ S& d6 {
} catch (Exception e) {# x8 n1 R0 f5 {: n, Y, |1 b1 X$ ]
System.err.println ("Exception stepRule: " + e.getMessage ());
; z/ X5 u* N+ r }
/ u' @; p, w) [: a3 F: d3 i$ `# Y9 D j/ Q2 n, i- z8 M& Z
try {
* [1 u5 @8 D Y/ V8 A$ n3 l Heatbug proto = (Heatbug) heatbugList.get (0);1 o; e$ h6 s- ]) k0 h4 F4 V
Selector sel = ! ^/ c' ?- \1 C& W+ z6 t5 E; I
new Selector (proto.getClass (), "heatbugStep", false);
; Q& q/ l9 B% N2 q u/ ^ actionForEach =
! j. F1 L7 P7 i" n modelActions.createFActionForEachHomogeneous$call
4 R& c/ m0 ^. K5 A* N; q (heatbugList,
" f( E1 ~) @( P8 W: w new FCallImpl (this, proto, sel,
+ a% \* J2 n4 \# d# e9 ^ N new FArgumentsImpl (this, sel)));& `5 h# w) S& \5 C% K
} catch (Exception e) {
7 Z+ ^, ~# r: `! _. ] e.printStackTrace (System.err);
, K$ w1 m$ h+ E1 ~4 m: y }
3 h, E: M" h/ z; o1 x : V* E! m# i* C+ s, M; X
syncUpdateOrder ();; m% I6 |1 Z6 g# b% y
0 h1 O9 t- m2 {
try {
/ u b# ?' s/ W modelActions.createActionTo$message + x, z; f |- G. b# u& L+ m& I
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 @0 R8 n& T; f6 L } catch (Exception e) {) y' r% g. J( n6 f' n9 ]
System.err.println("Exception updateLattice: " + e.getMessage ());
+ {2 F/ x/ Q, R* a* j [ }
" f' `# M$ d% O. \- X, j4 \+ L ( S& v' c9 ^* q! M8 u4 w
// Then we create a schedule that executes the, E3 ~ h7 X9 ^) Y# H
// modelActions. modelActions is an ActionGroup, by itself it
- d& t1 _3 b7 @$ T x5 N8 [& V+ e // has no notion of time. In order to have it executed in
; f: u9 ~2 b) ?$ R' X // time, we create a Schedule that says to use the
: g5 t. G% i3 o' l7 a4 n" K // modelActions ActionGroup at particular times. This" k' g8 [5 `: V8 A$ }! f+ k# p
// schedule has a repeat interval of 1, it will loop every5 j4 p" ^. H: n7 j2 D& P- w/ r2 P5 m
// time step. The action is executed at time 0 relative to
# x3 ~6 m$ i4 ~& N; i // the beginning of the loop.
b* u g$ i! v! m
) K, b* R* n: ^ // This is a simple schedule, with only one action that is& }5 X: z: R4 e4 [2 Y0 P9 w. m0 F
// just repeated every time. See jmousetrap for more
4 t: q a/ \5 M! ~" _, c // complicated schedules.
* q4 w3 r1 z: @( Q / q* h: e. ^+ _
modelSchedule = new ScheduleImpl (getZone (), 1);8 X1 M# C$ u, M
modelSchedule.at$createAction (0, modelActions);" P( J& u2 K* b2 r5 ^8 X% H' H
! h5 r* I' k4 D+ Z2 ~ return this;
$ v7 [# H- k3 u& { I } |