HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 M- ?# J2 v1 D0 J' ~3 k* ~. f. |+ w. k, A
public Object buildActions () {
/ r. y) N4 S* W; Q& p. e super.buildActions();+ `; V: f9 F# ^
b/ j$ B% t% F // Create the list of simulation actions. We put these in
( D; m ~5 M6 F2 ^4 k // an action group, because we want these actions to be
; |/ ?& F' v. }4 t6 N // executed in a specific order, but these steps should! B2 ^9 ?/ t' |) r- y9 w6 w i1 F
// take no (simulated) time. The M(foo) means "The message
6 j# n4 T, T4 Q9 `$ H1 y // called <foo>". You can send a message To a particular
$ s' a4 V8 B! d // object, or ForEach object in a collection.
- |- E6 p, k" W% `; A f% `: ^
! O" a2 ?2 |6 ^, h x // Note we update the heatspace in two phases: first run4 l0 j4 V: V+ W
// diffusion, then run "updateWorld" to actually enact the
3 o5 o7 t$ O W+ z // changes the heatbugs have made. The ordering here is" D, b8 L* L6 m1 {* ^0 O# R
// significant!" d" s8 A& ^$ T; Q6 K
1 b% s0 r4 ~* q# R2 ~! I // Note also, that with the additional
2 O' i0 t" G% Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 ?. U8 \) t/ s- q& | // randomize the order in which the bugs actually run5 V' W/ u$ a7 @+ V: A* s! O4 X
// their step rule. This has the effect of removing any1 V- {* w" j) S+ Q8 M3 u, i
// systematic bias in the iteration throught the heatbug; i/ T5 Y5 N: ^3 H. Y$ g% h
// list from timestep to timestep8 J/ J% G2 m8 ]3 N7 Y2 d
, H$ j1 V' N; f+ x
// By default, all `createActionForEach' modelActions have
% E; h/ M' k4 K" x" q* J3 d( {3 ^ // a default order of `Sequential', which means that the
) q: U7 Q+ E }7 L7 ~3 o // order of iteration through the `heatbugList' will be0 ^$ Z$ b( c3 U# B2 |) @
// identical (assuming the list order is not changed( D; N. v7 Q# r
// indirectly by some other process).
9 t5 K$ Q9 l! e0 ?0 w/ T7 ~& P; Y t/ ~5 J1 z8 t' _# y2 [& T2 M( v
modelActions = new ActionGroupImpl (getZone ());
* R. H1 c u0 u
: |- E% @& D. w+ Q: l+ Q+ b! r try {
! ]' H! w$ ^. y2 t modelActions.createActionTo$message6 F% z4 `/ N- G) Z! d! l
(heat, new Selector (heat.getClass (), "stepRule", false));, R3 H4 p! ^/ F6 p" N% h
} catch (Exception e) {
* H4 t) t" c* O; j) |! G& u System.err.println ("Exception stepRule: " + e.getMessage ());" I5 m t' g; O$ W/ n
}
) [. N& D$ }0 a! A" d; k
6 l- _3 }" ]: b& D try { E' `; W! u- k/ K" Y5 h+ I% A1 n, q
Heatbug proto = (Heatbug) heatbugList.get (0);# y( A6 f I9 I: b
Selector sel = 8 f+ T" J. c8 D
new Selector (proto.getClass (), "heatbugStep", false);
% f& p1 Q. a" F actionForEach =. z* J+ _5 f; j6 z
modelActions.createFActionForEachHomogeneous$call
( d- Y7 e6 q% u1 A( [% r (heatbugList,
" h( ~2 L z% f6 _3 U, N: O& N new FCallImpl (this, proto, sel,
( J* F: |# y2 x0 ~- J3 S8 I new FArgumentsImpl (this, sel)));3 J7 t: l+ U; M& X2 b* F& o/ d2 W' c# D
} catch (Exception e) {) ~8 v! d' A5 r/ A, K1 {
e.printStackTrace (System.err);
) Q U6 d+ E! Y8 u% D }5 ^4 `* Y D: |
& _! P1 n) e; x# W# M8 W! ^ syncUpdateOrder ();
4 U7 _' C4 z8 d. r4 |! S, S- d1 s6 d. j" O& W, O! S1 H/ x9 a7 {
try {
8 Q( G/ j9 l% }# G5 y% \ modelActions.createActionTo$message
3 v. P* p5 A6 c! j+ o( A, \+ v (heat, new Selector (heat.getClass (), "updateLattice", false));
/ q" q! h- E) T } catch (Exception e) {
' {6 `; D2 F3 u' c System.err.println("Exception updateLattice: " + e.getMessage ());
8 C& ^" o [+ r+ J' _' N: ]* a1 m }
8 \% v' z5 ~$ @& Z z: r) i h/ {2 O6 z8 a" M
// Then we create a schedule that executes the3 M1 W- C6 j# [( p/ V
// modelActions. modelActions is an ActionGroup, by itself it( }5 v+ O& g/ p9 \ j v% U6 w! _
// has no notion of time. In order to have it executed in0 ?" P% K& g( j- Z
// time, we create a Schedule that says to use the
* C: Z7 ^4 }% S% T* J7 H // modelActions ActionGroup at particular times. This/ t1 n8 }" s& T' m
// schedule has a repeat interval of 1, it will loop every& o3 m; b8 [( |0 z- S8 w
// time step. The action is executed at time 0 relative to( d0 z. F7 i) f! C
// the beginning of the loop.2 p9 n0 x/ f( q
5 J8 T6 U; Z( {& O$ _* I
// This is a simple schedule, with only one action that is
, z# M0 S; V5 X" D9 g X) q6 z3 n // just repeated every time. See jmousetrap for more5 {& c; x$ _$ P% C/ E) I1 z& ?
// complicated schedules.6 C, D0 _, a* y, ^
8 U8 R$ S. a% i modelSchedule = new ScheduleImpl (getZone (), 1);
a% o4 `" p2 W! ]9 L# d' ] modelSchedule.at$createAction (0, modelActions);
Z8 [: e5 P+ p3 v 3 `" h$ N2 U S5 m3 g- O s
return this;$ H( {" [& y- X# j# q" K0 \
} |