HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! [0 R! q: \6 X8 J& f7 @1 q X8 X
public Object buildActions () {+ ?9 {$ C% i* \, R N% w) q
super.buildActions();
; f2 I# f. e0 x( R6 Y6 V9 | L Z/ p( {' ~0 [3 Z; n; S$ y
// Create the list of simulation actions. We put these in; `( M3 ?0 i; ~# g5 X2 {
// an action group, because we want these actions to be
/ g7 U: w G' Y- D" W; ~4 _ // executed in a specific order, but these steps should
0 ]7 W3 }+ b. n // take no (simulated) time. The M(foo) means "The message
8 ~2 A$ \+ P0 X+ o- o9 w' e // called <foo>". You can send a message To a particular7 Z% Y8 @: G& D8 x4 P n: ]
// object, or ForEach object in a collection.
' J* f# f. t7 {" C. P" G G
: h/ |; i4 P- A/ Q // Note we update the heatspace in two phases: first run
6 B X: x, S) E1 m3 v+ T( ~; m // diffusion, then run "updateWorld" to actually enact the6 x# D2 B) m+ E2 V1 X/ B; B
// changes the heatbugs have made. The ordering here is
+ E4 D$ O6 Y$ [# l* `$ A6 a* C% { // significant! y/ U: }& t) K6 A8 M, `: X; z' _
1 n- U U! W# Q9 O // Note also, that with the additional0 w8 s J. l& D3 U; M
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 U! C1 v. X" ^ d! p9 ^6 q. O4 v4 h
// randomize the order in which the bugs actually run/ W7 {' `. I3 j/ s
// their step rule. This has the effect of removing any! a. d; `' k- D( j0 S4 D
// systematic bias in the iteration throught the heatbug
4 L+ h4 [" @& J( x, Y // list from timestep to timestep
: K1 |4 F( R8 c( I - ~ \9 |) @& [. ~3 i' h- U3 f$ F+ o
// By default, all `createActionForEach' modelActions have0 l" w1 q* r- b; p n0 R% n
// a default order of `Sequential', which means that the! N( k5 j+ b" Q# ]5 H
// order of iteration through the `heatbugList' will be5 o! z& K7 j( B7 A6 L
// identical (assuming the list order is not changed
2 B6 X3 i4 C" }( ^/ o$ W& {8 e // indirectly by some other process).
' y# T0 Q Z( a # {( C: t& }7 \* |% _( ^
modelActions = new ActionGroupImpl (getZone ());' n2 A; w' D7 _6 L
/ t* g& e" P. q" @" h try {- X) W) P3 [9 J, t3 U2 S
modelActions.createActionTo$message
; s/ n k' M4 x (heat, new Selector (heat.getClass (), "stepRule", false));
9 R" q: ^' b. A1 d3 ^% t- V3 M } catch (Exception e) {
4 W2 O& T3 a- U! s0 f' S System.err.println ("Exception stepRule: " + e.getMessage ());
1 G* s. s1 F( H" W9 H }1 z' H: ~% H: e' k/ i& _+ B5 m
- v2 V8 p5 i# B! W8 S4 [9 [% V! |
try {
/ K* s/ ~" l4 k Heatbug proto = (Heatbug) heatbugList.get (0);* f* y( e0 d5 Q3 V/ d( x
Selector sel =
) I6 t. N* ]( ^7 o: P v new Selector (proto.getClass (), "heatbugStep", false);+ R. x v3 ?/ Q; O7 a
actionForEach =
0 m g, @6 y- @3 @) C4 _ modelActions.createFActionForEachHomogeneous$call$ d7 w- m- [; \, K+ Z, O' o
(heatbugList,
' z. t+ q& }$ x6 I/ y new FCallImpl (this, proto, sel,
" p. i0 m- @- K* l d new FArgumentsImpl (this, sel)));5 N3 {% V% M: n! a- I
} catch (Exception e) {$ ]9 S* h* q0 `9 c2 U: Z
e.printStackTrace (System.err);& p+ F( {9 s1 I- y# x
}
3 h# e' W3 B$ S; T# X' [( o0 S ; n/ \' ]3 w! x6 a- {
syncUpdateOrder ();
4 e4 ]- S% G c3 t( e5 D+ k9 A1 r5 Z, L1 q7 r
try {
. {% n2 t, }4 r% L) m2 _ modelActions.createActionTo$message
- r) V) o* w% q$ q, `" ~: V" A) u& Y (heat, new Selector (heat.getClass (), "updateLattice", false));- ?* t9 r0 d. R+ [& z o
} catch (Exception e) {
( ? o3 F. i# j2 G4 M System.err.println("Exception updateLattice: " + e.getMessage ());* Z r" I7 v. M/ Y7 k
}
1 [- r9 y P4 ~3 b2 |
' g& _" e7 [+ [. `# d! L // Then we create a schedule that executes the' | ~$ d( f4 `# ?! }
// modelActions. modelActions is an ActionGroup, by itself it
2 R/ e! j4 b* s6 E% p0 Z // has no notion of time. In order to have it executed in( E5 N. r9 m; Z
// time, we create a Schedule that says to use the2 Q$ x6 ^7 K, j: y3 a0 `
// modelActions ActionGroup at particular times. This" A7 ?/ Z, p- a
// schedule has a repeat interval of 1, it will loop every/ i9 h8 ~* d9 W
// time step. The action is executed at time 0 relative to
5 f9 q, B, V$ \5 t0 O) t // the beginning of the loop. ?1 `& V8 f; i, c
+ B. M8 z$ B2 Q4 y$ T // This is a simple schedule, with only one action that is
- \& L; d& t: I$ k6 z- G // just repeated every time. See jmousetrap for more
" S _" x, P" M x* o0 ~' V0 ? // complicated schedules.$ ^2 ]/ w+ ~4 t' D2 T x- \: z
% z; w3 i3 m; R
modelSchedule = new ScheduleImpl (getZone (), 1);! P0 G4 F& y' ~( f; s
modelSchedule.at$createAction (0, modelActions);
- n% _4 c; m$ \" K8 L# i & T+ O0 @0 x2 \' M& s' i3 ^
return this;. m# w% d3 n' u
} |