HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# C7 _' O2 }1 m% A1 e7 t6 v
. w% ?+ `1 [4 D4 `$ v/ d! G+ G public Object buildActions () {
% G1 B2 G1 p* D. [9 ] super.buildActions();( y7 b; P) } l* w6 j: M
4 G1 |, R, |& U, l. |4 B // Create the list of simulation actions. We put these in. [ n# E" f3 B H
// an action group, because we want these actions to be: i6 n9 q0 \5 b
// executed in a specific order, but these steps should N1 S1 l( E5 f( `3 G9 P
// take no (simulated) time. The M(foo) means "The message# X- c7 m- c1 Y+ M I1 H+ a
// called <foo>". You can send a message To a particular) v& M0 }2 p4 R3 H8 o
// object, or ForEach object in a collection.
! ?1 h7 w2 i' W/ s- R# G
- g! q' t4 Y2 y. L // Note we update the heatspace in two phases: first run" |! F# B0 v+ |& s, w
// diffusion, then run "updateWorld" to actually enact the; s0 l: ^, o, r( ^5 w2 o
// changes the heatbugs have made. The ordering here is, K% V& c4 l# x, Z$ O
// significant!
+ d/ U" P& ^5 u
; s* Q. i, E" ]( ^, \9 f // Note also, that with the additional
' g/ |' J# [4 R" b1 m4 c- Y4 u2 Y \ // `randomizeHeatbugUpdateOrder' Boolean flag we can- q; {2 @% R6 `( J2 j( m4 ]$ t
// randomize the order in which the bugs actually run R3 ?( }( A. a/ j; g4 Z+ m
// their step rule. This has the effect of removing any+ {4 ~. x0 A3 X, T& W
// systematic bias in the iteration throught the heatbug
. e+ q" @! x& d; q4 E+ B# ` // list from timestep to timestep
# j1 x6 v) x8 U
: U9 c- i- o0 E3 ]. N" a // By default, all `createActionForEach' modelActions have9 g' g( ?" \. c8 J8 ^# ~
// a default order of `Sequential', which means that the
8 U8 n' N; Q2 J6 E9 Y' g i+ P // order of iteration through the `heatbugList' will be8 u0 t2 i) t P" a$ ?6 h) ~) c
// identical (assuming the list order is not changed8 c% q; {" s7 E/ ]
// indirectly by some other process).9 U- R9 J% u3 g, z8 E0 `) A0 I
$ q' V) Y$ B9 }0 `: c A9 T9 d- T. r
modelActions = new ActionGroupImpl (getZone ());
" R8 J' x0 g; A+ W0 p4 H
3 G# d9 Q- H4 m try {
p$ f! O" m: U5 j modelActions.createActionTo$message
& q1 A) C. d( T, w X. i (heat, new Selector (heat.getClass (), "stepRule", false));. o% E& u) h1 h s9 q. t
} catch (Exception e) {
9 K2 |' h+ {5 `( P C System.err.println ("Exception stepRule: " + e.getMessage ());4 e. F; u( B/ J) ~3 w$ t/ O5 y
}
# A+ A- O% A, c! z$ {* y7 [1 q5 d& |0 [3 A
try {
6 x* I2 [9 H- r7 j5 W! p* m9 A Heatbug proto = (Heatbug) heatbugList.get (0);5 L# s2 W* \5 o0 V- U
Selector sel = " L T8 e3 Z, @$ ?1 R7 b1 G
new Selector (proto.getClass (), "heatbugStep", false);! \5 M2 ?. ?4 O) [! e8 Z
actionForEach =3 Z* \3 Q. L9 d7 I/ w
modelActions.createFActionForEachHomogeneous$call
) `/ u O- Z) I; }* M0 W$ b% p) A (heatbugList,9 L8 e* T/ r6 O5 e7 \6 H3 X
new FCallImpl (this, proto, sel,. c( F% i( C( ~+ a" h3 g
new FArgumentsImpl (this, sel)));: V+ |$ \5 l3 t8 E
} catch (Exception e) {' S+ e2 s+ a( b2 g* h5 {5 V
e.printStackTrace (System.err);
' D0 f- {9 O J8 A }
4 m0 b( P& | _! B3 V: b
, |* O+ X! @+ o$ L syncUpdateOrder ();! f6 D) e8 F* n: }
0 K% I( l9 U) r. a1 A6 l
try {
/ Z' F: D3 w+ t) l modelActions.createActionTo$message
: h$ v( u7 n5 {, l8 J# T (heat, new Selector (heat.getClass (), "updateLattice", false));3 \- x' C& X3 R" t; v4 m. U8 c1 E
} catch (Exception e) {; F9 R: ]! L0 G( k8 q0 R k
System.err.println("Exception updateLattice: " + e.getMessage ());$ [; ~8 m" _8 x" w( x
}
7 X8 e1 t1 V0 Z
( p( M) ?& r' G // Then we create a schedule that executes the- R7 Y+ [0 ^0 q. x
// modelActions. modelActions is an ActionGroup, by itself it, u8 }$ M0 ?% ?- L
// has no notion of time. In order to have it executed in
3 g6 w& t5 X4 z g# \$ V // time, we create a Schedule that says to use the- Z/ s( p* s, I. }
// modelActions ActionGroup at particular times. This+ S- F. J/ k. x" }
// schedule has a repeat interval of 1, it will loop every R% z) ]1 P( a, X6 z g; C
// time step. The action is executed at time 0 relative to
. H& [! n1 f5 c! R. u7 z& \! [ // the beginning of the loop.
# y* ?$ C0 C: V5 V. k. y* a2 B
n( ~( j: s, t }/ }, R D$ c // This is a simple schedule, with only one action that is
2 x' G- ]) S" ] // just repeated every time. See jmousetrap for more
* r$ g" x1 l6 }" e* u, n // complicated schedules.
* V: U; e) P% F! Y# S( M $ {& C6 F ]: p E" o4 V
modelSchedule = new ScheduleImpl (getZone (), 1);5 a! r& b: E" ~9 o0 H
modelSchedule.at$createAction (0, modelActions);
! D, r7 }5 Y' D5 Q$ g
% |( o) O8 B d; r return this;
7 M7 v+ t$ X2 k% q1 }2 k } |