HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 U" M+ I' E1 ~" e# t* e" n+ P
! B6 B, @. S( P& F public Object buildActions () {. C$ |: Z! y" M6 c6 s
super.buildActions();
* b2 g6 t2 L* `4 o4 p # G) W2 g N0 X- `" G; L
// Create the list of simulation actions. We put these in
' v6 k; O" T6 Z) d& i // an action group, because we want these actions to be
/ k' c4 e- U& ]* N // executed in a specific order, but these steps should' i2 [% P ^' r1 m$ ~0 g
// take no (simulated) time. The M(foo) means "The message
" s' v2 ]& ]) ]+ d. S# p- w // called <foo>". You can send a message To a particular1 M- V! _# D* t! H- J
// object, or ForEach object in a collection.
( P# V$ i+ r& _$ U! e$ P" P+ P 8 x: {. `, M$ b. \# a' k( w+ h
// Note we update the heatspace in two phases: first run2 a; k9 L7 E& w
// diffusion, then run "updateWorld" to actually enact the
7 I7 w7 [% R7 l8 |' ?, Y // changes the heatbugs have made. The ordering here is
2 ~" q5 c4 a: {9 W& f" r0 [ // significant!( _9 H- H# o3 Q& U. Z, J6 H% i3 g( V
7 a- m, u% w6 [ // Note also, that with the additional6 Q: N. y# B; ]$ {+ _- Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ Q' N! `/ u9 q+ b. V& ]' a) u // randomize the order in which the bugs actually run
8 }% m, X7 Z( d8 ?/ y; S5 _ // their step rule. This has the effect of removing any
1 a. @7 `) P# j // systematic bias in the iteration throught the heatbug) Z. i- e( ]% e( D
// list from timestep to timestep' k6 Y' I( S* Z/ M+ n
0 P: b5 i) E# y
// By default, all `createActionForEach' modelActions have$ @0 q, Q- U0 D/ @ ]
// a default order of `Sequential', which means that the: C. \6 B8 e5 t2 b6 L" E2 ^
// order of iteration through the `heatbugList' will be, |) L1 ^) T/ q1 z
// identical (assuming the list order is not changed
* \. }! k( ~6 T // indirectly by some other process). n: B# T0 F. L7 O" K2 @
; l% e( k5 Y0 [0 A, M
modelActions = new ActionGroupImpl (getZone ());/ I& m8 Q" f* o, ?8 Z9 q+ Q" I, L
' w% m& V! u9 ^" ^& m6 Q
try {
+ K3 ?0 s, ], | modelActions.createActionTo$message
* x3 {* o+ _, E5 R" t0 q (heat, new Selector (heat.getClass (), "stepRule", false));
7 i5 ]6 `6 z/ u- q% H& F } catch (Exception e) {; V9 U& p, c, q" Y3 ^! {3 z1 t
System.err.println ("Exception stepRule: " + e.getMessage ());
' K) C: l( F& ^ }
* R# x3 k9 A3 v* {; k8 Y2 F0 I- J9 b% c
try {! ?# E$ x$ r& Q* k' x
Heatbug proto = (Heatbug) heatbugList.get (0);: v0 r) ]" K& Y
Selector sel = * S/ T- [+ o2 M' U
new Selector (proto.getClass (), "heatbugStep", false);2 J( [9 U" S" Z3 w
actionForEach =* _9 s; W) L& Q" R0 p3 U+ u+ A
modelActions.createFActionForEachHomogeneous$call
6 \0 I. E4 ]# ? (heatbugList,. L; _! R/ a& t+ H; K+ R
new FCallImpl (this, proto, sel,3 @" E% H: p- v% t
new FArgumentsImpl (this, sel)));2 y) d) X& o5 A. z/ R% z+ _7 _
} catch (Exception e) {
% q6 Z6 r2 @1 n0 j e.printStackTrace (System.err);
5 w6 P/ z+ F4 ?; t' S5 S: H }5 G# s6 }/ g+ L: e% h6 D
6 k S* y+ Z, }9 S+ e! j syncUpdateOrder ();& `9 @5 k9 Z4 F0 \! m( V+ ~
- q% A4 n4 ]0 `8 f
try {
# u' w7 W) M+ `. Q8 { H8 T, D modelActions.createActionTo$message
) k- @0 M# y# P9 O9 X (heat, new Selector (heat.getClass (), "updateLattice", false));
& \( r' s1 M/ G" Z& @ } catch (Exception e) {
: ^6 o* X3 a S9 a! k System.err.println("Exception updateLattice: " + e.getMessage ());; J/ \' s+ O) |7 i1 s
}
' `4 X* K& K8 A! T$ t% v
$ m$ r$ y- g& s& O6 O/ v1 J) y // Then we create a schedule that executes the6 E* q" `' l6 K$ \0 y& N9 [) x
// modelActions. modelActions is an ActionGroup, by itself it
1 J+ S) r' H* \$ U& ^- g6 t C // has no notion of time. In order to have it executed in L1 ^! O$ g3 f
// time, we create a Schedule that says to use the9 ?1 [! C& B. J7 [0 E! N$ R2 `* a' S/ H
// modelActions ActionGroup at particular times. This
, H, Q: ]6 b9 |, G# J. Z // schedule has a repeat interval of 1, it will loop every
" m4 Z" H. g& _1 c( Q // time step. The action is executed at time 0 relative to/ W* ]4 l1 u H( ?# u7 z, K7 Z- z, o
// the beginning of the loop.
, z1 N2 x, J: Y0 a4 M& R# J K! n. J) B2 }3 C6 Q5 c; o
// This is a simple schedule, with only one action that is
9 E6 W/ N. v7 W" A1 @ // just repeated every time. See jmousetrap for more
. q: ] n0 s8 U! ?/ s! T // complicated schedules.( P5 z) ~, n1 k1 L0 y
9 V' B; D4 i4 C
modelSchedule = new ScheduleImpl (getZone (), 1);8 A# M( `) G- a$ R- O# T) N
modelSchedule.at$createAction (0, modelActions);
/ I6 l' b) N8 o/ D
. M* T5 b0 h# f3 y return this;
. ~' v9 S9 g$ w! Y$ } } |