HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( b& h' w: A* d: O) B4 S$ k
/ j4 @9 d) a+ G public Object buildActions () {. `, X) Z: O6 s, |. Y
super.buildActions();
# J. e& R: n* p1 I+ }/ H 5 i8 G: l3 `9 P3 P. q
// Create the list of simulation actions. We put these in
0 c$ }" S; a; }) ` // an action group, because we want these actions to be
" a6 k3 K- x/ E, n6 l) E5 g0 S // executed in a specific order, but these steps should; ]! O' f- |+ w' [
// take no (simulated) time. The M(foo) means "The message
9 N) \$ h, P' J$ ?& o8 d3 S1 F // called <foo>". You can send a message To a particular3 i* [! M3 q9 s* t1 V+ J, Z% v: H
// object, or ForEach object in a collection.
$ {2 T3 V2 |. ]! D- W- \; F6 B : R5 J# Y! r: g; O
// Note we update the heatspace in two phases: first run. a$ j! {3 Q% Z8 D8 m
// diffusion, then run "updateWorld" to actually enact the2 x4 m* ?! g8 A& ]/ a
// changes the heatbugs have made. The ordering here is
; ]! }4 S8 m9 H // significant!' \/ Q- `& t5 [5 l3 y
9 @5 `) D+ x1 u5 ]& d! p) C% d // Note also, that with the additional
! t3 P# G6 b/ O- l7 O' M: j/ I // `randomizeHeatbugUpdateOrder' Boolean flag we can6 j& s K- Z& H/ g
// randomize the order in which the bugs actually run5 A' ~, a6 f) v& t6 H" i9 m
// their step rule. This has the effect of removing any
! Y- A6 R0 p! e# z1 }( C" ^ // systematic bias in the iteration throught the heatbug, w# x* _+ e# ^$ q2 q
// list from timestep to timestep1 t# }8 h2 y: a n% x: E6 M2 P- T0 C
- W/ y5 o" e. b, F% S- j* y; e/ Y$ I // By default, all `createActionForEach' modelActions have- H: I1 n) Z" F6 B. T
// a default order of `Sequential', which means that the
% R8 O6 c/ o! @ // order of iteration through the `heatbugList' will be8 B# N8 v; y @/ [3 b1 k0 B
// identical (assuming the list order is not changed5 a" l$ |" D( O: Q& I) a# J3 u+ O- D
// indirectly by some other process).
' I. u% P, Y; Z! c8 ?9 ~, b
: t7 E" z' e4 f, i/ a3 M- M modelActions = new ActionGroupImpl (getZone ());
; r4 L0 T5 y" n0 }% u Z
3 H$ [9 \5 P, G1 A% ^! N5 R; } try {+ |7 N/ H* @/ @0 e1 L t: }
modelActions.createActionTo$message. G0 D2 x* D- N7 A4 u- ]
(heat, new Selector (heat.getClass (), "stepRule", false));
* u8 O6 }6 x' T } catch (Exception e) {$ t( n1 |* E' Q/ T
System.err.println ("Exception stepRule: " + e.getMessage ());
" ?! Z" U; V$ }. v( W }
% z l# V9 A* T0 z" }- D$ B0 a7 F$ A0 n# X
try {
$ r8 {" @: {& j) j! ? Heatbug proto = (Heatbug) heatbugList.get (0);, e `( s7 ?' P7 X2 B9 ^
Selector sel = , W& \) ?3 d/ R5 r7 u5 @* C
new Selector (proto.getClass (), "heatbugStep", false);) q3 i6 I8 _: C1 R% W2 J+ j+ E
actionForEach =7 |% O: ?5 _9 Y3 T; e
modelActions.createFActionForEachHomogeneous$call
1 [7 Z; `& E1 q (heatbugList,
: \- v( F5 O' f! h; I; J new FCallImpl (this, proto, sel,8 L2 @* o2 k, o( Y0 d( o% u* f% }
new FArgumentsImpl (this, sel)));
8 O9 Q5 ~* k& _" q9 ? } catch (Exception e) {+ ~" i+ z7 p+ W1 M
e.printStackTrace (System.err);
" g' K( F B) n4 P, c }
4 e4 l1 u! c9 C3 C g2 L; O+ |9 j : O/ k8 G/ J3 D' v8 h
syncUpdateOrder ();: z8 `8 F1 N8 J
+ o& W! i4 e5 K5 c1 Y5 L6 ]# J
try {% W' }- `. q! e3 k) v7 B
modelActions.createActionTo$message
2 m( {! C8 \5 M$ D B! \( t (heat, new Selector (heat.getClass (), "updateLattice", false));
a" _, ]( l! L0 h; z, C } catch (Exception e) {
@2 z, t& e( X8 n3 A' { System.err.println("Exception updateLattice: " + e.getMessage ());
# q$ E5 \8 Z* s* Y1 h9 L+ W* ] }
5 ?1 u% m6 N: t* f* L " ~6 Y5 I1 p$ }' h( x0 [9 O. x* K
// Then we create a schedule that executes the
3 \& L; d; V* w+ e7 E* K3 e, t; W // modelActions. modelActions is an ActionGroup, by itself it
5 I: \ b h' x" ]6 R( a' w$ X // has no notion of time. In order to have it executed in
) J! D! G7 ~# |% N0 o! V // time, we create a Schedule that says to use the
. F G$ P/ ^' v; p // modelActions ActionGroup at particular times. This
: |6 G7 J; `7 n# u' m // schedule has a repeat interval of 1, it will loop every
3 L: Y6 T; l3 O& } // time step. The action is executed at time 0 relative to
, g* ~) h4 R. |6 y9 m7 i. I // the beginning of the loop.
3 l! \/ D' k3 S) m6 F8 h; ?9 N
6 T* }* C6 F9 y // This is a simple schedule, with only one action that is. }1 s3 @2 L& Y( ]1 \
// just repeated every time. See jmousetrap for more5 l) E7 X* g6 z+ z- w: {' z
// complicated schedules.: [/ C% r! g+ ?- w9 f" x6 e. m% }
% u/ K( m, M/ E modelSchedule = new ScheduleImpl (getZone (), 1);
) D8 R5 Q* _0 v7 t modelSchedule.at$createAction (0, modelActions);
- S8 U3 S% f! w F6 b/ b 5 i8 D1 c8 Q8 F/ ?
return this;" ?- ]1 b% f r+ G
} |