HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, W( }! D R$ T7 M9 m
% ]. q: O- \, W. X1 _ public Object buildActions () {2 [* Y( I6 P: u0 E1 n0 V+ j
super.buildActions();
. ]3 K3 V2 K& Y) J
1 w0 Y2 j- k- z+ j // Create the list of simulation actions. We put these in3 n6 x7 }3 q* J
// an action group, because we want these actions to be
2 S7 G; K$ G0 N+ Q2 v* N5 [ // executed in a specific order, but these steps should
: F' x! m, q1 j: X; J/ d4 q // take no (simulated) time. The M(foo) means "The message
) x9 R4 c* |% _ // called <foo>". You can send a message To a particular# n5 p) H# n3 H" P1 d3 G; f
// object, or ForEach object in a collection.8 c6 F2 X- ]% u0 V+ {# W% {' g
" S6 v$ }- _& k/ B0 } // Note we update the heatspace in two phases: first run
9 x7 o9 T% T3 h2 v // diffusion, then run "updateWorld" to actually enact the, d' k- N3 `$ @! g3 a: u1 n" [
// changes the heatbugs have made. The ordering here is
7 e7 p! p( x+ N6 T, e j+ Q // significant!
+ ]' j3 a& n% E$ K8 x, ]* `$ [
3 R+ u4 W0 d2 U+ i! o // Note also, that with the additional) y9 t: A. t. ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 _) ^' Y. \: [
// randomize the order in which the bugs actually run [6 I6 D! [ E
// their step rule. This has the effect of removing any
( q" d# E) ]% S // systematic bias in the iteration throught the heatbug
$ G+ q# l+ y- W5 Z // list from timestep to timestep* X0 m4 N* T7 V$ x0 F8 d& p
1 r, s" k/ s9 u // By default, all `createActionForEach' modelActions have% h& q6 J) O. E, m
// a default order of `Sequential', which means that the: s: q6 _: J% L& A' x7 D( @7 X% i
// order of iteration through the `heatbugList' will be0 }9 x8 R& J1 y
// identical (assuming the list order is not changed
% ^" q( a3 b r& q // indirectly by some other process).% _5 o- O6 v4 F5 W; T v+ u8 n
" u, S: C0 o4 G0 D' ^2 _
modelActions = new ActionGroupImpl (getZone ());
% _$ o5 D! V7 H5 V6 D' y
8 |4 `+ {( W* Q% B/ X try {: A+ M! ?" `: F( ?
modelActions.createActionTo$message o9 y; q% H K7 V: D) ]1 W
(heat, new Selector (heat.getClass (), "stepRule", false));0 j/ G5 ^- p7 |( l. a$ a1 W- m: i
} catch (Exception e) {
. A2 w+ {# g0 W; }3 G System.err.println ("Exception stepRule: " + e.getMessage ());: E8 G J5 ]8 B! Q
}
* o# W5 f' o& d' L* V0 ~- L
" L! U7 u* s5 P0 g try {
4 K; t" T/ W+ h2 r$ W2 p Heatbug proto = (Heatbug) heatbugList.get (0);/ H+ u( S( b- q
Selector sel =
& W: k7 L& ]' b7 w% m* {0 ] new Selector (proto.getClass (), "heatbugStep", false);( ]& m8 x9 c+ x
actionForEach =& X i% H. w8 }: H' X4 v$ O" ~
modelActions.createFActionForEachHomogeneous$call
. d* C6 Q( t+ }! W (heatbugList,; n: p* |/ i. T0 h, |
new FCallImpl (this, proto, sel,; s' s. G* ~9 \4 x# ]3 @; N
new FArgumentsImpl (this, sel)));
6 d$ M& a6 T# B4 p: b- Q3 f } catch (Exception e) {
" x% a: z2 C. I% B" ?$ {& @ g& N9 g e.printStackTrace (System.err);
2 E1 M4 _; s, Z$ E }
6 D; ~, M* u3 m / | o1 I1 W g4 j+ p
syncUpdateOrder ();
* z. j8 d% [6 U, a8 L0 s% B5 D
# [3 S0 \7 z2 n% B! P7 O& n4 k try {
5 ?( G2 ^3 w. `& Z5 A# \0 a modelActions.createActionTo$message
( k* G8 V7 B' z$ O+ ]7 t (heat, new Selector (heat.getClass (), "updateLattice", false));- h4 j+ u* W- ]
} catch (Exception e) {
0 E# D7 W8 x; @; e System.err.println("Exception updateLattice: " + e.getMessage ());" X8 |! T' @2 ?' L2 W% n! N5 q6 u* c; D e! o
}
; M" k" O# n1 _7 f2 K3 y
$ y) t+ \* w# A8 w/ A // Then we create a schedule that executes the- Q* q6 X s+ j
// modelActions. modelActions is an ActionGroup, by itself it% ^$ u W, M% L3 c& g
// has no notion of time. In order to have it executed in
9 J' n$ g" C \1 c, H" H. z. b E // time, we create a Schedule that says to use the
+ N( s) N: [% m2 |& S! |! Q // modelActions ActionGroup at particular times. This8 G* y) N" k8 X, h- E/ @1 r' o- j! w3 n
// schedule has a repeat interval of 1, it will loop every* c/ \+ y7 a# g0 s9 [! |
// time step. The action is executed at time 0 relative to4 {. h p( W. W( r+ M0 B; D
// the beginning of the loop./ q. \6 u4 F! ]- ?' @
% y) n1 R* ]: ?- z* I5 K // This is a simple schedule, with only one action that is
4 f: ~" H" Z- n/ q% N6 u- R // just repeated every time. See jmousetrap for more
* u. d4 x5 D: D) C+ b* r // complicated schedules.
/ ~* Y1 g0 ?' Z+ G; q- g
+ t0 R' ^$ x4 T% X' K! x) l modelSchedule = new ScheduleImpl (getZone (), 1);
, d3 y: ]) C* S; T. s$ @ modelSchedule.at$createAction (0, modelActions);& j* b0 G) |6 i, C' D0 s% i
2 b; r3 d% L- |) h9 F
return this;* S5 z1 p& A& d& N; U
} |