HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) q- T' d$ U6 q' k9 C Q5 p- T1 Z8 h
public Object buildActions () {- M3 }+ p" z/ _, A: D* f
super.buildActions();& K# q, Q' _8 |
/ s- o6 {7 q4 |' E/ T* t3 h, j
// Create the list of simulation actions. We put these in9 A/ j. U, P- Q7 O4 i
// an action group, because we want these actions to be. g% U; X' z2 A9 w h
// executed in a specific order, but these steps should2 e( B4 G. g' D6 A' b s
// take no (simulated) time. The M(foo) means "The message
* z" x9 L u1 f" c4 U // called <foo>". You can send a message To a particular+ w+ f0 q1 w! j6 v& r( w+ E
// object, or ForEach object in a collection.
" K9 Q0 n& ?4 l+ {. L, I
& y# b9 s1 {1 h. c1 d3 b4 e // Note we update the heatspace in two phases: first run. u b# f* n* ], s! Y3 E6 b$ z
// diffusion, then run "updateWorld" to actually enact the
2 j6 C/ v# b3 b% d8 n6 d3 H/ z // changes the heatbugs have made. The ordering here is
' _- A0 @ F c) Y8 K // significant!0 `, \( x( a: C7 G
6 B1 u$ t. N$ U( ]7 C/ W
// Note also, that with the additional2 P9 i% N+ M h% T' v4 {, d5 Q' J4 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 t3 ]0 W0 ~) M1 s; f5 W
// randomize the order in which the bugs actually run
: y, I5 ^. h# r4 F, r // their step rule. This has the effect of removing any
) y+ |, a, K6 _4 ?* U- C" t // systematic bias in the iteration throught the heatbug
2 ]# e5 Z+ s$ p3 i$ _ // list from timestep to timestep% v: r2 V2 k% w: G
- H! Y2 m/ k9 j" J& W
// By default, all `createActionForEach' modelActions have
, P! F# S* a, f, d$ C( n$ R // a default order of `Sequential', which means that the0 G4 x! X3 v8 V0 }6 q6 P8 w
// order of iteration through the `heatbugList' will be1 z S2 h! p2 q
// identical (assuming the list order is not changed
+ @/ @; y3 v4 V# ` // indirectly by some other process).
+ R3 Z8 U7 m U% f5 H 1 X8 B* ~" B) _2 m! o
modelActions = new ActionGroupImpl (getZone ());% E! a7 t* \5 i9 B! P& p
, H( Y7 k ~- a" T) P5 {
try {
3 J+ P- Z0 L4 c% N/ ] modelActions.createActionTo$message7 p0 y% h# ^" k y- i& u
(heat, new Selector (heat.getClass (), "stepRule", false));
7 V& w) |1 A% W u3 \ } catch (Exception e) {
; O; T- Y- e `1 g0 |7 {7 K System.err.println ("Exception stepRule: " + e.getMessage ());& U3 X. Z+ t2 s$ w5 C1 t
}
2 y' P( U, |3 {2 e S1 D# q0 p- a* W
try {' [4 @; ^" D1 j% `. T6 [4 {! f
Heatbug proto = (Heatbug) heatbugList.get (0);
5 c" H* `/ k8 r7 z1 K! E5 S Selector sel = # J; Z5 M( t) ?! g5 c& J
new Selector (proto.getClass (), "heatbugStep", false);" F, P+ I) @ S2 L* Q% f5 e
actionForEach =! [! C/ N# E2 e4 X
modelActions.createFActionForEachHomogeneous$call: V( F2 k8 \% ~2 G0 s* B/ W: r6 @
(heatbugList,
/ B, T! G- W/ P6 a+ J, L new FCallImpl (this, proto, sel,
$ l8 A' a- O$ l" e new FArgumentsImpl (this, sel)));
. y+ g5 j& H6 t7 Y. U6 I' i& K } catch (Exception e) {
' q# A5 ~+ D" z) x( y e.printStackTrace (System.err);1 p9 }: b) s8 E- i" ^
} }% H5 v1 k& Z+ ?! i
1 l/ P% |( [4 B* J% A y
syncUpdateOrder ();
7 ^0 w* ?% V9 O" y6 \. C" c' j$ H; _! y6 f2 {
try {
3 i- t# ^7 L, w7 W4 n. ]9 M+ ~ modelActions.createActionTo$message + K1 P, E7 r& _! u" e
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 Y% Q# t c( L/ ~2 B, f } catch (Exception e) {; d7 Q/ R% }) k, U- N
System.err.println("Exception updateLattice: " + e.getMessage ());7 q' f; u! r/ C B. e9 H" ~
}
' c" e+ Y+ L$ R* w1 L+ V
/ z" [- P5 r5 N V; i. W // Then we create a schedule that executes the' y( _: N* Z. Y2 @/ C; T& _3 l
// modelActions. modelActions is an ActionGroup, by itself it, p4 j2 I( a ~0 b& V3 t
// has no notion of time. In order to have it executed in
3 H* t$ f0 B5 d. ~$ } // time, we create a Schedule that says to use the
# |/ w7 {4 o' u5 O8 i! V // modelActions ActionGroup at particular times. This8 h# M5 L9 c% S, y! [: ]
// schedule has a repeat interval of 1, it will loop every* S6 y: d, u1 u5 \
// time step. The action is executed at time 0 relative to
! D r( F! @" C, Q4 S7 S // the beginning of the loop.
. l3 L, o" A9 n2 `2 e& T3 K \& X0 X2 B% s; J9 Q, {" E! k8 X
// This is a simple schedule, with only one action that is2 U8 i* I$ k7 U' f% c5 Y
// just repeated every time. See jmousetrap for more0 j% e+ ~% ~4 g0 O/ k) ^' V
// complicated schedules.
; T4 w% t- D! [, a ( ^$ D: n% `$ ~7 h6 H! N
modelSchedule = new ScheduleImpl (getZone (), 1);
8 X5 L9 f" F+ H0 x7 q6 J% j1 f! v' ^- v& H modelSchedule.at$createAction (0, modelActions);
% n4 V( D0 R6 r9 b+ I" x 1 I5 F" i* ~) Q |0 M. E
return this;
; S# i, L4 h6 M7 Z- F$ C H# O7 U } |