HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 C+ R) j/ T& b& h2 y7 c' |& ~
( v9 o4 G/ i" O. D+ ?; A( ] public Object buildActions () {
8 t* m3 O& `2 I5 g# f4 M( n1 e super.buildActions();
4 q7 i( [9 M1 ^3 H/ g1 ~
% P+ M5 m6 f' W8 b3 z! {, i // Create the list of simulation actions. We put these in
7 d ^- C% m7 \( r! t9 J // an action group, because we want these actions to be0 p$ e! G. W5 Y5 }
// executed in a specific order, but these steps should% G; K* t/ P% l6 V: C0 G, }
// take no (simulated) time. The M(foo) means "The message
+ b- @% ]; f! x0 W2 X' `/ `, ` // called <foo>". You can send a message To a particular) m- C1 e! X5 t! Y* q
// object, or ForEach object in a collection.* t [' l# `1 I! ]( q
- D; A$ B3 y: x8 t% P" Q( n- O; I4 g // Note we update the heatspace in two phases: first run: n) o; T! S8 @4 B
// diffusion, then run "updateWorld" to actually enact the
5 i! Q& L _* m |3 n; H: } // changes the heatbugs have made. The ordering here is& \$ m( W" U+ F* V
// significant!& Q; A" Q# S- b* d* R
: a, m( ?. u' O0 R- z2 u5 e
// Note also, that with the additional
9 `1 z) I9 m- r) O // `randomizeHeatbugUpdateOrder' Boolean flag we can% N Y. E( a+ n' Q$ ^2 x
// randomize the order in which the bugs actually run8 H: {' u: S/ r. u) c
// their step rule. This has the effect of removing any
. S2 L ]' r9 i% e6 V! m // systematic bias in the iteration throught the heatbug( W- a: ~/ H2 s/ @
// list from timestep to timestep
' B0 @ }4 ?) f# X' X
) s, P5 s; Z0 ~# Y; N7 K& Y+ C // By default, all `createActionForEach' modelActions have& i" b) x3 k) T) y4 M
// a default order of `Sequential', which means that the
/ g# E3 Q0 r! |+ w. a2 J" i // order of iteration through the `heatbugList' will be0 S Z/ U% Z/ @7 Y& }, i7 S
// identical (assuming the list order is not changed
: G# x% k3 `1 G% ~ ]$ p* Q // indirectly by some other process).
" g# ^& A2 L; y
* F" h5 l7 X; y' u modelActions = new ActionGroupImpl (getZone ());
, e( n; c- B) @9 T
7 j$ a8 X0 ^0 O' Y, _3 Y: | try {
/ s" U, x, L* ^# t; C modelActions.createActionTo$message
& A5 e" V V* j8 W! U' U* Y8 O" d (heat, new Selector (heat.getClass (), "stepRule", false));
: k8 _' V1 J, ~* {3 R1 g } catch (Exception e) {
7 h3 h( q ]2 v" D9 F! ^. c System.err.println ("Exception stepRule: " + e.getMessage ());
9 j4 N) x- d' U: M/ p }
/ t0 N3 B7 y" ~' t$ S, x
, b3 g: w7 x% K/ H$ \! p0 O8 Q try {$ f" W' B6 u O4 E: ~5 G" Y a
Heatbug proto = (Heatbug) heatbugList.get (0);2 W0 a2 b6 t6 s7 i. F4 T
Selector sel =
u, h5 U1 q& a* H/ d1 P- ~ new Selector (proto.getClass (), "heatbugStep", false);7 }8 `4 ?2 e+ }* r8 B' e4 O5 J" D: G
actionForEach =9 f7 }3 T5 j: f
modelActions.createFActionForEachHomogeneous$call
6 W: l/ @' g. I9 j0 e6 e& ^7 d (heatbugList,
/ E" C; F9 V3 j/ h* Z9 U* ` new FCallImpl (this, proto, sel,1 r& g/ [* A0 x
new FArgumentsImpl (this, sel)));1 `- n1 ^9 r# b. p
} catch (Exception e) {
1 u) m+ Q% b% @, S! W( G' t8 b( E e.printStackTrace (System.err);9 y6 U! m' d: ~& N4 r% n+ k* u
}
, {* I1 q/ e. o: V8 T; \+ F$ d4 u9 t: d
! P g2 m% V8 ]5 K. ^. W syncUpdateOrder ();
9 D! n; s& k! |- |4 L- G+ z- b; O% j+ }9 L2 [1 o* W, w
try {
Y8 w0 c/ J' P modelActions.createActionTo$message * a/ I: b! e- h1 x, F
(heat, new Selector (heat.getClass (), "updateLattice", false));- F# }2 V+ i" e5 I) e6 K
} catch (Exception e) {
& v5 l5 o7 w, \6 { System.err.println("Exception updateLattice: " + e.getMessage ());8 j* Q. [( K7 @9 { j
}
; M$ L* H8 {. l m; G2 ^) ^/ Y0 d
+ O5 V3 p# `* F7 j4 ~# h, V // Then we create a schedule that executes the
- _) I! U8 }! l0 Q; Q // modelActions. modelActions is an ActionGroup, by itself it7 V1 t' S& ^4 h: h# V
// has no notion of time. In order to have it executed in
! \# N) A; Y8 d" a: `# j // time, we create a Schedule that says to use the# B! S3 J! y" ]& P
// modelActions ActionGroup at particular times. This. e# C6 H: m% Q" {3 W
// schedule has a repeat interval of 1, it will loop every. W2 a0 v/ r$ a$ s
// time step. The action is executed at time 0 relative to, K+ g& c% ]3 U1 V" m
// the beginning of the loop.
4 M! g9 b4 d5 Y: U8 F3 O
; Q; `: P9 S3 K6 z" H9 z6 y2 { // This is a simple schedule, with only one action that is" v, O7 i6 [; g) r! u0 h
// just repeated every time. See jmousetrap for more/ M# V0 b" h1 X* R) H$ I
// complicated schedules.
& [( c. d/ _1 p4 c) b# ^ 6 W3 j. e6 u! p3 ?6 \) I
modelSchedule = new ScheduleImpl (getZone (), 1);' B$ {( {1 N2 C& |1 k. b# i7 }
modelSchedule.at$createAction (0, modelActions);, ?7 x; F5 i1 a# X* `. r
1 r# P; ^5 `- ~. h, G return this;
# p4 h7 P- a1 T& A' f/ X+ ^ } |