HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: j+ C- ` b6 d" Z
* _4 N3 V' e* l
public Object buildActions () {
6 T- M- y* U' G9 k- J5 H- V$ t super.buildActions();
" B" k+ M' ]* g9 T0 N 0 J" X$ a8 C+ v! z6 j
// Create the list of simulation actions. We put these in
& f/ {$ c0 y: ]' n4 | // an action group, because we want these actions to be
, r0 u4 t. y' j' Z3 ?* | // executed in a specific order, but these steps should
* c6 t* N% q7 [3 |0 | // take no (simulated) time. The M(foo) means "The message
6 J* L0 s+ h2 Z7 i/ U6 A // called <foo>". You can send a message To a particular: |+ p% R; p) c' Q3 ~
// object, or ForEach object in a collection.
. H5 x. s& q. l3 b+ V! ? 6 k/ y& U: L4 `* E1 E& `. A4 k( y) N4 }
// Note we update the heatspace in two phases: first run
m" t# ?% N5 {3 w2 U# ] // diffusion, then run "updateWorld" to actually enact the
/ A8 E$ i8 O! K* D: i( u // changes the heatbugs have made. The ordering here is
1 D( c; E& z" s8 q2 W# P! k // significant!
" l0 \- g9 F0 [. `6 Z, E r7 m- v 5 {/ i% |3 t4 I9 E1 c y+ ^5 r
// Note also, that with the additional
7 c! s+ s8 H/ r5 q3 a+ E( g0 U // `randomizeHeatbugUpdateOrder' Boolean flag we can7 ?0 l; Y3 H& O1 ` A4 `: \( a
// randomize the order in which the bugs actually run
: T6 G. x# K. v2 U2 l& T* u: W+ U- x // their step rule. This has the effect of removing any' y- e3 W+ b/ |1 J1 H% X
// systematic bias in the iteration throught the heatbug
& |7 a4 C2 k% U/ e9 S // list from timestep to timestep
3 x% E1 a# ?3 b1 C4 O* x( y
% Q) B& @" s3 R# u: Y // By default, all `createActionForEach' modelActions have
6 `$ D# ^2 ^, c, H0 Y/ c5 k // a default order of `Sequential', which means that the
, ~) \( R+ A( @7 c& {1 ? // order of iteration through the `heatbugList' will be
; Q6 r4 {+ c: i8 p: h // identical (assuming the list order is not changed
( R) P! V" D1 O* n& j // indirectly by some other process).- ^0 t, W7 N6 H! R. |% Y7 H; C
/ N6 `, O& c& s* I. l
modelActions = new ActionGroupImpl (getZone ()); ?3 W& F; K5 d, r/ D
. T0 O3 B# ]) W0 k- }1 j( e3 g# P
try {. T, ` d- X, Z, W
modelActions.createActionTo$message
0 Z; e9 C1 u( m6 R7 T z+ c) t (heat, new Selector (heat.getClass (), "stepRule", false));1 G4 x# @$ J: U% I* C5 x5 u
} catch (Exception e) {
9 B: _( W4 e$ W6 q/ n System.err.println ("Exception stepRule: " + e.getMessage ());
& K& c" G' ]% M7 ]( v9 N }
8 \* g. M: K3 Q
' H$ F/ Y5 G# W( Y( } try {$ A6 u0 p4 ?, d. _2 a' u
Heatbug proto = (Heatbug) heatbugList.get (0);
% C* l( ^, Z8 q Selector sel =
; ~7 R' _; y- p0 A! e# W new Selector (proto.getClass (), "heatbugStep", false);
+ z9 x) O& _$ Z! ~% x. P4 n7 @. e actionForEach =
4 \- c: c3 \, L1 Q; K* y modelActions.createFActionForEachHomogeneous$call- I" i$ U, K2 G' w: Q3 ^) s2 v
(heatbugList,, S9 f: \2 `& L+ O4 R3 u
new FCallImpl (this, proto, sel,
! l7 \* u2 z, W! I8 L" f3 F new FArgumentsImpl (this, sel)));. Q% G! E# `' x( L; e$ O. N
} catch (Exception e) {
3 O/ B4 n. ?! T e.printStackTrace (System.err);
* F" D" _: s9 q2 r; B! s }" N; j6 G) f- h1 t
) j4 ~4 y0 b5 W/ z* O, b9 m
syncUpdateOrder ();# C0 f4 H' d/ }% ^) a3 V
e5 Q3 I3 z5 U) n' e
try {5 x) A0 S- n4 ~2 j7 b
modelActions.createActionTo$message ; g4 r& o4 Z9 }* v# I! V
(heat, new Selector (heat.getClass (), "updateLattice", false));# u6 }' S f$ d1 H2 |9 x( {9 e
} catch (Exception e) {
6 X- E# a0 U c# l1 A- p* j System.err.println("Exception updateLattice: " + e.getMessage ());
$ {* i2 J$ R# E7 f0 o }
; H6 n3 ]3 g# b3 T/ c
& s, n3 c+ q5 c/ M // Then we create a schedule that executes the
8 c7 w |' h, a, n4 N9 h6 j // modelActions. modelActions is an ActionGroup, by itself it
( q. V, o* X& y% F3 f; ]/ s // has no notion of time. In order to have it executed in
8 M; T" L7 w& j // time, we create a Schedule that says to use the
& B. ]6 E; F/ ~8 ^: t+ E5 m, n1 r // modelActions ActionGroup at particular times. This4 {7 O: F& x5 v) n! Y
// schedule has a repeat interval of 1, it will loop every9 H7 x C2 m; D1 |
// time step. The action is executed at time 0 relative to) S4 @: z4 _2 b& ^ r/ `5 q
// the beginning of the loop./ k+ x# P/ N8 `9 |3 I( ~( {
/ S+ P3 m) c G9 v9 `* b: C
// This is a simple schedule, with only one action that is" k2 T z5 W& A
// just repeated every time. See jmousetrap for more
; B [3 I+ e% }! n // complicated schedules.: T, M, {; o4 b& n- c& w# |
4 V+ L: X/ k( F modelSchedule = new ScheduleImpl (getZone (), 1);, W- L1 [, H- f) I3 M
modelSchedule.at$createAction (0, modelActions);
7 F6 z0 v7 _8 y" ^. h2 D
- U$ p* e2 G8 x' ^) L return this;7 t4 a7 w3 r {% b3 Q
} |