HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: h9 M8 N6 V0 T9 ^. q: w
( N) ~0 O* t* G0 V5 d( x: u+ o) s: U1 U public Object buildActions () {
: P1 K, I3 _ m3 A, }$ G super.buildActions();
3 E# r5 C5 G$ T# f# z+ ]
% x* ~1 Z3 P+ ~/ u: n // Create the list of simulation actions. We put these in
W% X7 F& z& T // an action group, because we want these actions to be9 Q4 G6 a1 x; q6 P* f
// executed in a specific order, but these steps should( V! O2 e9 M6 |# t; N+ g8 a
// take no (simulated) time. The M(foo) means "The message
% F; M; i2 g& i/ s4 U( f% v$ D2 a // called <foo>". You can send a message To a particular9 M$ Z) }- `( x! V
// object, or ForEach object in a collection.
" \" w) H* |" e. C 4 o$ f" B+ B6 ?7 k0 ~
// Note we update the heatspace in two phases: first run
4 a) |) h- N/ p- |% Z // diffusion, then run "updateWorld" to actually enact the
% l% a0 L! w0 g5 h; q // changes the heatbugs have made. The ordering here is. Z6 i+ ]( n. J* ^% ?- f, p
// significant!& E* O) C/ j$ a7 G' S% ^
1 S9 M) e8 J) W2 S/ ^
// Note also, that with the additional" h+ p6 ~0 p/ n9 ^! A. F5 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can! T- N' Q2 {" M
// randomize the order in which the bugs actually run8 X% D/ |; m: ]# h" |- l
// their step rule. This has the effect of removing any( |2 c. R; t& c0 N" S; |# b3 @5 T
// systematic bias in the iteration throught the heatbug
6 H8 }, V+ L5 |! {' r" `- }2 v // list from timestep to timestep
t" z$ }% t4 e$ d2 Z8 P/ g ' }# M: h& B' l
// By default, all `createActionForEach' modelActions have) E. j* c2 C* h6 |! x T
// a default order of `Sequential', which means that the! E- A6 H' K( |4 C) P1 y. {
// order of iteration through the `heatbugList' will be* K+ U+ }, Q r6 n( J; z H S
// identical (assuming the list order is not changed
; P" e+ t/ ?( Z# V- R // indirectly by some other process).: |% z! h# n* g
% w9 f2 R. ~. p/ F2 V5 r) B modelActions = new ActionGroupImpl (getZone ());
; X4 I3 ?0 u8 a7 b% K
1 P1 M; X5 U. n1 d# U7 ]) @8 t; l try {
t7 r: `8 h+ a modelActions.createActionTo$message
: C( W% ]" V3 R: T (heat, new Selector (heat.getClass (), "stepRule", false));7 r- T3 M0 F! r: h7 L5 ~) P: m7 Q
} catch (Exception e) {: V# {3 s' f/ }: ]
System.err.println ("Exception stepRule: " + e.getMessage ());
+ i/ [5 u" _3 w! n# u# D- g3 V }; o4 G; k& }/ A0 h
5 e1 x/ X: s7 x) k3 F9 m6 k try {
% x! l: e+ ]$ g Heatbug proto = (Heatbug) heatbugList.get (0);
1 g/ [' n+ V3 ]6 [# f Selector sel = ' H1 F& B- O8 S7 |
new Selector (proto.getClass (), "heatbugStep", false);& [# D8 v4 s2 o
actionForEach =
/ }) W+ p6 r: r modelActions.createFActionForEachHomogeneous$call0 U6 [+ W: A, c, F3 ?
(heatbugList,
, z g O; s, |$ c4 d* Y2 ^/ Z. ^# G new FCallImpl (this, proto, sel,
9 S- d* r, {% w @7 Y new FArgumentsImpl (this, sel)));+ y7 H0 J" z2 L7 B( x6 X; F6 E
} catch (Exception e) {0 `1 E5 R6 W, c% X( p
e.printStackTrace (System.err);7 h$ u l/ Y. A6 G4 V$ r1 b+ D
}6 Z7 }5 `, Z; i
5 Z9 c0 E H# x9 c
syncUpdateOrder ();
4 O. c! `6 q- Q" \* O2 G- {- W8 a
; `7 N! I! u( Z9 {# L7 }. t$ a try {
% f- [' u) J, o. x9 E& r' H modelActions.createActionTo$message
( `7 o0 @6 \5 b (heat, new Selector (heat.getClass (), "updateLattice", false));, I9 L" I* {" b6 z- X$ }, |& Q7 ]
} catch (Exception e) {
/ w3 S0 \. N5 V System.err.println("Exception updateLattice: " + e.getMessage ());2 ` G4 L! R, f
}
p$ I; y! [/ t0 s
% j& Y9 D0 Y# t/ G // Then we create a schedule that executes the4 d& f; C6 f* O1 ^
// modelActions. modelActions is an ActionGroup, by itself it, T6 }8 j: r9 q3 S: ~- K+ j
// has no notion of time. In order to have it executed in
$ \% c# s) w8 G0 E. ~) U" T // time, we create a Schedule that says to use the/ A2 \) G' H9 N3 T
// modelActions ActionGroup at particular times. This
1 u' S+ v/ j2 e2 d // schedule has a repeat interval of 1, it will loop every
: L6 \/ W8 @) U( o // time step. The action is executed at time 0 relative to3 k. ?( ^( _# `( n5 G E2 x$ W. U% n
// the beginning of the loop.0 K0 `+ g2 T7 Q
. Z4 B) ~/ L- Z+ b
// This is a simple schedule, with only one action that is
# d ~; Y9 ?$ G( n' q8 y- F$ E // just repeated every time. See jmousetrap for more5 G! @0 O2 n' d0 L8 r/ A9 W7 d
// complicated schedules.
0 g( v9 f% }- c) @! X
7 V9 J$ @8 g! g modelSchedule = new ScheduleImpl (getZone (), 1);! W) r9 ^2 K. q$ [% o5 ^# A' F
modelSchedule.at$createAction (0, modelActions);9 o5 t, s* M1 J0 U' f
; Q; E# p9 V; L return this;) n; L. K4 u1 S4 K; Q/ t
} |