HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: N |9 ^, r$ b
& Y# G5 \- C4 }3 }' h' I public Object buildActions () {
U, j% b: ?6 G9 q& L super.buildActions();
: {# C4 F7 I; [' _+ e- C6 }4 q9 A
8 [# z" v `, b0 m" z1 ? // Create the list of simulation actions. We put these in1 M4 @; F& T8 N" Y7 L8 E' P. F7 y
// an action group, because we want these actions to be
I: S0 M: a1 j d! ^: t0 Y$ S // executed in a specific order, but these steps should
/ G( l& O5 m& R7 T // take no (simulated) time. The M(foo) means "The message
G7 o' s1 ] E; o6 S // called <foo>". You can send a message To a particular
" ]5 f: e' ^* x! C) v) k6 N3 s# s // object, or ForEach object in a collection.
( P/ e9 H, q% R. k; ^ P- B. x0 h, G8 C% S
// Note we update the heatspace in two phases: first run( C+ V9 O4 t! ]9 L: S! o y5 _
// diffusion, then run "updateWorld" to actually enact the7 E4 A+ a. T+ g8 `; H
// changes the heatbugs have made. The ordering here is6 r! ^5 o3 V6 s9 T
// significant!: V! }3 z3 R* q v7 I0 _- f# B
4 J, U" f3 ~; P6 w& b' s // Note also, that with the additional5 b4 i: Z. I- l. x# Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can Q0 i$ d7 ~( P2 N$ L: A
// randomize the order in which the bugs actually run j( O8 U0 Q2 @6 Z
// their step rule. This has the effect of removing any3 f) W' h* U& e0 }, K
// systematic bias in the iteration throught the heatbug
5 N1 Y4 ^# ]1 L // list from timestep to timestep
3 L; k6 I6 M. E5 j( [ 4 c* F2 o0 _/ E+ w& i' X0 M
// By default, all `createActionForEach' modelActions have, a# Z2 R* `0 n ~1 ^) H
// a default order of `Sequential', which means that the$ J1 C- P/ W* ?3 j& Y4 L
// order of iteration through the `heatbugList' will be
0 [3 b6 L I/ L // identical (assuming the list order is not changed! K4 j2 s0 v, ]$ U }
// indirectly by some other process).% }" S, j$ \1 y1 s" X! ^8 |
R u& N. L% v; f, e
modelActions = new ActionGroupImpl (getZone ());+ A4 A( U9 i- w8 S; n
2 x* u* \6 X& Z try {% J. v5 p [$ x' |4 _
modelActions.createActionTo$message
; m9 d& o. V: d1 d0 }9 W (heat, new Selector (heat.getClass (), "stepRule", false));
# D' Z S' H6 l. c- [ } catch (Exception e) {
: L7 l* ?7 X* M' A$ v7 t; _ System.err.println ("Exception stepRule: " + e.getMessage ());
t. i, ]% w9 ?5 X }9 Y. U: y$ F7 G1 _! V
" o7 j: u# q7 V p0 a+ d
try {
! `* s d" l" }! O# k! m Heatbug proto = (Heatbug) heatbugList.get (0);; c$ s+ Z1 ^( [+ B6 S# H6 U
Selector sel =
% z: ]. X8 l, Y% c$ E new Selector (proto.getClass (), "heatbugStep", false);: n. s# ?) \% b- x C7 L
actionForEach =
5 @6 w4 G9 B: o$ A- n modelActions.createFActionForEachHomogeneous$call
" ]& J6 w1 }* A& e. d6 N7 U$ @8 _ (heatbugList,
' g2 X7 u% O7 i( Y# t7 x( _ new FCallImpl (this, proto, sel,
! \/ m+ |( |' T* a c new FArgumentsImpl (this, sel)));
# A% D+ \; s% g4 |0 p } catch (Exception e) {% e% t7 }7 [8 R# l( U
e.printStackTrace (System.err);2 H* ?( C1 a, j' q9 g4 L7 A# U
}
- \% K" p# r8 P0 Z6 W% h0 q
! T' Y- l! \9 e syncUpdateOrder ();* L8 J* d+ ]0 l' w+ |% T8 B
/ } @5 D# z3 U( ?0 Q/ \' D try {% j% C2 `; ~( L) {; l) W
modelActions.createActionTo$message
m4 O K9 c6 h R (heat, new Selector (heat.getClass (), "updateLattice", false));
8 w& n; t5 W! B% k) ]) R7 z } catch (Exception e) {
& W8 l `, e: f! O& r, W System.err.println("Exception updateLattice: " + e.getMessage ());
2 Y/ m, I, X+ B3 d$ n: t' D }
- p: Y- ]) c* _/ J5 c; T' O + d/ J- j; q! }1 h9 Z
// Then we create a schedule that executes the6 {, [; t4 K; ? S! t& f
// modelActions. modelActions is an ActionGroup, by itself it
2 r( C1 Y. r0 h7 s% t. G // has no notion of time. In order to have it executed in6 |* m: O9 h9 E. \5 S
// time, we create a Schedule that says to use the( d& X2 t9 V* g- [0 A3 I
// modelActions ActionGroup at particular times. This
% E4 R- N% g" g7 E- { // schedule has a repeat interval of 1, it will loop every4 k3 d) b6 P p
// time step. The action is executed at time 0 relative to D# v7 y, }: P& d; h L# K4 r
// the beginning of the loop.+ @# X5 x7 [! L) o
& H8 ?! ?* @. k* Y! @8 P
// This is a simple schedule, with only one action that is
; a/ N; h$ ?6 ~6 G& t // just repeated every time. See jmousetrap for more* P9 d8 {/ e9 k5 W( B
// complicated schedules.
& }: o* P! n$ B9 I : e A, U# ^$ k ~9 i
modelSchedule = new ScheduleImpl (getZone (), 1);
7 `* |) O% j$ f" h, R modelSchedule.at$createAction (0, modelActions);
! T/ o! q3 s O$ A9 _ 6 ]! k+ U8 F0 u5 @5 h! v! k1 U: A: |
return this;6 h0 d- o3 u; m8 D4 [* G
} |