HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# w" h {( S! e, V8 M# k
2 L! t _, j3 O/ \7 G; d& B2 }
public Object buildActions () {
( _ m- n: D$ F super.buildActions();
2 y \, [3 v9 v5 { # x& l) B1 V0 l4 P* h
// Create the list of simulation actions. We put these in
7 W. H, M/ l5 J5 B$ H1 D7 N" q // an action group, because we want these actions to be
! }1 ~$ u- J, m- o // executed in a specific order, but these steps should# Q5 d. \' Z! T6 v
// take no (simulated) time. The M(foo) means "The message0 L0 t; U8 B- j& f
// called <foo>". You can send a message To a particular5 P4 l: o. c& j/ ]3 F1 i
// object, or ForEach object in a collection.
6 Z2 c, V" Z A& W. \
3 d* C/ K0 E' R' y // Note we update the heatspace in two phases: first run$ f3 M$ S9 E7 L- H$ {1 J: Q
// diffusion, then run "updateWorld" to actually enact the A& P' M4 ^8 r @. F$ p
// changes the heatbugs have made. The ordering here is
0 L- z6 g- O( V& u // significant!
. b% |! ] M5 C- U$ v- v
% ~( }7 G$ a+ u6 z' z9 D; _+ |, [4 u // Note also, that with the additional5 D M# X8 d7 N
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( ? {! \1 k; w! Q% _ // randomize the order in which the bugs actually run
! X0 K: T9 Q5 e // their step rule. This has the effect of removing any; y$ p+ Z6 z7 E7 r0 x& r2 h0 a
// systematic bias in the iteration throught the heatbug! H) o0 ?: m+ f# y% y0 [
// list from timestep to timestep
* t& ~1 F, S" a, f/ w, W' k ! @( q% P+ y; v- }, K7 w
// By default, all `createActionForEach' modelActions have
x9 e2 n, O* p // a default order of `Sequential', which means that the
9 Z$ O" y9 B6 j! l. I. w5 P$ j // order of iteration through the `heatbugList' will be
- h7 A9 Q9 p; E4 K, ?# V9 t // identical (assuming the list order is not changed
1 D& n& U6 K2 Z+ r+ D // indirectly by some other process).9 o# q8 Z1 B7 b0 r7 H: |) ?
" V" X- D" C4 ?0 J) y+ m modelActions = new ActionGroupImpl (getZone ());
% g9 ?1 h, u) s. u) D0 \% F! V0 o8 P" Q S+ d# `( [3 G: ^3 v
try {
) j8 |8 b0 s6 O; J modelActions.createActionTo$message2 L s: [ ?$ @" K6 V1 I
(heat, new Selector (heat.getClass (), "stepRule", false));
0 h* i5 z8 n2 }+ ?# b) [ } catch (Exception e) {
3 E( [% ~6 n" T8 q# U. |* |* _ System.err.println ("Exception stepRule: " + e.getMessage ());
5 u- N. V/ Q, L- s7 X- m }
: s# c2 \* v2 Q6 \. T- J& _) e% F( Q
+ p, \$ y2 O) d# [0 H" @0 v, T try {) l' F8 D' L2 F6 r
Heatbug proto = (Heatbug) heatbugList.get (0);
& A2 Y3 C" A% B3 h Selector sel = 3 O9 {; _- E: [6 O! p7 j
new Selector (proto.getClass (), "heatbugStep", false);3 C7 |- y7 r; K6 x" T$ o# w
actionForEach =! F( U0 a& G. p
modelActions.createFActionForEachHomogeneous$call
0 N% n6 t. Y ?( d/ b$ X (heatbugList,
$ C+ |9 _* h' Q/ x. R! R) s# i new FCallImpl (this, proto, sel,* {; k4 m1 E+ H( w. U
new FArgumentsImpl (this, sel)));
2 g1 ^/ m( M2 Y+ O! u8 Q0 m } catch (Exception e) {
0 ~; Z8 X) o- F- Z0 e* m! [ e.printStackTrace (System.err);1 M2 x0 D, b7 n4 z" i/ m
}
$ f0 a i- M8 ` 3 S& W* }" W% K! A
syncUpdateOrder ();/ X0 U, j5 W5 {. N% k; T7 m
/ y/ } q6 @' {; B try {
& D, U7 N( `! S. c7 n modelActions.createActionTo$message
* o& X) z# _3 N4 f (heat, new Selector (heat.getClass (), "updateLattice", false));% K: k/ p9 B+ a+ k0 n9 y& o5 I7 a
} catch (Exception e) {
! @ i% u& ?* \6 g System.err.println("Exception updateLattice: " + e.getMessage ());3 i+ _( i) h+ M* ^" k' a
}
/ D q' P5 c k5 _1 M5 |* V/ z 4 N& |: ^, v" P1 N
// Then we create a schedule that executes the4 R' F" n+ D7 Y& m" c% @' Y( m
// modelActions. modelActions is an ActionGroup, by itself it- s0 Z. P( y) o. O X
// has no notion of time. In order to have it executed in
. H( w" i3 g( Q, K4 H/ }. | // time, we create a Schedule that says to use the: q) D; y$ c" K. Z; s
// modelActions ActionGroup at particular times. This7 i1 w+ r8 [4 u" n: `3 u6 f
// schedule has a repeat interval of 1, it will loop every
8 G) R8 P) @' ^3 h // time step. The action is executed at time 0 relative to
& `3 D& @. ~# ^8 \: u% x! p8 ^ // the beginning of the loop.
: p2 @; y( | R8 a
2 n. z1 T, @1 ^( m9 p // This is a simple schedule, with only one action that is, z4 m9 E3 |2 ]
// just repeated every time. See jmousetrap for more- ^" {( [7 _6 s+ }0 j6 U5 P }
// complicated schedules.
1 W# t3 L' d5 {1 k3 O5 L / F3 w7 q/ W' P" J% S/ M
modelSchedule = new ScheduleImpl (getZone (), 1);
, [7 s8 @& \3 }/ W* P modelSchedule.at$createAction (0, modelActions);
# H% G# P' `, w6 l- P: U3 \) D ; H- V+ H1 y4 T- V7 W- H9 [
return this;, Z* W) Y, \- n; o
} |