HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ~: C! g7 S& h5 p5 ]& z
j* J$ z6 @7 O4 o% ] public Object buildActions () {5 f) s* U, v7 f9 ^, ~! u
super.buildActions();
6 q g+ h* B5 Y4 U& v , s/ q/ v- l: a& B" d- u" n
// Create the list of simulation actions. We put these in
: e7 A5 e. l; B7 W% S; \7 e! G8 t // an action group, because we want these actions to be
7 }' K6 b! M7 k# [ // executed in a specific order, but these steps should, [) Y1 ?. z. o2 m
// take no (simulated) time. The M(foo) means "The message
( U9 d8 x0 H' y5 D0 L, r // called <foo>". You can send a message To a particular0 l0 Q' z2 ^# f5 b
// object, or ForEach object in a collection.2 V; y: L% n- B
! X l5 m! r$ O$ f) W
// Note we update the heatspace in two phases: first run% T; E; [+ K9 X5 `) i" r
// diffusion, then run "updateWorld" to actually enact the( \, I9 C* c1 }
// changes the heatbugs have made. The ordering here is! R5 u% q: `# }( @! n6 l
// significant!
6 r) W0 @$ F! p2 S+ J. Q* k + o" \% g9 e" Y
// Note also, that with the additional) y$ C& p% {& R# s7 M+ d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ c; {: Y9 i X" l% { // randomize the order in which the bugs actually run; p' f( q: V& Q* z" F
// their step rule. This has the effect of removing any6 u! w, d5 J7 ^- R( m
// systematic bias in the iteration throught the heatbug+ _6 T( J1 ]/ O4 D6 ?$ q4 Y$ i
// list from timestep to timestep. a2 E; |. n& h$ r8 B0 U
; Q, j5 c) H/ E1 {2 _0 ?# y // By default, all `createActionForEach' modelActions have
: Y) E+ {9 E$ R: ?& Z! e // a default order of `Sequential', which means that the
3 O: n( h; k4 ]0 c3 N0 Z+ i // order of iteration through the `heatbugList' will be4 s, ]. L. u c- [ U$ ^4 L6 Y
// identical (assuming the list order is not changed9 l6 l( c2 D0 ?: Q
// indirectly by some other process).
' w, k$ b2 x+ j [9 ^* p' [' F 0 B& s* K. J2 ^0 N, Q; ~7 U
modelActions = new ActionGroupImpl (getZone ());
, w' \* ^1 { a5 v
/ n; N/ l1 x+ }/ A3 I try {2 C" `2 F; x0 V# L+ ?1 S4 d0 A8 L
modelActions.createActionTo$message) F8 p5 n, Z# Y5 q# ?+ m2 u0 u
(heat, new Selector (heat.getClass (), "stepRule", false));# @# d- J/ @, {' h
} catch (Exception e) {
; Z; J& i7 s5 _( g System.err.println ("Exception stepRule: " + e.getMessage ());
6 ]3 k$ b' v# _/ h, ~: g: {7 b+ O }
' G5 F7 C. k" @, r: A, s) t3 n" v" |& I, L2 b+ [2 u
try {
$ g, G8 m. w) M Heatbug proto = (Heatbug) heatbugList.get (0);
3 F1 Q' P8 L G Selector sel = 8 v$ ]' o8 D' s* C. g
new Selector (proto.getClass (), "heatbugStep", false);# D3 a9 P3 W; W: G9 ~
actionForEach =
1 H, {, X. |* q9 b( J+ G( A modelActions.createFActionForEachHomogeneous$call+ E: }( ?( y2 n7 z( \% H2 ?
(heatbugList,7 v. z0 [& |5 j
new FCallImpl (this, proto, sel,+ i# B! W: `6 s' C/ f8 z
new FArgumentsImpl (this, sel)));
' E) N A& O5 m/ R+ a9 g) J- R$ e } catch (Exception e) {, N/ E: k1 D0 k- z' Z ~3 r
e.printStackTrace (System.err);
5 L- D5 p8 x+ l( S- T: J z$ c }' o" n- [+ |( p- H* |$ Y2 S! D
: J, {/ k& h1 i0 i. [
syncUpdateOrder ();
$ b. T3 b: y% @+ j1 D7 S {7 }" p# x8 }
try {9 i/ u% U. }1 F' }* a
modelActions.createActionTo$message ! v* o4 T+ z) }& ~; J/ o
(heat, new Selector (heat.getClass (), "updateLattice", false));' [+ r. g! `1 V& C5 Q
} catch (Exception e) {; {" a3 G- v# h9 V( K; @7 \
System.err.println("Exception updateLattice: " + e.getMessage ());2 D- p1 f3 I3 ]3 w# p9 \/ k7 }
}
; Q7 Y1 ^& q, @5 T
) b' c* B* s" i+ K* h5 |7 Y // Then we create a schedule that executes the
4 R! d; s j9 a- j3 A/ @9 d // modelActions. modelActions is an ActionGroup, by itself it# K H" _# R9 N* Y7 r4 j$ S' q
// has no notion of time. In order to have it executed in
4 y/ O6 s0 V& j3 J9 o* o( _ // time, we create a Schedule that says to use the
/ p4 r; `- s& {: r // modelActions ActionGroup at particular times. This. p) u9 I, A4 g) n1 ~1 ?, {* P
// schedule has a repeat interval of 1, it will loop every
% I9 u5 A% r+ a1 L3 I* E // time step. The action is executed at time 0 relative to
* [; f* o1 y( h' o+ v8 e+ @ // the beginning of the loop.0 b# z3 ? f$ U
( [; p7 a5 e" M! A) B
// This is a simple schedule, with only one action that is, h& l; v% M9 B1 Q
// just repeated every time. See jmousetrap for more& D4 b \( f: f* b5 A G1 n7 y6 n
// complicated schedules.
; O8 s. [3 U% b7 q; E
" k8 D, ^: j& k( K2 e5 d modelSchedule = new ScheduleImpl (getZone (), 1);& A# R4 O$ P. C( y% n4 t
modelSchedule.at$createAction (0, modelActions);
5 U0 A3 S9 }" u2 |/ f o) X 0 k5 D2 s8 x( G, x1 X/ Y# [" X
return this;* f j3 H8 z& [ h# @
} |