HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ U# `2 \# N" u; ?5 f( K4 W% r/ s8 A+ l( c4 B0 N# Q3 @! R
public Object buildActions () { n, e3 u8 d6 r- I' X: i. T
super.buildActions();
( W* T O C& w L8 j+ C( {8 `; t& j: E- l8 f
// Create the list of simulation actions. We put these in* X8 L2 O/ C: p8 i' W, U; r
// an action group, because we want these actions to be
( A! `0 _% ^/ v. A$ p // executed in a specific order, but these steps should
* m* h9 S, W- a+ `5 y& p // take no (simulated) time. The M(foo) means "The message
7 }' K- L1 D9 V9 [( @3 v: S6 [ // called <foo>". You can send a message To a particular6 e" Y, u9 B9 w: h' z! S. ]+ Q
// object, or ForEach object in a collection.
5 z& h! f) E- C3 g : |' ~/ g7 n' W" v: S
// Note we update the heatspace in two phases: first run
+ K9 ^/ M! |( D) i& } // diffusion, then run "updateWorld" to actually enact the- v3 k& I% H% ]
// changes the heatbugs have made. The ordering here is
% \1 |" g, k( Q // significant!, E) K9 n$ R: g
8 i5 q! k9 `9 Q1 p1 A // Note also, that with the additional
& O: \% T; b$ j* r# B // `randomizeHeatbugUpdateOrder' Boolean flag we can' V, f& z3 N4 I4 j8 b$ |, O( _3 }8 U
// randomize the order in which the bugs actually run
* Q8 D8 }) C ?* f2 | // their step rule. This has the effect of removing any
1 D V/ r& O8 T) ]; w$ E // systematic bias in the iteration throught the heatbug+ Z! T/ `2 s$ v* K4 u
// list from timestep to timestep
/ [0 ?# p: W _6 M
% ~% `, K0 n( R! h$ N' [! T0 C+ S# q9 D // By default, all `createActionForEach' modelActions have
3 R% W% [& s z. A // a default order of `Sequential', which means that the
+ A2 M6 E% j' D7 i0 p: ?% Q+ a // order of iteration through the `heatbugList' will be
v S+ t4 C6 {. K: R // identical (assuming the list order is not changed& F! h( r* W% ?: e( E6 t$ j
// indirectly by some other process).
U1 L7 c$ n3 u& l9 @7 ^' T# o, A0 p
+ ]7 m4 d. c q/ b& F modelActions = new ActionGroupImpl (getZone ());' R. ]/ N1 t# V
$ N; Y3 Y$ r) p; Q" q try {2 a0 R$ y `5 g" F+ x+ S
modelActions.createActionTo$message
2 j$ P3 {9 p4 I L' r. F; u n4 r (heat, new Selector (heat.getClass (), "stepRule", false));# f$ U# Q3 Z8 _) @! G! q
} catch (Exception e) {2 C, U/ p2 l2 n; ] f5 Z
System.err.println ("Exception stepRule: " + e.getMessage ());# Q+ y4 a, d- f5 J2 K
}& _$ L- a; _ Y* N
) s- O6 O* G: ~& s3 x/ O
try {; d& p5 A" n8 W( ?
Heatbug proto = (Heatbug) heatbugList.get (0);" h9 ]6 t4 z3 m( N6 W% u+ Q" n3 H, T
Selector sel = k( @+ ]3 \' C# j3 b: S
new Selector (proto.getClass (), "heatbugStep", false);
4 @ s* P: |% Z/ r2 l0 e actionForEach =- p( ^ K: M" c+ F1 J
modelActions.createFActionForEachHomogeneous$call
; K5 p& c1 L3 b4 O4 N( M (heatbugList,
8 r, D% ]. z, [; ]# Q1 X5 { new FCallImpl (this, proto, sel,
8 m: a' N$ v. z9 C new FArgumentsImpl (this, sel)));
: D: r- I& `$ `4 O/ @9 Q } catch (Exception e) {& U- P2 h9 M; B
e.printStackTrace (System.err);
& Z2 L% U. z, [$ _! T; n {/ E }8 D; A* Q1 h5 ], S, C J
7 j6 m. T3 C+ |& F$ `; j( p; W syncUpdateOrder ();
9 d& g$ A$ u& S! }3 P8 L& s, I) Q7 K. D1 Y+ g7 F3 ?
try {5 q5 {: U |2 v3 E; \8 t
modelActions.createActionTo$message - X9 m& x6 [0 p, }6 o( G6 r
(heat, new Selector (heat.getClass (), "updateLattice", false));) H. X* X2 t1 T ]1 G
} catch (Exception e) {, v6 n) e# }' {: B$ F5 ?" O5 Y
System.err.println("Exception updateLattice: " + e.getMessage ());
; t1 |2 J8 k+ d* X2 r5 U" S }
; M$ S) {7 X# l 5 u2 |, J8 D' {$ s% d# L, t0 ~
// Then we create a schedule that executes the
7 i& W4 S& r8 R3 w. @- C // modelActions. modelActions is an ActionGroup, by itself it4 g2 Y# _7 d8 }2 A; @/ m) T
// has no notion of time. In order to have it executed in- A) G1 t! n% C! g5 H" u& Q4 n
// time, we create a Schedule that says to use the& U. @; c9 F; d% t! I, \6 S
// modelActions ActionGroup at particular times. This
$ m) ]3 c$ F$ e! J" O9 H8 } // schedule has a repeat interval of 1, it will loop every
9 K4 @) Y: Z+ Q y4 _ // time step. The action is executed at time 0 relative to
?5 b( p9 y) u // the beginning of the loop.
7 I* a6 ]5 D6 |7 y/ U/ h# F$ ?- a
// This is a simple schedule, with only one action that is
+ O# k; e; W( z# y8 R$ V // just repeated every time. See jmousetrap for more
) O. v/ M! P" r% I0 \1 { // complicated schedules.
6 I# i6 B7 c! }+ f0 O6 U6 n+ G " B5 F9 { ?7 L4 O+ \
modelSchedule = new ScheduleImpl (getZone (), 1);
4 E9 M3 o/ d! \( ^ modelSchedule.at$createAction (0, modelActions);0 I8 d9 H8 E- Q+ `
, T) a( M; m7 d# X return this;! _, `; }/ c% ~; u* W
} |