HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* L" `/ [+ v2 v/ G8 N8 F! ]
. p! {' d" `( J: l* E( E% A% [* A6 t
public Object buildActions () {
T3 P+ }: J' u" s4 g super.buildActions();2 K4 h7 I( g2 M" i; J6 E
& u+ p9 V" V% t( ^' b& _
// Create the list of simulation actions. We put these in$ q) q5 p" b% b" Q! x; ^7 E
// an action group, because we want these actions to be# B5 Y& G1 g2 V
// executed in a specific order, but these steps should
$ r" Z: c# S* f" a+ D0 j // take no (simulated) time. The M(foo) means "The message" K- X+ B$ W( t' Y
// called <foo>". You can send a message To a particular$ M* B/ ?0 r) d1 e7 {0 D9 ?8 d
// object, or ForEach object in a collection.
* ~! a: A) h9 ^$ _5 T2 k: L
6 u( j8 o+ O+ q0 H // Note we update the heatspace in two phases: first run
. W8 Z, f3 y! G# a7 _ // diffusion, then run "updateWorld" to actually enact the* M( s% g. o5 M8 F, T1 W
// changes the heatbugs have made. The ordering here is
: ^, c% h* R- q' i4 e" s' _ // significant!
1 u2 n/ L2 {/ {$ l k
: P, M- K) y( m5 j // Note also, that with the additional
u$ `! w2 z8 i, |' h( m3 I // `randomizeHeatbugUpdateOrder' Boolean flag we can
( Z$ Y% Z0 Z4 f7 I // randomize the order in which the bugs actually run/ ?% Z5 y- [- i" N/ ?" p
// their step rule. This has the effect of removing any9 U- [3 ^" t! W
// systematic bias in the iteration throught the heatbug; ~: j9 ~8 h: d) ]2 j3 u% a ]
// list from timestep to timestep# V& _+ }' t0 X0 ?4 o
4 O7 ^3 k1 k9 w8 u' y: U* S! \ // By default, all `createActionForEach' modelActions have; Y# j, q& |2 }
// a default order of `Sequential', which means that the" ]* t! w& _* k7 N; j9 ?1 z
// order of iteration through the `heatbugList' will be& g6 {6 T# f& @+ `1 n P
// identical (assuming the list order is not changed, G" l( }: C. {. ]+ T+ x
// indirectly by some other process).
% y/ @4 h5 j, ]; K1 @ 4 _* v ~6 o: m9 T; w
modelActions = new ActionGroupImpl (getZone ()); e7 V. R8 w5 C$ f
4 ~. k1 d/ F7 H, `* a6 a, l1 X
try {. K6 ^$ ~4 w0 H/ q5 V
modelActions.createActionTo$message" @" k2 v) v0 Y# Z
(heat, new Selector (heat.getClass (), "stepRule", false));3 v9 R5 }9 T/ u# H/ i |% I; P
} catch (Exception e) {
. f v( M$ {2 t$ J System.err.println ("Exception stepRule: " + e.getMessage ());: U; A; t/ _' W( Q( r
}, H" _ j, A) ] t- b6 i! z' y
. Z; A4 z* X" D5 o try {
4 ~7 s0 k! a( g* y9 D Heatbug proto = (Heatbug) heatbugList.get (0);
5 i1 B- ~ U, p5 F- a Selector sel =
5 X0 X: @9 v1 B _5 x new Selector (proto.getClass (), "heatbugStep", false);
M( l: J1 g' m6 B6 M actionForEach =
* O. W B( M- [- S3 X modelActions.createFActionForEachHomogeneous$call+ {1 S) L- ~3 }& ^3 W
(heatbugList,+ w; v( [( |) ], s6 n
new FCallImpl (this, proto, sel,0 w( ]8 b0 w# p9 F7 `5 j" P
new FArgumentsImpl (this, sel)));5 N& T* h& S; ]
} catch (Exception e) {! k! Z# ?# T$ T
e.printStackTrace (System.err);
% @! U: _2 w* y$ |8 [ m }
; t: ]5 S. f) ?* @
, a4 T, Y6 Q& m* B+ ]7 O) u syncUpdateOrder ();
5 c9 R( h) J2 }7 m( U) z: X
; \- { g8 ~, Z# i* y# P' \ try {$ O% Y y! ]" ]5 [# j( S
modelActions.createActionTo$message
: m: S- M, A* z% t5 f8 \& [1 S (heat, new Selector (heat.getClass (), "updateLattice", false));( w5 A7 o- m, ]9 |
} catch (Exception e) {+ `; d7 `" S" f1 B% `6 e% r
System.err.println("Exception updateLattice: " + e.getMessage ());
4 ~/ K6 |1 w4 u3 `5 o$ U' O }
& h$ P K5 k" W9 B; f0 V/ r) v 9 P# o3 x! X3 X: p- I( h
// Then we create a schedule that executes the
, j: g+ F0 S, p, o: [# K3 W$ X // modelActions. modelActions is an ActionGroup, by itself it
g0 m: f) x! Q( _. b // has no notion of time. In order to have it executed in5 ]/ r: f# C3 ] L6 d' v$ Z t
// time, we create a Schedule that says to use the
' Q0 i; g: p R; J // modelActions ActionGroup at particular times. This
- y5 a& W' i. E$ E3 y# c // schedule has a repeat interval of 1, it will loop every8 |3 i5 [" }, Z: V/ s1 y1 @1 M' E
// time step. The action is executed at time 0 relative to! Z; {+ r8 Q% d7 s% I
// the beginning of the loop.
$ C7 R# N5 }, D1 f6 ]! g7 \, h' O+ J6 S3 r+ k
// This is a simple schedule, with only one action that is0 H7 n, T- A' z2 c% e+ G
// just repeated every time. See jmousetrap for more
& t( T5 z3 C$ }1 C2 E" V, W* _2 n // complicated schedules.9 P6 L0 r0 R ]; a1 x
! ?) V, y; Q7 A/ }1 P
modelSchedule = new ScheduleImpl (getZone (), 1);* h }$ z' O5 m3 @. q, p: [
modelSchedule.at$createAction (0, modelActions);, m7 F/ x7 o( T" ]8 Z. q
" ^/ x- J. j. |% f/ v4 A return this;
: s0 P" @! D! \) ~ } |