HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, B+ V& L1 f! @, E
$ O+ B9 [2 X6 I public Object buildActions () {
) j3 K$ u4 b- T" F( S super.buildActions();
# ^: u4 W/ x# Y
! ~. L! A8 c6 {( i7 Q8 |& [; [ // Create the list of simulation actions. We put these in
( \( A( x/ w5 g& \+ G // an action group, because we want these actions to be
- t2 W2 i. j: E) Q C+ A- S // executed in a specific order, but these steps should3 G) d# g$ H( ~$ @' a8 U2 D
// take no (simulated) time. The M(foo) means "The message9 ]/ w R2 F) D3 d
// called <foo>". You can send a message To a particular
! ^' N; M0 t) k6 W; F `3 n // object, or ForEach object in a collection.
" N, u" b6 F9 S' ~' A0 l1 i. [ : [# B1 H, a- N* r- Z' T4 x
// Note we update the heatspace in two phases: first run3 ~" z8 Y) W% R5 g: z
// diffusion, then run "updateWorld" to actually enact the
/ z7 K M0 N6 b6 H) F // changes the heatbugs have made. The ordering here is/ @- z2 t3 X: n. G) l
// significant!; c$ D- o/ S# s# u3 }
) g" ~" k& C" ?) Q% @- g1 h // Note also, that with the additional$ g5 N ~7 w- \* a) o( S
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ e" [, q8 Q/ _8 A) f( h. a
// randomize the order in which the bugs actually run6 i& J7 C$ w V& |2 h* L
// their step rule. This has the effect of removing any; U9 W6 h: [4 e' H* \8 X. s
// systematic bias in the iteration throught the heatbug& ^1 N) T p# j3 c) E
// list from timestep to timestep& A& b7 J2 `; W5 C N* m( { i! S% p* m
`! R. C: @0 x+ \- k, M
// By default, all `createActionForEach' modelActions have
9 U. m5 w2 g6 q$ J% o // a default order of `Sequential', which means that the
7 V+ c; V4 j" } ] F1 w2 y# R // order of iteration through the `heatbugList' will be
# I8 F/ r' s0 h1 M // identical (assuming the list order is not changed/ c: n+ T! [1 D' k+ A4 ?6 \4 s; i
// indirectly by some other process).- W; v2 F/ }7 {+ [ J/ x
7 \& C- N3 ^. f; b; R" i: h. v9 [% b
modelActions = new ActionGroupImpl (getZone ());
~# q5 d& M6 U3 g* m2 t( E7 K& W9 y; k; i% R* o
try {" k/ ~5 J1 B* ?2 c/ f8 @
modelActions.createActionTo$message
* j) f% {9 q: h3 ]* d% H2 D, _, Z- Q (heat, new Selector (heat.getClass (), "stepRule", false));2 e# H. ^+ ?4 I7 W. j
} catch (Exception e) {
; C" |0 X$ X) o2 E System.err.println ("Exception stepRule: " + e.getMessage ());: e2 O# M7 H7 q' e' ?
}& v) ] x( F5 E- {7 z1 y
- E4 o0 z4 k) C/ Z# C try {
% M6 }6 d* I+ n! y2 {" E Heatbug proto = (Heatbug) heatbugList.get (0);) ^) }- D J! A$ f) b
Selector sel =
! Y" y9 r' o8 V: ] new Selector (proto.getClass (), "heatbugStep", false);; t+ W1 j: J# m, W
actionForEach =
& S4 R# m2 ]7 E2 U9 C modelActions.createFActionForEachHomogeneous$call4 v* J t( V/ ~# p T4 F
(heatbugList,
/ u- J7 H7 g: {5 E. V, D new FCallImpl (this, proto, sel,% p( }" O1 F P5 m1 p! F q* n
new FArgumentsImpl (this, sel)));' n5 B0 v# J6 \7 u4 e
} catch (Exception e) {. k; R0 }- l1 X( b* k+ t: F
e.printStackTrace (System.err);
, r1 p+ H: H( w6 Z& q4 V }4 W ^6 E9 h' R5 z m) ?
v- D" P) S6 u6 w7 @% J- x
syncUpdateOrder ();6 ]7 y& k3 E' R0 z% Y8 }$ p ^
* R: |7 f6 ^8 z4 Z# k4 Q
try {( z: o& [ `0 q S- V3 {1 k% s& U. _
modelActions.createActionTo$message
) X/ n1 @0 h$ p# g (heat, new Selector (heat.getClass (), "updateLattice", false));2 Q, T: y: s7 d$ g. z" e
} catch (Exception e) {9 r; L# v+ E, T
System.err.println("Exception updateLattice: " + e.getMessage ());
* l5 L6 E3 L6 n6 T0 ` }1 O& W/ N9 @* i3 I, c+ r
, A0 J( [4 L! `# J6 Q // Then we create a schedule that executes the) `8 ^4 i6 N9 d; `
// modelActions. modelActions is an ActionGroup, by itself it
7 @. s+ E! O, V N // has no notion of time. In order to have it executed in- i7 e. b% q' {6 \
// time, we create a Schedule that says to use the
+ W0 V9 ]8 q4 P W7 Y1 X // modelActions ActionGroup at particular times. This
: o3 B/ y2 O: r8 d) o // schedule has a repeat interval of 1, it will loop every% r, o# Q4 j/ ^7 R6 \& o
// time step. The action is executed at time 0 relative to
+ A+ t+ U6 J5 W // the beginning of the loop.
' |' ~2 s. d* m6 Q* E8 M3 b* r: G8 V7 W% e0 G
// This is a simple schedule, with only one action that is- b4 o( }6 S5 D
// just repeated every time. See jmousetrap for more
' C+ ~) ~: [5 r7 n4 i // complicated schedules.
% S( c3 }, b# P . |" T# x/ O% V# M4 F. `# v% t
modelSchedule = new ScheduleImpl (getZone (), 1);
& D4 b6 m( W. F4 _7 y9 B) A modelSchedule.at$createAction (0, modelActions);
! r' \4 _0 P9 z ; e# U6 p8 N1 D# K: f* V H) \
return this;% m( M( x: Y* m7 t8 z
} |