HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# R3 Y: p9 a* S; r+ z
$ a) S! c) A7 \' K5 K# F( Q" r K( ~ public Object buildActions () {& `. l$ K& ^4 G9 G# G' L% X
super.buildActions();7 t1 t x6 c4 J! j# Y7 g' W9 {
& M; N2 W: C4 }( O2 b // Create the list of simulation actions. We put these in& v' M& _" c: J+ n6 O6 H6 I W5 U
// an action group, because we want these actions to be
4 N3 I$ \0 G. t$ c4 K* u // executed in a specific order, but these steps should0 E0 V8 l9 H. ]" h" ?: {' n+ ~
// take no (simulated) time. The M(foo) means "The message, V) P9 H- m6 z+ ?2 G Y
// called <foo>". You can send a message To a particular4 }% k$ ?, F; E% ~7 `
// object, or ForEach object in a collection.
: g4 h6 Z7 C3 k: | 7 x$ q, J: N7 F6 e8 ?& R2 a
// Note we update the heatspace in two phases: first run- `& c) Y+ X8 f* a q! ^
// diffusion, then run "updateWorld" to actually enact the! S3 i4 @! X |* K( l
// changes the heatbugs have made. The ordering here is& p3 {: A$ l6 z
// significant!
$ O5 x7 I0 r+ Q% U
6 y, c9 @' Q4 b* F+ v3 {1 n( O0 m // Note also, that with the additional7 ?9 T _7 L& i* ~% U6 b* }
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* V+ X1 {$ q/ B* T9 [ // randomize the order in which the bugs actually run, f& ]: X6 v6 W7 x5 e% U
// their step rule. This has the effect of removing any
; a% m [9 X" i! F9 g // systematic bias in the iteration throught the heatbug
) Q8 t8 _/ d. ~; n) m // list from timestep to timestep+ ^/ |- k/ R' ~! Q+ X
4 U; Q; M+ ]- s# @( |6 ^
// By default, all `createActionForEach' modelActions have
- o: i7 @( s2 ]" X // a default order of `Sequential', which means that the; n8 u1 Y% P; q
// order of iteration through the `heatbugList' will be
3 i0 ?+ ?3 E; d% q7 u# \3 K6 k // identical (assuming the list order is not changed7 }" a t/ V$ X( v' s& z
// indirectly by some other process).
. h& H4 `% B* H: y( m2 z& m
+ S8 a, ]3 m2 b modelActions = new ActionGroupImpl (getZone ());9 }1 i* _+ Q" p2 Q1 v
X5 ` V$ C( w# I9 y0 Z* ^6 x try {
! G( L, o7 E# {! n' e0 D modelActions.createActionTo$message
% l* w5 Y+ p' Y* B (heat, new Selector (heat.getClass (), "stepRule", false));) h( V$ a0 E' a0 v$ } x
} catch (Exception e) {/ s1 j; ]2 n; J: z( J& B
System.err.println ("Exception stepRule: " + e.getMessage ());5 v! b M& d# u6 z
}
& w, X1 B) g2 c% X2 B; b3 B) W* N+ Q9 x @4 t" {+ W
try {
1 L6 w. j$ F# I0 T+ [2 j, E2 X Heatbug proto = (Heatbug) heatbugList.get (0);
, U3 N6 p9 d, ]4 B9 N2 V% H" W, Y Selector sel = - B9 m X% T9 W# n, v2 b
new Selector (proto.getClass (), "heatbugStep", false);7 R& l& E8 @4 }5 A
actionForEach =& D5 U- ~% ?: ?8 _7 O0 G
modelActions.createFActionForEachHomogeneous$call8 Q$ J- d; `) H: n7 }+ h4 X6 V
(heatbugList,
. v) A6 Q7 f# S' n new FCallImpl (this, proto, sel,
, |$ P3 T1 I t% [- N+ W new FArgumentsImpl (this, sel)));
( [+ F1 f8 w# { } catch (Exception e) {
- Q5 r% E: ^( X. R( ? e.printStackTrace (System.err);
1 b r3 w. @# |! ~ }
' V T! L, V5 }
1 X0 F+ q+ v X% s6 X* a syncUpdateOrder ();
! m5 l5 q* x. ?3 Y! P6 ]
4 u2 d* Y6 n5 B' u- N try {
; F/ q5 E9 x* i7 j modelActions.createActionTo$message . W, N6 K4 |, J# X4 }
(heat, new Selector (heat.getClass (), "updateLattice", false));
) {0 B6 c' D1 {; F } catch (Exception e) {3 p+ h* M& L' }/ j2 _' D% X
System.err.println("Exception updateLattice: " + e.getMessage ());
/ P" b( W5 T3 {, ~3 [ }+ O$ E/ u6 _( M9 d9 d0 Q9 @. b8 o
T2 I" G/ Z- D0 ~# y
// Then we create a schedule that executes the# g; ~+ e+ ^+ E2 S6 l q3 ?
// modelActions. modelActions is an ActionGroup, by itself it2 ~6 V3 J0 L1 h0 ]0 ` \6 S2 N
// has no notion of time. In order to have it executed in
. a9 X' b$ F" o' G' ` // time, we create a Schedule that says to use the
% E2 q8 }( s& y) Q1 L // modelActions ActionGroup at particular times. This8 Z4 W# H$ g& G
// schedule has a repeat interval of 1, it will loop every" p/ o0 k- ]- ]0 ~. a' i* o: A
// time step. The action is executed at time 0 relative to. L$ F: d2 V( [, u4 h0 _) D& h1 {
// the beginning of the loop.
3 ]& ]: ^7 y' c7 O# ]7 T/ u# `- G, @ ^7 o) J) b. M
// This is a simple schedule, with only one action that is" h% m* V4 r1 [" M G- ^2 {! A
// just repeated every time. See jmousetrap for more
& r. l6 R' I, K" d9 U5 | // complicated schedules.) X$ g1 F, O0 l
" B, p+ H( {: S6 l1 ~' J modelSchedule = new ScheduleImpl (getZone (), 1);2 ~" i7 Y: `: U# q' E4 J4 Q3 D9 I7 ~
modelSchedule.at$createAction (0, modelActions);$ k4 o) t* N4 p' D7 K$ p! u
; S4 L" f3 K( m; ] return this;
' z y0 x# e7 ] } |