HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ v8 S3 f8 Q3 _+ N% k E% r) M }! A8 }9 ^3 r* a6 G7 m
public Object buildActions () {
) j& o" o2 T, b& | super.buildActions();+ X5 e- n, H9 ^. ]9 s+ E, \
( l" X r; T7 t, B4 I6 f( r
// Create the list of simulation actions. We put these in: k2 R2 O" B3 j+ H j4 {
// an action group, because we want these actions to be. d" o' A' ^$ J" T
// executed in a specific order, but these steps should
0 j! q0 E9 [/ [2 P // take no (simulated) time. The M(foo) means "The message" g4 L$ m- x, S. L) R% }+ v+ Z
// called <foo>". You can send a message To a particular/ [" V( z$ C+ k6 ?+ T* W) m. e7 L/ \' E
// object, or ForEach object in a collection.
" l, a4 s0 q" a* n. K% ?* B# V% b
2 i; |9 d4 ^& ]: { // Note we update the heatspace in two phases: first run8 V( U$ _1 O. Y; `1 G0 H1 t
// diffusion, then run "updateWorld" to actually enact the6 L# P8 l" m- l" |) i' D% [9 j
// changes the heatbugs have made. The ordering here is3 n: s i8 G& }: N" v b
// significant!% X- w- @, n: f. V3 F
) h) c' i) f7 _3 V) ~; Q( f# @ // Note also, that with the additional/ K" M, n5 I) D# s
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- R6 _; w/ J, ?4 I- Q7 R // randomize the order in which the bugs actually run2 J/ N4 N& d" `' M/ u
// their step rule. This has the effect of removing any2 R. J. A* n6 z }7 N
// systematic bias in the iteration throught the heatbug
2 U! t) p3 r+ U7 k. W; z$ v // list from timestep to timestep3 b! @) Z1 b# ~" `, |. L
- O o0 V1 `! G% ^9 W* B" ?- K
// By default, all `createActionForEach' modelActions have
, B' n0 ^! _' E // a default order of `Sequential', which means that the
- g$ A( z: ~3 |5 H; G4 ^/ z( m# T // order of iteration through the `heatbugList' will be% T; L7 d$ \/ S7 t: q1 x. Z
// identical (assuming the list order is not changed( c" @# v9 W4 {6 A5 F9 F0 u
// indirectly by some other process).
( ?- A3 b1 b% q/ U4 Q
+ J0 ~5 B; A, m" W; g) i. B$ F modelActions = new ActionGroupImpl (getZone ());( g' u7 u, G% V1 K7 X: u
I; t- h' m) n5 D) P try {
: v! b$ y/ f' c) c modelActions.createActionTo$message# V. @+ o7 ~6 E2 w
(heat, new Selector (heat.getClass (), "stepRule", false));
1 X6 i1 f5 M F% G/ ~- ~6 @ } catch (Exception e) {
* Y4 ]' Z3 L. E5 @ System.err.println ("Exception stepRule: " + e.getMessage ());
i8 x4 P! O( s, d( R# X2 B+ r7 d }
$ }# |' k S& a
; u% J9 O, D0 P+ l3 ~ try {/ t- j7 T* Z: f9 ?
Heatbug proto = (Heatbug) heatbugList.get (0);
4 U0 r% X+ b7 |+ l$ C8 a Selector sel = 6 {2 K0 u& k, b! Y. e
new Selector (proto.getClass (), "heatbugStep", false);
5 v. `& X2 o. u S3 Z actionForEach =; ]4 I8 A/ S# Z. s3 J) P
modelActions.createFActionForEachHomogeneous$call
$ p' e1 h$ W8 }0 y, v/ k# Q3 v# R4 H' G/ S (heatbugList,
2 C! u$ |, }% X# n new FCallImpl (this, proto, sel,4 ]7 B1 d8 [0 m3 o: R; h- i
new FArgumentsImpl (this, sel)));
: ]5 p, K8 _# e! |0 h1 i3 C! M } catch (Exception e) {' N# ~, _3 \, }/ S' \6 U
e.printStackTrace (System.err);
" F& P. N7 {* K, n }( i8 B& N( N# {
. V! S0 O. ?3 Z c
syncUpdateOrder ();1 N* w8 a+ l; v2 V3 G% Y& L
! d! I- ?" R6 X* [& w
try {* k. U" k0 h, {; K, w* _* f: S
modelActions.createActionTo$message
2 [5 H* f; E6 L, [ (heat, new Selector (heat.getClass (), "updateLattice", false));' c9 d+ v2 @6 f' j: P
} catch (Exception e) {$ h- c9 n4 R6 L: b1 a0 E7 l1 s
System.err.println("Exception updateLattice: " + e.getMessage ());
, y7 f! {3 m1 E, N }
! o' n& C% |# q6 D4 n , V9 s1 l$ L( t% d( c5 t' h9 c
// Then we create a schedule that executes the
0 ^' G# v" q8 {& Q, [0 ~ // modelActions. modelActions is an ActionGroup, by itself it
" E) U5 L# M/ |) U- o // has no notion of time. In order to have it executed in
) X, Y' M7 r! y1 `9 T& ^- v // time, we create a Schedule that says to use the
+ F4 k2 ? k0 j# g' J // modelActions ActionGroup at particular times. This
4 m O8 K7 Q; X$ A+ |# I$ @3 M4 U' m) s // schedule has a repeat interval of 1, it will loop every- J$ ?/ o) i' m' v1 C
// time step. The action is executed at time 0 relative to
: @' t) M( \+ l& ?3 C7 h4 ~+ d // the beginning of the loop.9 k1 i- F5 l; ?8 _3 ]
) _5 _8 j3 O0 E; E* n% a6 R. P/ O3 w9 `4 M // This is a simple schedule, with only one action that is
. Z" z9 U5 |8 ]( P* O) }4 A! W // just repeated every time. See jmousetrap for more
# j5 o. J& F$ _" L // complicated schedules.
6 f6 `3 j0 c7 J1 G0 _
" k) [5 W0 u0 U7 U6 f2 a modelSchedule = new ScheduleImpl (getZone (), 1);
2 a- b# x- F- j8 S modelSchedule.at$createAction (0, modelActions);$ l) R# s4 h# W* Z) f8 d
: T1 ?1 H! ^. I5 r( M- s7 t
return this;1 ]! z6 W- j2 R7 y) _) b
} |