HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ z; h' _9 e j5 m* C, a# U# j( j+ Y; I4 ] \
public Object buildActions () {
1 B" Q6 ^6 w/ j$ h super.buildActions();4 b7 h- Z. u0 c
3 a0 d. e5 Y$ E' |# E$ P2 x/ f! c // Create the list of simulation actions. We put these in
V; Z* r8 E6 k' \* Z& I$ ? // an action group, because we want these actions to be; [ Y1 ]7 l) U
// executed in a specific order, but these steps should
2 w" r: r' ?; G2 u5 m# F/ p0 b // take no (simulated) time. The M(foo) means "The message! G7 e; r9 ~+ H, ~5 L
// called <foo>". You can send a message To a particular8 b' p2 Q" q0 a: p
// object, or ForEach object in a collection.
; l/ D2 V7 k, W, N, x6 q$ v5 }% V
5 @$ P/ v4 z: J# Z, l1 j // Note we update the heatspace in two phases: first run+ l. M/ A7 p5 T
// diffusion, then run "updateWorld" to actually enact the
1 E s* a, B( u9 T // changes the heatbugs have made. The ordering here is
; e- |- L; T z. B8 d // significant!
! M0 J; }( \7 X! h, ]
0 z9 Z+ Q e$ a' O // Note also, that with the additional2 m. Y# |. x2 w$ G, _6 T
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 ~. y& |% D, F% [0 b2 ~
// randomize the order in which the bugs actually run
7 t" v8 E8 s4 g, a // their step rule. This has the effect of removing any
, T: i+ A6 F0 r1 Y& ~2 \: @ // systematic bias in the iteration throught the heatbug
7 [. _/ a$ @9 E$ p/ O9 ~ // list from timestep to timestep
& q0 d J" J e% a+ z4 {& F
6 G' U# u d5 @1 P // By default, all `createActionForEach' modelActions have
) B3 X' b- z# ~ // a default order of `Sequential', which means that the( L3 Z9 F6 f2 ?$ j4 ^" l/ L' a. e J
// order of iteration through the `heatbugList' will be0 z! i* y. P4 Z) c1 n
// identical (assuming the list order is not changed
7 f, I+ K; n* r1 E( v+ ^% W // indirectly by some other process).
5 p7 e0 ]& E4 y) A8 h/ X' o
' G6 u2 }. e2 G0 y ~$ T( G modelActions = new ActionGroupImpl (getZone ());
# @/ a4 ], Z6 S* Z
6 t. ]. ~) e% I" I: e+ T: h try {
3 m3 M% E `9 O9 {& K9 o9 W4 ^ modelActions.createActionTo$message
; A% p* z) ~. ?7 F8 Q3 T: J (heat, new Selector (heat.getClass (), "stepRule", false));
3 d. @0 D( X" h: A! l% W& p( X7 c } catch (Exception e) {/ \+ Q* \! V4 n4 A$ T
System.err.println ("Exception stepRule: " + e.getMessage ());7 |% p* x/ l* R7 T: l( _+ T
}
% x* x _, S5 {8 g, S2 y- [" l
! e u u, f. Z( J9 w try {
* j1 C5 u3 ]! H: {% E: o' ] Heatbug proto = (Heatbug) heatbugList.get (0);
6 P! ~: Q; t8 ]: D Selector sel =
9 A/ ^* q, {8 o6 k% x/ c9 U7 L/ p new Selector (proto.getClass (), "heatbugStep", false);
% b, O/ D8 {1 }# k, F, u actionForEach =* G9 P/ Z* G% {0 d( f
modelActions.createFActionForEachHomogeneous$call: l" \7 K9 u& v4 Z! v/ a
(heatbugList,
8 W% Z& r5 w* b1 l! V new FCallImpl (this, proto, sel,/ T& T8 s, V& l, M3 ?9 |
new FArgumentsImpl (this, sel)));
- y5 F- { e& g* y. [! `6 { } catch (Exception e) {
; {) i( H8 {# ~# c$ P2 ?" U e.printStackTrace (System.err); j T- n' y" A
}! \2 o4 f$ S0 g2 P
1 d' a0 T* Y" J( r
syncUpdateOrder ();" a5 h7 H1 f% U2 `' e
8 V5 B: [" d, O3 }4 G& I, @ try {
" T' I+ u/ w+ D$ C' i* b" T modelActions.createActionTo$message
6 y y3 Q: c' }; `& n# I1 i4 O9 l (heat, new Selector (heat.getClass (), "updateLattice", false));$ K* ^( X. k" S2 H, B
} catch (Exception e) {& B6 I4 x/ V8 ^$ e* m `
System.err.println("Exception updateLattice: " + e.getMessage ());
. Z3 D$ {& j) m% U& ` }
' l6 i+ d/ h5 u3 a ) Z! Y( ?' _! C. ?
// Then we create a schedule that executes the
* h! t4 P2 b: ^ // modelActions. modelActions is an ActionGroup, by itself it2 j& J8 N( J( F( a1 z
// has no notion of time. In order to have it executed in- }* L- R- n+ T
// time, we create a Schedule that says to use the
6 C0 g! L9 b/ ] j // modelActions ActionGroup at particular times. This% Q' X9 ?4 U+ ]+ _: v2 b# g6 o: y
// schedule has a repeat interval of 1, it will loop every
* m" p7 S5 D9 p# @$ {1 B" J // time step. The action is executed at time 0 relative to+ s7 V, S( K2 A4 c8 i4 s0 e. M& a
// the beginning of the loop.
" S8 X ~, t P
* n, F' [' E' Z" T // This is a simple schedule, with only one action that is6 n2 N4 L1 r' S! ^/ [) N6 l* y8 n
// just repeated every time. See jmousetrap for more
! b3 w: ^8 n- g. o) V. t( d // complicated schedules.3 q6 ^9 S# g7 _6 n6 M& @3 U
. `2 N. x1 b. h# S ^7 P modelSchedule = new ScheduleImpl (getZone (), 1);
* b- q# {; h( ?+ ]$ _& Y/ H8 C modelSchedule.at$createAction (0, modelActions);. e* z8 O( q5 `% l2 T$ v% U
" M% |& z3 p2 l4 G0 D+ D) z- @3 z
return this;
: |; u% T0 P2 Z* f } |