HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, ], |* r% S: @( A2 m I% i, Q# }* q
public Object buildActions () {
& B/ v) ~* Y6 c/ j super.buildActions();
% A/ ?% m7 Q( U$ w e6 a( k + m. K- n( o7 C. A/ W- M( K
// Create the list of simulation actions. We put these in
1 B; \2 c/ p' g/ n // an action group, because we want these actions to be
, K6 j7 |/ I" N7 y+ F, { // executed in a specific order, but these steps should
+ b3 E# I2 g" q // take no (simulated) time. The M(foo) means "The message
* ?6 X$ b1 I% w0 b l6 C7 l2 C // called <foo>". You can send a message To a particular8 i5 b4 P# [& `9 c/ d' {' t
// object, or ForEach object in a collection.$ J( W8 t7 O4 ?% m o1 f
: X% }1 j! O" L' x
// Note we update the heatspace in two phases: first run: J: G+ V X5 E8 I% V% F
// diffusion, then run "updateWorld" to actually enact the- D' v8 ]8 \" E! _
// changes the heatbugs have made. The ordering here is
# a: }9 h& r5 l4 \2 x* k5 n1 n3 W // significant!* T4 r! y. D" F
7 W2 g( B; N) G/ _ // Note also, that with the additional- Z; R# K- z! ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 Y* h; ?2 f# A1 T0 h. `; G // randomize the order in which the bugs actually run
. o6 ^+ `8 w1 U* J# c8 _ // their step rule. This has the effect of removing any
; C' o1 w8 A& v5 d/ b // systematic bias in the iteration throught the heatbug
- H6 o) {. |+ v( W // list from timestep to timestep
6 n% s' {1 w9 }" W. H / c2 N: Y/ Z9 {$ Z, ]/ R
// By default, all `createActionForEach' modelActions have% A: a& \) A$ Q. t; {
// a default order of `Sequential', which means that the
6 {! h. d" t0 ^) D. B // order of iteration through the `heatbugList' will be: S" s6 ^, t) a" J3 a
// identical (assuming the list order is not changed% E3 z8 U1 C% w% x
// indirectly by some other process).
# L' X8 d0 L* X q+ S; ^% H, o9 [
# R. _0 x9 A. |( o! g s modelActions = new ActionGroupImpl (getZone ());
% b/ f& N \! N0 H7 o0 h
3 j! G F+ ?* V/ E% s. c try {
! a1 m3 h% | a* [) m modelActions.createActionTo$message
9 Y' o# m5 l3 ]: P6 i (heat, new Selector (heat.getClass (), "stepRule", false));6 J2 J% r) H% y4 _4 G8 ?
} catch (Exception e) {) G5 v8 S& q& l# T! J @! e
System.err.println ("Exception stepRule: " + e.getMessage ());
- ^2 k/ L+ I3 ^4 W1 a }1 ~7 C$ F! a! \( n4 R" F. F
1 ~5 O3 p6 n. _ ?8 D try {
! I# C U. M/ l2 A, n: M Heatbug proto = (Heatbug) heatbugList.get (0);2 u$ e' j& Q3 ]8 S8 Q. r2 |, \9 J
Selector sel =
, J$ A0 a. p' z$ n; ] new Selector (proto.getClass (), "heatbugStep", false);
* P8 u T( q0 r& m actionForEach =
# z, v& o- f) ?7 j( @ modelActions.createFActionForEachHomogeneous$call
2 M$ Q( B( y7 c8 w (heatbugList,4 S9 o3 B" S+ ~7 g. C9 ?+ I
new FCallImpl (this, proto, sel,% S# W- i! O. {6 b
new FArgumentsImpl (this, sel)));
8 y" }0 p9 Z5 P, d6 }4 L } catch (Exception e) {" F4 n/ P1 f2 f7 Q7 W" }
e.printStackTrace (System.err);
9 J, J$ i; n7 r8 G4 A% E }7 v/ X6 P* ]* d9 ~9 x- m
& _/ \9 ^3 r' h& } syncUpdateOrder ();5 j* b4 p5 @; A
: Q! o8 }) l$ V2 k$ w" c! I
try {
|" ?4 X/ C' Z% ^7 J9 ^6 m modelActions.createActionTo$message & p9 K3 } Q8 B( X* {5 N+ R( n
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 }1 G: A- P; R; { } catch (Exception e) {
% ]' t* O; K% R2 \* o0 r8 ^ System.err.println("Exception updateLattice: " + e.getMessage ());
6 H. M0 w' Y) Q4 k }! n( a8 s) E7 T1 ^/ d; r
q! v( M. {0 L( x/ D- k9 `( @
// Then we create a schedule that executes the
3 @; H% G& B# M7 k6 ^4 P // modelActions. modelActions is an ActionGroup, by itself it
4 T7 T5 X, h; X' D' D2 ?3 {- g // has no notion of time. In order to have it executed in- c* Z2 L8 |$ J! W$ D- R1 J1 z
// time, we create a Schedule that says to use the
4 k' F, ~" k! h4 v/ A // modelActions ActionGroup at particular times. This3 v, g4 d A7 t, J" h
// schedule has a repeat interval of 1, it will loop every8 W- u1 M& `3 t f; R' }
// time step. The action is executed at time 0 relative to
; {% K7 p' ~; J0 T7 }9 K- k, C // the beginning of the loop.- z. a$ `9 v2 J" ?7 }
; ^9 U8 |4 q6 u" H: q# M // This is a simple schedule, with only one action that is( c9 c/ k4 z) C( M$ w# R
// just repeated every time. See jmousetrap for more
% O+ t& |5 y. U! e: _ // complicated schedules.
/ Z& ^2 W* A) d; G 0 m2 w$ r6 _" l" x
modelSchedule = new ScheduleImpl (getZone (), 1);: c6 \+ B& q+ @8 h
modelSchedule.at$createAction (0, modelActions);
2 P- l* W( O d y1 @/ V* x0 X 0 \; t& C/ t$ Q' _
return this;
5 t0 e6 j9 J1 {" v* X9 l } |