HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 u( K9 k' c) ]
% n$ S& K7 G& m& T2 L/ o: w& c public Object buildActions () {
" |4 x5 f: D, C super.buildActions();
, M5 e9 {7 l$ R9 J) { o0 x/ ]
7 _6 J9 N$ R1 z5 Y) n9 E% X // Create the list of simulation actions. We put these in* M7 `! S$ R! c
// an action group, because we want these actions to be
0 ~' A, ^9 O2 h! n // executed in a specific order, but these steps should
! F* t3 L6 z+ v& V/ ^4 n# `) d9 s // take no (simulated) time. The M(foo) means "The message
4 D; p3 }# l b0 o9 ]+ Q5 L // called <foo>". You can send a message To a particular3 h5 {- v1 q) G a
// object, or ForEach object in a collection.
1 n/ z9 T* D' U# h; @ * w) O6 U9 p8 [% `) b
// Note we update the heatspace in two phases: first run
- X' L" E% m& R8 \! ? // diffusion, then run "updateWorld" to actually enact the
2 e1 H, B* F b/ F7 K9 ^# N/ H& a // changes the heatbugs have made. The ordering here is; O8 i0 J5 ~+ w2 q! ^: g
// significant!/ N" e/ P6 N J+ \
! K$ u+ [: L. q1 F0 [
// Note also, that with the additional: z, W- {' ~$ w6 o; K$ u6 P% w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ c. a3 @/ H4 m! f9 J, ~ // randomize the order in which the bugs actually run
, P( r8 `. H* s5 ` s: d% z // their step rule. This has the effect of removing any
' y+ S6 e, `0 Q; v. { // systematic bias in the iteration throught the heatbug$ E' i- F# D' m2 O$ e
// list from timestep to timestep& {/ ^' B$ j( q* S4 ^0 Y
/ ?' w8 O8 a* @0 l3 b# f // By default, all `createActionForEach' modelActions have7 O: c6 v$ M4 s& g2 ?
// a default order of `Sequential', which means that the
! a4 `" b# r! n( z( A // order of iteration through the `heatbugList' will be
3 U E0 @7 J" W1 e6 ~* m8 X( u // identical (assuming the list order is not changed
2 R3 n- q' i) O3 @6 Y // indirectly by some other process).
0 x* ]/ i- {1 g) f( A 5 F/ I9 h; ]& V% x, W: X+ r
modelActions = new ActionGroupImpl (getZone ());
) I4 H( Z& X/ I" C' Q0 P" M
, ^2 }2 }: [& \' P try {
0 h9 ?5 @. k& F+ G) y modelActions.createActionTo$message( m9 v3 t3 D8 t, P2 O9 a
(heat, new Selector (heat.getClass (), "stepRule", false));
. X2 S6 l7 f! E: J6 }8 c( F$ q6 m } catch (Exception e) {8 I0 q4 w" t' N
System.err.println ("Exception stepRule: " + e.getMessage ());& k3 k5 B" S1 J% x# i: \. y# {6 V
}% S8 T0 V2 C3 V7 m
) r# O' t/ R) f2 J, G5 ]1 X
try {
$ a2 ^5 _8 Y4 y1 u. q Heatbug proto = (Heatbug) heatbugList.get (0);' x" J k( T4 O1 p' z
Selector sel =
# i q; [6 m# q$ c4 l; [ new Selector (proto.getClass (), "heatbugStep", false);1 L6 n% I2 w- k# D/ a" N$ O
actionForEach =2 Q' S# Q9 ?. T1 t5 z A# u
modelActions.createFActionForEachHomogeneous$call
* B, y! Z( M, T8 \/ d2 d" B (heatbugList,
5 C' ?, J* o# H! M9 R% Z new FCallImpl (this, proto, sel,
. }; K2 s9 E% j. c4 m% ~6 c( z new FArgumentsImpl (this, sel)));
% ^1 ^6 ~" O' f5 n' {1 F4 Y0 c } catch (Exception e) {) r6 I4 _* j0 ~4 P
e.printStackTrace (System.err);8 ^3 Y6 J8 I2 k! ~. x- ?/ }4 f
}! U" c3 ~9 C! |
; k; j) P& o- i& g7 y syncUpdateOrder ();
0 @5 b3 |$ P$ n
4 B4 Y( S9 w5 @: c6 J X1 k try {
+ |5 F0 a4 @. G/ x modelActions.createActionTo$message ) T6 X4 u$ h# S8 E% S
(heat, new Selector (heat.getClass (), "updateLattice", false));7 P) \' q1 g% t
} catch (Exception e) {
1 v' d+ K0 w1 W; _ System.err.println("Exception updateLattice: " + e.getMessage ());# I1 K6 `& c: I3 W: j6 H* J, f7 e, |
}$ H: K y* S; G# ]
, U E1 h1 F4 L8 K# i4 h
// Then we create a schedule that executes the
) y& A7 c5 k: I' T; x1 q0 y // modelActions. modelActions is an ActionGroup, by itself it0 s# ^6 ^& Q8 p0 c6 C7 j
// has no notion of time. In order to have it executed in$ f4 K7 e: \& d- \
// time, we create a Schedule that says to use the8 y; y, [$ J+ W, F6 J1 D
// modelActions ActionGroup at particular times. This
7 }/ a4 j8 t* Q // schedule has a repeat interval of 1, it will loop every
3 |: S; h) S Y3 N" g // time step. The action is executed at time 0 relative to1 W; @: }7 [+ V1 F; K) Q
// the beginning of the loop.& Y) k6 {, f1 C8 @) r( u/ ?
( ]9 O0 R3 T$ b, J0 U2 |* ]
// This is a simple schedule, with only one action that is
, Y) ?* |# W. C" u7 Q/ p // just repeated every time. See jmousetrap for more+ J1 v. V$ \* Q; d B
// complicated schedules.. {; C+ Y! Y& z* R6 s2 M1 p1 @
, ?# r# m2 E0 K x; L" \3 M modelSchedule = new ScheduleImpl (getZone (), 1);
8 v; u- T1 J; Z, ] modelSchedule.at$createAction (0, modelActions);- R5 Z& W' @5 Y1 U: X
8 O* t+ e; ^) z. D return this;5 z f$ q3 u& h! C. l$ k0 f) j- H
} |