HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" U; O& y! n4 n/ ]* t+ Z
U" _9 \/ ?# i* M public Object buildActions () {3 _ i; p& d/ U2 n- G6 [& d
super.buildActions();
8 f( K9 |! R. |& T" _ , Y) R/ V$ B% p
// Create the list of simulation actions. We put these in4 K7 h9 D/ z6 x; f( f
// an action group, because we want these actions to be: }9 N7 w/ c* }* L# h4 }% ]9 B
// executed in a specific order, but these steps should
" z0 D/ c+ V! v/ w, t // take no (simulated) time. The M(foo) means "The message
6 {+ I& z# I6 n* Z% N, ]6 V. w0 u( _ // called <foo>". You can send a message To a particular- M6 D- S. s1 N# [! D3 j& W1 w h1 P
// object, or ForEach object in a collection.9 y# P& i2 d- ]- [& k' M
& r1 u( C. k% h( P: D! _: G Y // Note we update the heatspace in two phases: first run
9 p$ b) ]& G5 b6 Q4 H- r // diffusion, then run "updateWorld" to actually enact the) a, e0 b" ^ Y0 _" a7 s3 t
// changes the heatbugs have made. The ordering here is
, O% ~2 A: D6 K4 I3 d1 R) w // significant! s) d7 c; J# b& B i6 U# h( `. h# s
$ W h3 x% g9 i' g5 W! W; O. x" }5 k% Y
// Note also, that with the additional
1 j, C. M- o- M$ t // `randomizeHeatbugUpdateOrder' Boolean flag we can
! W" {# Y" q$ W- K- O# J // randomize the order in which the bugs actually run
- E( R" o2 a9 @- ~7 s: L9 r2 r! O // their step rule. This has the effect of removing any: @6 \; B( |: l) U _$ x5 t' g
// systematic bias in the iteration throught the heatbug' p6 A, T- g, ~* B5 ]
// list from timestep to timestep: w# U8 ~: E8 u/ y; q
5 d/ @( u' k- b; O
// By default, all `createActionForEach' modelActions have$ \/ y0 W! m9 F- G2 g8 H. T2 q
// a default order of `Sequential', which means that the* m$ Q$ a: A; _6 B
// order of iteration through the `heatbugList' will be
2 r- o' Z9 X$ p/ N/ o // identical (assuming the list order is not changed K" Y: B4 T5 {; c4 @$ \
// indirectly by some other process).
% n; M7 }) ^9 m5 [& h Q1 j $ ~6 q$ T- q. y# d8 ?
modelActions = new ActionGroupImpl (getZone ());
' ]' J; o9 M: e, J0 _
+ I8 Z3 [$ m/ q4 E try {
4 B5 h- j& R3 a: K7 @2 \0 v' U modelActions.createActionTo$message+ o9 r( ?) V: ~, k, S
(heat, new Selector (heat.getClass (), "stepRule", false));: T, L" O% n7 u. m, [, j
} catch (Exception e) {
' U/ ]! B+ v4 B3 [! Y System.err.println ("Exception stepRule: " + e.getMessage ());' Q) X' g' v8 B# {9 {+ l
}* B7 N+ @4 t; A& P I: i
8 Q- f8 }9 _. {$ Y0 s
try {5 r- ]- F) |& {1 K# E* T0 ]
Heatbug proto = (Heatbug) heatbugList.get (0);
. B4 |$ P% i& D Selector sel =
" R: t9 l3 P c0 ~3 R new Selector (proto.getClass (), "heatbugStep", false);
- _' t0 |5 |& G {( D# C actionForEach =5 k2 M4 z% L0 I% D) N# x9 f9 \
modelActions.createFActionForEachHomogeneous$call' f# ]- I, v6 a7 Z
(heatbugList,
. S0 Q+ L' X, a8 p G2 _ new FCallImpl (this, proto, sel,) s# g4 g9 m9 r7 s i: z# Z1 p
new FArgumentsImpl (this, sel)));; ]+ O! Q$ g0 h6 J* F1 k5 K
} catch (Exception e) {
" A% T; x Z9 x# d e.printStackTrace (System.err);
% P# e3 R6 h! e; b7 P6 Q0 m }
) e' ?# I, V. x 4 S% L X* l9 e' Y
syncUpdateOrder (); N, f: A C% ^9 W
+ a: z+ M' o9 x' i) w. ^+ M6 b
try {9 m; b) r# Z$ F$ Y9 J
modelActions.createActionTo$message : c* o5 W' g$ ?0 @
(heat, new Selector (heat.getClass (), "updateLattice", false));. l1 r3 `; |" g8 a! B
} catch (Exception e) {
: L) U+ e, p* U) B: L0 D System.err.println("Exception updateLattice: " + e.getMessage ());
0 C C2 A/ u, b4 u }* {# ~4 a! P0 j/ b3 ?
; N& D! k, M* V2 K% Y
// Then we create a schedule that executes the
! |& b/ ~1 {4 [ // modelActions. modelActions is an ActionGroup, by itself it
3 u. v* d) j( y1 V6 d |0 j- F // has no notion of time. In order to have it executed in
5 ?4 R/ X- B, E4 u" y7 f% O // time, we create a Schedule that says to use the
/ C$ o. O; G; E; I3 g // modelActions ActionGroup at particular times. This; G" U) E- _4 {4 C
// schedule has a repeat interval of 1, it will loop every7 m) W+ i" K1 a& q6 V
// time step. The action is executed at time 0 relative to
4 q0 _* V& N/ f% E4 Z2 _ // the beginning of the loop.0 y* l2 f- B3 @7 x( B6 T3 u' g4 J: ?
% j1 C0 C" I' R3 \ P+ a // This is a simple schedule, with only one action that is& D6 l* |3 g- t5 F/ E! N3 v+ k
// just repeated every time. See jmousetrap for more
& i- Q6 G5 |: U' ~+ O9 V2 | // complicated schedules.
9 ?8 q. i) V0 [2 m" D+ V' B 7 f1 D; U# j% [
modelSchedule = new ScheduleImpl (getZone (), 1);" E" u$ i) F( f
modelSchedule.at$createAction (0, modelActions);( p2 l( z+ O* @" t
( p; X. o8 } z% F" S9 u return this;- |1 B0 U+ N7 u h
} |