HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 t! D* S* z: ~8 ]& u; K/ K0 h
. |5 t5 K' E, ~5 H
public Object buildActions () {* [4 b" s5 @9 o; a( T' S# Q
super.buildActions();4 y3 t' ]+ z0 u; P7 ]
- y# ?; O. h! r, \3 e/ G, P. K
// Create the list of simulation actions. We put these in; S& U& a# l, d# L% Z0 r
// an action group, because we want these actions to be' t# ?4 e$ J. W! ]3 a8 E. [
// executed in a specific order, but these steps should% q& ^. q6 ^6 S% I6 E
// take no (simulated) time. The M(foo) means "The message+ ~' ]* f/ F# [# E
// called <foo>". You can send a message To a particular+ k, V2 J: N c2 X H" b) h2 e d
// object, or ForEach object in a collection.
I3 ?1 l- r$ E
! ~3 Z# P( R2 O( [ // Note we update the heatspace in two phases: first run* r. |5 o9 K* j5 W7 ~7 E
// diffusion, then run "updateWorld" to actually enact the
$ a3 s8 d4 i% \- ?+ s( p // changes the heatbugs have made. The ordering here is
# {0 e, \9 H% U8 o+ o, M0 \2 Q" A // significant!
2 s1 q4 ]0 f7 J9 o w) r4 ^
$ w) ^5 T* W' H& |1 k- s- R% ` // Note also, that with the additional
1 V/ N! m2 C. g: H // `randomizeHeatbugUpdateOrder' Boolean flag we can: Q; V j& N, c; P8 t9 q6 Q
// randomize the order in which the bugs actually run
3 q! I( Y4 d# N/ Q* i7 ^* _; m( ] // their step rule. This has the effect of removing any- m7 |1 `8 B' }+ d5 y* B
// systematic bias in the iteration throught the heatbug8 k$ S0 |$ X4 z4 @9 {3 T
// list from timestep to timestep
% j9 s3 s" M$ R5 ? / K; O0 O) |: _$ j7 x" f; i5 @
// By default, all `createActionForEach' modelActions have. Z4 N+ H8 C& E; z# c. Z
// a default order of `Sequential', which means that the
# E A( ^2 Q u+ l# n& { // order of iteration through the `heatbugList' will be5 H+ o* e7 d9 a* c
// identical (assuming the list order is not changed
+ ^4 l+ l. p# @/ D" Q J) N. l% F // indirectly by some other process).3 ?& ^- o$ b4 Y) t
! |5 ^* b- a7 u% h; l @% t modelActions = new ActionGroupImpl (getZone ());
# ^. \7 @& Z% P1 V+ \8 F& |4 H! O& q* c4 L
try {( \) q" ?+ ^9 X
modelActions.createActionTo$message: B; C: x. @, D; L: M N6 f5 z! ^
(heat, new Selector (heat.getClass (), "stepRule", false));4 V" w' _2 J' Y$ i$ x
} catch (Exception e) {$ l R7 q9 ^* d! @
System.err.println ("Exception stepRule: " + e.getMessage ());% i2 | s0 k8 H( }
}
: b6 G" Y% [9 J$ C& A/ G" o/ j* L' b
try {
% C. _# F2 t, ~+ q; t* m Heatbug proto = (Heatbug) heatbugList.get (0);
$ I5 U2 r8 U- n9 Z/ Q+ t4 q Selector sel = 2 I; Q8 u- f8 r+ e
new Selector (proto.getClass (), "heatbugStep", false);, i8 P% V; e3 ~7 h, U/ \! I' D7 P
actionForEach =3 T+ ]+ @! B) z: |4 G
modelActions.createFActionForEachHomogeneous$call- P3 i U0 }9 t1 L1 w
(heatbugList,3 k: b5 X- @5 m
new FCallImpl (this, proto, sel,
( [( l2 [$ u2 d# g$ N- e( i new FArgumentsImpl (this, sel)));
* {' r9 |6 S; Q5 r0 P8 A/ j } catch (Exception e) {6 M0 P. u; S5 V
e.printStackTrace (System.err);
" h; I3 J4 S5 v4 r S: `8 v& U }
8 e) p$ W1 k: {3 p+ V
2 I. Z( Y5 L9 H4 |: H/ @ syncUpdateOrder ();3 h* K" a8 c; E# l
9 x. ?8 v) H4 {/ v8 S: u
try {# D: o0 ?4 h4 ^( K9 Z* O' w
modelActions.createActionTo$message $ Q! g# t5 n" Z& v) A1 A6 X
(heat, new Selector (heat.getClass (), "updateLattice", false));- f% U0 ^0 P# \/ \* S- _
} catch (Exception e) {% n; h! A' P! V* ^# r7 G7 J4 Y- L
System.err.println("Exception updateLattice: " + e.getMessage ());
5 M: F6 X! m4 z( |) B" _ }
" w# ]- Z S7 D, P9 M* _. y: C
! E! M: ^! _* P7 p1 ] A2 i j$ W // Then we create a schedule that executes the
0 t" U' D9 X: j, } // modelActions. modelActions is an ActionGroup, by itself it! W4 Q, ^1 |: E
// has no notion of time. In order to have it executed in
4 C7 D4 ]4 |0 ?: s // time, we create a Schedule that says to use the( O7 n9 K, I- o9 n3 {& x+ Z
// modelActions ActionGroup at particular times. This% `' C; g1 `; o4 V# S5 q
// schedule has a repeat interval of 1, it will loop every. l; d1 _8 D7 J! c/ G" r
// time step. The action is executed at time 0 relative to
6 r7 J& q9 \: u0 `% j8 ^7 | // the beginning of the loop.2 T2 [6 a8 H$ _
' u, b8 F9 x# ~$ k // This is a simple schedule, with only one action that is: a" `, s E% ^% o: V+ Z( Y7 q2 v2 X
// just repeated every time. See jmousetrap for more
& s: |2 Y$ e2 D/ ~2 k // complicated schedules.
. \2 G; e% w" Q1 V # r5 f% i" O5 i
modelSchedule = new ScheduleImpl (getZone (), 1);
3 W" E: @# U" V" u/ D0 U0 V modelSchedule.at$createAction (0, modelActions);
* y$ {6 ?9 I' N8 ? & l3 a! y( g% g2 y x' R4 f6 a
return this;9 y. l& y% k H) d- e# m. s
} |