HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 b6 @3 \2 l! T; A
4 W; w. T; a) L5 k; L% j public Object buildActions () {/ ?1 {- I0 ?. U! K% j- ?
super.buildActions();
8 j& s; `- q6 |
" \9 x* `+ Z" @ // Create the list of simulation actions. We put these in
( ^, y# u' F7 y* t // an action group, because we want these actions to be
5 {, B5 }1 \$ f3 { n // executed in a specific order, but these steps should
+ E1 h2 b2 p7 l% w, \( v# t$ S5 [ // take no (simulated) time. The M(foo) means "The message
3 N1 J) }9 R k: _) t // called <foo>". You can send a message To a particular
& d7 C6 _* E/ d! [ // object, or ForEach object in a collection.5 n F3 ]* E/ t# q8 f) Z5 e4 ~9 F
/ c0 C5 j0 R: |# r# u. ]" W; c
// Note we update the heatspace in two phases: first run
5 Y2 A0 d7 G; I4 L/ q& }+ F( u // diffusion, then run "updateWorld" to actually enact the
$ R: W1 M% p5 }! r- O+ Q* ?; e; n // changes the heatbugs have made. The ordering here is
6 q/ P/ r* Q4 X; Z; }' ] // significant!
, g4 a8 k9 r/ X/ w/ q, `. u
& y3 {! J% z1 D. ^( y: m // Note also, that with the additional1 g* V9 d/ t- f' E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 j# Q1 a* `+ |( v0 Q6 \& L% F( l% a // randomize the order in which the bugs actually run
1 G/ V6 w8 @! V; M- | I' f' O+ P // their step rule. This has the effect of removing any4 H- J0 A; A Q" k c6 ]9 G
// systematic bias in the iteration throught the heatbug
" j( O1 m2 I3 n) k7 l% q // list from timestep to timestep6 p# s+ w; ~4 Q1 Z9 h
- z$ C, a* r7 U& u5 |
// By default, all `createActionForEach' modelActions have
8 g: F0 |3 G% K3 O( D7 Q // a default order of `Sequential', which means that the
# K/ `5 W$ ~% s: { // order of iteration through the `heatbugList' will be/ k+ k) f6 j4 U- A
// identical (assuming the list order is not changed R i; O: ]& c) s
// indirectly by some other process).
3 k: C0 @7 K2 E5 ^ , j$ Q! ?7 V" ~0 V
modelActions = new ActionGroupImpl (getZone ());
% b6 v2 R9 A# K# o2 A2 D' M0 D, K' n
try {$ _3 ^* E# e, K- }0 C
modelActions.createActionTo$message
8 d( z M) N' R1 @* u" x; n (heat, new Selector (heat.getClass (), "stepRule", false));
+ i E, l, I+ C4 y0 R2 U" P/ j& N7 [ } catch (Exception e) {' Q/ ]7 I, o, w& Z8 [) D
System.err.println ("Exception stepRule: " + e.getMessage ());
1 W" L& N$ R4 `2 [4 x9 q3 y }' v D8 O4 S% @$ m1 L) ?. @( s( Y$ f4 s
* o7 v3 V2 X* U) ~
try {9 `8 Z. J5 {# [9 }( ^2 i# r) D
Heatbug proto = (Heatbug) heatbugList.get (0);
1 \: c' O& F1 w3 Z+ ^5 ? Selector sel =
6 K5 m& m$ F' p9 D) ] F new Selector (proto.getClass (), "heatbugStep", false);4 _& b- y8 X t8 w: P r% K
actionForEach =
' m9 y: Q( m- y" z# S% f modelActions.createFActionForEachHomogeneous$call
7 u+ R: j4 k! x (heatbugList,
1 A: Y0 r9 a- Z new FCallImpl (this, proto, sel,; {; ]% f# e& G
new FArgumentsImpl (this, sel)));
3 f- H/ |4 H6 I2 _( t$ R; |: ? } catch (Exception e) {, F H9 d( d8 f, z( F! ?" S; D/ H& m
e.printStackTrace (System.err);
q2 b: ^6 |& ~4 i" z1 V1 n$ v }* w. D+ s9 C" N2 c( p$ x7 r: P: s& M
* O K; j5 ?* K; x+ B
syncUpdateOrder ();$ F' n7 s+ H: |$ u3 {
! B( l4 r3 j7 y& K$ }& _# `
try {
3 d& `' \4 F* X" j$ g8 A0 S modelActions.createActionTo$message
3 j" y/ ^& l4 s' X! e# P# E (heat, new Selector (heat.getClass (), "updateLattice", false));
: D5 I* p3 v; w S+ p; { } catch (Exception e) {# I* T# _+ T+ d) b5 N( x& L
System.err.println("Exception updateLattice: " + e.getMessage ());( U* e, d! J# D' s/ k+ g" Y
}& V3 u5 e g0 O& K$ z; T
) X- Z" U8 U( M9 N* u/ |+ ?
// Then we create a schedule that executes the
2 D4 K8 F J# l1 n // modelActions. modelActions is an ActionGroup, by itself it
% x$ U) D' x2 J$ A6 P4 Y, [ // has no notion of time. In order to have it executed in- y: O" M% g7 s. R2 @! B
// time, we create a Schedule that says to use the
R/ V' }5 s9 ]( E% ^ // modelActions ActionGroup at particular times. This
5 H3 t- W8 l# N // schedule has a repeat interval of 1, it will loop every8 _; o3 I' _2 G9 ?' c; E. B
// time step. The action is executed at time 0 relative to
5 ~8 i4 _, S% j' h# d+ a/ q% L! a+ q5 O // the beginning of the loop.
7 R( H* c( s6 |$ d- Z. d
Q3 r6 i Y# I2 G* v6 e // This is a simple schedule, with only one action that is* j- X- o( L6 G6 Z: L1 F2 y
// just repeated every time. See jmousetrap for more9 I4 |/ U( O; R# q& p, d6 S! O
// complicated schedules.6 l0 w& r! V. b9 Q: b. ]
& c) e1 k ]( q1 W1 f modelSchedule = new ScheduleImpl (getZone (), 1);$ G O* F/ V! c1 O3 R
modelSchedule.at$createAction (0, modelActions);
6 J+ m( w5 \" x: K( ?; o- E* W
; T, l5 E4 r* y* N# c return this;
6 C/ x8 J8 l$ @' W4 O; K } |