HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. T! m9 |& L2 H3 v. l
+ E( u( j8 c4 l& t' _ public Object buildActions () {
8 P/ o7 f/ H: A super.buildActions();
( n( T6 Q7 O0 c ' M1 V) M+ s; C( ^2 w. T- \; k) s! d
// Create the list of simulation actions. We put these in
# v. p' A3 z9 w* r. ]3 a1 D9 r& R // an action group, because we want these actions to be
3 G- Q5 L7 j1 U* d/ D. @ // executed in a specific order, but these steps should$ [+ x. |" N9 U) U- n, l
// take no (simulated) time. The M(foo) means "The message
" G+ B6 ]0 r3 C* r0 `9 b // called <foo>". You can send a message To a particular
+ n! Z+ h/ U/ h1 | // object, or ForEach object in a collection., l) \( U4 @! q1 J
5 v5 ^ G: E; T+ W: R3 r
// Note we update the heatspace in two phases: first run; U8 z8 ~! X& d* V1 F6 _) {
// diffusion, then run "updateWorld" to actually enact the1 `( [ H) `) O" M( E
// changes the heatbugs have made. The ordering here is2 w- Y& ~ u5 }/ U* E/ ~/ r
// significant!/ `& E* V% p. `1 ]* f
5 W) b- o1 l: L* X6 U) h s: R
// Note also, that with the additional
5 ~! o7 c4 p, S8 n // `randomizeHeatbugUpdateOrder' Boolean flag we can! m5 _- H9 I& U' ?( L
// randomize the order in which the bugs actually run
* s D1 f( ~9 m2 ` // their step rule. This has the effect of removing any; Z5 O7 s. B; w0 ]3 S4 a" s1 L
// systematic bias in the iteration throught the heatbug
( L; k i/ [( s# Q; w // list from timestep to timestep
3 G7 Y: n# j! B( O: J: E 8 c! B: G2 C1 P; d: p
// By default, all `createActionForEach' modelActions have9 ]: O7 u `1 `4 l( J ^
// a default order of `Sequential', which means that the
5 X3 N3 \* k+ U& z6 e7 H" I- h0 u // order of iteration through the `heatbugList' will be! j# g% l/ H% q; _
// identical (assuming the list order is not changed, v; f% ?# h+ W: c7 O4 z
// indirectly by some other process).0 d& I& \9 ~$ H# `1 |8 r
* {+ `: ~+ [- v8 a" b; C' p
modelActions = new ActionGroupImpl (getZone ());
6 L, o! V& x5 j0 T1 n Y2 `! n4 e8 a* J0 o/ D$ K
try {7 Z, X. p! C& @/ D
modelActions.createActionTo$message. X% P7 b0 H$ R
(heat, new Selector (heat.getClass (), "stepRule", false));$ L4 i: L' ~* L7 C4 H
} catch (Exception e) {% n$ u% p; s! @0 ?( r
System.err.println ("Exception stepRule: " + e.getMessage ());
/ q) L9 x0 c( G5 D- Z }
+ H/ D, Z, y1 _$ C
1 I# U$ M& \) k k try {
9 u# t9 o5 }6 x, l) U Heatbug proto = (Heatbug) heatbugList.get (0);
" B T1 t7 W+ | Selector sel = ' p/ p6 q B$ O" C
new Selector (proto.getClass (), "heatbugStep", false);
5 H/ z$ g3 p, b1 B$ q/ | actionForEach =
" U6 ^# G5 U- z% G+ j2 i modelActions.createFActionForEachHomogeneous$call
! Z- x: y' G. ]8 W: f (heatbugList,
! I' B) O, {7 Z- r9 x: I3 a: v& B' b new FCallImpl (this, proto, sel,
" W Z6 T' m6 H O new FArgumentsImpl (this, sel)));# W0 f8 b; E, t9 I6 d- w; a
} catch (Exception e) {6 T0 @4 N& {. a/ Y
e.printStackTrace (System.err);
% A4 B; C. i* e5 X9 @+ f }
$ r$ ?2 S- w& Y$ J" W( E
2 Y( M+ g" V; h% d8 X) F& o syncUpdateOrder ();
' l8 \2 P% N# Z9 r
1 J, ?/ j% r p8 h1 D% j! d5 G try {% Z! c; M& s0 F2 `. w( }! F
modelActions.createActionTo$message 4 ]8 p. n) i$ ], _# i
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 D z2 W- d, M5 ~ } catch (Exception e) {
+ k% Q) z7 ~5 S1 P9 r( A/ b6 Z5 S System.err.println("Exception updateLattice: " + e.getMessage ());# E1 K! K0 t9 m+ s* m ]0 ]
}
; ]" @$ D3 y# Z2 z, v) _* Q# I0 E
9 W- f) G6 h5 [; z( }- O: H5 C // Then we create a schedule that executes the
( V2 J, g% |2 V // modelActions. modelActions is an ActionGroup, by itself it
, f6 ?' w7 _$ ~ // has no notion of time. In order to have it executed in% f7 J4 x7 o5 r2 p
// time, we create a Schedule that says to use the' Y5 E& V6 J; z! p% D$ j3 `, ]$ A
// modelActions ActionGroup at particular times. This$ `( l* }) r' ~7 E% ]1 P" b
// schedule has a repeat interval of 1, it will loop every) u# [0 U) {9 j: ]7 M& K
// time step. The action is executed at time 0 relative to
8 { C. s( C4 F+ y3 J/ {# L // the beginning of the loop.
2 i- V. O$ z$ c1 L1 R. g; j4 E" M! _$ K! k; I. @$ j% r- i2 A7 }! A
// This is a simple schedule, with only one action that is* [7 S& F4 i; `( u9 A
// just repeated every time. See jmousetrap for more
5 \) @3 ?" ~2 \) D! [2 C" O // complicated schedules. ]! _, C0 B8 ]& J8 I: M
+ _; z2 B0 v0 d2 U" `
modelSchedule = new ScheduleImpl (getZone (), 1);- c& \1 x, n$ x. B4 z
modelSchedule.at$createAction (0, modelActions);- K& [6 s6 A! N& f0 G8 p" _$ s( L
- }8 S! M8 n* F
return this;
* u) h: h) M/ } } |