HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' s: F: u$ T9 W5 @5 v4 e0 a0 G3 K1 x* O' M: q* S! C" F
public Object buildActions () {
( ]- n" |0 `: a+ }. {# I5 R. b/ M super.buildActions();2 u t( h) m7 O+ ~* r
) ^! u- `6 {. B+ y5 V' |3 |
// Create the list of simulation actions. We put these in* E% }9 ^, j# j6 M- I$ R4 e
// an action group, because we want these actions to be2 i9 B+ q0 H7 z. N1 p
// executed in a specific order, but these steps should( V' r( Y0 O& S4 R7 y
// take no (simulated) time. The M(foo) means "The message
3 A7 A7 `% i: Y0 k. Q // called <foo>". You can send a message To a particular" G8 w. j2 k L! I/ [& b F+ O
// object, or ForEach object in a collection.: s6 H, H: v: j# l: M* {
v3 ]2 E3 z! Y' K$ B // Note we update the heatspace in two phases: first run% p8 F! I* O; K8 k; Y; ]
// diffusion, then run "updateWorld" to actually enact the6 y W4 ~) z+ R0 m7 c
// changes the heatbugs have made. The ordering here is
% p( B5 g: F% {5 J% o // significant!
- Y' Y6 s" X! I+ d
: X2 T7 K/ b5 j: \) L: b ]; c+ h. f // Note also, that with the additional9 I# l, U% y9 w, r/ |( [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) B- K& v, }! x // randomize the order in which the bugs actually run
- p% p, s8 P, e* [ // their step rule. This has the effect of removing any/ x' m4 n' x* L k
// systematic bias in the iteration throught the heatbug
0 L% _, B& q( S) G+ u- j7 O" A // list from timestep to timestep0 C3 f- o- f; w' S
+ a0 d. C& T& o5 R* G( v1 ? // By default, all `createActionForEach' modelActions have0 P r: B0 X3 W2 [! _
// a default order of `Sequential', which means that the c% i# |. E, d; C! h4 \! j9 B) O; e* n
// order of iteration through the `heatbugList' will be
3 K& r- J5 D. I$ t // identical (assuming the list order is not changed! Z9 X4 N6 J- H \4 d
// indirectly by some other process).
: ~+ p$ H9 E3 r! ?* R + i# B' ]6 n0 e2 e" C" [* t" F
modelActions = new ActionGroupImpl (getZone ());: ~& f, c) |0 p/ a$ W% W) h
% L P* `( c3 b try {
$ \6 w3 {" k/ O) u modelActions.createActionTo$message
1 K' L, Q3 ?: G" \' h (heat, new Selector (heat.getClass (), "stepRule", false));$ y1 b3 I/ E l1 `7 C6 O3 M# v9 w& l
} catch (Exception e) { u" [; R8 G* K8 s% t- |
System.err.println ("Exception stepRule: " + e.getMessage ());
3 |1 n1 p# |, ]. S" |3 J }9 L( c5 ~0 @; l6 c
0 j$ D8 ]$ K2 s" S try {% T- ]2 K3 E3 E! o" v5 F7 n
Heatbug proto = (Heatbug) heatbugList.get (0);4 z9 z6 r. r2 Y& E. `- ~
Selector sel = % ^3 O, c, v3 X8 Z# A# ~6 O
new Selector (proto.getClass (), "heatbugStep", false);
# K! v& U" N" H4 L1 V" S$ \( W actionForEach =
5 k# k( p1 h# ~7 U modelActions.createFActionForEachHomogeneous$call) J& J: m$ q4 Q; v3 b4 R: [
(heatbugList,
; v# [9 A. V3 ~! A3 {( N new FCallImpl (this, proto, sel,
" O! J" m6 O! `" n new FArgumentsImpl (this, sel)));9 w; t, {8 W4 E$ Z1 x: _$ Q: Z W
} catch (Exception e) {9 a9 ?$ i: g* c) W8 e% |5 K
e.printStackTrace (System.err);
9 E- M8 I. c( c% o/ ^/ Z }8 @7 J9 x( D0 U5 E$ N. _! `
* v! C/ A7 ~) C syncUpdateOrder ();2 @# q7 e9 l) @+ v. v
7 S$ D: t0 o) z1 o# f" i! q0 a try {# G- v6 y. w; ~. h" r8 I
modelActions.createActionTo$message # E- G# x% r2 \9 C$ e q
(heat, new Selector (heat.getClass (), "updateLattice", false));
" V: @: G M( S6 U3 P5 R8 O/ w } catch (Exception e) {* M' T, Z* J* a8 j. S
System.err.println("Exception updateLattice: " + e.getMessage ());, d$ }; W! u# m6 Y; H9 U1 j
}
! X# u o( }. T! U4 \. q
) r- d6 j; y7 p. U! g6 e' u. Y // Then we create a schedule that executes the) M* \. \* F2 t0 f1 f6 C% D3 e# ?
// modelActions. modelActions is an ActionGroup, by itself it! O' X" ?; W- ~& b, E
// has no notion of time. In order to have it executed in
5 _& ~1 z' f* Q" d* v4 a6 U0 N // time, we create a Schedule that says to use the3 ?3 x+ ~' `4 S% s+ x2 E' k
// modelActions ActionGroup at particular times. This
1 v2 E0 t! C5 ^, p( o y // schedule has a repeat interval of 1, it will loop every4 I& n3 [! F5 ^. |" i. T
// time step. The action is executed at time 0 relative to
. U( Z/ Q* E+ T2 m O6 I+ v // the beginning of the loop.1 `; f$ ]* ]7 t4 C$ B" o. L
/ h e2 X) M S3 I5 r8 [1 O
// This is a simple schedule, with only one action that is4 F) u6 \7 S" L8 i/ S9 M( h, S
// just repeated every time. See jmousetrap for more
" x8 B- A# n5 b: k9 H: {# F // complicated schedules.0 x2 v) y7 j" I" v$ i+ S
5 S3 y; a7 y( g# F @$ O modelSchedule = new ScheduleImpl (getZone (), 1);9 Y; k- `6 s6 d! r9 m( _
modelSchedule.at$createAction (0, modelActions);- M* K4 e& p# P- y! U. p5 p
- K1 f3 ] q( |( u* Y6 W return this;& j- q( [8 x$ {4 M ~
} |