HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ] M- B, I$ p+ h' u
" x4 I$ ]1 |: j3 H/ I' O- w9 L
public Object buildActions () {. s! w7 U& G1 x. c2 Q0 }
super.buildActions();2 ]% v( P/ G7 H
0 p2 e! R+ u$ v // Create the list of simulation actions. We put these in
( v: L h, h% |; }0 I; X5 k7 r0 W // an action group, because we want these actions to be( g8 d9 f. V8 y, V/ E h
// executed in a specific order, but these steps should, g5 B3 U# l# _1 h
// take no (simulated) time. The M(foo) means "The message
6 d/ Q! B. j* i& G9 ?& C // called <foo>". You can send a message To a particular
$ I1 \" p& D3 {" ] // object, or ForEach object in a collection.
' i# M- k$ `3 g6 ^
% H k+ m& X1 d* r // Note we update the heatspace in two phases: first run+ a) b: U) Q2 @8 {" N: o! o
// diffusion, then run "updateWorld" to actually enact the
" T5 Z# k" c3 x6 C // changes the heatbugs have made. The ordering here is6 c, C! `; y% [! ?* W
// significant!" K) `' d! F8 e! P) h$ ?
* u9 [7 j4 p; g" C. O0 S ?- w // Note also, that with the additional! V+ M& e8 O2 j: T
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 V5 a0 j4 L8 L, Y // randomize the order in which the bugs actually run' u! R' P+ `- i; ?; d
// their step rule. This has the effect of removing any, g! R4 E' D& z+ D% `. L
// systematic bias in the iteration throught the heatbug
# J; c+ c7 R! n // list from timestep to timestep p4 ^, w, j& K1 T2 p* W r
- P6 X$ L. F/ F) t6 J" V
// By default, all `createActionForEach' modelActions have' {% s6 Q, i. D9 _
// a default order of `Sequential', which means that the5 J/ Z6 M; ?, y: h
// order of iteration through the `heatbugList' will be% `) W5 ^$ K+ H# Q0 W% q% s
// identical (assuming the list order is not changed
3 ]4 l0 o' J9 F7 ^( D0 w: S2 T // indirectly by some other process). m) [0 J( o; k5 o4 y
6 L( L- K4 W% e- Y; m5 D
modelActions = new ActionGroupImpl (getZone ());
% {! U. R; @1 [6 ^: q1 y+ d. S, r
% f% G! t' E3 f$ i' C+ X try {
+ v! s8 {* d# l5 C3 o) C modelActions.createActionTo$message6 D3 V# c( Z; S6 l# O# E; o
(heat, new Selector (heat.getClass (), "stepRule", false));6 d0 [9 W8 z# `( l9 T, m# Z4 g9 `
} catch (Exception e) {
; [8 a/ p6 ^1 J$ |& U! @ System.err.println ("Exception stepRule: " + e.getMessage ());
0 Y4 X( V" r( Y) D! D8 O! a1 R5 {) Y }: g. q5 w$ [' }6 x
+ Q! Q6 G7 v) H! {0 s! C$ x1 l
try {' |( _4 g/ Z! i- P. ^7 e
Heatbug proto = (Heatbug) heatbugList.get (0);
5 I- b6 J0 L: C; H. Q Selector sel = . ?8 k, i3 y$ u( N8 m
new Selector (proto.getClass (), "heatbugStep", false);
$ f0 }# B6 p. { actionForEach =0 g0 M$ }2 c" L6 ^! s" v9 C4 o1 W
modelActions.createFActionForEachHomogeneous$call5 N3 h/ p$ h8 |4 i. T) z2 u
(heatbugList,7 A, J4 R7 x/ g! T# b" Z
new FCallImpl (this, proto, sel,3 N- b) }# v! N4 k. B
new FArgumentsImpl (this, sel)));
# P$ ^7 u! u' @ } catch (Exception e) {4 `7 d: r: [5 r, W5 k4 K6 u, y
e.printStackTrace (System.err);2 r. m( q. F) f* ~. O% b- r' {
}
) j0 r3 q- c& h
/ m4 I! K* F$ b( E: f syncUpdateOrder ();
6 H4 E: p) R" p4 N3 f( Z6 `& g# T! E7 W) v2 i) i& L: a
try {' L* C. A& T2 P' i7 ]; p1 T
modelActions.createActionTo$message * Z8 e/ m# y' t' w+ s
(heat, new Selector (heat.getClass (), "updateLattice", false));; R; ^7 X/ P% x v) f1 l3 k1 N0 P
} catch (Exception e) {- g0 W- i3 o! U! F+ i( N
System.err.println("Exception updateLattice: " + e.getMessage ());& B" k1 @) j2 d1 i" ] H
}
( j" i+ ?7 Y0 I/ J" p/ T! j; m 1 R1 i$ Z% e; {( V' t
// Then we create a schedule that executes the
2 L+ \% | O5 c2 J% v3 F- B: G2 A // modelActions. modelActions is an ActionGroup, by itself it# N4 m% J' ^& q
// has no notion of time. In order to have it executed in
5 J7 B' S$ C7 q" N1 k3 r // time, we create a Schedule that says to use the
/ Z6 m9 D2 l6 M/ v // modelActions ActionGroup at particular times. This
N, d# a# A1 h2 m& k // schedule has a repeat interval of 1, it will loop every
, B0 C6 I+ F( q4 z( Z+ e // time step. The action is executed at time 0 relative to
' F& Y$ a6 m9 p: b // the beginning of the loop.
1 S$ o3 U: e% E2 K# o- O$ x
) E0 E ?0 O4 S8 M& M, I$ h# j // This is a simple schedule, with only one action that is
; T$ O) `/ I) e& N }4 p0 _3 F! | // just repeated every time. See jmousetrap for more
) M) q' G3 U0 b/ x$ F% D // complicated schedules.3 `* i; p% q; |( b0 d! i
0 S# z/ b- X9 g/ k1 N3 I2 n modelSchedule = new ScheduleImpl (getZone (), 1);
( ^2 b( T0 b/ Z+ s3 ?4 C9 f modelSchedule.at$createAction (0, modelActions);2 ]8 o% N- d6 m9 O8 s- V% y
! l) h' k, G5 i
return this;6 Y! |- z3 A+ h4 _5 \
} |