HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" V! Z& T, W9 j: w( p6 O. f
. q0 X3 e+ J" ^3 w+ G8 T4 `
public Object buildActions () {$ z( }% M- U# K7 i
super.buildActions();
8 _+ T& `9 [; M6 l2 w$ }
) _2 k* f' t6 l. m5 J5 o8 O // Create the list of simulation actions. We put these in
$ v) L: I" w. U7 w+ r5 C) Y // an action group, because we want these actions to be
q% B% L% g2 J: n0 K // executed in a specific order, but these steps should1 h" P+ I4 s. J8 M& G3 N8 U& H
// take no (simulated) time. The M(foo) means "The message
- x/ M3 M+ W& m' n( O; x0 S5 Q // called <foo>". You can send a message To a particular
; p$ w9 y! l2 z7 P9 M$ q( Y // object, or ForEach object in a collection.
: k- W% ~5 s1 d + g5 \4 l- s. u/ d
// Note we update the heatspace in two phases: first run1 m1 c! L- z; h0 l+ k7 x( V
// diffusion, then run "updateWorld" to actually enact the# }, n2 h3 O2 |' v$ ?6 I* U
// changes the heatbugs have made. The ordering here is
, I9 y7 `6 p2 u; h _ // significant!. K+ ]2 Z3 }: z$ t- d$ A% t. h
6 I8 Z$ B$ a0 t // Note also, that with the additional
$ w4 ?0 t3 B7 E // `randomizeHeatbugUpdateOrder' Boolean flag we can6 ?0 n8 c1 B, o
// randomize the order in which the bugs actually run" U3 Y0 |: f4 U$ n! X; }
// their step rule. This has the effect of removing any" d9 f0 c+ b% ^! d
// systematic bias in the iteration throught the heatbug
7 e+ c) H3 d7 x& x. s- \9 j // list from timestep to timestep4 [) x/ l" e; y1 ?' V2 [
s3 o; b. ^8 x4 G) ?- f
// By default, all `createActionForEach' modelActions have8 f) r& U/ K( J0 T
// a default order of `Sequential', which means that the, j7 S0 u3 e1 N! L* s1 t
// order of iteration through the `heatbugList' will be
% [% p* U ^1 }9 \1 C! F // identical (assuming the list order is not changed
7 i* I, M; u4 R# z% h$ R // indirectly by some other process).* j, y8 s* b+ Q+ g' I
* T! s# I: L( i t8 {2 [ modelActions = new ActionGroupImpl (getZone ());/ @% M6 |- i+ c7 m/ f; X
- L) v5 i4 f ?$ j `; U8 t try {
, i- p! N" T% m A modelActions.createActionTo$message
1 T' I Z8 m6 H) r8 S0 t( l (heat, new Selector (heat.getClass (), "stepRule", false));
3 O* |1 u. T* r+ `7 B* [0 [% @8 P4 p } catch (Exception e) { F2 M7 R5 @, B9 X* @3 F
System.err.println ("Exception stepRule: " + e.getMessage ());/ {: v6 V) [+ \7 _
}9 t4 G" G! v5 |
2 F1 y) J: _' w& C1 m! N
try {
9 E- {+ y) N5 A* E Heatbug proto = (Heatbug) heatbugList.get (0);
# r# t. B7 }& Y Selector sel = 1 w! l6 ^5 [$ {% O0 w
new Selector (proto.getClass (), "heatbugStep", false);
" `, o/ ? i4 ~' f/ q! T# L3 _ actionForEach =
3 `' a: c( f. ]6 u. Q' i modelActions.createFActionForEachHomogeneous$call) G* S& P# {' W# |4 M
(heatbugList,
/ h/ }- c0 X c6 h4 }0 E* S new FCallImpl (this, proto, sel,
/ N, K$ X* f0 Q% |0 A3 z5 g, ` new FArgumentsImpl (this, sel)));
& @7 V2 c' u* C } catch (Exception e) {
) {6 B6 c" c2 {/ R- m e.printStackTrace (System.err);* Q& v/ |, Z- l$ l( |/ d
}
- B) }& ]' i1 d8 g , `! L; [7 w' ]3 ~- m- _
syncUpdateOrder ();" i0 u$ r9 S0 c( N8 m
4 r$ {7 B# W3 J9 y
try { X& [/ c. w5 L; O; h
modelActions.createActionTo$message ' C2 C/ p4 L- n. N3 N$ Y
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 J/ i; ~3 p0 W. @; f( x } catch (Exception e) {
* Q( H7 v/ a8 w9 o5 p+ Q System.err.println("Exception updateLattice: " + e.getMessage ());
_( h# r% h5 D. R4 A: E }% \" C* Q% V' P! V) Y% i
, l& g4 G8 ^1 O5 `6 o2 ~; z- f // Then we create a schedule that executes the
( a' I% L5 \( i; | // modelActions. modelActions is an ActionGroup, by itself it3 k6 B8 q5 O& F4 N3 _6 A: h
// has no notion of time. In order to have it executed in
" S: z/ I, ^' L M* _ // time, we create a Schedule that says to use the. E4 R# l0 x; u1 N
// modelActions ActionGroup at particular times. This) k" N g. o9 V$ \
// schedule has a repeat interval of 1, it will loop every
. X) |* K- o }1 B // time step. The action is executed at time 0 relative to0 I0 |( o1 q: m" K( r0 ` e
// the beginning of the loop.* n& D5 P' |8 B* ]2 |% g2 F
: _% `, e z% t7 N3 x% H9 I* }
// This is a simple schedule, with only one action that is
+ L; s, h4 N, B$ K8 t6 b r // just repeated every time. See jmousetrap for more
: \% e" x# \$ R |( j/ d2 k& W @2 I // complicated schedules.7 h/ z C+ e# r8 l1 r4 \
0 L" ^& E& B4 ~% a Y- h( z+ ~
modelSchedule = new ScheduleImpl (getZone (), 1);
" r/ \% b, U( k6 B4 K1 D modelSchedule.at$createAction (0, modelActions);& V' \# ~! u2 |& s* \' ?( k) j
# o- } b$ s. y Y return this;2 `6 E3 ]- m9 Y8 _" q! ^
} |