HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& B: T l' \. M) G, b0 w, b0 Y& H* d8 i" S) @
public Object buildActions () {1 d D( h% U3 N9 L
super.buildActions();' w2 P9 k1 N% y( N
& F. Y2 ~( ~$ K% a2 q- ?) @
// Create the list of simulation actions. We put these in
0 v5 Z( F @1 \$ {6 w2 b( J // an action group, because we want these actions to be+ e# a% Z/ I) Q/ Z- d* _
// executed in a specific order, but these steps should
* k j f) k# S // take no (simulated) time. The M(foo) means "The message
0 r# P5 s& U, y9 E9 \4 A2 i // called <foo>". You can send a message To a particular6 V( p# C L" k. y
// object, or ForEach object in a collection.4 S: k9 |7 Q1 o% x" F2 T% @) P
' J6 |$ {7 Z. i. _# t0 U6 I // Note we update the heatspace in two phases: first run
$ n% X0 N/ D; \0 g+ d3 ~ // diffusion, then run "updateWorld" to actually enact the
. s1 @$ q! w7 Y H // changes the heatbugs have made. The ordering here is
; `. N5 i0 O6 ?+ R. I1 ~; n // significant!
/ L$ f# W: `! z# i : ^6 q2 G% y/ M& S( I5 l
// Note also, that with the additional- ?5 \% p2 v0 j' U
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 r& _: {6 v0 ~7 A7 ?/ } // randomize the order in which the bugs actually run' t9 T$ y# G% q; k% c. e$ _
// their step rule. This has the effect of removing any, u- A" ?: T" E8 ^, {/ t0 i
// systematic bias in the iteration throught the heatbug
4 h# k0 A3 I. X& ^ // list from timestep to timestep: E7 |; _2 n6 A& U( G
& Y* q5 t) d6 t8 I
// By default, all `createActionForEach' modelActions have* e+ d: W: u; f. s: u
// a default order of `Sequential', which means that the& W# Z P, U# d; Q2 K
// order of iteration through the `heatbugList' will be# v- h* s ~$ c$ c
// identical (assuming the list order is not changed$ c6 F5 W4 s& {9 `; u
// indirectly by some other process).
: w; Y7 c$ q: l 4 Z: d! L/ D! G. \$ @' ^
modelActions = new ActionGroupImpl (getZone ());
1 j, [- X4 X! b' A( ?! Z" l3 i6 o% J4 |
try {
) Z' p" m9 O+ y modelActions.createActionTo$message
7 O1 l5 Y7 E5 d& S+ @! Z1 z" C (heat, new Selector (heat.getClass (), "stepRule", false));
! ^1 S7 R8 z" v0 S# W/ e } catch (Exception e) {4 [: @) [ W9 _8 i$ [) P
System.err.println ("Exception stepRule: " + e.getMessage ());; B$ x) f" J. X4 d
}* Y/ D; D( p K
6 r( h' K, c3 {5 [
try {" B$ e+ E8 J4 W1 o* O
Heatbug proto = (Heatbug) heatbugList.get (0);3 `8 M9 f2 ?, y0 S W ^
Selector sel =
: s" A* ~0 K1 o6 t8 F new Selector (proto.getClass (), "heatbugStep", false);* J: C3 r6 |+ y% B
actionForEach =
, b- E9 V$ [ @! q5 v( E2 {" [ modelActions.createFActionForEachHomogeneous$call; M3 ^6 n8 L; m5 W# v0 G
(heatbugList, ], t$ A c) Q! r V( C
new FCallImpl (this, proto, sel,
3 `! v, y# N- c1 }, J8 t. e new FArgumentsImpl (this, sel)));) Z' e- t, a/ k/ e @
} catch (Exception e) {
4 q. N2 N& \4 K T, N$ f% h0 z4 | e.printStackTrace (System.err); D+ H, r8 D: }; M0 n+ o$ G
}
" ~8 c" o; ~7 X! i
' k5 k; W' ~: U8 a syncUpdateOrder ();3 d( u3 Q+ x3 N
' r: B. F; B0 P7 q. Z+ q try {& C0 Q$ v" S& E4 e: [4 q
modelActions.createActionTo$message
/ O) t9 f. b( w$ ?8 b6 o (heat, new Selector (heat.getClass (), "updateLattice", false));
, i1 W7 x+ W% A2 \3 X& Q! N } catch (Exception e) {/ Y8 C J! U4 k' f+ g
System.err.println("Exception updateLattice: " + e.getMessage ());; s. G. h" z7 `1 ^( _5 Y) P
}
- S+ B: u0 l+ g: ~$ N: a 5 p5 c3 R4 |2 h- D7 S8 B5 p
// Then we create a schedule that executes the( E( R+ R) I6 G m5 p: k: Z
// modelActions. modelActions is an ActionGroup, by itself it
3 k" t0 ~+ c9 h // has no notion of time. In order to have it executed in
f/ Y+ n, P6 R8 j // time, we create a Schedule that says to use the
- {: L8 u1 D" F // modelActions ActionGroup at particular times. This
8 V& G9 ?4 P. w // schedule has a repeat interval of 1, it will loop every
+ @" H6 U, ]# y- P2 z8 g // time step. The action is executed at time 0 relative to
, M8 w( U& A/ F/ N6 Q9 F // the beginning of the loop.$ h3 \3 S) Z& Y
9 b8 f$ }, Y z3 _3 q$ B
// This is a simple schedule, with only one action that is( K/ _) t+ |6 F/ p: G k' D
// just repeated every time. See jmousetrap for more
( ?9 Q! z; [, q6 q4 k" q+ j. o // complicated schedules. J2 m) g+ ^5 X
\, j5 a5 }: q& D) _! G& y
modelSchedule = new ScheduleImpl (getZone (), 1);2 p5 X; w# C% {) l) p
modelSchedule.at$createAction (0, modelActions);
+ w; c, G8 P3 c: n! ?3 x$ \3 X8 e7 E - ?! W* z5 z' j3 \! S
return this;
8 a- t% h- F- l } |