HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
a( `( k Q' j. S9 \ E' Z Y" }/ W- {
public Object buildActions () {- t* T0 q, \9 ?/ p
super.buildActions();! G5 @" L, f: Z. X) ^/ N
+ L. G' A* Z/ U // Create the list of simulation actions. We put these in2 t% j8 u' Q9 Z& K
// an action group, because we want these actions to be3 y6 u6 ^0 e2 L, J5 x" n& n
// executed in a specific order, but these steps should
* U; j8 B9 R1 q) e* V4 \! ?5 A // take no (simulated) time. The M(foo) means "The message
4 Z6 t7 U9 g) e- ?; \ // called <foo>". You can send a message To a particular
# N9 M: Y) X5 x5 `: L+ Y // object, or ForEach object in a collection.
6 @& I2 @: J# w- i& K3 @' r" T F' i" l7 ]% k% D
// Note we update the heatspace in two phases: first run+ A8 [" M; ?$ h) O
// diffusion, then run "updateWorld" to actually enact the
' T2 G5 a9 _* [$ Y // changes the heatbugs have made. The ordering here is
* l' Y& i4 Y0 S* Y! }$ Q // significant!5 h4 X' r8 t" K* i% B; F
% x: _& z! X% c$ J( |& S
// Note also, that with the additional
7 _; o: d& {% N4 S- F6 u' b- u // `randomizeHeatbugUpdateOrder' Boolean flag we can
) O1 e) ~# T1 A: c9 N1 ^2 i8 O7 q // randomize the order in which the bugs actually run
h. n/ Y# R9 O# y0 m // their step rule. This has the effect of removing any1 d2 D6 }/ N) X" ^
// systematic bias in the iteration throught the heatbug; O6 H9 n6 n7 T: w o+ i1 L5 s, g* H
// list from timestep to timestep
- j* f2 q2 z9 U C |0 F2 y4 O $ Y2 x# X9 i) Q" I- n9 I* P/ [
// By default, all `createActionForEach' modelActions have
* D% n$ v: c4 z# I7 T // a default order of `Sequential', which means that the
- F8 `( x0 M% N1 e1 P0 b // order of iteration through the `heatbugList' will be8 Y* X6 H |. V9 Y. C! w0 v9 ^
// identical (assuming the list order is not changed
+ c; p, X }. S5 e // indirectly by some other process).4 i# Z1 g* f7 t w' D5 T
& @8 {' R1 u% d: C% r modelActions = new ActionGroupImpl (getZone ());( ?& P% Q7 S$ [6 K1 [% }5 |/ l
( v% a+ ]) y! X y try {8 C9 U$ O# d, |$ i
modelActions.createActionTo$message
0 n+ D- Q! O$ T3 H$ T (heat, new Selector (heat.getClass (), "stepRule", false));8 V, L7 }4 J- R7 A8 @2 T
} catch (Exception e) {
5 Q7 U8 m2 o1 I System.err.println ("Exception stepRule: " + e.getMessage ());
1 U/ @! Y- r# s/ r$ t8 b: g) [ }
8 Z8 r1 J9 t, y7 d7 {/ G7 s( F$ I) l% T0 j5 M
try {
" V0 v* q- J& E+ S6 I, Z Heatbug proto = (Heatbug) heatbugList.get (0);8 K7 N, ~, u: }3 ~4 r! _
Selector sel = 3 @% \- }& C/ e2 r, N, j% o
new Selector (proto.getClass (), "heatbugStep", false);6 I' X9 n5 g/ J9 m* q- D! X8 d
actionForEach =
9 t# L1 N9 j. k; T2 ~5 P modelActions.createFActionForEachHomogeneous$call( l: z( k3 a3 b/ O+ Y2 E& s1 g
(heatbugList,7 D8 Z( a9 k+ Q+ _
new FCallImpl (this, proto, sel, K9 Z2 O: v' N6 a2 s; o" r
new FArgumentsImpl (this, sel)));
# ]" S" o) Q* H; M3 h& m6 I5 n } catch (Exception e) {
" | }. w- F1 z$ X- ? e.printStackTrace (System.err);9 h! ]6 O" d9 }/ Q- ~
}2 R4 y2 E" J; s6 T7 K- a3 {! h
$ R1 |$ [5 u0 M9 }4 |- w syncUpdateOrder ();
# ~) c/ ?( k% W0 }' K, z6 a# h, B+ L7 R1 v8 i
try {
' _# R0 j" {! Z6 R; { modelActions.createActionTo$message
3 Z' D6 {$ K! e1 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
4 p3 f6 G+ `" [) D& V } catch (Exception e) {$ S* d/ B+ I Z; X; {& Y
System.err.println("Exception updateLattice: " + e.getMessage ());
5 p$ e! E2 Z' ~( N0 @4 k4 } }
. c8 U; c+ F% E' [# H( y, @
+ p, r. @: f$ l3 Z" p# j // Then we create a schedule that executes the
- e, k* Z6 j0 q" Z( q& j6 j9 C // modelActions. modelActions is an ActionGroup, by itself it3 D( Y+ ?% Y6 _9 G
// has no notion of time. In order to have it executed in
, N& X8 R: ~9 [: Q1 ?; t9 }" | // time, we create a Schedule that says to use the2 x) W8 ]9 p' z; L4 B
// modelActions ActionGroup at particular times. This5 t6 N8 T) ~ `9 s) J, R
// schedule has a repeat interval of 1, it will loop every
2 S# ~" @# ?, q u- u1 D; w // time step. The action is executed at time 0 relative to* ~$ k) e I0 D0 _$ n) u/ t0 V D
// the beginning of the loop.
+ N2 j* ?: o- Z- J, v$ y/ p1 f" u6 ]3 A' M# ~
// This is a simple schedule, with only one action that is
* W8 C( {7 I$ G/ b // just repeated every time. See jmousetrap for more
4 { w; @- X7 P& B% ~$ k& N // complicated schedules.4 L; e( h+ J4 L" { j5 L* U
# M+ k$ I# G7 o9 a* {& U( D4 Y. j modelSchedule = new ScheduleImpl (getZone (), 1);5 X6 k# L$ g0 \) Z: z4 E% X- c7 P4 j- Z
modelSchedule.at$createAction (0, modelActions);
. S3 \" r! w) w+ z9 q# \
! |$ F/ N! E* A/ F4 C; ^8 A! W return this;
" L# _" V, {. B# q! l/ B } |