HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, m$ k9 I4 g7 ^2 S( F7 H, j# {& F% y- ^; U- E
public Object buildActions () {
0 M: s7 L5 G# M: I5 c/ F" V super.buildActions();- u/ _' w/ H& h( {' l- @4 w
- v( Z! d/ X2 ?! s3 u, g' ? // Create the list of simulation actions. We put these in) @3 H W! V. {
// an action group, because we want these actions to be
6 V8 X1 M( o, ^4 y D) `9 p // executed in a specific order, but these steps should( G$ N; h+ S" R* w
// take no (simulated) time. The M(foo) means "The message; q- r( q0 x, g) D) [- ^
// called <foo>". You can send a message To a particular
" z- I* H* d! H: R' _( W // object, or ForEach object in a collection.
$ O5 m! Z, K* }4 ~+ t" [; j * G3 u0 I6 n0 a3 m& P r
// Note we update the heatspace in two phases: first run
6 P6 ^1 Y+ [3 L- L9 [ // diffusion, then run "updateWorld" to actually enact the
. g1 k8 v9 i5 ]- u // changes the heatbugs have made. The ordering here is
0 T, W/ N! p7 t% H3 ^* ? // significant!
$ c1 x9 w$ ]/ K, @3 h* R
3 z3 K/ O( C& p4 p. `7 V3 Q // Note also, that with the additional4 F0 Y# A* H) h
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: [2 G/ h. L2 Y3 s // randomize the order in which the bugs actually run% ]- |$ h6 x- h, a
// their step rule. This has the effect of removing any( L2 V* T0 c5 G0 A/ \# {
// systematic bias in the iteration throught the heatbug$ i5 V" S5 n/ O# n
// list from timestep to timestep
$ {+ k% g' n& k" _
6 }/ \! E( c, n. g) L! ~: O // By default, all `createActionForEach' modelActions have
# l: _1 ^2 j$ n( J- H // a default order of `Sequential', which means that the* X" A+ H! x# c. O5 ~
// order of iteration through the `heatbugList' will be/ h+ h! H7 ?* p [
// identical (assuming the list order is not changed
* p# _2 l, Y/ B& ]9 | p // indirectly by some other process).1 g3 _1 W- K9 M5 C6 z( W# }
- d3 j. d: @9 A$ v/ A modelActions = new ActionGroupImpl (getZone ());
2 P5 v1 N, {' ^. e; @) g3 O' B
# J9 [3 ]( d' X$ F: u; N$ ` try {
! H/ N5 p/ O+ U' ]& o) M/ ~% W# P' S modelActions.createActionTo$message
7 F* N+ z+ G) W (heat, new Selector (heat.getClass (), "stepRule", false));6 \9 |2 a# ~" S d7 e
} catch (Exception e) {
2 S2 M1 }- b* }- k3 o System.err.println ("Exception stepRule: " + e.getMessage ());* V/ w7 x3 L0 S2 j0 {% T. u3 G; s
}) I7 s: m2 S* H8 M, k
6 p0 R' o$ O! J1 v8 A try {
' H: v7 j) C+ w8 t; m Heatbug proto = (Heatbug) heatbugList.get (0);$ X6 F# V0 u0 e9 |5 H9 W8 A
Selector sel = ( B* K9 F' _! [4 Q; l: i! B$ V
new Selector (proto.getClass (), "heatbugStep", false);
" y% a* v5 m1 c/ B- }' C actionForEach =! R2 y# h; j, b* F4 C9 h
modelActions.createFActionForEachHomogeneous$call
" J1 Q% b; Z* h5 i6 \ (heatbugList,3 V8 @8 ?9 p6 [+ C$ s: h3 y2 d$ ^
new FCallImpl (this, proto, sel,) N9 _. o/ P# l, G9 K+ x E
new FArgumentsImpl (this, sel)));9 w- R2 B: D8 w% C d
} catch (Exception e) {2 Z" \# t- q) e* p1 V) L
e.printStackTrace (System.err);
+ a4 x8 X8 @: Y8 v; i- a2 o }
+ u) B$ o- {- m+ [/ j, q& i2 C / A" y. ?7 O# y' v1 s
syncUpdateOrder ();
! c! b* q+ s1 Z8 ~8 d; y1 ]# F0 O, D$ L3 `2 E' R1 _: ?8 R0 [
try {6 {# z! Z; Q# I* Y4 `4 e
modelActions.createActionTo$message . U- @2 O* D! D1 a. h( j
(heat, new Selector (heat.getClass (), "updateLattice", false));: ~2 Z4 K, o) ~ J9 Q+ D5 X
} catch (Exception e) { V5 W. }8 h( E% E1 N! u
System.err.println("Exception updateLattice: " + e.getMessage ());1 H; C' R$ B6 d- w" S. a- S
}9 ^5 M* z8 _/ @; ^0 S" e
; [% Y1 P$ U3 g& F // Then we create a schedule that executes the S4 ]6 W' U8 Z
// modelActions. modelActions is an ActionGroup, by itself it9 q+ \% o; u- L9 C" }( c
// has no notion of time. In order to have it executed in
/ B( ]; j" \# o( d( _8 @: m // time, we create a Schedule that says to use the
s+ o4 @& ~/ V% I; H3 g" @4 ~ // modelActions ActionGroup at particular times. This: S3 U% c1 d# Q
// schedule has a repeat interval of 1, it will loop every
; [- D4 K r& n, D+ O& l // time step. The action is executed at time 0 relative to
9 g6 b G; i$ G8 _# b // the beginning of the loop.& |% x* d5 |9 G" u( K
, `5 K- G$ c# I# r6 e // This is a simple schedule, with only one action that is% M# V6 z% @: F8 z# t
// just repeated every time. See jmousetrap for more C' N" d$ q& w; ]! }7 {
// complicated schedules.
0 u; L l+ M; f; J
% p) m6 f" P; g, q$ z% n; C modelSchedule = new ScheduleImpl (getZone (), 1);
# Z- v: @$ F9 a modelSchedule.at$createAction (0, modelActions);4 s2 `) B( R4 `4 c2 v1 m/ D. C
. H+ o b( M) }$ G% s [3 J' X- C
return this;) K6 ]$ n7 s4 [3 e
} |