HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 a' l+ F4 q0 _, W |
+ v0 B/ @* c$ P6 a public Object buildActions () {% a6 E" o, L' a9 B7 E9 t+ G3 l
super.buildActions(); v& z" {* Z4 z, F
# R q3 O( g k3 @- ?$ W# O
// Create the list of simulation actions. We put these in/ U9 h: ]# y7 O& X. z
// an action group, because we want these actions to be- N0 x8 W- \$ |/ x/ \! |
// executed in a specific order, but these steps should
0 ^. i) b% w. F& L/ Y. g0 k! ] N // take no (simulated) time. The M(foo) means "The message k$ ?% `4 E* {: [
// called <foo>". You can send a message To a particular
5 ~. L7 z- n( q% V" I5 U! A // object, or ForEach object in a collection.. h. P& `$ V! a' m: |9 h, k: a$ e) l
; Y1 |& c8 [0 j8 w0 d1 H+ u' g
// Note we update the heatspace in two phases: first run
6 i# z+ W# A' h# |( D6 c9 j // diffusion, then run "updateWorld" to actually enact the
6 ~, J/ T: V7 ~: i5 U // changes the heatbugs have made. The ordering here is
( N! `: b& a: L9 }% U. S- G // significant!/ J( A. Q8 v1 \6 q, N: W" ?
0 x$ B) M) t# j+ c, d
// Note also, that with the additional
$ \: t: s9 h& Q Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
- R5 K, c) b) G2 s Z6 ]$ x // randomize the order in which the bugs actually run
! J" ]" s* _& \; t% x+ m% l+ @1 S7 ~ // their step rule. This has the effect of removing any
: S( R! n- ^4 G* d+ R, F // systematic bias in the iteration throught the heatbug
- b7 @7 O$ V4 \& Q- v. L9 t! c) k& ] // list from timestep to timestep% Z$ J, T. L/ w5 V
' A- g N6 q( v% i& C% V9 g
// By default, all `createActionForEach' modelActions have
; Z3 X/ Q& ~% O. ]& ~' x$ [ // a default order of `Sequential', which means that the4 Y+ P' W5 g& v: e: m
// order of iteration through the `heatbugList' will be
7 z$ G6 F; Q! _" {; v // identical (assuming the list order is not changed
' S+ a' v, P. H // indirectly by some other process).
) _/ F v( Y7 w& s; p6 x9 ] ( T/ m, `4 S' T9 y
modelActions = new ActionGroupImpl (getZone ());
* F# \" b5 S, I2 ?' c2 R1 o" q0 u2 [' B# _4 C
try {
* A! _! S0 q I$ l; E1 ^; \ modelActions.createActionTo$message
+ T* }; z E, j( {6 w3 t3 c! Y (heat, new Selector (heat.getClass (), "stepRule", false));5 T: x4 l" k& W8 P; x E, y5 m
} catch (Exception e) {
( J/ f- P! F8 L( W6 o ^/ F System.err.println ("Exception stepRule: " + e.getMessage ());$ k/ j& ~1 W' D6 i
}
5 C% x/ A# b% u. a6 N; y! f9 I
& H. i/ g7 z1 _" \ try {! Y U+ W) j @2 h6 D* l/ m
Heatbug proto = (Heatbug) heatbugList.get (0);* b" p# Z4 k8 \7 p/ e
Selector sel =
' h5 O1 }. _3 s7 Q5 c: O new Selector (proto.getClass (), "heatbugStep", false);
% [* ~/ m) M& r' F/ d actionForEach =
" k. G- ]2 t# {" _$ J modelActions.createFActionForEachHomogeneous$call
( Z M% S* h) t5 Y+ D9 J" s6 ` (heatbugList,7 f" o+ S+ C( X: o' ~
new FCallImpl (this, proto, sel,; w$ w' f3 O5 b7 t2 b/ R6 b
new FArgumentsImpl (this, sel)));. X3 ?( n& h9 ^7 ~# Z+ c$ u+ V
} catch (Exception e) {
1 d& H9 m& c n Q' V2 a1 A7 H9 z9 w e.printStackTrace (System.err);( l- ?( U J* I- _3 c% b
}) R8 ]5 ^0 [0 q
3 T. f* z7 [" o& j" _* a/ o0 W( S syncUpdateOrder ();
& m# R" b T& `* F* `' t
' ]9 K2 l$ e( y% c1 Q: }& L try {+ l+ R. B/ }" s5 f+ Z/ k
modelActions.createActionTo$message 9 |/ i7 u- O3 z- w
(heat, new Selector (heat.getClass (), "updateLattice", false));
; j# o& C/ {- ]/ j% f( I8 I6 I( v } catch (Exception e) {" u$ c9 O, ^# k8 D9 Y1 x
System.err.println("Exception updateLattice: " + e.getMessage ());# A& s7 B; R' z% I9 t
}
% J& |9 T& E9 D
( b. l2 F( b: p. j1 u // Then we create a schedule that executes the% g# u e2 \5 r) Y" ^: X& B& l5 ?
// modelActions. modelActions is an ActionGroup, by itself it! U* Y" q6 i' |' C, _5 X; K; G
// has no notion of time. In order to have it executed in: l, C" n, J! `* Y( \
// time, we create a Schedule that says to use the
) K$ ^1 W; I/ M$ T' E // modelActions ActionGroup at particular times. This3 K- h: T6 h$ I
// schedule has a repeat interval of 1, it will loop every# K9 }/ o6 d# u) P3 z* D0 q6 H y
// time step. The action is executed at time 0 relative to# l& W; b! W. z+ P
// the beginning of the loop.0 ]$ O2 d7 G% i# o
% a9 A2 @% _6 D* Z2 `7 y // This is a simple schedule, with only one action that is Z( v: {- e) S, l. t- s
// just repeated every time. See jmousetrap for more8 g. A. b% X; U
// complicated schedules.# E- ?9 `/ m, r
/ e5 _ y, K! V7 T/ k9 Z
modelSchedule = new ScheduleImpl (getZone (), 1);
7 \1 _0 F. p! B5 B% D' N modelSchedule.at$createAction (0, modelActions);: y6 p+ C+ X4 w4 g8 T3 \5 y
( h+ K% e: T3 F# @1 U' @! j% \
return this;
" Y9 S; d4 l4 [# R. v, g- t } |