HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 |2 S2 V2 [3 b3 A; ^
! S3 h( ~$ @4 Y4 N
public Object buildActions () {
0 [/ ^* @8 l/ A* d9 `8 ? super.buildActions();! |/ }9 _6 B8 H% a2 A! ^2 P$ l8 M+ b
5 e% p: |& I) l% I3 t
// Create the list of simulation actions. We put these in
6 X' n: v; R% n6 m1 k% U# } // an action group, because we want these actions to be; ]2 O8 S6 Q6 j
// executed in a specific order, but these steps should
% \: o) S6 a& G( I7 ~ // take no (simulated) time. The M(foo) means "The message* ^' j( s5 K) g3 r1 I- D
// called <foo>". You can send a message To a particular+ `# \! C; w) H. U6 {
// object, or ForEach object in a collection.
T% g% u- B1 o( D* A$ B4 X. X! x
* L% b; B1 m" p) G6 R // Note we update the heatspace in two phases: first run/ Z T/ q" ^ s0 C1 W9 F& R+ M
// diffusion, then run "updateWorld" to actually enact the: y: Q0 ]( e# t- f- r4 O6 m
// changes the heatbugs have made. The ordering here is/ C) K/ u8 V/ N
// significant!
5 _1 \# \# A: P0 ^7 v; d
- M' {' C1 E- e2 D // Note also, that with the additional# j- [! m8 N3 w! C4 L' P4 R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 @0 n3 h8 _7 h6 S // randomize the order in which the bugs actually run
+ l! v7 j6 N( V# k% S: [ // their step rule. This has the effect of removing any
7 U8 ~0 h1 J1 x // systematic bias in the iteration throught the heatbug' C5 c( E9 D/ R8 u- H7 _$ X
// list from timestep to timestep
0 [2 g, V! A" d3 g+ m: g% D) Z
6 Q' ?/ U6 X! e" X, j% Q& s& J // By default, all `createActionForEach' modelActions have
; ^* j2 g) f; u5 b: r' g$ G0 @) R1 L4 v // a default order of `Sequential', which means that the
9 W- w2 r1 ?+ B$ }; | // order of iteration through the `heatbugList' will be
+ r6 a2 {$ ^& C6 R9 e // identical (assuming the list order is not changed
; v# ^6 O' n5 b6 q // indirectly by some other process).
9 H. C/ Y0 Q1 J
9 Q5 @* ?( M0 O1 k4 Z, Y; m modelActions = new ActionGroupImpl (getZone ());
& G# L- @4 v/ C% ~! Y b# F3 q p1 u
try {* h" `, [ H, W. a
modelActions.createActionTo$message
- ~/ \+ b0 d% i4 i8 I% J& h( x7 ?( L (heat, new Selector (heat.getClass (), "stepRule", false));
: K H+ o1 F2 \; h+ v! b } catch (Exception e) {
. y/ M, E; z) e3 g2 T System.err.println ("Exception stepRule: " + e.getMessage ());2 F; p- m$ ^; O2 Q o. }
}. d# C; N2 [" a" z2 w& K
$ {6 u y1 [4 i; ~
try {* T" g4 x1 V! v; F3 L( K; {
Heatbug proto = (Heatbug) heatbugList.get (0);
+ p; Z. y1 [2 f& c# Q6 \# P3 a Selector sel =
/ d" S, Z, H6 T6 ?' V2 z new Selector (proto.getClass (), "heatbugStep", false);0 c: S9 G( r9 B4 B
actionForEach =
6 o! I) G0 P7 M5 G% r3 D modelActions.createFActionForEachHomogeneous$call
T% R8 M" s2 A5 _, A2 r& v (heatbugList,: B9 U R5 ?, r6 G
new FCallImpl (this, proto, sel,
* e7 B% k8 W) X new FArgumentsImpl (this, sel)));
+ p! W: h% G% t* ~ } catch (Exception e) {
9 { L0 T& Z o+ c1 r e.printStackTrace (System.err);
2 q) q+ z& d8 r' h+ p+ z, l0 ?' }9 T% [/ t }
j/ [0 C6 h* }4 z) d2 U 7 d, D. {" G8 i& r/ W) ]8 e
syncUpdateOrder ();
9 H. r: ]8 j8 h7 L
/ M( i6 {% A/ N! T' H$ \9 u try {
" e0 K0 J- { ~, g modelActions.createActionTo$message
- x2 W) {9 m! P/ D" p (heat, new Selector (heat.getClass (), "updateLattice", false));
' D- E& C3 l; X; } } catch (Exception e) {
* V/ `* C. u( K, b" m System.err.println("Exception updateLattice: " + e.getMessage ());
/ ~8 m2 |3 O K- w0 z3 n }% _6 \: h. U7 K2 c: e! r+ ?; J7 E
" [; j; i) k% L/ e5 W u
// Then we create a schedule that executes the
9 X; K4 Y6 r8 M+ D // modelActions. modelActions is an ActionGroup, by itself it5 G* P$ W* _+ ?+ {+ J4 t: T! d8 r
// has no notion of time. In order to have it executed in
" U, p9 F- u( X; w5 Z2 g2 l* [ // time, we create a Schedule that says to use the
& @- D$ s7 H- @2 \ // modelActions ActionGroup at particular times. This* X# Y3 m2 {$ ]4 T4 H
// schedule has a repeat interval of 1, it will loop every
$ R! G0 y7 o9 _6 R5 n // time step. The action is executed at time 0 relative to$ n- L+ ~" o. }) M. G
// the beginning of the loop.1 F" F; ~2 k4 A) B. l7 t( a
+ r6 t+ ?% v1 \) ?- B
// This is a simple schedule, with only one action that is5 L- T2 }4 F. Q) \
// just repeated every time. See jmousetrap for more9 U( ?3 ~, l Q2 c8 g" Z8 k3 _3 q
// complicated schedules.
$ N4 J: D @6 n5 i& N$ X ( |' I, s6 @5 f; H8 o" `4 `/ W4 r6 v
modelSchedule = new ScheduleImpl (getZone (), 1);
. a1 X0 m9 A- H* n modelSchedule.at$createAction (0, modelActions);
# j- C. W9 q; V& w* l
|, u G; l" C0 ]2 a; F" e return this;3 E) V: y9 _* t0 u# x r
} |