HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 Y3 Y, U8 n4 ^7 V, ?7 h
2 \0 i6 z& G6 f2 e* R public Object buildActions () {
9 W" p2 B9 @: ^6 {: |5 C# T super.buildActions();& w0 ?# T9 U* L8 q, S: F. Q
0 p# B# }1 `* A# M" ]
// Create the list of simulation actions. We put these in
7 r1 @) `/ o3 a // an action group, because we want these actions to be
/ [. Q, x% V% j& [ // executed in a specific order, but these steps should9 D7 G8 U$ Z% Y& Z2 n- K
// take no (simulated) time. The M(foo) means "The message7 m% G0 j. P* F' t. [$ \
// called <foo>". You can send a message To a particular4 K2 B5 ~" o8 Q* f
// object, or ForEach object in a collection.
% ]$ U8 w% O8 q i
0 Q: x1 m Z+ y+ W* I! H // Note we update the heatspace in two phases: first run' P+ V9 [8 p$ |! h: E0 Z
// diffusion, then run "updateWorld" to actually enact the
, o" v3 Q/ d/ Y) e! W5 X! Q // changes the heatbugs have made. The ordering here is
1 d1 Y* ^/ J8 T // significant!2 J; n$ O/ {) @! Y1 T% Y& a
9 l1 q6 Y( S& b6 z // Note also, that with the additional6 B8 @9 D: |1 d- @, f
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ x! i+ q" m- C$ t* A2 Z+ x* \ // randomize the order in which the bugs actually run9 C! I" N+ }: |) D
// their step rule. This has the effect of removing any
% U- b5 w2 N8 R // systematic bias in the iteration throught the heatbug
' F/ K4 x3 z* x; q$ v+ I // list from timestep to timestep
6 n3 d- A& g% n& R , R2 `! A& {; u3 x% I, v3 O1 {
// By default, all `createActionForEach' modelActions have; ^0 o B1 ]5 G$ z7 e4 Y2 a8 H
// a default order of `Sequential', which means that the
8 C( P4 f. w: R' Z4 k) F6 O // order of iteration through the `heatbugList' will be
( t2 m- P7 ? L# b // identical (assuming the list order is not changed
! _! y2 o; z; W* i, F0 { // indirectly by some other process).7 l4 \. Z& V6 b/ h. z
9 J/ S$ F) E5 F2 A: G* f) Y
modelActions = new ActionGroupImpl (getZone ());
* w" Q9 a1 b3 s2 A
2 ?! N" ] v5 P, ^: L# J8 _ try {2 _; A+ V4 q# n$ |- V+ F) G
modelActions.createActionTo$message
* p. M" V. f; s) P5 t (heat, new Selector (heat.getClass (), "stepRule", false));% {8 Q R3 X0 X/ f- Z
} catch (Exception e) {; X5 w$ I0 z0 o. w
System.err.println ("Exception stepRule: " + e.getMessage ());
6 K7 N. v9 `. t, R5 _ h! ~2 t' @ }3 A. ?5 z6 D1 b1 ]2 _
& u$ k& j1 x5 n# x+ T5 ^ try {2 K( ~1 |& E, l7 x
Heatbug proto = (Heatbug) heatbugList.get (0);
- ]1 K; q- l" o/ O6 o( [ Selector sel = 5 K' y- A, D4 w7 O
new Selector (proto.getClass (), "heatbugStep", false);( _9 j$ N8 e U& C
actionForEach =- O3 @' w1 B) R7 D* F' ~2 y0 M M
modelActions.createFActionForEachHomogeneous$call$ d% {1 ^* D; H/ j. U: s e, X! L
(heatbugList,
. ~# A( Q3 V/ I$ y3 K; _; ` new FCallImpl (this, proto, sel,
- p' }* h3 V, a2 B new FArgumentsImpl (this, sel)));) a: \* u7 S2 B* W" E5 [4 V
} catch (Exception e) {
( c* g5 Z4 i. t& T4 c0 e e.printStackTrace (System.err);7 J" d) ~; Y( ` L" n
}
; |6 t: G" j# j6 _* F
* Q6 L; V: D- D5 } syncUpdateOrder ();
8 ~- R# ?5 J$ m% @
1 G; U4 D. h% j) ?9 j try {
) c2 P6 i3 t% |7 k1 B6 |/ c modelActions.createActionTo$message
! d: W F; v3 ?) C3 ~ (heat, new Selector (heat.getClass (), "updateLattice", false));+ P* c! T" }2 x( L
} catch (Exception e) {# p" V) | p' v: z& t+ J
System.err.println("Exception updateLattice: " + e.getMessage ());
8 u6 h: G9 Y" {" D }
/ u( f) L, T5 }, C
( ?* X+ W$ v$ j M) A# h6 Z! P // Then we create a schedule that executes the
) H- ^6 Z" q7 C- I // modelActions. modelActions is an ActionGroup, by itself it
$ n4 _9 a6 ~0 i0 V // has no notion of time. In order to have it executed in4 ~3 _* t% K8 I( n9 ?
// time, we create a Schedule that says to use the
4 @7 v2 s: d6 }% Y# Z$ T // modelActions ActionGroup at particular times. This( J* u1 x4 F. i( A$ n
// schedule has a repeat interval of 1, it will loop every9 r+ W. o# z8 _; G6 G2 a
// time step. The action is executed at time 0 relative to
) V0 |$ n2 ~' H- _; U // the beginning of the loop.
; k% B5 z' `: L: O& Q/ c1 @- _* J) G
9 E5 m7 f& ]& ? // This is a simple schedule, with only one action that is8 B9 C% ]% ~+ T7 h5 w2 {
// just repeated every time. See jmousetrap for more) c" X C( @9 o: ?2 h" U7 r
// complicated schedules.
; M* q1 A& _$ c7 s; R4 K: |9 b1 Y: V& X
4 A% g5 _ G2 T0 N modelSchedule = new ScheduleImpl (getZone (), 1); I% f; }) i# I& {5 D. i
modelSchedule.at$createAction (0, modelActions);$ R# _, [) s- E& x# o# y
8 a. M6 q8 k$ \% r7 Q
return this;6 K5 v1 T' H3 L: m% y
} |