HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; m# t; T5 z. {, T( c4 W0 \# \- f2 ?
public Object buildActions () {
) q0 Q% L' w6 i- @0 ]8 N super.buildActions();
: I6 t h f5 T# x
8 ]! I# c1 `0 F3 E6 k9 o: ^$ g // Create the list of simulation actions. We put these in
. ^# x5 ^" V" t8 w, _* n/ I' }8 Z; z // an action group, because we want these actions to be [- w6 h$ F5 f
// executed in a specific order, but these steps should9 v5 ]9 N7 a( L# [- O* E2 K9 w
// take no (simulated) time. The M(foo) means "The message. u& k- Q2 k0 j( Y
// called <foo>". You can send a message To a particular
4 _3 f" @6 o6 r& o0 e // object, or ForEach object in a collection.
U7 O+ N& R: G- B: i
: r W% h! B) f7 z( f // Note we update the heatspace in two phases: first run. o( I% d' I9 p( @. i
// diffusion, then run "updateWorld" to actually enact the
2 a+ n3 j0 d6 u8 x // changes the heatbugs have made. The ordering here is
( p" l7 i5 Y; d; O$ o, \% _/ @ // significant!
+ t1 f2 ~* ?/ @; b, q* C3 H o; t 1 Y( K1 V C7 X) s
// Note also, that with the additional
) o0 |/ a; Y7 X2 z9 z // `randomizeHeatbugUpdateOrder' Boolean flag we can( F+ s1 I5 r% b( G# R" v' h6 l- ^
// randomize the order in which the bugs actually run
* P, f+ s, r! g' u- ` // their step rule. This has the effect of removing any
# u8 R3 D* n* m" o0 z5 I1 G // systematic bias in the iteration throught the heatbug
8 r% @8 Y1 j; o- } // list from timestep to timestep9 @) Q6 f! Z" T' ?/ `) d# p! ]
' T# g5 ^5 s5 m( I! {5 H // By default, all `createActionForEach' modelActions have
& V3 T- s+ Z- k. @" Z3 m, y' L // a default order of `Sequential', which means that the% H( ~2 m% x7 Y% k
// order of iteration through the `heatbugList' will be- ]" a; X% V( y1 x, a$ m' f4 H3 p
// identical (assuming the list order is not changed
% p" D- H, ^) A) i6 c' Y" t" s // indirectly by some other process).* G a! E+ @; }. A0 a# P
+ \/ j7 S, ` G, O# ?0 | modelActions = new ActionGroupImpl (getZone ());$ t$ {6 `6 m0 R. ~/ \1 ^
?" B6 a4 q A: ?( J try {
, \4 |+ _- x' t" O6 L& J+ Q/ W; i modelActions.createActionTo$message
8 a" K9 E& ]2 t (heat, new Selector (heat.getClass (), "stepRule", false));
0 j* k2 p( P; f8 @ } catch (Exception e) {- M/ Z4 h2 H1 l& x W# p& Y
System.err.println ("Exception stepRule: " + e.getMessage ());; n2 s# q/ [) O5 m" Z
}
* A6 P4 `4 P0 O, {
8 o, ^ n. T2 j try {' E, Q: Z* v0 {2 K8 m) {
Heatbug proto = (Heatbug) heatbugList.get (0);2 [) m! P2 R. q* W+ x3 O" S; f
Selector sel = 4 H( f3 [9 |* m* |( h
new Selector (proto.getClass (), "heatbugStep", false);0 i( d/ f9 Q$ k+ n
actionForEach =
+ t; \' m2 }: i" m5 I modelActions.createFActionForEachHomogeneous$call& p+ z4 Q% \, \1 d' ]3 T0 W% S, f9 i
(heatbugList,3 C4 D) f$ ^2 }% ~4 r
new FCallImpl (this, proto, sel," H0 s; V" ?9 d8 d+ f
new FArgumentsImpl (this, sel)));; g4 }. Z5 P! o$ H4 m7 \0 ~
} catch (Exception e) {$ L' v# l: V u. h
e.printStackTrace (System.err);9 X( u% N/ o/ _) u
}
2 s S8 d' c# o& U1 G# Y1 i
2 \+ z1 M% d, r2 y syncUpdateOrder ();
- G7 R0 I6 {" o6 [- _4 _
/ N9 o3 b. _) y1 d" n try {9 H% u5 T0 |* x& P2 t
modelActions.createActionTo$message
. q6 g7 M9 A/ m (heat, new Selector (heat.getClass (), "updateLattice", false));- ]% g0 j2 t+ [; [
} catch (Exception e) {; a) Q4 ?; ]) C% L$ @- p3 x
System.err.println("Exception updateLattice: " + e.getMessage ());. v* |: c# j0 H! m3 D4 v8 B2 g
}* x5 E# b Q% k! e4 W; ]0 U u
5 M, W5 Y3 r$ @7 H: a7 s // Then we create a schedule that executes the* c9 H+ Y1 n# N9 `: g, z
// modelActions. modelActions is an ActionGroup, by itself it3 R# `/ |4 V0 M; O' e/ E% k
// has no notion of time. In order to have it executed in; c4 ^$ @0 U* w5 |# b( x% n# I0 i
// time, we create a Schedule that says to use the
" \+ N5 O6 P6 w // modelActions ActionGroup at particular times. This) l- X, r9 ^! T- C
// schedule has a repeat interval of 1, it will loop every
2 Z9 `* L( O! t // time step. The action is executed at time 0 relative to
" S; C. U5 m) ^8 }: Q* m) ]+ | // the beginning of the loop.
+ X( M6 U( x+ f q& T6 N2 ~ f8 r) i |& m
// This is a simple schedule, with only one action that is. u/ O/ Y `( @$ ~2 a
// just repeated every time. See jmousetrap for more
! a P/ ~# H) ?% E5 R // complicated schedules.
) a. N( n$ @" s& `0 g+ k $ b3 x* B: V* w# s; B1 d2 D
modelSchedule = new ScheduleImpl (getZone (), 1);- R# h; J T2 W1 V2 i
modelSchedule.at$createAction (0, modelActions);
( ~: R H& ^: \8 w! u3 ] " c6 m) K4 n, p8 z7 J! _$ E, F
return this;
) [+ J# T, G3 m2 x. J$ `" c9 k. z6 R } |