HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 R; ~9 I" Y. A+ X4 j4 P9 g2 \" g2 h5 F1 G; j* K
public Object buildActions () {. d4 T$ C6 d& a: |2 k+ Z1 k4 n
super.buildActions();
* |1 ]$ w. d! o q' `7 V1 r 3 X" z, ?+ Q* H4 z3 F
// Create the list of simulation actions. We put these in& e9 ~( S) N' @& s- o0 c+ A; r
// an action group, because we want these actions to be& N( J6 q8 H. _ @* l
// executed in a specific order, but these steps should
$ e; S/ V1 W, P4 z5 S. D* C // take no (simulated) time. The M(foo) means "The message7 O( }" `9 H% G
// called <foo>". You can send a message To a particular/ _3 G% @/ h- |4 p$ b) e" g% O
// object, or ForEach object in a collection., v3 l6 z. A0 p: M% `9 V& E- x9 F
! [! V4 K" ~' s i* d // Note we update the heatspace in two phases: first run! n G% j7 D7 z$ W1 p9 {
// diffusion, then run "updateWorld" to actually enact the
: ~. ^6 D9 {& T5 k: h5 T+ | // changes the heatbugs have made. The ordering here is7 g6 ?, R' j. @# e' N3 D p
// significant!! x, Z4 q4 ?; W: K6 D" P, p' o6 _
& r O! c& w, I5 p
// Note also, that with the additional5 U0 B4 K, f+ ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can% ]6 _) B9 k( Y& l1 {/ @' B
// randomize the order in which the bugs actually run
% d% k0 q& P" v8 Q, g* x [" Y // their step rule. This has the effect of removing any
+ x1 q, r* g1 ^3 D+ Q- v+ x, c // systematic bias in the iteration throught the heatbug
& E5 g) g) j; e" |" ^ // list from timestep to timestep- ]3 h3 g& U6 o2 Q" A% y# X
# G- v0 t# S- d& J# j% c6 c
// By default, all `createActionForEach' modelActions have3 W& G7 u$ D L; t6 @0 f- d+ P
// a default order of `Sequential', which means that the' ]9 c; e8 I9 H* I4 R A5 q
// order of iteration through the `heatbugList' will be
& ^: b n+ P1 r // identical (assuming the list order is not changed
2 G# k5 ^0 w/ o6 D // indirectly by some other process).
. k6 {& O- i: ]+ l3 ^9 n+ C& Q
/ v$ u1 Y& z& h modelActions = new ActionGroupImpl (getZone ());! T% I! {$ k# }: `
- w; g+ I# E, D% M1 G+ X
try {
4 j: ^1 E7 a+ g% _+ e modelActions.createActionTo$message. J6 |+ S5 A+ X" E1 ]
(heat, new Selector (heat.getClass (), "stepRule", false));
4 }2 s! d) b1 ~; V7 ^1 T4 c } catch (Exception e) {
; F0 @4 _4 G# x; t E System.err.println ("Exception stepRule: " + e.getMessage ());5 n9 v4 |2 C W6 W5 m# v0 @2 i$ R
}
! U+ w, z* j9 {; ?3 e, r
7 f6 d/ j7 `. K; O try {
g* H/ [ \9 _3 t7 T" {( B Heatbug proto = (Heatbug) heatbugList.get (0);6 [, Y- r( O9 ?8 D4 N* F6 D
Selector sel =
4 t. f8 m4 ~; Q9 a. q; e new Selector (proto.getClass (), "heatbugStep", false);. Y( j" V& S9 j0 k+ A% O, S' z
actionForEach =
; j5 i& H2 n& |7 F modelActions.createFActionForEachHomogeneous$call
0 M8 g, x c9 o! k& J2 S (heatbugList,/ T0 V9 e4 k( o0 q& \
new FCallImpl (this, proto, sel,7 w( s" d0 J7 P N
new FArgumentsImpl (this, sel)));* D8 p5 f) h, u6 T6 ^
} catch (Exception e) {
4 F! |' I/ S; ?: a, j e.printStackTrace (System.err);
& v. i" F& @5 A6 ^. x }
! x4 U4 K3 M K& w; m4 b' g
" s. R9 B* A* Z' B syncUpdateOrder ();
+ M, n c2 a9 V) ^/ b- n4 E) a$ |6 {. |
try {
$ O* [ e0 j1 o, P8 N* N# W modelActions.createActionTo$message
, ~. ]* f: e% E" z( c9 g (heat, new Selector (heat.getClass (), "updateLattice", false));
% A9 A5 V' w4 ] a } catch (Exception e) {
! ]: d/ Z! x7 m6 l) P System.err.println("Exception updateLattice: " + e.getMessage ());/ t1 s4 L" D" l3 }; E/ M
}
/ Y+ \! y0 u# A- C + K% r- `% n, N0 Y' F& A' {2 E
// Then we create a schedule that executes the7 X o% s# A9 ?2 A$ a2 a F
// modelActions. modelActions is an ActionGroup, by itself it
7 c6 G% u. ~/ x // has no notion of time. In order to have it executed in# q7 W- o* }) U1 Y
// time, we create a Schedule that says to use the
0 C0 `/ {6 f3 X // modelActions ActionGroup at particular times. This# [6 x: B# S& T7 }7 _# {( f% T
// schedule has a repeat interval of 1, it will loop every
- h" }# _: W+ B7 E // time step. The action is executed at time 0 relative to g$ I1 p5 ], k8 j7 a* Q' S% H
// the beginning of the loop.
+ F) P. [8 u: K- n
) ]! {5 a% q) [* G // This is a simple schedule, with only one action that is
( g6 T4 C" z) X4 K2 w' S, @ // just repeated every time. See jmousetrap for more
/ Q! }+ Q" x8 F/ z% y" q // complicated schedules.; H- K* L9 ^& ^5 E3 c
( W$ A b7 N! V! B9 y& R modelSchedule = new ScheduleImpl (getZone (), 1);, Y- i" \+ ]( f
modelSchedule.at$createAction (0, modelActions); o' _( ]! _( X2 [
0 o! D2 {0 ]" F) k/ ]1 F
return this;: k+ B0 M3 _, T
} |