HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 H. {5 z: Y& [6 `6 ~. k$ a
7 G- A) s8 g. ]- z1 _0 M; z0 D% y public Object buildActions () {, j1 _; n/ V) g! E' h
super.buildActions();
8 D, t; V- X2 @, R- L' s+ u
, k4 P4 @8 }+ R // Create the list of simulation actions. We put these in B! [# V# b) r
// an action group, because we want these actions to be
9 C6 B& P3 m3 y: {) ^! } // executed in a specific order, but these steps should9 V1 H1 u$ y. v: Y, b
// take no (simulated) time. The M(foo) means "The message5 V0 ^/ [3 k* M5 e5 o0 \
// called <foo>". You can send a message To a particular
$ K" L! v: T; T" y! h: [+ N% w // object, or ForEach object in a collection., J9 P v$ v; ], Z1 p, v
- w5 I5 _" F9 S9 C // Note we update the heatspace in two phases: first run
$ x4 c5 p+ F( M5 y# ~5 R. L // diffusion, then run "updateWorld" to actually enact the, I5 M& ~ v2 e p
// changes the heatbugs have made. The ordering here is
) [) Q$ Q8 ] X+ E // significant!: F+ Q" i8 u* F7 w; q
: S. Y. @! `2 k // Note also, that with the additional- }, v" w% W, Y+ X0 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 U$ |5 f6 {3 m# Y // randomize the order in which the bugs actually run% A O! B$ k& Y
// their step rule. This has the effect of removing any) u" O2 A9 j- k; U) v8 ^' \7 i8 Z
// systematic bias in the iteration throught the heatbug( O' g9 k) k' }: i0 h: l
// list from timestep to timestep
% Y6 L' y! s8 H : g- ^5 }9 _; ?& O2 W
// By default, all `createActionForEach' modelActions have
# v' ]$ D. X* x: ^) q4 { // a default order of `Sequential', which means that the
4 T+ ~! b9 H. ~) k5 ?( h! V // order of iteration through the `heatbugList' will be& J% h2 M e; F" A4 O. U! s
// identical (assuming the list order is not changed k: H1 Z7 X3 l
// indirectly by some other process).
c- k& P& _# r1 k9 h$ i
7 s4 K9 ^( v- M A) m+ x5 Q& Q modelActions = new ActionGroupImpl (getZone ());
* O! m3 l( B/ G x+ l0 M' |
5 _* M# |3 ?; O) N6 c# P0 B3 | try {5 r1 r$ ~) i. d# P, M+ S
modelActions.createActionTo$message5 U1 O! F; @7 @8 `
(heat, new Selector (heat.getClass (), "stepRule", false));
& e4 x" z1 S2 |: V6 V! } } catch (Exception e) {
& F t) Z7 P! K7 r9 ?' X4 Z- m* ~ System.err.println ("Exception stepRule: " + e.getMessage ());
; m6 _, v `- M9 N, _ }
0 J2 A/ a, G2 N; J
! v2 u1 V) _: F( o0 q. k4 {6 S2 l try {5 q. @) k1 l4 @7 W& l
Heatbug proto = (Heatbug) heatbugList.get (0);
Q. P- o# E! P5 W# c9 f% y i" L2 T Selector sel =
1 l3 @. t, y/ s* W x new Selector (proto.getClass (), "heatbugStep", false);
. N; P4 D) W: M actionForEach =
; M `5 ^; o4 t; y modelActions.createFActionForEachHomogeneous$call: ? i- B+ b& \
(heatbugList,* o8 a! E v" n: g1 d9 l+ j
new FCallImpl (this, proto, sel,
4 \! n+ l+ I. z$ O$ v new FArgumentsImpl (this, sel)));
8 I. H* ^' x O9 g4 i } catch (Exception e) {
" ?2 u6 }7 u8 {8 `6 T e.printStackTrace (System.err);- _$ f# h" }% y, K @% l, g1 P* q
}
$ g/ u: }7 Q( s$ R: z
6 X/ m( S# Q* s5 g7 J syncUpdateOrder ();2 p0 b: T; `$ }# q7 \ z. L
. `- m6 W0 E: Y' ^( `% M2 [7 T! u
try {
3 q, c: h# P- ]. w. j/ @ modelActions.createActionTo$message 0 q8 ?$ r" D0 x: h0 r5 N6 ^. P
(heat, new Selector (heat.getClass (), "updateLattice", false));) ]+ k9 r& B9 I- U+ r- z
} catch (Exception e) {0 I4 N; t5 u# H* P! ^
System.err.println("Exception updateLattice: " + e.getMessage ());& H' |2 z, C; N% X* {- a
}7 Z# z' Y8 Y9 y8 V3 ~3 X
" i6 `2 h2 e# M, V9 m2 S
// Then we create a schedule that executes the( ?/ F. O5 E8 R. T; @0 L3 c
// modelActions. modelActions is an ActionGroup, by itself it
9 t5 X! @7 D' j, D0 E1 e // has no notion of time. In order to have it executed in; D; y( p0 o: j0 D4 b* Q4 D
// time, we create a Schedule that says to use the5 s1 r: k/ m. e' T2 |2 t( {; x3 H
// modelActions ActionGroup at particular times. This
2 d7 I5 W5 C( M4 n; L4 W* m // schedule has a repeat interval of 1, it will loop every1 v9 b k! h" H, R8 w
// time step. The action is executed at time 0 relative to. ]- ~+ w2 G) V7 ~% Q
// the beginning of the loop.
: O r P ~: C. [( m1 l, b) W: P ~! ]/ i1 W+ V: [
// This is a simple schedule, with only one action that is
: V6 b6 D( a5 A+ Y" v // just repeated every time. See jmousetrap for more8 [* V) e1 U% l2 z1 ]+ C1 [0 u# o
// complicated schedules.
' [. c( c8 T4 L3 h5 }3 u
4 C; r' D; e+ B2 A' |4 } modelSchedule = new ScheduleImpl (getZone (), 1);
' ^+ l0 ]' T9 R N modelSchedule.at$createAction (0, modelActions);5 `% d( M2 k) U! S# y
! V; _# c% \! q# [& M/ m
return this;
4 i- o( s5 L) O/ Z9 J# m8 W } |