HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 |. ?7 H) t' R( z
6 h; Z( Q) V5 c; p: _# l% {
public Object buildActions () {# t2 h# a, W: j% @
super.buildActions();
$ P# Q# r! t$ @2 f5 u% O
5 q Z' o! H" u0 t6 [4 Z // Create the list of simulation actions. We put these in
- u/ m: _, j+ ^- L6 y( b# C // an action group, because we want these actions to be' ]2 a8 _6 I. v; h$ x
// executed in a specific order, but these steps should
( E; c; \! ^/ b* E" G // take no (simulated) time. The M(foo) means "The message- Y/ l. b' @+ ^' k; L
// called <foo>". You can send a message To a particular
4 A3 I# p: @! _& \ // object, or ForEach object in a collection.
5 |% s3 s8 X. C: Y- v2 z. S
, I0 ]9 M/ C- @3 e: G1 j! ] // Note we update the heatspace in two phases: first run
+ Q, K$ E+ r: ^* @ // diffusion, then run "updateWorld" to actually enact the, D+ X* k0 g# A+ [! ?& V& ]
// changes the heatbugs have made. The ordering here is
+ [( q) f& W1 U+ d+ t' | // significant!
! q' Q( e6 x2 y5 a! l , E% h3 }3 I, n# Z9 A
// Note also, that with the additional8 b, X7 N2 K5 a9 Q9 s# a" L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% T. n9 P1 \& r: v // randomize the order in which the bugs actually run
9 _9 ~: v7 X' ? // their step rule. This has the effect of removing any* I* h S* C5 L+ ^3 r' H
// systematic bias in the iteration throught the heatbug* x3 Y# H0 U( U& [# f3 ^# z
// list from timestep to timestep
4 U* ^4 u7 Y/ S; x; |! Y
* G- T) o8 w5 H) T' a // By default, all `createActionForEach' modelActions have% j9 k* f8 U+ N" B$ W% O) J; p
// a default order of `Sequential', which means that the
G7 S- V$ a% l; [ // order of iteration through the `heatbugList' will be
* U& m- u& d& {+ [/ E D: x // identical (assuming the list order is not changed
" O; v! T/ W+ k( Z, K // indirectly by some other process).3 d6 ^4 O# b- y4 o; B) Z/ G
I; [( a7 V* n% G- Y7 r/ y& O6 z
modelActions = new ActionGroupImpl (getZone ());, Q- B# p. R/ T, } g! C
/ B9 _4 u0 j8 F( E! l try {
7 q' G5 Q0 V6 |2 | modelActions.createActionTo$message3 v6 Y( b* w+ g! j& R/ U! G9 h
(heat, new Selector (heat.getClass (), "stepRule", false));6 H" {! S5 u9 \) N
} catch (Exception e) {4 {7 \4 O9 L6 P, V
System.err.println ("Exception stepRule: " + e.getMessage ());
: R; E# w s! }! Q6 [' B }
$ ^9 T! _1 N; @9 g( A
! a7 ~3 J0 v) w2 v! n& Y' e try {9 n) d' a8 s5 } g
Heatbug proto = (Heatbug) heatbugList.get (0);
! _4 ]1 g) Z$ v1 D. \ Selector sel =
- f9 G& w6 z: f6 h6 L1 I+ K6 a new Selector (proto.getClass (), "heatbugStep", false);
5 M' G. D1 z' o" P) k actionForEach =
: ^: @' ?: I) |( \- G9 X modelActions.createFActionForEachHomogeneous$call9 c- h" u" ? `& N7 V
(heatbugList,
5 l, p, |+ X% L3 C3 r new FCallImpl (this, proto, sel,
5 P1 S) l6 P5 ~4 k2 A4 L) ~; C8 o1 S" w new FArgumentsImpl (this, sel)));; Z, ~+ V4 ~4 y8 w! U
} catch (Exception e) {& s. ]+ C5 v/ P5 |+ g5 Z# X
e.printStackTrace (System.err);+ P6 ~1 R* P8 m. v
}& Y% M3 O6 e; L$ t0 E! S) ?4 t
" Y. Z1 `! a5 d8 U
syncUpdateOrder ();8 y2 l A1 n+ ]4 d# b7 Q7 M2 J
' w$ j' V3 o; k5 ^8 D: c# o try {2 c, }# I2 V1 O' g. p# P' d% M
modelActions.createActionTo$message
/ ^0 d1 z( j: q8 b r- H7 ? (heat, new Selector (heat.getClass (), "updateLattice", false));
7 }$ G& u q) t; _ } catch (Exception e) {
3 o6 J" C& s* r* |# b* Z! r1 r0 X System.err.println("Exception updateLattice: " + e.getMessage ());/ R3 g8 _6 |2 s* v' Z' R
}) \8 y, J* |: W+ n" _
v5 Q# Y5 X9 s5 m5 [; j
// Then we create a schedule that executes the P7 h1 u; D4 T" {8 Y8 F1 S
// modelActions. modelActions is an ActionGroup, by itself it' L/ U: f! O* h0 D e" a+ M
// has no notion of time. In order to have it executed in7 a$ }6 ?- W% F; W0 h9 o
// time, we create a Schedule that says to use the; |2 t% _$ C6 f% u! e. Y
// modelActions ActionGroup at particular times. This# { u/ P3 e( _2 }& _; k l& S* |
// schedule has a repeat interval of 1, it will loop every( k! b4 a( V+ m' [, B3 k
// time step. The action is executed at time 0 relative to- |7 p- |" o1 p5 I3 G0 T6 Y. q
// the beginning of the loop.
8 w# k6 f; H5 v& y# {% X
2 B; ?2 X! s0 ? // This is a simple schedule, with only one action that is
$ k- S5 Z) y1 E: w$ S# X/ { // just repeated every time. See jmousetrap for more
! E* O( |' T9 J, S' v // complicated schedules.- [( f: E8 O" d4 \. M
# t' ]4 y! N0 \ modelSchedule = new ScheduleImpl (getZone (), 1);% m5 a2 r# i$ V% c
modelSchedule.at$createAction (0, modelActions);1 a) V( \. O& H( P% F% n
J+ I2 g5 i! B- P return this;
3 @* U& g- {% I. m1 z } |