HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 T$ D) q" |9 {; R* h$ Q
* ^2 [9 ]5 L% {# ] @. K
public Object buildActions () {! z6 l" |, o7 e, H( Z7 G
super.buildActions();
8 M# A$ N9 F3 t/ N& L& Y4 t
/ ^0 g2 E9 e( Q& G // Create the list of simulation actions. We put these in ?! U4 x' l+ c2 C; Q/ p
// an action group, because we want these actions to be& {1 e; T5 }( T1 M$ @7 B0 a" l9 {
// executed in a specific order, but these steps should8 v9 O3 d6 B; F ?& \2 ?
// take no (simulated) time. The M(foo) means "The message
! D! G4 ?8 S9 W% o& o0 | // called <foo>". You can send a message To a particular T! @; d. O) i6 [
// object, or ForEach object in a collection.
! a: s9 B1 R3 \4 |9 v 0 ^! w" ^* D E* L0 |7 q
// Note we update the heatspace in two phases: first run
+ @1 K- V% a& i. O/ U) ? // diffusion, then run "updateWorld" to actually enact the
, [# N7 B) E+ a% b: [ // changes the heatbugs have made. The ordering here is% a8 _! O, D" V4 U
// significant!
9 s9 W7 L, S2 p9 m. K ; F7 M, W& D* I5 ?6 d( ]/ L7 J4 R
// Note also, that with the additional
$ T7 c2 o c$ J3 @0 N // `randomizeHeatbugUpdateOrder' Boolean flag we can
# A7 {( C& s1 b7 F( ?! t9 E0 D // randomize the order in which the bugs actually run
7 c+ T7 y# P2 L- t3 X // their step rule. This has the effect of removing any
' Q$ R% }9 W( A7 G! | // systematic bias in the iteration throught the heatbug
, Y( z1 Q$ O; D# w/ D6 A // list from timestep to timestep
E3 N4 a; V$ U4 ]* G
, X( D) b9 U/ K) E // By default, all `createActionForEach' modelActions have
7 s5 {4 H1 [7 ~& [# N) J% @ // a default order of `Sequential', which means that the
+ R; x6 J1 i- x2 `% ?1 [ // order of iteration through the `heatbugList' will be
$ i3 b$ |- z. N; i# n // identical (assuming the list order is not changed
5 Z7 x! k% h) ^0 B8 _ z // indirectly by some other process).* i! p5 l6 W2 a3 f# }: k* H
X+ S' e) H4 P( }. T. H modelActions = new ActionGroupImpl (getZone ());1 [! A' \9 Q8 D' n; ~6 g
' t, v% q; Z) \ try {* [3 m( ^) v* p
modelActions.createActionTo$message: C; q9 S6 u, N [
(heat, new Selector (heat.getClass (), "stepRule", false));/ L5 w% |5 R9 Y$ @ ^
} catch (Exception e) {$ v" u' X/ s/ G3 b. {
System.err.println ("Exception stepRule: " + e.getMessage ());
' d) O4 P' k8 j }4 u9 C6 {' y1 n
2 I* P/ E. j; x$ j, S
try {& e7 S `) m5 B2 O
Heatbug proto = (Heatbug) heatbugList.get (0);
& [2 r9 P" p2 z4 L Selector sel = + A$ ^3 D. i! c7 I- }6 j3 i
new Selector (proto.getClass (), "heatbugStep", false);
" O( d! }$ L7 b actionForEach =9 H4 @) @! t5 F( O
modelActions.createFActionForEachHomogeneous$call
4 m( ?1 X5 Z& |+ k! A2 c7 B s3 @* p (heatbugList,
7 i7 @( E0 ~: N new FCallImpl (this, proto, sel,
! b/ C0 H7 t2 [: Q1 C new FArgumentsImpl (this, sel)));$ E; s% R2 i$ Q$ D/ R$ L& z0 o
} catch (Exception e) {
) q9 }1 l7 R+ ]/ D5 Z e.printStackTrace (System.err);
8 n2 j; [9 P g9 i' [ }9 j7 A& {% Q& _* S' g
# B5 v) R: J# _, H, i syncUpdateOrder ();
; ~7 p6 k! L! d" ?# f8 ?! T
, z4 u6 O8 B: y5 u* m+ A try {* f/ J9 Z2 M7 m, |
modelActions.createActionTo$message & V+ q9 l" i5 Q$ g; t$ i5 H# z
(heat, new Selector (heat.getClass (), "updateLattice", false));
) _) ]( a( f5 F$ ^# G } catch (Exception e) {
. U- ^7 T/ K+ B& `) S! z, H" v o System.err.println("Exception updateLattice: " + e.getMessage ());
; C0 n9 h, \0 p9 y }
* r: s3 @/ H8 j$ z* B) I0 K % V% t. b# O# c4 _" Z
// Then we create a schedule that executes the- T! w4 {: U' ^4 H- i# R' M" V
// modelActions. modelActions is an ActionGroup, by itself it9 S7 c3 A; D ]% f- x& C3 a* r. s9 k/ b
// has no notion of time. In order to have it executed in6 d/ I% |& l ^! v) u
// time, we create a Schedule that says to use the" ?7 i2 s: ^4 }8 O. R$ F/ \
// modelActions ActionGroup at particular times. This
1 {# `* c) [2 I$ o/ j& F // schedule has a repeat interval of 1, it will loop every
9 c+ z2 H9 z% C- [# S! m% m // time step. The action is executed at time 0 relative to
$ B4 f+ g) K2 M' e2 d' c) [ // the beginning of the loop.( O ? F0 |9 ~1 ^" c M
# J+ [$ [$ P, o4 t" n; v
// This is a simple schedule, with only one action that is! Y7 G* ~6 [! |8 R+ D P
// just repeated every time. See jmousetrap for more; T, ~3 q$ h" q/ w5 X
// complicated schedules.
) u" V; k9 f# |" \5 n% @
- p7 m1 U8 s0 C' ^) E/ z4 K. E% z modelSchedule = new ScheduleImpl (getZone (), 1);
& M/ V/ O$ h: Z( z modelSchedule.at$createAction (0, modelActions);
; c7 _# v8 z# m3 m
, U" }$ q; ]4 F4 N return this;0 w& V& a9 ?8 r# i2 s0 E2 Y8 K
} |