HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( p* c) @# Q: y8 g! @' u
- U' ?! w: s. h) t: M7 j
public Object buildActions () {0 P+ J+ o. J. x8 ~, E
super.buildActions();
P6 L# Y0 p. b" D' c/ \7 O
d$ `0 \" W* u! S- l // Create the list of simulation actions. We put these in B+ P- G/ d2 o' Z1 I- ]# K1 m# H
// an action group, because we want these actions to be' Q4 V `; E/ U+ O z
// executed in a specific order, but these steps should
4 w' s, W/ t; Z% C // take no (simulated) time. The M(foo) means "The message
2 m$ a" d. e7 ] S: B // called <foo>". You can send a message To a particular
& H, R. g& H! V6 w6 ~) ] // object, or ForEach object in a collection.) O- e o2 V. y& Y. K, [
) z& h/ e" a; C
// Note we update the heatspace in two phases: first run
( Z# u/ I# ]* ?# i+ B- J. f# Z // diffusion, then run "updateWorld" to actually enact the7 h0 U$ o& k0 k" U5 J' Y; [7 U/ Y0 W! Y9 k
// changes the heatbugs have made. The ordering here is; o G' q2 x4 l! ?. @! L6 f" v
// significant!
m( M( I6 z( z1 t+ \
$ C- |' M0 M8 s1 s" V! T // Note also, that with the additional9 t" h3 |1 f& s: y4 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 J3 L& N! j/ D5 W# r% Q // randomize the order in which the bugs actually run6 K9 B' I! L7 f2 N" W; }, p
// their step rule. This has the effect of removing any9 J& T4 |6 p& X; u
// systematic bias in the iteration throught the heatbug5 L" W0 V3 @% p! N! _8 l2 W7 H
// list from timestep to timestep
- r& s, d( c. E" |, ]4 m ( F8 o! } G4 y& Q. i: D
// By default, all `createActionForEach' modelActions have- U7 c) i& r# K Y- W6 u; X! g
// a default order of `Sequential', which means that the) ^& \& |+ n2 D# P
// order of iteration through the `heatbugList' will be
' [, a% {; o1 D6 P8 w/ N: j // identical (assuming the list order is not changed
+ V2 B' v' r. {6 }% z4 @/ X8 | // indirectly by some other process).4 D+ B I o' u
& L( e1 l" C- `7 d, h
modelActions = new ActionGroupImpl (getZone ());- j( g' `0 u; r0 i
% q5 M5 T" U3 a# O+ M7 Q try {4 j/ _, J" l" y5 I- n1 j- a. Q. T2 a/ [
modelActions.createActionTo$message
' N8 a3 B. I! L; c (heat, new Selector (heat.getClass (), "stepRule", false));3 O1 c4 ?+ \( S; Z, }+ n
} catch (Exception e) {
8 Q; _# k( q0 N$ W& P System.err.println ("Exception stepRule: " + e.getMessage ());
6 n# v7 O* G2 o& S8 p }
7 b& C* a' |7 d" U9 p: N M
6 I$ ^! @" V( R" O. F1 Q try {& w$ z7 I8 v6 I% o! O
Heatbug proto = (Heatbug) heatbugList.get (0);
. N8 b d2 p V: ]6 j$ W Selector sel = , p; s, q) u: K) A$ y5 U
new Selector (proto.getClass (), "heatbugStep", false);
! }, d! |! q% Z" P" J$ E2 n' y4 r actionForEach =! W& P) g8 t5 Z1 B" B( B
modelActions.createFActionForEachHomogeneous$call
+ R p) ` i% h (heatbugList,
2 V8 R1 P( ?( J new FCallImpl (this, proto, sel,
# M6 ^1 }3 i" ^- y4 u/ O q; K new FArgumentsImpl (this, sel)));6 t$ d+ O4 u+ E
} catch (Exception e) {
2 ]9 P* m1 Q' ^! t e.printStackTrace (System.err);% M4 w" Z" k7 `9 ?$ r
}+ ^( d ~# g2 O% i
: J0 ~$ J' O" ?* x# }
syncUpdateOrder ();1 T: F1 f1 q4 N/ c a5 v
+ o; N1 H4 J# Q5 D1 T: c try {
; t% }7 p) m0 P" E' Q, V" n modelActions.createActionTo$message 5 w4 ^/ }1 U" s0 u4 X
(heat, new Selector (heat.getClass (), "updateLattice", false));
( {1 ~# V8 M' R6 A } catch (Exception e) {
( L4 ?$ J$ P0 Z* K System.err.println("Exception updateLattice: " + e.getMessage ());8 [ I. r! I8 y( j! J/ s4 s6 k& O" Q
}
7 A4 l4 J. B4 h; l : s# J$ ], |! D" s
// Then we create a schedule that executes the
6 t& c0 l' F) d2 a1 o3 I // modelActions. modelActions is an ActionGroup, by itself it
: T/ s2 p$ H0 ^) h- j // has no notion of time. In order to have it executed in
, p( o) D$ V+ m8 Z, n" _! U // time, we create a Schedule that says to use the
# F2 A4 r* c ?/ O5 V3 c // modelActions ActionGroup at particular times. This
" {, V8 d' J( t; X6 z // schedule has a repeat interval of 1, it will loop every0 T& x$ |- p; t1 ?9 }' r0 ]
// time step. The action is executed at time 0 relative to6 q/ A6 S: h1 w
// the beginning of the loop.
9 b& f7 _! E/ }/ U$ _9 l4 k% ?' T# C3 C$ @7 S8 Q% _) Q3 ]
// This is a simple schedule, with only one action that is9 a- G0 @. y, `0 T
// just repeated every time. See jmousetrap for more
; K- [4 K9 v6 o" `6 z3 J8 h [ // complicated schedules.
# W; `0 x9 n/ h . E. n/ l# \7 q
modelSchedule = new ScheduleImpl (getZone (), 1);
" v3 r9 \2 u+ x0 k6 _. V modelSchedule.at$createAction (0, modelActions);+ S7 i8 V* K: {+ y- L3 S
' w1 W. _# w8 ~' ~9 Z return this;+ k9 {: x3 I3 h$ Y
} |