HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 t( w$ x( G/ @1 s, E/ q
9 [8 e& c0 H8 p! S# i1 | public Object buildActions () {( @5 C# J. L' ^) J/ U6 B! L
super.buildActions();
7 V5 r) H! F v
# C3 [0 c+ O- s% l1 R // Create the list of simulation actions. We put these in
. w* p+ C+ i# T% h4 D/ W! V // an action group, because we want these actions to be
" F, }9 z' B- G) w' I, m8 ?. K& E // executed in a specific order, but these steps should
' J7 \5 C9 S6 p5 c // take no (simulated) time. The M(foo) means "The message B8 _/ w/ G) Y1 J
// called <foo>". You can send a message To a particular$ |0 v% }7 L* D: ~8 {% ~
// object, or ForEach object in a collection.+ R. ]7 B' s9 Z/ }; r. W
6 N$ j2 f; v( y$ ~& {# M$ O: ] // Note we update the heatspace in two phases: first run
% Z8 C& H0 e- F" t' Y" B4 Z // diffusion, then run "updateWorld" to actually enact the
/ n! w6 V. P4 G0 C // changes the heatbugs have made. The ordering here is5 l3 N$ J7 o$ A% Z; \# Z
// significant!
- P0 z; _% c; j$ Y
1 y2 J. e6 V6 V8 ?: i // Note also, that with the additional
2 ]9 ?+ @1 W0 ]- k2 K7 a" U // `randomizeHeatbugUpdateOrder' Boolean flag we can/ ~$ ?( F0 d4 M; b1 r8 j4 | W
// randomize the order in which the bugs actually run6 y; V: `* }: E0 S e0 Q! v q
// their step rule. This has the effect of removing any& x9 j" G3 F4 i, i; k
// systematic bias in the iteration throught the heatbug4 t. s: i2 \& U
// list from timestep to timestep: Z" i: G2 g+ x: u# D
. n) y8 U8 D4 l' y
// By default, all `createActionForEach' modelActions have
( w% X3 }+ [! J, q // a default order of `Sequential', which means that the9 M1 R7 m. T# h3 i4 P4 }7 f* N
// order of iteration through the `heatbugList' will be
1 |! {0 C4 h4 Q // identical (assuming the list order is not changed1 d' Y; m# A6 y" _" R; Q% _; }# T+ n
// indirectly by some other process). b4 K \# v2 I" @- d- n( k% ]7 g
- H' ~5 {0 b8 u/ O
modelActions = new ActionGroupImpl (getZone ());) f. F' m' \/ W j9 T' h+ g
* N8 P3 J, ~$ `4 J8 [! o" K try {. B ^& I( G) H
modelActions.createActionTo$message2 R8 n8 _' H6 J% z1 v
(heat, new Selector (heat.getClass (), "stepRule", false));
/ ]7 Z+ ]7 W5 @6 `+ k4 s$ m6 p } catch (Exception e) {
* k% l( c9 m* S0 B System.err.println ("Exception stepRule: " + e.getMessage ());
X2 g; L' O1 O- E } k& V9 G7 d# _) W* p! x6 _
9 c; Z- S7 g# F% x7 ~) X5 k try {
" i! @' \ R$ ~9 a Heatbug proto = (Heatbug) heatbugList.get (0);
% a+ d2 O# _. o" Z, z Selector sel =
: M6 K2 I) w. Q& T3 t new Selector (proto.getClass (), "heatbugStep", false);& u- h: z* [- H6 ]
actionForEach =
7 p3 ~$ ?) w- ]0 y& r9 _ modelActions.createFActionForEachHomogeneous$call7 w; B- {% c7 G2 m$ \8 g! Y% Q
(heatbugList,
& X/ p, E/ w9 N$ X# g7 s, S$ w new FCallImpl (this, proto, sel,
w3 t5 v3 D9 m new FArgumentsImpl (this, sel)));+ r5 M3 g6 m' `% b! [1 b
} catch (Exception e) {
! i; J8 o. Q% E% p6 ~$ l e.printStackTrace (System.err);+ I0 K7 b/ T( \; ~
}1 B2 u' t, C4 a, b$ y' h+ a1 |
' N7 x" K! T, ~ syncUpdateOrder ();
u$ r! o" W% l1 I# z+ a( J# Z- r7 S+ x1 a% c V+ _# L
try {
8 o/ T' b) {! z% m3 W modelActions.createActionTo$message . V ^- P# C5 G& H+ ]) Z" D: n m
(heat, new Selector (heat.getClass (), "updateLattice", false));
. x/ p# t2 x- S } catch (Exception e) {
* }7 e Q5 H& E$ B; N G System.err.println("Exception updateLattice: " + e.getMessage ());. g1 A$ e; A, P9 k: ^
}
/ g' F9 {) o/ W7 O4 B& H4 i2 m @$ d 0 F' z1 u% g: ~# i& s0 h
// Then we create a schedule that executes the
3 g& Z, t! k; s/ F7 t" U! W // modelActions. modelActions is an ActionGroup, by itself it7 r' `0 p7 N ~$ Q) d; x
// has no notion of time. In order to have it executed in
7 K& r% v, j, |4 F% U& k9 o // time, we create a Schedule that says to use the
8 Z4 h J, w2 Z# F8 W9 A# r. x // modelActions ActionGroup at particular times. This4 A( S+ [# |( c- F: V
// schedule has a repeat interval of 1, it will loop every
2 B" n& k" D# X/ V, a) ^+ D // time step. The action is executed at time 0 relative to
6 {# X g) r* C& S( p# j // the beginning of the loop.
8 @8 X! ^' r& e0 G- K: Q e) h) q" H8 V4 j& M' y+ E6 Y
// This is a simple schedule, with only one action that is* A, \& W' H ~: K/ E% O
// just repeated every time. See jmousetrap for more
# [: e5 r5 w9 o% j' |! G' o // complicated schedules.
" y% |; x) s2 O& `- q2 u
% `! X+ u8 f8 g* y. I8 N' V modelSchedule = new ScheduleImpl (getZone (), 1);( c8 y4 f+ G8 q( ^8 B7 F4 Y
modelSchedule.at$createAction (0, modelActions);: U* T% X B' K; T
+ q2 e7 D, z/ j2 L# i return this;0 A% i4 `' A/ U* a" x* q
} |