HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ Y9 d0 M2 x2 [8 Q9 j, |, L0 ^( u, D
public Object buildActions () {& c+ b9 @6 ~8 w; n9 } K
super.buildActions();
; _ {8 e: }% s! w
# W7 L/ S- ^7 L ?2 t0 h // Create the list of simulation actions. We put these in: H7 ^0 Z6 o2 z/ U# I
// an action group, because we want these actions to be# B* o* R: [1 d
// executed in a specific order, but these steps should) c5 |3 n/ P6 H0 |$ V& O$ u
// take no (simulated) time. The M(foo) means "The message
( E" N' j, ~& T7 C // called <foo>". You can send a message To a particular
2 ^6 }3 v! z0 k) I6 F, H4 N$ f // object, or ForEach object in a collection.- p( {3 ~+ Z7 i$ \
6 N$ J" O- g: A! w // Note we update the heatspace in two phases: first run/ R- Z3 x2 J% Y# a/ E. }8 `
// diffusion, then run "updateWorld" to actually enact the
& |8 L7 Q2 i7 q+ i2 ~8 Q // changes the heatbugs have made. The ordering here is
3 q( _2 P g h/ L B& f/ w // significant!& H' |, o+ c3 A) R4 L% b/ ^1 [
/ k/ F! G8 V1 L! D" W
// Note also, that with the additional9 C3 C) A* L2 c i: Y* L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- B+ e' F V& m j, f3 v // randomize the order in which the bugs actually run/ U6 M5 t: x+ S4 U
// their step rule. This has the effect of removing any/ E) N* z; M3 Q6 Q: s* p) k
// systematic bias in the iteration throught the heatbug H/ V- w* r! W' s, Z
// list from timestep to timestep
& v3 ?) X2 b& I0 r z# ]5 k+ {
) b# J; X$ B0 P; c1 B // By default, all `createActionForEach' modelActions have& f, u" k- s% E. Q, {: r0 e. E
// a default order of `Sequential', which means that the/ A2 K2 @1 T6 e2 U) d& i; y
// order of iteration through the `heatbugList' will be
6 D3 g7 u; D" `1 Z$ U1 z$ B4 A' ^* _ // identical (assuming the list order is not changed! J. r, F+ J1 y' n
// indirectly by some other process).
0 s5 ^- L: x8 s+ \* y- y& c4 \' G , ~- d( F. Q8 R* N
modelActions = new ActionGroupImpl (getZone ());) ^6 d$ q2 W$ a6 |8 p
. X0 J( q4 L; U4 a try {
# c8 N; w2 B A! R modelActions.createActionTo$message4 p/ w# |; X: f
(heat, new Selector (heat.getClass (), "stepRule", false));$ ?2 @" \. ^" {, _0 }- E
} catch (Exception e) {- N! x1 H h* j2 p! q3 I+ _
System.err.println ("Exception stepRule: " + e.getMessage ());
/ @. q) f0 y8 c }
2 \4 v. I4 n7 X8 ]2 I% P, c. T/ C T \* O8 a7 @
try {* V+ }: d4 B |5 ]! N, V
Heatbug proto = (Heatbug) heatbugList.get (0);
H3 E$ b1 l3 W3 Z M4 U Selector sel =
/ [6 K1 o- y$ w. T: c new Selector (proto.getClass (), "heatbugStep", false);
/ A A. ]8 p, O# c0 S actionForEach =
2 k( f% @! q+ n* s7 v! o modelActions.createFActionForEachHomogeneous$call
- V7 G+ Z" N. ]! j+ O O (heatbugList,! p# J. m: G5 h3 c! v6 Y
new FCallImpl (this, proto, sel,
; k& v' ^) y9 y2 x. J new FArgumentsImpl (this, sel)));; d. D* R/ k3 I8 i5 T/ B. @7 I
} catch (Exception e) {
- r0 j- i7 L- P2 L e.printStackTrace (System.err);
; z* t: i J5 ^! [+ Y) C/ t I }) G) ~* P" M: j! B* o0 X3 T+ L* v
) M2 z; S: @- s% L) U# I$ O5 i7 } syncUpdateOrder ();* J" C) U' {& Q, X8 s* \) e
. z, R c+ B. ~2 P, U0 `( ]* m* ^ try {
0 w$ n& z, A$ H5 L& r; n3 c modelActions.createActionTo$message * `" j& {( L; U* f+ Y
(heat, new Selector (heat.getClass (), "updateLattice", false));7 Q9 a8 a/ d2 D3 H: _" i( o/ K
} catch (Exception e) {" ^2 }, U2 t8 ?: n$ H) r0 m
System.err.println("Exception updateLattice: " + e.getMessage ());, r* g/ n1 c9 r( R9 C
}
) {: X+ O6 q3 B( j& f* M, E
% O) o- l" p. X8 {/ { // Then we create a schedule that executes the$ ^- l, a; G; Z+ D6 o
// modelActions. modelActions is an ActionGroup, by itself it
7 f7 G/ D- ?" T: V+ D, @. D // has no notion of time. In order to have it executed in
2 \ e2 X* h+ ~6 l/ A* h // time, we create a Schedule that says to use the
6 ?7 k+ S" K; [# h1 N' P) C# g% t // modelActions ActionGroup at particular times. This
$ N O' x Y# Q7 o) _1 v5 B // schedule has a repeat interval of 1, it will loop every/ z, D- \8 V0 n9 h4 Z- z' T
// time step. The action is executed at time 0 relative to+ f9 ^3 m) i! v# U& J# x, [
// the beginning of the loop.! d$ K9 Y6 W" b/ ~
& O0 m$ M8 J$ y# `5 T, L2 W
// This is a simple schedule, with only one action that is
2 b, Y4 ^. T% O- Z" C- O& b // just repeated every time. See jmousetrap for more
4 H n5 E, m- X- l0 R# {$ D4 W // complicated schedules.: Q- {) o( A9 ^' V9 n0 @; N
, R9 W. P7 \- m, o5 F* M8 n
modelSchedule = new ScheduleImpl (getZone (), 1);
4 f+ F5 V8 K# ~) c- Q& b" q0 \ modelSchedule.at$createAction (0, modelActions);, i2 W4 O: t! I( ~$ C
9 o* T5 H" Y2 F$ m: w! Z& J" w
return this; u$ g. ^7 F5 K, ^$ ]6 |
} |