HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) G9 ^. T/ n5 R6 m4 L# Q j- v0 _+ _) B" r$ B' H
public Object buildActions () {7 O' ~2 d3 R9 A6 l# z
super.buildActions();
, Q+ W& J( a' h8 r ) h5 w, z9 e6 Z' l h) u5 \) k* r
// Create the list of simulation actions. We put these in: k2 V5 B! @* ?8 x
// an action group, because we want these actions to be
/ k- S5 T$ F! |8 Y3 n. l // executed in a specific order, but these steps should
8 I8 U O# y- T: O" c8 Q1 V // take no (simulated) time. The M(foo) means "The message
! Z, n4 Z1 ?$ \) B# B* v // called <foo>". You can send a message To a particular+ G# [) m& \) b# N* m' x
// object, or ForEach object in a collection.
L) z0 K- N, Q/ Y+ e e; Z
* a* H' y1 j; q4 {" h // Note we update the heatspace in two phases: first run: @' v# m s' O% `0 k* G
// diffusion, then run "updateWorld" to actually enact the+ v3 Y* k" d( s6 H1 _
// changes the heatbugs have made. The ordering here is
2 o" f3 N5 s1 Q& G // significant!
z. ^& ]0 R: k- R . v! w t0 X% _8 t; c9 m0 F& e% n
// Note also, that with the additional* c3 y% W& E0 e, O8 s8 i, T
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 X! s; S! e% g; Y
// randomize the order in which the bugs actually run
. ~2 i v, a% B, ]7 ^ // their step rule. This has the effect of removing any) y6 X& a9 k) A1 b: u
// systematic bias in the iteration throught the heatbug
0 g w+ S) B* Z. _7 O // list from timestep to timestep
4 F5 n, n$ e4 c6 U9 H8 |
4 s4 b% w( u0 h# N* T8 ? // By default, all `createActionForEach' modelActions have
" \$ R: `, ?% o9 x // a default order of `Sequential', which means that the" _; _) ]2 Y1 m
// order of iteration through the `heatbugList' will be
4 m7 h2 P4 n% x/ h* Z7 O/ @ // identical (assuming the list order is not changed
$ b2 b' j9 S, [# \. i! v" K // indirectly by some other process).; p" ^6 d, j( V! d" J
( i8 F, b; j/ o3 @ modelActions = new ActionGroupImpl (getZone ());
2 o% F! c" I9 G+ l7 ~& p4 n7 h, E( ~% r' M
try {5 W4 ^: P5 q {6 D, Q i# M _
modelActions.createActionTo$message
( t7 c# k! x; f. Y& O (heat, new Selector (heat.getClass (), "stepRule", false));
& O0 ]( g9 N. w: ?8 h5 q) W } catch (Exception e) {
0 D/ P% z$ L; d0 m1 m. W System.err.println ("Exception stepRule: " + e.getMessage ());% m# O: n1 ?# d$ E, e- s( J
}
2 L6 t! ~" E. l9 O. w( G: _4 ? R7 e( W, ?- N3 u9 O8 Z; ]
try {
$ G1 l& e# \8 S! ` Heatbug proto = (Heatbug) heatbugList.get (0);
2 _4 A. v4 N" n Selector sel =
" X5 {7 `3 E2 `+ F7 R6 d: B- N+ K9 a new Selector (proto.getClass (), "heatbugStep", false);
3 N, Q: B7 c( @3 t: p, k: M actionForEach =
( B) z3 h! _8 N! j modelActions.createFActionForEachHomogeneous$call: s0 g4 |$ G# h$ U" z
(heatbugList,
3 @4 b" U+ ?6 U. X new FCallImpl (this, proto, sel,% p! x/ y% Y, H, e9 A% [4 \/ _
new FArgumentsImpl (this, sel)));
% o) s# q, |0 U/ M1 H8 { } catch (Exception e) {* M* I+ L# ?7 W% H9 o6 H
e.printStackTrace (System.err);
4 Y) Y) u' K# \; }6 b. E9 y7 ~0 U. l9 [ }
4 S( f1 ~* B6 ]. z/ z0 R7 n 6 n' Q. L9 P. O, M/ G1 t
syncUpdateOrder ();
* O% k! v9 n4 A! D' a! w: Y! S1 y/ l* a
try {( C* {1 W, F$ {7 X0 |! @( r4 X
modelActions.createActionTo$message
0 T9 k2 }4 s& v5 J; [1 l& b (heat, new Selector (heat.getClass (), "updateLattice", false));
) C8 ^8 s: z8 Z; a6 x5 G! H4 @; j } catch (Exception e) {
: ]5 Q6 ]: Z. y) M7 F, e System.err.println("Exception updateLattice: " + e.getMessage ());+ R2 J( w9 I# f
}# p: |; d0 ~$ c
$ i! p; `3 i5 k% v# y" J
// Then we create a schedule that executes the1 Z! Z% b! j& K$ J
// modelActions. modelActions is an ActionGroup, by itself it8 _$ X5 i# p' W3 j7 r- b: X
// has no notion of time. In order to have it executed in& j0 r4 s I0 h6 O1 M- }
// time, we create a Schedule that says to use the
: }* _7 p6 a9 n // modelActions ActionGroup at particular times. This
( I& I/ C, d% }, z* N // schedule has a repeat interval of 1, it will loop every. @- q7 O$ d6 \8 r. p9 Z) ~+ T
// time step. The action is executed at time 0 relative to* }- n9 a0 }$ X2 w
// the beginning of the loop. c) l; r) K" X6 b
/ x( o2 e- X Y$ l
// This is a simple schedule, with only one action that is" g( \/ S6 r7 b, r, b4 ]
// just repeated every time. See jmousetrap for more
) i9 ~* ], K$ g7 N0 ^ // complicated schedules.
% X( u5 U( I& @3 ^+ ]' x# G- d; D9 a
1 G) o# D/ s; b* m% A9 N modelSchedule = new ScheduleImpl (getZone (), 1);
' |+ W7 Y* k' F# E% G0 J+ W; J9 V modelSchedule.at$createAction (0, modelActions);
" A8 a" H6 F [" U3 O7 V: M3 d
' Q8 n# k! Z1 R- w return this;% S5 }4 O [4 i5 B& C, u
} |