HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 k! ~/ \7 y$ f% g* v5 ?" |- t V$ _. ?( e) ?* l. R1 j
public Object buildActions () {, q* O) e& E4 B, W; z6 p
super.buildActions();, w# I# V* e; e& @0 x" r+ H. V
a8 L5 M' v, U0 O+ v, i! h4 g8 z // Create the list of simulation actions. We put these in) v+ ]9 {' L% w7 M" U9 u. |
// an action group, because we want these actions to be6 z5 ?6 `9 q4 p9 C- z. M6 S+ O7 [
// executed in a specific order, but these steps should
; }- j' M: [- o3 H# r: A // take no (simulated) time. The M(foo) means "The message# D+ ~9 ?! N7 h
// called <foo>". You can send a message To a particular$ Y0 ]( E" m* q( h& ^# d
// object, or ForEach object in a collection.
}3 H* K+ p1 j * e. U0 D+ [2 _3 T% n
// Note we update the heatspace in two phases: first run0 c6 R# k! B8 N$ B4 y
// diffusion, then run "updateWorld" to actually enact the7 A4 Q: w$ _$ ~' W- A
// changes the heatbugs have made. The ordering here is
: X1 S) N5 ?9 q9 d // significant!
( M/ f+ `7 q! B' Q2 C7 H6 {2 _
2 J+ R; e9 l& i6 ^: [6 v4 z // Note also, that with the additional8 \2 B; X* l4 ~/ c% Y- Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. L" e5 g/ i8 ]2 p // randomize the order in which the bugs actually run
2 [: s. h9 G" j. F* J8 E3 b // their step rule. This has the effect of removing any: B8 c, N1 k# @4 o& g
// systematic bias in the iteration throught the heatbug/ r9 \0 `" ~# ]0 {, a" _7 V* B# P4 `3 a
// list from timestep to timestep7 o3 R/ r N4 M3 p' k4 s
, q2 x1 I0 O& q- p8 U5 A" G5 O9 g // By default, all `createActionForEach' modelActions have( D, r9 S2 G- d) N+ L: C
// a default order of `Sequential', which means that the
. N, g* ~9 ?7 n6 P2 I // order of iteration through the `heatbugList' will be
; f; [" O/ o; N // identical (assuming the list order is not changed3 N, R% i) B1 K6 {- X; J9 s8 H
// indirectly by some other process).7 A7 u u9 T7 H* t) D
* K; I; `& q; `, h9 ~ modelActions = new ActionGroupImpl (getZone ());% g8 A( `) e. b. Y" I4 A
- T, N( e9 c" a0 t# r6 X. J8 U
try {
+ S0 l$ ]# s/ z1 {& x0 t modelActions.createActionTo$message
" j3 H6 `& p" z5 S3 T* i Y9 ` (heat, new Selector (heat.getClass (), "stepRule", false));
0 T( Q- _4 d6 [2 l6 Z4 r8 n& e } catch (Exception e) {1 S w- p5 n- e* N |# L
System.err.println ("Exception stepRule: " + e.getMessage ());
" j7 `' `8 h# G6 l }- P- R6 J- K5 y1 @- y
. q! N: v+ i8 ]7 y9 p% k& h+ E try {
# {: H( \1 x2 b1 u& L Heatbug proto = (Heatbug) heatbugList.get (0);
# ^/ j. I2 W2 c9 M Selector sel =
Q. G5 o) ~6 j" d new Selector (proto.getClass (), "heatbugStep", false);
6 J) p1 e6 i5 { O; n, {/ u actionForEach =) K% w% C) f) W
modelActions.createFActionForEachHomogeneous$call/ L* u4 d" f1 _, D6 A/ ~
(heatbugList,
0 M. S- _+ p' k new FCallImpl (this, proto, sel,
* n; [5 s' `& o" x new FArgumentsImpl (this, sel)));, Q* M) f4 U1 X5 j: Y
} catch (Exception e) {$ q# j9 Z) I& n; R
e.printStackTrace (System.err);
2 H. x- O4 }/ E* F }
* m: o; H3 w" i1 t! k ' f' o0 S+ n9 x. `
syncUpdateOrder ();
0 s& r2 x7 b# ~% T/ ]
3 P$ U+ {0 z9 L; {5 R. F- C6 F try {4 t7 _6 e8 U. N- Q
modelActions.createActionTo$message + L7 h6 R4 H' W" f' H
(heat, new Selector (heat.getClass (), "updateLattice", false));+ S/ g. B/ m) c0 @8 ~' ~
} catch (Exception e) {
* L" F0 v. i/ h' y* k; M9 b7 @ System.err.println("Exception updateLattice: " + e.getMessage ());* `! S& k8 r3 u& ^# ^
}6 o2 [5 m' V$ }6 z6 |
0 g, G) M/ D3 W [ // Then we create a schedule that executes the; _% L) s. [# c5 }1 e# T+ \1 V
// modelActions. modelActions is an ActionGroup, by itself it
# s. G# B* O0 r, k; V // has no notion of time. In order to have it executed in$ N' L5 D' c, B( X7 ?( ~7 C
// time, we create a Schedule that says to use the
) Q8 G; x j6 a // modelActions ActionGroup at particular times. This) B! R: M' F" u0 l' m! \
// schedule has a repeat interval of 1, it will loop every
5 z: c9 `! l: e" ?+ x. a1 y // time step. The action is executed at time 0 relative to0 P3 g3 E r- ^6 I' A7 h
// the beginning of the loop. p* M6 r/ n* O- k8 v: \
) _0 L) H& m+ r" D
// This is a simple schedule, with only one action that is
+ r+ |$ E ]8 J7 ^ C0 A // just repeated every time. See jmousetrap for more0 z+ t% r' d5 Y" ^, d
// complicated schedules.; l5 d, ^. w" h3 G% b, q# L2 u
! X' f/ P. c8 j0 h modelSchedule = new ScheduleImpl (getZone (), 1);$ \ B' Z- _" E: R
modelSchedule.at$createAction (0, modelActions); O% k' R: U9 y r
7 e/ H! j0 u& ^
return this;4 e, U* F1 U7 Y" j* l* @
} |