HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 A7 G$ M' i* v5 |' g+ C1 L
' O1 U& Y1 a5 y) p( F
public Object buildActions () {
& z F8 k( N9 L8 G super.buildActions();+ ]5 U) l/ y/ y4 B4 L! A. I
9 r; A' n1 B& ~ // Create the list of simulation actions. We put these in
1 X+ e. J8 s1 |; J# l // an action group, because we want these actions to be
& k$ N0 i2 s8 Z4 r7 F // executed in a specific order, but these steps should
8 L; q4 J' A* e4 n // take no (simulated) time. The M(foo) means "The message& t6 o) o; c2 c- i7 \ b
// called <foo>". You can send a message To a particular# L6 g) O+ b% K
// object, or ForEach object in a collection.
$ @, y5 F* A: E/ u9 w1 F) [ w8 d! Q2 T# w
// Note we update the heatspace in two phases: first run
& b4 l/ I" q4 O8 ]; r7 }+ g, b // diffusion, then run "updateWorld" to actually enact the$ }+ i, C! D9 N: [' q
// changes the heatbugs have made. The ordering here is
+ ^" z( E+ m2 F0 O // significant!& P/ q4 h8 c1 m0 q8 P
2 \3 m/ u7 [" X X- u2 i; n // Note also, that with the additional* k% i& s2 e. h0 B- ^) `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 P! A9 Y6 |. l1 |9 }$ i8 D // randomize the order in which the bugs actually run
+ j/ [( o' V" v, R% m // their step rule. This has the effect of removing any1 W( z: y1 N! ]
// systematic bias in the iteration throught the heatbug
6 O1 s. T7 v, G- o // list from timestep to timestep7 C0 @( D% V- U! N% A
, W% d/ A0 `* K6 ?0 y- M
// By default, all `createActionForEach' modelActions have
# ~2 _6 ?, M. I3 ]+ G: L7 S1 I" p% L // a default order of `Sequential', which means that the5 u/ S" o2 d/ @1 G
// order of iteration through the `heatbugList' will be) ~. X0 _' B! n: x. E
// identical (assuming the list order is not changed
( K2 {4 u5 e3 M: O8 I7 _! b // indirectly by some other process)., ?* F- Q7 Q4 I
4 }: H" r2 D3 X4 `$ T4 o ^ modelActions = new ActionGroupImpl (getZone ());, o/ P) ]: J( e5 r& M
1 S1 A/ w5 } z try {0 f1 ]6 ~" S) m5 x* r. v; ^5 n
modelActions.createActionTo$message# X% u! `/ n8 T$ {9 `
(heat, new Selector (heat.getClass (), "stepRule", false));( t2 o- J$ g- s5 s
} catch (Exception e) {
( C1 @/ K- e0 m) A6 m3 O6 l; L System.err.println ("Exception stepRule: " + e.getMessage ());! _5 a8 n& Z- M4 r, D" P( m
}6 d. C# `) P9 |) p" o" @
. ^: p7 E! R' q& e, B# W try {4 F d5 L4 Z% E* y' @
Heatbug proto = (Heatbug) heatbugList.get (0);! w% P5 ?6 {5 V) ^( {' h) b
Selector sel = $ @$ t7 L$ t. d( O- z
new Selector (proto.getClass (), "heatbugStep", false);
) o8 E% l3 v+ q! w actionForEach =& K. j9 g/ V- }" I1 Z
modelActions.createFActionForEachHomogeneous$call
0 A4 J# C2 r {$ Y (heatbugList,
6 a( q# r6 O& C/ ]/ W$ i K- T8 P new FCallImpl (this, proto, sel,/ g. _+ D6 {0 A. U: B" w U/ Y
new FArgumentsImpl (this, sel)));
4 @* ~8 ^! P0 }5 \# t% c9 ~" m } catch (Exception e) {
) K) N% f6 x/ D- k1 B5 p e.printStackTrace (System.err);* @, k1 V) x% [
}$ V8 N8 w5 {8 H1 ` o! Q8 d% l
- [# C- {3 A/ U5 z# [4 Q3 r: L7 [
syncUpdateOrder ();
. H! z2 f5 Q# E8 N, l
" E/ A. f" I1 V try {, | w6 d. v2 U
modelActions.createActionTo$message
# m0 ^5 f, R% }, c (heat, new Selector (heat.getClass (), "updateLattice", false));
Z4 p! w: e- n: [: Z8 Q6 g } catch (Exception e) {
9 Z8 e c5 W- K$ b. s+ i System.err.println("Exception updateLattice: " + e.getMessage ());; {; n a8 u, S' J& L7 `' b
}
7 r0 w3 M2 D9 E$ L6 X" ?( j% K
- n; n+ `+ v% H% F2 T4 f // Then we create a schedule that executes the. M# R3 b) @/ ] I2 u& `0 j
// modelActions. modelActions is an ActionGroup, by itself it! r4 `' s+ c& ^* E3 z+ R
// has no notion of time. In order to have it executed in
# I( D/ X6 f" ?+ d // time, we create a Schedule that says to use the& Z, S7 W5 d, a( G. ]$ v n8 k
// modelActions ActionGroup at particular times. This
7 B& U+ n5 ~" T7 z1 }4 X // schedule has a repeat interval of 1, it will loop every/ p/ Z* c& h$ D, u5 d) k3 a7 `
// time step. The action is executed at time 0 relative to+ o% ^2 B; r9 {. \8 Z; W& N
// the beginning of the loop.
* s b. [9 y: w/ u; p- i) z
/ ?5 B- U4 q* G5 ]" w; \0 z // This is a simple schedule, with only one action that is
5 T" @/ V" H2 H // just repeated every time. See jmousetrap for more/ ]7 T, h9 {9 @
// complicated schedules.1 V/ y: i1 }2 q2 }5 a5 y9 l2 w0 n% J
: J- p8 m! ^. H9 u* n7 Z% y7 ~
modelSchedule = new ScheduleImpl (getZone (), 1);1 |4 M' U8 V+ R1 m0 T- P
modelSchedule.at$createAction (0, modelActions);
7 w7 T8 ]6 s+ J- n0 N' g 5 G* l# s X$ v: G/ {' T
return this;
5 I {1 f2 z( c7 Q5 @6 b' E } |