HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 ~6 X1 I A1 `# |
+ A, ]# j0 J- c$ T& `; t4 [ public Object buildActions () {
9 o# ?2 s# s7 ? k1 D super.buildActions();
6 }- h G' \& Q% w+ a& d8 K
1 R4 L! k; K1 @* J+ u1 I/ W // Create the list of simulation actions. We put these in
& A8 t! v4 J- u+ Y: A% U // an action group, because we want these actions to be5 S. m9 G# p( H
// executed in a specific order, but these steps should
% Y0 W! I( z9 j5 q7 M // take no (simulated) time. The M(foo) means "The message$ L2 B5 B. ^4 ]7 C' t: {
// called <foo>". You can send a message To a particular
6 v2 r3 |% A) ^! u. X) d8 H // object, or ForEach object in a collection.
$ `$ [ H( ~( @) B( B9 m
7 l: ^. ~: ^% K: f& [- r // Note we update the heatspace in two phases: first run
8 c; A7 @( ?* M' X' K! H // diffusion, then run "updateWorld" to actually enact the0 _. Q8 I6 y9 F5 n9 m
// changes the heatbugs have made. The ordering here is8 h: J$ P& t5 B. l5 C/ d, T
// significant!: e" r! E. @, g! y& j
2 m/ A) I2 z* Q7 H& N" T // Note also, that with the additional
5 U6 h2 B4 o0 d; i) {/ ^6 g* d // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 Q+ Q" e% Q) Y! E; B4 J8 c2 s2 t // randomize the order in which the bugs actually run
; P. d$ n9 {1 U // their step rule. This has the effect of removing any5 [- L# ]! _: {4 p& I+ u
// systematic bias in the iteration throught the heatbug
( A; `& Y4 h1 P) t: F T // list from timestep to timestep
9 U- V% L& c O4 h
: M* d/ V+ b4 W! [+ l, M // By default, all `createActionForEach' modelActions have# L i) f, |0 N/ L
// a default order of `Sequential', which means that the
2 V. W- f& A" s! y, Z) { // order of iteration through the `heatbugList' will be3 R. m2 J9 Y9 k4 h, A) K
// identical (assuming the list order is not changed( b7 z8 ^+ V0 r1 f
// indirectly by some other process).5 J0 p5 p9 p' M! D
; V) F* i- F) `! l
modelActions = new ActionGroupImpl (getZone ());
# y9 B( G; ~2 I$ H) }9 a
: d g- J! K; ?, ^5 c try {$ M4 K7 E) E5 E/ Z; d5 P5 z
modelActions.createActionTo$message2 w1 L. i: _& w
(heat, new Selector (heat.getClass (), "stepRule", false));( C0 ?6 c! ~7 {: {3 L
} catch (Exception e) {
' e' [( K: U% t5 {8 R0 ^ Z System.err.println ("Exception stepRule: " + e.getMessage ());
# r- v6 {5 m- }. D3 m0 S }8 _8 p+ b) s! [: O
. i8 e: T7 |$ U) G1 | try {% ~/ q! Y6 g% E5 W& q
Heatbug proto = (Heatbug) heatbugList.get (0);
$ `0 @! |; y6 M" }5 y- F `1 l Selector sel =
$ Y2 k3 _: \4 n new Selector (proto.getClass (), "heatbugStep", false);5 h5 y: n1 D8 r# R f8 m
actionForEach =( q2 } y( k; `8 b$ x- R
modelActions.createFActionForEachHomogeneous$call9 n% V- G; J* v4 p q: g ^
(heatbugList,/ t$ h9 S: ]3 |0 Q) }; x
new FCallImpl (this, proto, sel, J9 _6 s ~6 w$ x5 ^) G8 d! I
new FArgumentsImpl (this, sel)));
6 O4 O" t9 [! ^1 V( V } catch (Exception e) { w: G8 Z: n( d
e.printStackTrace (System.err);
0 N- C0 @7 `$ ~ }! s! N# z4 j* G! {5 b. ~5 u
' [: [$ g, w3 F3 G
syncUpdateOrder ();
. V9 D/ Y7 m/ B7 Z& r: P% [3 g
2 x, n4 v3 |+ J& {6 ^+ M L: J try {
: ]+ d5 |2 [: a0 a1 | H$ |! n+ P modelActions.createActionTo$message
& x O8 u8 U5 p+ L. a4 v (heat, new Selector (heat.getClass (), "updateLattice", false));+ [. e$ X+ s- j& I3 |* u, ~
} catch (Exception e) {, b/ \' c" x9 y- `" k
System.err.println("Exception updateLattice: " + e.getMessage ());) t ~6 `$ ^5 U
}
4 H' N" ^. @3 V. t7 \1 K3 x; L: {1 R
7 R% t8 L. D( j: y // Then we create a schedule that executes the
+ P7 R- c V# x // modelActions. modelActions is an ActionGroup, by itself it
8 ~4 Z. |9 d: y: C& \: d$ G5 z7 W // has no notion of time. In order to have it executed in! D. b: I" G8 j2 u3 S- _- r6 j
// time, we create a Schedule that says to use the ]3 }5 K8 N; K& F: f
// modelActions ActionGroup at particular times. This
% r8 i- M1 W3 H // schedule has a repeat interval of 1, it will loop every
9 q2 K% ^9 S3 T) W // time step. The action is executed at time 0 relative to
% G3 t; v# {" ]) |0 u) c% p) P // the beginning of the loop.3 r, i+ z1 E# G& v' l8 t
4 V" \1 ]" V4 `( X6 q4 h/ F
// This is a simple schedule, with only one action that is2 u4 k. f% B7 w4 R) E
// just repeated every time. See jmousetrap for more
4 s: K" k6 ^/ M F# N6 H // complicated schedules.
9 t: r. Z, P/ k5 C' ~ $ [' h3 R9 _* H% I7 X+ s }
modelSchedule = new ScheduleImpl (getZone (), 1);
& S; C3 e- E' v( @# s modelSchedule.at$createAction (0, modelActions);
- g: e; {' T4 f# m
, G4 D% j! i4 P- h: U/ z return this;
" G! T" k: z3 ^+ T7 h$ m } |