HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- h* J5 e" b! |& Z( g
0 l; l' O8 m: p+ ] public Object buildActions () {
* o$ `# _; x( h8 ]2 \9 X) s super.buildActions();
' q s% H6 A7 {! A1 `: x 0 x2 A& g2 V. o8 U" T
// Create the list of simulation actions. We put these in
2 B5 D" \ t) }' J8 l ]7 ~. i c // an action group, because we want these actions to be2 s7 m2 L& Y; m* D1 X
// executed in a specific order, but these steps should& @' f1 X |) i2 i3 J0 m2 y
// take no (simulated) time. The M(foo) means "The message
' E! N0 o7 ?5 b) i( Z* @ // called <foo>". You can send a message To a particular2 }3 |* R) i v2 {% Z: A/ _
// object, or ForEach object in a collection.. Y7 I/ w1 _( V5 `: t- U
; @0 K+ o! d- S3 }
// Note we update the heatspace in two phases: first run
: \9 K4 i0 }6 r! o8 f d W // diffusion, then run "updateWorld" to actually enact the i& |7 X; ^' n$ d
// changes the heatbugs have made. The ordering here is! d; y W* l: K2 G1 n
// significant!) U6 s7 X# N2 i0 w5 ]
3 |2 p: T5 j! w: b
// Note also, that with the additional+ f8 T1 p! F% |+ R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* r/ N: M! z" o3 F+ D' _2 b // randomize the order in which the bugs actually run
# o7 e/ \3 {, {: [ // their step rule. This has the effect of removing any. X1 A3 u) a/ M' Z4 U
// systematic bias in the iteration throught the heatbug
# `* E3 |5 K- \8 T# s0 ^ // list from timestep to timestep
# }% H. O& O6 u" r" ~1 E @ - Y. j# J+ o. |' Z3 k8 q& A# s
// By default, all `createActionForEach' modelActions have
5 g3 M+ W; }# ]. B+ u' j // a default order of `Sequential', which means that the
3 @5 h7 M/ z7 E% h // order of iteration through the `heatbugList' will be$ [) @( g3 _3 H
// identical (assuming the list order is not changed
8 X6 G+ M* [: ?3 E // indirectly by some other process).; X: l0 U) u7 h, k: \0 t' X
: p5 j& }0 t$ V3 f
modelActions = new ActionGroupImpl (getZone ());4 ]# c1 L" J& U) f
: Q# F; H5 d8 i
try {/ e4 O8 K: @/ U0 \3 z9 X, m
modelActions.createActionTo$message v; i0 b2 m3 s/ \1 I
(heat, new Selector (heat.getClass (), "stepRule", false));1 U' c2 j9 Y7 W7 H9 ?
} catch (Exception e) {
1 A, a* H; V8 v1 _ System.err.println ("Exception stepRule: " + e.getMessage ());, B8 k* W7 w0 i
}
$ T+ C, m G9 F0 A! i: b2 K+ B
5 U7 w+ a4 t8 ~3 G$ f' | try { H+ w4 e9 L% G p* v+ ]- j# Z0 S
Heatbug proto = (Heatbug) heatbugList.get (0);
8 }. Q! u& `1 i. i$ ^' m, U. c9 \3 W Selector sel =
7 w- z3 l. k; c+ f" l new Selector (proto.getClass (), "heatbugStep", false);2 \ b: I; T7 b" _ W9 e
actionForEach =5 c+ G' x$ h* l: o# Z/ K
modelActions.createFActionForEachHomogeneous$call
0 A' T% s& a. J7 k (heatbugList,
: L# H9 P# m8 N new FCallImpl (this, proto, sel,
) s7 {- C' `* t: o$ y5 H new FArgumentsImpl (this, sel)));
9 ^' F/ {3 l7 S$ Y; ~ } catch (Exception e) {/ D0 M4 Z- x) x$ H0 i/ i
e.printStackTrace (System.err);
% I5 L3 d. K* P! m& G+ V! k }
: ?1 i R6 X: z8 {+ X2 K
; x S+ o% ]2 E# S/ J# E syncUpdateOrder ();
5 ^) |5 X$ s V. k1 V
4 l A* U, \4 Q8 m9 S2 p try {
7 n6 b, P% _& o Y5 Y% a) d# d2 J modelActions.createActionTo$message 9 H& l+ W$ ?6 h7 @3 O
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ e7 h! |' F- G/ W j$ R. {4 g+ f } catch (Exception e) {+ E3 F" O2 I( ^6 u3 J* v
System.err.println("Exception updateLattice: " + e.getMessage ());
: {; E0 d! o4 n( y4 S+ A }4 }2 [5 z0 t% m: p I u* D8 J# N
2 j( e' |1 T1 c7 k // Then we create a schedule that executes the' k. N" S4 ]5 z$ \3 z
// modelActions. modelActions is an ActionGroup, by itself it( G* i6 r3 v2 {% m) e0 N: @
// has no notion of time. In order to have it executed in
9 y6 {( A. g" G! x) w // time, we create a Schedule that says to use the
. u* H2 Q; K- Y& d3 R _# s$ Z // modelActions ActionGroup at particular times. This# h- o, I" g- r) D0 G/ C$ c
// schedule has a repeat interval of 1, it will loop every. c; M; S$ a+ E
// time step. The action is executed at time 0 relative to
9 ^! _+ b0 w7 X9 ~ L5 i // the beginning of the loop.
5 C; ~! ^1 u% S6 }$ f) c/ ^/ |1 }2 P _
// This is a simple schedule, with only one action that is0 ?& n4 `5 j0 P% X4 r
// just repeated every time. See jmousetrap for more3 g' P+ s: K! a) L
// complicated schedules.
! y+ v, S+ I6 a; ]/ o
* t3 R$ Y' y9 g+ q* `1 ^# r modelSchedule = new ScheduleImpl (getZone (), 1);3 ]$ c$ l: `9 h& e' S5 n* m2 Q
modelSchedule.at$createAction (0, modelActions);
4 E% y. q' x9 l" T+ N 7 l0 z0 H5 }. c: g7 A3 _
return this;4 v6 y' F* J4 G
} |