HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 u. @ j6 _! v" R1 \) D
" m! Z1 G3 ?2 J public Object buildActions () {1 Q- a, Q7 F1 D% x+ @; J! ^5 ~3 M3 z
super.buildActions();
" b" E% s. V, N! n; j5 S5 g 4 e6 t3 C/ Z& {
// Create the list of simulation actions. We put these in
9 k& h3 H/ R# g" U- s // an action group, because we want these actions to be
6 h8 i9 [ R T: Z, f: j3 b // executed in a specific order, but these steps should/ u8 ]9 X& [4 `0 n1 U5 i: V
// take no (simulated) time. The M(foo) means "The message) a4 O J1 `! J0 E% K* A
// called <foo>". You can send a message To a particular R& X( h: T4 E7 P7 U. j
// object, or ForEach object in a collection.
/ u! q N; i: L ; a" P' x4 K' V! z
// Note we update the heatspace in two phases: first run
o* O; z' @2 p1 ]* J* q( A // diffusion, then run "updateWorld" to actually enact the. u$ s5 _0 j4 J
// changes the heatbugs have made. The ordering here is
9 r6 J- Y5 i" Y2 y; r // significant! M8 i8 D* ?' F6 b# {0 @3 C
/ M; T+ C/ f/ d5 s5 L3 g // Note also, that with the additional( c3 k6 u) a$ ?+ C8 K. `1 S
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ }% A+ l2 {6 L/ V
// randomize the order in which the bugs actually run6 @) S, a8 L5 f/ s. h
// their step rule. This has the effect of removing any7 R4 p7 Y \, ^, m+ r
// systematic bias in the iteration throught the heatbug
: c5 _' v! ~# F: j: M* y$ A. S // list from timestep to timestep
! S4 d) S( y+ D0 u1 i & M; r' b: [( Z$ c$ l. n/ U/ C
// By default, all `createActionForEach' modelActions have" L/ z9 b7 s) `1 _6 v: [9 J
// a default order of `Sequential', which means that the {( \9 C0 Y! F9 W4 i
// order of iteration through the `heatbugList' will be
7 f+ _% M( f# Q/ B- _. _ X // identical (assuming the list order is not changed0 @( a- u) l$ ^, I7 P |9 s9 t
// indirectly by some other process).
6 B8 T5 x+ ~) h" ]. ^1 h $ l% O6 M6 n7 N- o0 }7 E
modelActions = new ActionGroupImpl (getZone ());
* i9 P) T2 T) }" U/ f" i8 Y( o
try {5 ~+ t) T2 ?/ J* b+ x9 B
modelActions.createActionTo$message
/ }0 f {4 e/ H! J% E (heat, new Selector (heat.getClass (), "stepRule", false));
+ M" i( o# u2 H8 t, L% a } catch (Exception e) {
" ?1 S! G7 T& O$ s$ S% S" } System.err.println ("Exception stepRule: " + e.getMessage ());
8 N3 r, L4 w0 } }$ J$ y3 p1 b) S E) k
. N6 K. D( l7 x# I/ }; a" N% O try {
; A! f% l H6 J( j Heatbug proto = (Heatbug) heatbugList.get (0);
( Y) h4 C& Z3 C0 ?5 o' W Selector sel = ( K6 r6 L( ^# q/ \! o
new Selector (proto.getClass (), "heatbugStep", false);
& \" I; F; ?: J- w' { actionForEach =8 w; q9 n* a( P4 L
modelActions.createFActionForEachHomogeneous$call5 W! M4 P. @& t8 E8 M2 ?
(heatbugList,
5 p8 t7 J4 M+ c6 O new FCallImpl (this, proto, sel,
) g) R" D% q1 o- _4 E new FArgumentsImpl (this, sel)));
d( U/ W" |' _2 y+ I5 F9 x/ e' M4 | } catch (Exception e) {+ @ s8 h8 a) [; a
e.printStackTrace (System.err);' ]# d( `, T" k4 S
}0 k6 G1 _0 j% K
* V4 L! c( m' } syncUpdateOrder ();$ s1 M9 u% I5 H, X, o+ P8 W
/ I9 q" ~8 f; H- g. c4 P: u) Z try {
K- I. p7 K4 F# W6 h- e( } modelActions.createActionTo$message / F* s* q% C; y( J6 m2 o
(heat, new Selector (heat.getClass (), "updateLattice", false));3 j# g4 a3 i, V2 c# F+ V; R0 d9 o
} catch (Exception e) {
6 p0 ]1 U( j2 p: y, f System.err.println("Exception updateLattice: " + e.getMessage ());
( w2 q) f& e0 V" l+ T/ l6 b7 Z! N) X5 S }6 K# I1 @2 V* m4 n0 J; H
( `( c; u2 `2 M5 N // Then we create a schedule that executes the# o( g- n7 G- w7 e2 V2 W: M8 K7 c8 }
// modelActions. modelActions is an ActionGroup, by itself it4 k8 x* Z, s3 i# f9 ?8 S# }
// has no notion of time. In order to have it executed in4 J1 n% m5 t+ C! ~, V8 t6 F3 N
// time, we create a Schedule that says to use the
) [: y2 a9 h1 ]" F# j5 \* @: R // modelActions ActionGroup at particular times. This
3 N& ]7 T: x9 ^3 h' O4 u; O // schedule has a repeat interval of 1, it will loop every
$ r" J: q" b% h3 E7 ? // time step. The action is executed at time 0 relative to
0 v `- @! O$ l6 `; E- M // the beginning of the loop.
: |4 }+ `* e$ Y# Q) [1 |# R# n1 X- A
// This is a simple schedule, with only one action that is
& v- z1 u; m; W8 J0 t/ S. G& t3 y // just repeated every time. See jmousetrap for more# c8 O. q) F& q C8 y0 Z
// complicated schedules.: H8 I# T/ A. a& K& M4 q
0 I, O. A" J0 v! {! T* ]$ ?
modelSchedule = new ScheduleImpl (getZone (), 1);
7 u; ]( z3 @+ W6 x4 G4 E modelSchedule.at$createAction (0, modelActions);
( M" h, }) i+ [9 ^5 R6 ?2 l
/ Y5 A/ }1 K: C$ j* Y3 P% L k3 l2 c return this;1 r+ Y4 l9 d( q2 p) V" W
} |