HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ {$ }" j5 Z$ u/ V" l' r7 s* I0 W, o: t' a7 H
public Object buildActions () {
# G* n5 X! _# ^1 _ super.buildActions();
, F) X/ C: ~' M2 x
+ L8 e% w$ S+ I* V4 E$ E/ M- V1 [ // Create the list of simulation actions. We put these in$ O) ^6 q) v; R7 ^7 ~
// an action group, because we want these actions to be
0 F7 b* T# E+ g // executed in a specific order, but these steps should
* Y* d: R: ?+ X. w+ T; C: S // take no (simulated) time. The M(foo) means "The message0 ^" v/ t' P* b% S+ B6 l
// called <foo>". You can send a message To a particular
: a* J1 f6 D& W2 C6 E2 u // object, or ForEach object in a collection.) e3 F7 ^5 p6 q6 @2 q$ [' @
2 s$ F0 E0 z6 k
// Note we update the heatspace in two phases: first run8 [0 o4 T. |, @* _7 m
// diffusion, then run "updateWorld" to actually enact the, I; k( E4 P/ H4 k1 k
// changes the heatbugs have made. The ordering here is8 y; E/ x( v8 n6 k ^( F( ~
// significant!5 @) m5 u) k4 @5 @3 H% `. O
* E6 p4 B# @! t: ?+ t* \' t3 i" Q // Note also, that with the additional
7 w8 e, h% s. Q2 I. V/ p // `randomizeHeatbugUpdateOrder' Boolean flag we can4 S; W* P( N. C; v
// randomize the order in which the bugs actually run
. J: Q% o2 h5 X6 G ?7 c# b- r // their step rule. This has the effect of removing any
* M) m2 @$ n6 ?. e7 e- b8 _/ r: I5 O // systematic bias in the iteration throught the heatbug+ h. h! h' g- t% m5 A0 U
// list from timestep to timestep! k( {4 E' `7 I, S
* ]0 L& o! i7 O6 g3 a8 w // By default, all `createActionForEach' modelActions have, D3 h3 T( [2 T
// a default order of `Sequential', which means that the5 X$ i$ Z" p( q
// order of iteration through the `heatbugList' will be6 q M/ F* k$ S; s( u9 o
// identical (assuming the list order is not changed
' q4 H& q; m: _! n& F7 M // indirectly by some other process).
( r5 H7 O* b6 }7 U- J% k9 P , u! j3 ]- Z# _$ L1 e; F1 w: V, j; [9 Q6 b
modelActions = new ActionGroupImpl (getZone ());" b+ S( ~+ |; c: s. U* T! D! I
, h" r+ h) j6 E+ ~
try {# f4 J. Z* D- ]9 r
modelActions.createActionTo$message9 L' [% y& |- ~/ a/ o8 Y, c7 X; F
(heat, new Selector (heat.getClass (), "stepRule", false));
6 u' j$ I) k8 K4 \$ t } catch (Exception e) {
$ d* q/ G% D0 B3 @ System.err.println ("Exception stepRule: " + e.getMessage ());
) N$ e6 ~/ F0 Y" X6 i: H& W }
! L3 P% J# s7 k, ^7 A1 T& S: s) y
* g' W0 J( H) V+ ^/ A try {
# d7 B( b( l: N" B Heatbug proto = (Heatbug) heatbugList.get (0);
8 ?. D; {( i: B Selector sel =
/ l0 W1 f6 d1 r* B% D new Selector (proto.getClass (), "heatbugStep", false);# q6 E* U* j3 E H. W% N! C
actionForEach =
: S* e+ P) `" ?4 I, ~, ] modelActions.createFActionForEachHomogeneous$call
# B' F* x8 h; _2 ? (heatbugList,
$ k8 r7 ^. z, e% ? new FCallImpl (this, proto, sel,
1 z# f; q4 q( I; f* ` U new FArgumentsImpl (this, sel)));
2 r& c& v8 M! S4 f } catch (Exception e) {/ Y- \) j9 i+ }. Z! O) i" V
e.printStackTrace (System.err);
' @3 r6 K) H5 T# a8 [2 D }+ H, ]2 t" P. X `# H& m
/ y6 C. z d+ v- n# e syncUpdateOrder ();4 Q* y3 x; J7 c, O4 S
9 N6 X/ m9 r2 Z8 j1 x! z
try {) K: L5 ~' t6 z+ y) D* E
modelActions.createActionTo$message
- a, y- R: r% I5 r4 k9 R$ ?: l (heat, new Selector (heat.getClass (), "updateLattice", false));
( p }; S1 ^7 { } catch (Exception e) {
' m, ]; i. H3 p. c) Y System.err.println("Exception updateLattice: " + e.getMessage ());
+ \3 U6 M2 u3 T) z* j/ v, e4 } }6 r* I {0 Z m, Y+ T) }6 `
# I! ~- T1 ^8 ]' J5 T // Then we create a schedule that executes the: c5 a4 c( x( ?1 h. P( f; \
// modelActions. modelActions is an ActionGroup, by itself it5 c% R( M, p: i! }" l
// has no notion of time. In order to have it executed in& v* [' E, W7 V) m- m0 W: r
// time, we create a Schedule that says to use the
o$ B1 k, ?. T6 s) v; d // modelActions ActionGroup at particular times. This6 i8 R- Z, |4 g% R
// schedule has a repeat interval of 1, it will loop every2 }, V/ S; C7 p
// time step. The action is executed at time 0 relative to' g: S7 n% B: [* P' \
// the beginning of the loop.5 G" Q3 ?8 H2 N2 C: V$ [) r- A* N
$ B- M" }3 j- y8 C) v% K' D _
// This is a simple schedule, with only one action that is1 n k: K$ [3 g( b5 A+ n
// just repeated every time. See jmousetrap for more2 o4 f) E# N, l3 c+ z9 p
// complicated schedules.: q; c* E; g" f2 c8 r, z$ N- d5 e
: {% q5 M# ?# D K2 W5 u; i- e modelSchedule = new ScheduleImpl (getZone (), 1);
6 e: X7 @8 L# R# q2 K! r modelSchedule.at$createAction (0, modelActions);
0 ]4 X- T& m) G6 r2 r / R& ~0 o; Q: g, {) l0 g1 C
return this;
' ]3 a: t; I/ A2 H/ z } |