HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ a2 l" n. |. y, X
, S; k6 [2 T# D" `( } public Object buildActions () {3 ?- F8 N2 r7 L5 J
super.buildActions();3 j9 p* J5 [) u5 C. [/ s" H( k2 t) J
$ R7 k9 E& J" V
// Create the list of simulation actions. We put these in9 Z0 q5 @! a4 F4 g
// an action group, because we want these actions to be
9 U7 q6 f* g! T1 M! a" U q // executed in a specific order, but these steps should
]/ A. H9 B+ [/ t$ v$ F // take no (simulated) time. The M(foo) means "The message
. ~. ]4 Z) s9 \5 \& I // called <foo>". You can send a message To a particular
6 d; G' C1 ~. C // object, or ForEach object in a collection.% A7 T6 o n& ]7 [5 t3 `
$ b4 }* b% d7 |* |& I( j9 c8 k
// Note we update the heatspace in two phases: first run( I' H4 N, t5 ]3 A
// diffusion, then run "updateWorld" to actually enact the
1 q$ O' Z1 W2 E- v // changes the heatbugs have made. The ordering here is2 S; ~2 t0 e. p
// significant!
( A2 z1 L x8 e9 a: Q
' e* A% L* ~5 x+ S0 r // Note also, that with the additional$ @6 y: ]$ ^8 o5 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
! H$ X2 C. B6 X' Q // randomize the order in which the bugs actually run$ V% ?' W5 a2 {' n# I* v5 ~$ r$ q7 ^
// their step rule. This has the effect of removing any/ D: L6 b) R1 f* K
// systematic bias in the iteration throught the heatbug
& \8 K6 l/ ?1 T& E; E3 T% }8 F // list from timestep to timestep
( P9 K7 M% P( ]
; o+ t! P4 A* o: f/ C // By default, all `createActionForEach' modelActions have
7 R) ^% V) \* F- F // a default order of `Sequential', which means that the" b, X3 M* u- D4 w( p' b
// order of iteration through the `heatbugList' will be4 U% ^, K& i0 I! L/ R' c5 d, E. E5 \
// identical (assuming the list order is not changed
- h) R0 N4 X" f // indirectly by some other process).4 ?) o! s3 P# z, w/ X5 G
5 o" f/ B8 H* L modelActions = new ActionGroupImpl (getZone ());/ h( \8 D. r7 Q4 @1 K% D" q
i" l& e! d& I9 N; `3 n* ^7 H" @
try {, G! d0 r( c5 b0 p9 x: u7 E9 G
modelActions.createActionTo$message( `- P0 M# l8 H2 H A
(heat, new Selector (heat.getClass (), "stepRule", false));. X5 M+ ^# @! ^: b4 q2 o; a: J
} catch (Exception e) {
) B6 i' z/ ]) w# W# P9 ?* f& c System.err.println ("Exception stepRule: " + e.getMessage ());- m1 E# ^/ \ p/ d. L3 W+ B5 P
}9 }; ?% K/ Q8 q9 G/ a3 o3 p q
- {+ @- y+ s' R
try {
$ ]/ P o, v& O1 [6 l: w. _ Heatbug proto = (Heatbug) heatbugList.get (0);! K3 V3 ^" h) [% u9 F
Selector sel = ) V$ F+ ]- |' b' t
new Selector (proto.getClass (), "heatbugStep", false);
3 Q$ a- c: O1 e7 j3 o4 ~ actionForEach =
0 R6 k; d' D; z6 [* Z1 J5 h2 @ modelActions.createFActionForEachHomogeneous$call
/ ~# z3 Y4 h! R | (heatbugList," t- M# {$ G8 s* }) I+ N! ^
new FCallImpl (this, proto, sel,
4 T* ^5 Y! Y m' \( K0 j5 K& z# b6 `/ m new FArgumentsImpl (this, sel)));! ~0 h# `1 {; D0 ?1 \
} catch (Exception e) {! R' G" J2 F5 ~' n$ n* d) D
e.printStackTrace (System.err);* h8 L, N) E# x8 y8 Y
}
* [! }4 @% H4 ^$ h # z/ y8 I" t: @+ a5 K3 B0 T6 _
syncUpdateOrder ();- U9 J! d" B: {- A; T; m o* \4 w
3 u# E4 w$ S$ O
try {
9 }7 b4 R. Q/ a% O% }7 T. u5 s modelActions.createActionTo$message ' f2 H3 r3 T. F" \
(heat, new Selector (heat.getClass (), "updateLattice", false));8 v5 S. x/ ^7 N0 m, X7 ?/ |
} catch (Exception e) {* U" z6 d0 g1 E. f1 x* \( J
System.err.println("Exception updateLattice: " + e.getMessage ());
. _: Q; K3 ^$ D/ T& {+ y }6 x; h# |6 q4 K* r+ [0 O
0 p% a6 I* ~+ Y" Q6 O& O* b // Then we create a schedule that executes the' w4 u7 o( u% ~$ }( m+ `- ]
// modelActions. modelActions is an ActionGroup, by itself it
% f, b3 k7 S, K0 \0 E' F, g // has no notion of time. In order to have it executed in
& {" ` X" n. e- j6 t, Y // time, we create a Schedule that says to use the
; c/ ~0 B& v, j) b" [6 Z( k0 O // modelActions ActionGroup at particular times. This7 n5 J! U! k0 ]
// schedule has a repeat interval of 1, it will loop every
& N) X' h8 x$ r // time step. The action is executed at time 0 relative to: c1 R3 q# J2 R! B* p0 A j
// the beginning of the loop./ S. J/ X2 E% n2 y/ D+ G7 z
/ S7 ~8 B8 d0 w+ e" L; h
// This is a simple schedule, with only one action that is
3 x \- V1 Z- e- C5 ]) x // just repeated every time. See jmousetrap for more, Z! j3 d: j2 Y% {
// complicated schedules.
6 a& z& ?; k: a' D6 O
# d+ M$ r6 W+ H7 L: b modelSchedule = new ScheduleImpl (getZone (), 1);
2 U+ q/ l5 q* U3 t1 L- h modelSchedule.at$createAction (0, modelActions);
( D- m3 ?. G6 `6 T . H% u/ S% ?, v# M8 ^' M- @5 ~
return this;9 Q, u2 I4 A3 E/ Y# v$ ~
} |