HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; ?0 g' w/ s/ F) O4 a) \6 B
( Q! U. t2 v6 J- _5 s public Object buildActions () {
: K! b7 A5 ~: E0 @/ L) y) b super.buildActions();8 G9 T& W1 I6 d5 W1 z8 T1 c/ ?
, P) W5 {/ B. g: Q+ q8 }
// Create the list of simulation actions. We put these in- B$ N5 R X: p$ @6 X; b# v7 i$ k
// an action group, because we want these actions to be3 j% U: _& u. v, ]% P
// executed in a specific order, but these steps should
5 G$ a8 ]% ^) d7 [# T) w // take no (simulated) time. The M(foo) means "The message: D" b, `% E2 V8 [
// called <foo>". You can send a message To a particular
- L; e. L T' i# v, K+ i7 T // object, or ForEach object in a collection.
* u& e& U( @2 [! R
T- s, u; ]% J, Z" s // Note we update the heatspace in two phases: first run
$ L+ L) D" n" g' p- A // diffusion, then run "updateWorld" to actually enact the& j7 i8 k1 r0 z$ e
// changes the heatbugs have made. The ordering here is
2 O/ B! j7 B' a' R4 y // significant!
$ f5 B( C; o2 O# w/ ]6 T; |7 Q
! {0 }2 p+ e, s# { // Note also, that with the additional
6 l4 m. u: P7 [, o( ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
( }( J8 {0 L; K& g // randomize the order in which the bugs actually run: X; Q6 f( H" N
// their step rule. This has the effect of removing any; I+ X8 r: E R' J) q& r
// systematic bias in the iteration throught the heatbug
5 z% f4 e" l! m$ K1 O // list from timestep to timestep
+ A& L; B7 q* @( n. M! o
( y& l1 [. u3 V2 b% [1 J // By default, all `createActionForEach' modelActions have& i: m9 U$ N9 C/ S% d! s
// a default order of `Sequential', which means that the
: j& b1 T, k* L5 w, K7 Y( y // order of iteration through the `heatbugList' will be' \/ N7 s* V, a9 i$ X- N
// identical (assuming the list order is not changed
& E. E7 g. l3 }1 Y: d: F" e4 P- |. @ // indirectly by some other process)., _: Z' H9 E: P; f8 a
4 Q7 _! D, \/ P: x# S0 ~# F! Z, [5 ^6 h
modelActions = new ActionGroupImpl (getZone ());/ {! Z& p7 O' D4 ?3 C' i
( D! j9 \6 P; P; Y! ]
try {1 q9 ?0 T( ^3 j/ x/ m
modelActions.createActionTo$message
, S3 s. P' Y" @+ Y$ \% G+ ~. a (heat, new Selector (heat.getClass (), "stepRule", false));0 Y/ Z1 G, H3 P2 L% a
} catch (Exception e) {) Z' ~6 O% L% X$ y
System.err.println ("Exception stepRule: " + e.getMessage ());
+ `; t% }" c% c' z# i8 W+ | }
6 F2 v+ L3 N9 _$ r5 |/ I- [* x6 F
try {
o7 N4 V6 D2 @ Heatbug proto = (Heatbug) heatbugList.get (0);
8 S6 @( Q# x! ?1 X Selector sel =
( V6 u0 _5 f0 R( h4 N% x new Selector (proto.getClass (), "heatbugStep", false);
' ~: ?! ? T! ^3 r actionForEach =2 i! b" p* n+ Q$ }( o" K4 v
modelActions.createFActionForEachHomogeneous$call
8 ?$ n; C8 ?1 [3 P$ o (heatbugList,1 J$ {0 n$ c" f; | @9 K
new FCallImpl (this, proto, sel,
; n# G5 x9 Y+ @% p \ new FArgumentsImpl (this, sel)));
$ |' g: R( p" f) ^ } catch (Exception e) {
! X/ I) R1 F1 Z4 T$ e# M e.printStackTrace (System.err);
- b; O2 h8 |6 K9 H }2 j* o) i9 W& Z. ~" v8 u
: @- L, L. F E+ X( A syncUpdateOrder ();5 U' h$ C" |) U+ J# _" a
# F+ H% {- B% g7 |5 K
try {8 K" L" E/ W) i W: _" M! N
modelActions.createActionTo$message ) W9 ?5 y Y! |6 t6 l2 a0 f- s/ X" z
(heat, new Selector (heat.getClass (), "updateLattice", false));
' g, }1 m" U" L+ x' X$ c% I9 q; a } catch (Exception e) {& i9 b3 C2 t8 L4 d
System.err.println("Exception updateLattice: " + e.getMessage ());
6 g; X5 i( x% B, Q/ b }
! I* p. L5 H/ _+ o* U7 r
: Z) E+ d& N5 [* r/ o // Then we create a schedule that executes the
: l: m( f9 n. l0 \ // modelActions. modelActions is an ActionGroup, by itself it
, R5 x; W9 g5 g- Y // has no notion of time. In order to have it executed in0 D0 Q; x. ?9 L Y W
// time, we create a Schedule that says to use the+ @3 r+ N/ f) F
// modelActions ActionGroup at particular times. This5 h/ }' I: h2 |8 P7 [1 x
// schedule has a repeat interval of 1, it will loop every) `7 A* C5 X2 v- j/ c; s
// time step. The action is executed at time 0 relative to
+ q" a) E6 C8 F5 a8 }7 S/ l // the beginning of the loop.
: X% g8 v9 F4 @$ b" x5 P( N
; E o9 F) I0 g! c4 W+ x // This is a simple schedule, with only one action that is
" p% P V+ K4 k$ \ // just repeated every time. See jmousetrap for more/ Y W* x4 X% @. p Q" q$ m3 y
// complicated schedules.9 _5 S; ~3 \& E4 B" L& v$ m
, b; }" P5 I/ Y- B: | modelSchedule = new ScheduleImpl (getZone (), 1);* Y( I: O6 f P
modelSchedule.at$createAction (0, modelActions);+ F' c5 ?+ e9 f+ Q
2 `2 x* r2 m1 B2 l3 ^ return this;# ?: h6 U! l# p" }# T
} |