HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! z* i. j" b7 Z4 B0 I( {" D, ?* P; x* _4 Y9 }8 L
public Object buildActions () {, O/ X5 r: k4 M+ a r5 \
super.buildActions();" X& r% G2 @) O1 j7 S
! @8 R" t4 r+ z1 k" Y# ~0 W // Create the list of simulation actions. We put these in9 d: `' G3 m- z, K
// an action group, because we want these actions to be
, t: t( F. k' z+ h5 X$ c0 R // executed in a specific order, but these steps should; L! h" |/ r4 d8 |5 b; K
// take no (simulated) time. The M(foo) means "The message* a. t* J2 Z$ u& L
// called <foo>". You can send a message To a particular3 x$ O: A7 i. Q
// object, or ForEach object in a collection.
0 L Z ~+ `4 C$ W8 z1 i . [- U S t5 Z- y) n! ^) I
// Note we update the heatspace in two phases: first run
4 q6 S9 e/ `4 N. x% O- n // diffusion, then run "updateWorld" to actually enact the, j% V- X l' E6 E! f" E* ]2 e% M# r
// changes the heatbugs have made. The ordering here is7 G4 X& v n' t4 a
// significant!
, g* u7 T/ P3 ?8 J% d, G0 ~
; K* X+ M; K' m( g, F% S- }9 P // Note also, that with the additional
9 w0 E8 j2 P7 d4 x/ h: z& ^5 o // `randomizeHeatbugUpdateOrder' Boolean flag we can* m% R1 X0 P, K7 l4 |. |
// randomize the order in which the bugs actually run" p& J0 X$ K1 F+ g/ Z! O2 t0 F
// their step rule. This has the effect of removing any# {9 q5 m/ e: G6 m! l" {
// systematic bias in the iteration throught the heatbug
' _; O+ T: B V0 _$ [6 W // list from timestep to timestep
0 H2 f2 Y* K) ~! s) |; ?: x
! ~& U3 k* k7 m9 y# q // By default, all `createActionForEach' modelActions have* r3 K7 r" g0 F" }' V
// a default order of `Sequential', which means that the- \ @- O6 t9 W- Y; K. N
// order of iteration through the `heatbugList' will be* M1 q# `9 P; D# s; |
// identical (assuming the list order is not changed+ ~7 X1 Q/ o8 \" B% G }9 {
// indirectly by some other process).
* {9 ^' `: |: d% S , ~: M. R0 I; i4 r! L9 h0 W
modelActions = new ActionGroupImpl (getZone ());
: l X1 D" l$ _5 F9 \0 _/ i6 q4 z4 l, P$ d$ G3 T4 S6 x: ?' T
try {1 X3 m# P N" X; D
modelActions.createActionTo$message H. a6 \. M# Y( H- z5 u
(heat, new Selector (heat.getClass (), "stepRule", false));
/ y* t* V+ o, B/ W) f } catch (Exception e) {
( I1 `, S& z$ [ System.err.println ("Exception stepRule: " + e.getMessage ());
/ v/ z5 ^& B+ |/ X1 l }
6 B( Z; K0 _+ \0 w% ^% h! n
& z% G' g/ t) v. f/ f: s! ^, I9 Z try {0 z8 O; v/ `" d/ \3 t, a
Heatbug proto = (Heatbug) heatbugList.get (0);! f, \8 N% t ~7 v8 C) D
Selector sel =
+ d4 L$ f% k4 }" f9 h! } new Selector (proto.getClass (), "heatbugStep", false);
! F0 h' ^4 B: F actionForEach =2 i. q5 c% O2 G! N# p
modelActions.createFActionForEachHomogeneous$call
1 ^) h' G: w+ Z/ W (heatbugList,
- `$ x& R/ N/ x new FCallImpl (this, proto, sel,
2 k6 h- y. @ R; ]( B+ F new FArgumentsImpl (this, sel)));
' S4 o9 L u: X/ L } catch (Exception e) {3 O7 L( r- q! Z- H; c1 @0 G
e.printStackTrace (System.err);3 z) \( H% U; p2 A% Y
}. o9 U, v- p" L1 \, a
4 z/ K; v, {- `( i( c3 w syncUpdateOrder ();& E. J; M) R0 h: E8 n
- |. q4 ]2 s9 s5 ?& J8 W try {% P( v- m/ Z5 T* m! v7 ^
modelActions.createActionTo$message $ l9 q- F$ h- O7 C# i* ]3 t+ I
(heat, new Selector (heat.getClass (), "updateLattice", false));2 b, I0 J% {1 ^" P" ?! @
} catch (Exception e) {
* p2 _5 z8 O e4 M1 R9 _/ P: E System.err.println("Exception updateLattice: " + e.getMessage ());
' i9 D# D# ]; Y: } }
' s7 @& K) y$ y- e1 E) [7 t 4 G2 U! C' l: T5 d
// Then we create a schedule that executes the# ]. _/ K* M# ^! _2 J
// modelActions. modelActions is an ActionGroup, by itself it
( E' e' z5 h" p) c& c o4 h // has no notion of time. In order to have it executed in4 u# X! Y; ~3 Y/ D2 t6 y8 @8 _
// time, we create a Schedule that says to use the
. w) I7 G8 a) h* b3 o+ Z8 p" v // modelActions ActionGroup at particular times. This
& W: D( V! ~; }* u2 g: Q // schedule has a repeat interval of 1, it will loop every
) i7 v9 x- c+ S1 z9 n3 Y2 o // time step. The action is executed at time 0 relative to
7 @5 D" x! E5 o. }$ z- E // the beginning of the loop.7 O# O1 {' w6 z& g
r. {; v+ p9 _* Q1 i. M- C3 @
// This is a simple schedule, with only one action that is' f! }' x) A$ s3 J: v5 i0 [& x
// just repeated every time. See jmousetrap for more
* K5 f+ @" U o$ D! l- q8 s // complicated schedules./ U) B: i% }/ {1 A
8 ?8 z4 w( c( w$ K q2 K
modelSchedule = new ScheduleImpl (getZone (), 1);
8 z7 v9 O* o, ]# a+ J modelSchedule.at$createAction (0, modelActions);/ M7 G/ R" M5 J; e1 T
# Z& k) P2 ]7 u [+ }2 u4 g! X* H& v: s
return this;
2 }- d3 H4 J$ i6 a; M+ j } |