HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 D& ^$ y. g& _# {* N, x- |7 `% ^7 W" B6 A8 a2 F8 ` O
public Object buildActions () {& Q2 T/ n$ v4 t9 U( F# E- O/ O' A7 x
super.buildActions();
- U% g) `* E2 Q/ D
% U' r& U7 ]% f" s // Create the list of simulation actions. We put these in
4 M {5 j* p+ P% N* w // an action group, because we want these actions to be
, F" d& w% ?- G1 M3 r) b; B; B // executed in a specific order, but these steps should
. A g1 O' S- ?1 z$ T // take no (simulated) time. The M(foo) means "The message/ q+ Y4 K- [# B, L# Q; R8 P; g% C/ |
// called <foo>". You can send a message To a particular/ }2 X8 Y u$ r2 o# }
// object, or ForEach object in a collection.
# k7 x9 ?. S( J; O$ J
' f' y* g& n1 r8 T# g A2 E // Note we update the heatspace in two phases: first run
3 i4 l9 F; b7 M* w4 I6 j // diffusion, then run "updateWorld" to actually enact the
# {$ x* z6 I- M x+ ?+ a. E // changes the heatbugs have made. The ordering here is4 ?2 W, e) w$ e" p
// significant!% r# o5 K3 |* c7 e& p/ V1 J/ [
% ?6 x( m/ }: k( s3 J
// Note also, that with the additional
[& c$ ?2 H3 C5 W4 |! u8 r // `randomizeHeatbugUpdateOrder' Boolean flag we can
* [4 a" K* _" u' a // randomize the order in which the bugs actually run7 f5 h7 |# Y0 t# ]
// their step rule. This has the effect of removing any
: V7 o* \' ~/ c; Q; Y! p // systematic bias in the iteration throught the heatbug
+ F" r. h7 @% \" p // list from timestep to timestep( V" d- F% C4 j+ K H9 o% z% K
5 R9 R5 W' [6 V T( @, a
// By default, all `createActionForEach' modelActions have
( F+ E) P5 N0 H5 F // a default order of `Sequential', which means that the
5 [8 v( R2 O- A( {& ` // order of iteration through the `heatbugList' will be
* Q/ t! f+ M' t; m" ~& m6 F/ s // identical (assuming the list order is not changed
% M; C8 e$ _# g4 ? // indirectly by some other process).2 Q& V+ \) b* |! p Y
) z; _6 D5 N% d: x0 H modelActions = new ActionGroupImpl (getZone ());
6 s7 {9 Y4 [, i5 f
$ ^9 d; C4 u$ ~# o2 F& d try {8 i+ D- J! W& t# O4 ?9 I
modelActions.createActionTo$message6 [2 p% s8 e4 R
(heat, new Selector (heat.getClass (), "stepRule", false));
2 h' [2 x8 R2 H } catch (Exception e) {5 _ ]5 u4 ^* V. a1 d
System.err.println ("Exception stepRule: " + e.getMessage ());
) Y. e9 M1 o% b: H }
' n" o4 ]0 E6 v6 R! m
: `( {+ E( \2 M! W; D% V( ]9 X1 ~+ q try {
- u) x& {& N: x1 t Heatbug proto = (Heatbug) heatbugList.get (0);
+ T: s: R2 g/ l; I4 ~7 I Selector sel = & d0 B+ a% J* q, v/ Y: G0 _- y
new Selector (proto.getClass (), "heatbugStep", false);
! \- E6 A/ f. ~) x& Q5 [ actionForEach =0 }5 W& w3 F5 F4 x: g7 Z
modelActions.createFActionForEachHomogeneous$call( D- Z$ w* R: @& k$ j
(heatbugList,
# c% m% }, J5 _8 i new FCallImpl (this, proto, sel,3 d( i9 Z2 N8 h
new FArgumentsImpl (this, sel)));
1 r% s/ O. K/ s* b' L } catch (Exception e) { ~8 d8 P8 `& P5 \7 @0 W/ L& @
e.printStackTrace (System.err);
% n* F" D4 c& M6 n2 Y; h }
8 x6 f* W6 T& i+ b/ W
I, U1 I& e2 [9 \- G# b7 x syncUpdateOrder ();
+ s+ d: d* Q/ h1 M' z5 W/ z1 p5 p0 T# M3 Y
try {& @1 U' v7 L5 G. S
modelActions.createActionTo$message
- T/ ^+ c% r- }! @ (heat, new Selector (heat.getClass (), "updateLattice", false));4 Y& q" q1 @" P/ z
} catch (Exception e) {
# l; ?' Y2 G1 g9 k System.err.println("Exception updateLattice: " + e.getMessage ());: E, ]% e3 X' V. @% ^7 {, Q9 V
}- a# ~/ @$ i& M/ l( i' V
, J# Q0 I% G2 Q! Q+ @
// Then we create a schedule that executes the
3 ^' Z8 `; X4 c' k$ X, y' i$ A // modelActions. modelActions is an ActionGroup, by itself it
8 J* n0 M" @$ O# D1 x, u) e: l* Q* B" @ // has no notion of time. In order to have it executed in
! a( u. ?( _, P) D; @3 _; w5 e // time, we create a Schedule that says to use the" V1 z* [! ~: g; A: s ^
// modelActions ActionGroup at particular times. This0 f4 E2 Q% s1 l
// schedule has a repeat interval of 1, it will loop every. H: T! A- ~3 \$ L3 n
// time step. The action is executed at time 0 relative to
* Y% W, x5 p' ~0 h* t6 k, A4 G // the beginning of the loop.+ o/ @. z4 A$ N; i D! l2 Q
( d% ^! _1 t. p7 O- l1 V
// This is a simple schedule, with only one action that is8 a3 }6 r# m& @" _
// just repeated every time. See jmousetrap for more
" A. O- u5 N! e, V // complicated schedules.
, ], @$ y% G: V, d; j' H
9 a8 m, H8 W% l) e6 }& p modelSchedule = new ScheduleImpl (getZone (), 1);1 [' r" f6 L# ]" l5 l0 R2 h
modelSchedule.at$createAction (0, modelActions);2 `! o4 V! `4 c0 J# o0 R; g" V( C
# t$ r9 ?: Y! `. C% t% R
return this;
% t( v% y& H1 v3 L1 B+ |" v* }$ L } |