HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# d% q2 K1 ?0 i0 C- C8 {
, ]1 i* J% g4 [
public Object buildActions () {
! @! M6 \& r" }! J4 `# v super.buildActions(); ?9 l- i$ \! \* c8 s- K+ A
' ^2 R# r& b8 ]9 X) P7 _4 _ O // Create the list of simulation actions. We put these in2 F' z0 e. [* c) J0 Q" f
// an action group, because we want these actions to be6 H' z; c0 X5 @( }5 B( x
// executed in a specific order, but these steps should7 X) I, B+ L) F5 f9 n; m8 Q1 m
// take no (simulated) time. The M(foo) means "The message
1 n* p& z1 B# S+ } // called <foo>". You can send a message To a particular, {) @# Y: J/ R1 D* t0 q* o( i% V
// object, or ForEach object in a collection.
# Q* q+ v" Y n 6 Z: ? G$ T+ ~% C1 z+ p: X- z' _
// Note we update the heatspace in two phases: first run
( `6 A; y$ p; Q7 |7 g# G // diffusion, then run "updateWorld" to actually enact the3 {- r3 l; G6 u1 R# x4 U7 G7 |
// changes the heatbugs have made. The ordering here is& R' t" H" b4 s% I$ ?
// significant!$ A, D( e# h2 z
0 r0 c8 _- d* X0 y# y' _
// Note also, that with the additional# Q- e& t3 j# [9 o8 r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; E, M. C& T( p! p% j) y // randomize the order in which the bugs actually run
, e, y9 c& u2 }1 t8 D& u. Q // their step rule. This has the effect of removing any
& l7 ]# U# _! P7 A // systematic bias in the iteration throught the heatbug
" H8 p. C$ @( |& |9 v# J9 ?# ] // list from timestep to timestep
7 x& ^5 L' Y1 w% a0 y; g5 ? ) N7 y/ ^% p/ Y7 K
// By default, all `createActionForEach' modelActions have# p$ q1 ^+ X; a
// a default order of `Sequential', which means that the# d/ K7 U, O: N& ?5 M. \3 L7 |1 }
// order of iteration through the `heatbugList' will be: W/ |4 Q$ q @8 \& Z1 _- F0 F
// identical (assuming the list order is not changed+ R4 N9 \+ `; A0 }, \7 q
// indirectly by some other process)." J/ F$ f6 M e4 M8 Y
0 h5 t/ h% U5 n modelActions = new ActionGroupImpl (getZone ());( J) J9 z& F! B' ]2 u# S
1 i7 g. R5 {- t+ |# ?0 P try {
" O8 Q! }! y O" S) x modelActions.createActionTo$message* ~, F4 ~, _9 `0 s4 f# P
(heat, new Selector (heat.getClass (), "stepRule", false));% \# }8 s& s5 k" L( q% L
} catch (Exception e) {
) k; [! a+ N. I; F! u( r System.err.println ("Exception stepRule: " + e.getMessage ());; ]/ n7 R" m+ D/ O8 O' `
}* G% L2 m: }3 A' n; S! e$ r8 w" ?6 t
' i/ P; U3 ^/ e _7 V" f try {
5 ]. }+ R( Y& ~ M4 l$ `' b Heatbug proto = (Heatbug) heatbugList.get (0);2 I X i( t5 _+ D9 M
Selector sel = * q- W* m% y& r: f" v0 ?
new Selector (proto.getClass (), "heatbugStep", false);
) l! {3 Z: s+ m actionForEach =
7 I) }1 S8 N$ m* J1 m modelActions.createFActionForEachHomogeneous$call
8 C+ A, l: j* Z$ S: [ (heatbugList,+ h7 }5 k3 U* w
new FCallImpl (this, proto, sel,, S' V+ m1 n/ V% [6 M9 y
new FArgumentsImpl (this, sel)));
- W5 g( i: e+ F" V0 P+ m2 h4 R+ Q! S } catch (Exception e) {) I, B% w! r; B6 Q" f: ?
e.printStackTrace (System.err);3 P/ x. q4 `( U
}! ?& f9 V+ H% u) D5 y' ]
+ n2 e# R7 y1 }: E l$ O
syncUpdateOrder ();
7 ]6 w7 d$ Y) d7 r' V2 S! a( P) t+ o/ L
try {: t9 S5 n2 _! G5 K
modelActions.createActionTo$message
) V2 ~ p* i$ a" t1 o3 T* V (heat, new Selector (heat.getClass (), "updateLattice", false));& U$ u' J- Y) N2 v* B) b3 t4 k
} catch (Exception e) { R' o9 ~9 A+ Z& e+ j& V
System.err.println("Exception updateLattice: " + e.getMessage ());1 }/ [2 _1 w' E0 C
}1 y K. j" C! O I3 {0 X& ^
# D: n1 O+ u4 ? F+ w- n; z/ n // Then we create a schedule that executes the' D9 H' d: F; x% ~
// modelActions. modelActions is an ActionGroup, by itself it
# ?+ q1 a8 [# b" g# x0 o/ n // has no notion of time. In order to have it executed in
# x) q; J' p3 ]/ l7 i K // time, we create a Schedule that says to use the8 Y& H0 e' G' F
// modelActions ActionGroup at particular times. This
1 T: t' l$ W! t8 ?* m5 n% V7 Z // schedule has a repeat interval of 1, it will loop every5 _& A, K1 I& G
// time step. The action is executed at time 0 relative to
4 F/ k- d( y3 l0 b6 m // the beginning of the loop.
, x3 m. e& o& p; s% q3 o; t$ i; w8 i5 |! B0 r
// This is a simple schedule, with only one action that is" u- o# C3 c/ P4 P
// just repeated every time. See jmousetrap for more. Y$ ` v5 V' z: K
// complicated schedules., h! _6 i( C$ b( c2 y3 U
! [4 U: H! M! z: I# g, ~: n( C( t
modelSchedule = new ScheduleImpl (getZone (), 1);
& M0 i# I8 P' D% T5 ^ modelSchedule.at$createAction (0, modelActions);4 B! h B: w W) o( j2 X& {
! I0 j; q! c3 R5 m& M$ b return this;
- r1 E7 h% R+ R" d$ h& ] } |