HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 {( X; g! d: J
% G+ {' X, b% _; x public Object buildActions () {: N( f( v# U" ?' j* V+ J' J
super.buildActions();
% @5 c# G$ h; [- e/ o& a5 d8 z . j! A9 O9 ^( u. J N. d
// Create the list of simulation actions. We put these in/ z- g" T0 N' o8 L C: g
// an action group, because we want these actions to be
* _( g& e/ Z/ f1 O3 s b6 |5 h // executed in a specific order, but these steps should- i* ?4 r1 A( Q' ^
// take no (simulated) time. The M(foo) means "The message, J. d0 J8 ]9 ?$ a1 ~* e
// called <foo>". You can send a message To a particular8 j* ^% H U1 b( j# A$ n/ W
// object, or ForEach object in a collection.- ]+ ~" _" D' |. y) P- n, X
% I# f, M& ]/ f5 y // Note we update the heatspace in two phases: first run! H- g/ p1 N" @& i# S; U" D
// diffusion, then run "updateWorld" to actually enact the
1 d. f t- i1 L& d // changes the heatbugs have made. The ordering here is
?4 G3 U) R) |6 m1 | // significant!
0 D3 f9 k" p( ~9 u9 j- G2 ?% i2 L; {% Y & N" W! Y4 A4 I' ?
// Note also, that with the additional
! e+ O9 ~ b9 u! z P# M' t# w& ~6 { // `randomizeHeatbugUpdateOrder' Boolean flag we can
' e. c j. T, _6 ^ // randomize the order in which the bugs actually run; `* C! U% K# x- o' f( l0 E) L
// their step rule. This has the effect of removing any) i9 Y# s8 y5 N" Y7 {# \
// systematic bias in the iteration throught the heatbug
' a7 ]3 o: {0 z4 A# k9 x // list from timestep to timestep2 r% }4 a" z/ ^- s. p. d
! h! v2 }' ]1 W- X$ P# \+ D: M( K // By default, all `createActionForEach' modelActions have8 k, I9 z1 U4 G* v" G: y
// a default order of `Sequential', which means that the" _, O$ \- n! s: I! K
// order of iteration through the `heatbugList' will be: e1 M, I* V# F) V' t
// identical (assuming the list order is not changed" b6 V' M+ ]1 Q: `8 U4 y
// indirectly by some other process).5 s, T2 \, \; E* Y1 r
3 B* G. @1 U3 ^2 Q8 s modelActions = new ActionGroupImpl (getZone ());
: @) _1 `% K9 W7 d* l* ~
2 H8 F, y u. Z: O try {
7 j( V+ a# C) H4 p/ j% m3 C modelActions.createActionTo$message$ m9 l* Q2 b" L) j; W7 K& {
(heat, new Selector (heat.getClass (), "stepRule", false));
/ Z) L" E' v# f } catch (Exception e) {! g% W) J# i4 W
System.err.println ("Exception stepRule: " + e.getMessage ());
" X/ Z* g4 R J& W }4 W. |/ T) b& G
& J! y7 k9 t( N5 a4 x try {% R5 U4 i# P6 C) F7 Y/ z
Heatbug proto = (Heatbug) heatbugList.get (0);/ f9 o+ E8 R4 C1 a
Selector sel = 0 C' U( U0 ]+ G* s E, i) d$ V5 Q
new Selector (proto.getClass (), "heatbugStep", false);! _2 T. }2 u6 k/ J: j8 c( Y
actionForEach =' l4 z6 ]( c) t2 P' V6 o& C
modelActions.createFActionForEachHomogeneous$call2 c/ p2 o3 U3 R4 o6 P q
(heatbugList,( p, ?3 ]: E+ E/ V# n
new FCallImpl (this, proto, sel,
! Y8 I# Y/ T9 Y) w! Z( l0 d0 o new FArgumentsImpl (this, sel)));* A& S+ ]( Q7 L& G& q
} catch (Exception e) {
; D' ~8 G" J& [+ R, [) v e.printStackTrace (System.err);( F: C5 u1 P/ P3 g' J
}
$ ?0 J1 W3 {! j2 t: k3 ~ 4 S' Y2 a# X# R+ i/ U0 N/ m4 u4 [
syncUpdateOrder ();
! p2 f8 n$ G8 M7 S, ^* Z$ W. ~) p- V
try {
* h) @0 v q$ A modelActions.createActionTo$message
1 |, ^& ^) ^" ?" S (heat, new Selector (heat.getClass (), "updateLattice", false));9 Z. e/ L- S! w3 F( m
} catch (Exception e) {
+ y) b' C* Z( q$ V) o( Q System.err.println("Exception updateLattice: " + e.getMessage ());
?# N& t0 t1 X# ~+ [ }
6 h# \! ^; o( i# u8 ]' p/ ~0 _ 2 O& h" f9 T) v/ r' v
// Then we create a schedule that executes the
% D& T; p/ Y+ S( U // modelActions. modelActions is an ActionGroup, by itself it1 `3 h# r# y4 q2 _, E# e0 _
// has no notion of time. In order to have it executed in$ _1 Y% E2 ?& b/ r; l$ x
// time, we create a Schedule that says to use the
# `+ i6 R! _$ B% ]& x5 O p9 f // modelActions ActionGroup at particular times. This9 O9 o: ]2 k n
// schedule has a repeat interval of 1, it will loop every
3 B) b9 r! Q. f5 j+ X- N# P // time step. The action is executed at time 0 relative to) G3 ~% C2 r- F: A! B% i- R
// the beginning of the loop.
+ _3 D1 z0 l/ s' ]* Z2 [
& I; O! v! k0 l- P% q9 R // This is a simple schedule, with only one action that is
, i: e1 T/ {& A! J8 ?% W // just repeated every time. See jmousetrap for more9 {* w: q7 f% {
// complicated schedules.) }. ~3 R7 m$ E l4 f9 S: r
4 n" i8 d/ W2 @) F; H9 \8 s5 W+ g
modelSchedule = new ScheduleImpl (getZone (), 1);# d1 Z2 y+ u) ]4 o: t2 _: a
modelSchedule.at$createAction (0, modelActions);+ A( X" X% C( C" L# R8 |# M
# S" E' L0 m; j- a
return this;% v' x& j. E# }3 n; h9 H' P
} |