HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, w0 p6 _9 ^3 ^% d4 {1 m8 q
5 i& t9 {# b. j% q+ T* z8 h8 K public Object buildActions () {' d, o, G- p" R, V( D
super.buildActions();
# v! M6 G2 x1 ] 5 `" K2 _! p% z+ D9 ~9 h" v* k! i2 ?
// Create the list of simulation actions. We put these in
$ q) F8 R0 }: ~* {' b5 G5 w5 i // an action group, because we want these actions to be
+ N# x0 _& Z# H$ H% R // executed in a specific order, but these steps should
* D1 r0 s* A0 e" J // take no (simulated) time. The M(foo) means "The message
' T1 [- K* M1 M( X N5 ^: t // called <foo>". You can send a message To a particular
7 ~2 v7 v7 ], v! v% V, [ // object, or ForEach object in a collection.
; l$ O: v" V5 M( |
6 a9 X! O* C" r+ B, p- x) B // Note we update the heatspace in two phases: first run% Q3 ~9 Q! H3 W, n
// diffusion, then run "updateWorld" to actually enact the
# r1 u5 h. m4 E5 t; Y! W- h& T5 Q // changes the heatbugs have made. The ordering here is
* f+ a% I/ g" R5 h1 u7 ] d // significant!+ \% Z! i# X; y# I+ H! h7 `
9 m" y% R8 j! Y% Z, r; G9 y9 w
// Note also, that with the additional
: c' ] v0 _ M5 V // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 m, a2 i; d& t" ~ // randomize the order in which the bugs actually run
% L& J* ]" J& {! @ // their step rule. This has the effect of removing any
" `4 k- O; p- b# v& N // systematic bias in the iteration throught the heatbug# k6 G. W: Y+ K* F6 c+ C7 |+ T- n
// list from timestep to timestep
% v) ^5 ]- z8 O) p9 ~# _4 L 9 H$ E) P$ S7 n
// By default, all `createActionForEach' modelActions have0 I* j/ Y& b F, X0 W9 m) j
// a default order of `Sequential', which means that the
, e: @, y( N' F/ F // order of iteration through the `heatbugList' will be, U7 \9 A4 d S1 a" I
// identical (assuming the list order is not changed
5 \' ^+ c6 Q3 a! U& j3 ` // indirectly by some other process).6 C9 Z' X2 z& H% V' d) @) J& p
2 h, x* ?' _# `; P+ i
modelActions = new ActionGroupImpl (getZone ());
/ K1 F: ?0 g- I7 |; k, S# i3 y# U4 }3 L
try {
6 A+ g; D+ m5 S4 c8 ^+ P7 e4 ] modelActions.createActionTo$message
, e- d2 C$ ]* b" q/ C ^ (heat, new Selector (heat.getClass (), "stepRule", false));
* U4 |( b0 @' l5 C6 l: s/ B } catch (Exception e) {8 ?- ?( K! ]3 [* a) K
System.err.println ("Exception stepRule: " + e.getMessage ());5 L' q. \0 w; J2 \+ t7 ?! N/ D' n
}' v6 V4 h; L, ?6 a7 B+ M
) K- ^0 N4 h- k$ _4 h1 H4 m try {5 x1 ~' V9 x p$ X7 p
Heatbug proto = (Heatbug) heatbugList.get (0);
. v c0 T9 A$ S0 P a3 ^ Selector sel =
& n+ [9 O. ]3 N& l new Selector (proto.getClass (), "heatbugStep", false);
( _5 o. ]3 A% K$ o: j, H5 l7 W actionForEach =
d, G9 e. Z, r5 ^ ~ modelActions.createFActionForEachHomogeneous$call
1 b' L) U, h) Z4 s) z (heatbugList,
( \5 @; s% M, O/ n6 F- e5 q new FCallImpl (this, proto, sel, {+ U8 A' A6 Z0 s& f
new FArgumentsImpl (this, sel)));
! ]; m9 e; @- j) g8 m } catch (Exception e) { _% A) S1 q# p" d& Y7 ?) s0 R, x
e.printStackTrace (System.err);# ?( B. y* C1 o
}
5 k+ t' a2 P5 P' p6 O, j 6 w: e0 T# y6 [9 w
syncUpdateOrder ();
7 e- F- y+ W+ b. z; ?! n' m! a# A, M* n2 i9 J6 d. H* }- a" s) F9 q
try {
$ ?( Z6 x' P8 C7 @. g modelActions.createActionTo$message
5 h, b k% p6 W) k6 S! N; n (heat, new Selector (heat.getClass (), "updateLattice", false));
; ^. ]+ U2 p% x2 L5 M } catch (Exception e) {
^. u: |& p" _$ \ Y System.err.println("Exception updateLattice: " + e.getMessage ());% @8 D& o' ^" h) `/ k
}6 o( Z" i1 E" s# \' }8 y
. ~, G9 R, Q6 K- E+ G6 [3 N
// Then we create a schedule that executes the
) T: C# |, {- t& E2 D7 m8 q1 D // modelActions. modelActions is an ActionGroup, by itself it- ~0 c/ u* p' t
// has no notion of time. In order to have it executed in" M' E( a4 @: M
// time, we create a Schedule that says to use the
+ ~( p# X# N8 W* g // modelActions ActionGroup at particular times. This- L% w, W: o s' } \/ [ g9 _
// schedule has a repeat interval of 1, it will loop every
# h3 ~, X* h$ Y5 b2 y // time step. The action is executed at time 0 relative to
' C; [4 b' f: ]' b, z // the beginning of the loop.) @; {+ L7 b. J. n7 U* H0 F
7 W+ s [ t$ J. Q // This is a simple schedule, with only one action that is
. R3 i3 q! {/ j, ~- _ // just repeated every time. See jmousetrap for more9 H! e ^6 e4 D# A$ @* X: g& H
// complicated schedules.
u3 a9 r/ j' p4 I8 X% S& p 3 S8 S. {& ]& V
modelSchedule = new ScheduleImpl (getZone (), 1);
, @4 t8 h+ f, p! _% r( g6 t4 M2 \ modelSchedule.at$createAction (0, modelActions);+ K [% ~0 ?. W; K+ V
" r. P& @" M9 c3 j7 y return this;
4 h- P: ^( c1 f- j6 F6 g! B7 ~ } |