HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 N& J/ \6 k# h6 _) O0 s: B: \+ z
* l; ~; N: U4 c' d8 ~3 l7 T public Object buildActions () {
, G; c% S6 e2 x, R4 ~+ o1 | super.buildActions();; ^* A3 z! t% r
7 U2 `9 O; B% P. ] // Create the list of simulation actions. We put these in
. r5 W$ U: q4 l2 b4 o0 b // an action group, because we want these actions to be) n5 y! {$ P3 z0 u+ i
// executed in a specific order, but these steps should
, N) y, }$ f. x9 ]" i2 S0 v // take no (simulated) time. The M(foo) means "The message
. [5 V1 I! M1 @4 N // called <foo>". You can send a message To a particular ?4 w( ^ ~, B M8 L! V
// object, or ForEach object in a collection.
9 i! ?% ?9 y& [$ Z% P 9 `; x; u6 c& f7 R1 j
// Note we update the heatspace in two phases: first run/ P2 i8 A Q1 S# f
// diffusion, then run "updateWorld" to actually enact the& ]" L5 ~- d J! K; B
// changes the heatbugs have made. The ordering here is
5 J8 o6 Z5 x7 g // significant!
- C2 B' u ]! \" X% K
5 t! ~0 \8 S6 s9 j1 q // Note also, that with the additional
3 S$ D! G4 Z- W+ h) ? // `randomizeHeatbugUpdateOrder' Boolean flag we can) j; f# j+ Z6 ], [% x
// randomize the order in which the bugs actually run
: Q4 D/ |$ J9 w" h // their step rule. This has the effect of removing any
' u9 R, H- O& M8 S" H // systematic bias in the iteration throught the heatbug2 @& [/ O, I8 {$ e$ T
// list from timestep to timestep
: [+ k" e9 s. d8 b* A: T( h$ ?
4 }1 n1 t: [/ q' G2 f2 c7 y' \ // By default, all `createActionForEach' modelActions have( o9 Z- M" H: K0 q. h- Y' p; o# }
// a default order of `Sequential', which means that the: O, f/ V9 U6 j- G( S
// order of iteration through the `heatbugList' will be) R5 [8 w$ V1 q9 ~" b
// identical (assuming the list order is not changed4 S* j; {( ^' ^/ c3 |2 A
// indirectly by some other process).
6 L# k# {4 v$ r' x; U. w. e& g, n ; R3 P \. [3 F |
modelActions = new ActionGroupImpl (getZone ());+ I$ |9 r2 I& J# o+ Y
( p) [+ t- x+ Q& e$ H- d P try {
4 }9 i! W- `+ g5 w. A modelActions.createActionTo$message% `+ r) q0 o& t/ X
(heat, new Selector (heat.getClass (), "stepRule", false));: `; {0 {: T, c6 f7 }9 m) `
} catch (Exception e) {5 h+ j+ U# P; `: E: _" w
System.err.println ("Exception stepRule: " + e.getMessage ());4 C* C7 T- ?6 B* I+ ^
}
* _7 w7 X2 g9 Z: `/ o: `4 w
, c Q* G8 i' w# h& g) D: G8 L8 U try {. B4 X0 S5 ]% s3 q
Heatbug proto = (Heatbug) heatbugList.get (0);
5 a8 |. U1 I: t1 s* a1 [6 v Selector sel =
0 A5 v3 v+ y, ? new Selector (proto.getClass (), "heatbugStep", false);
9 C8 s, V9 U' y* ^8 p actionForEach =) ~; u! u3 Z" t9 K8 o+ N' j |
modelActions.createFActionForEachHomogeneous$call; c: V3 ?) J9 P Z
(heatbugList,2 Z" S5 c9 E" h6 i9 u: e% x
new FCallImpl (this, proto, sel,
4 j/ L3 G) x0 E, {3 S% @8 M& _+ P" ~# h new FArgumentsImpl (this, sel)));
+ m- s) r; A, X- e# Q } catch (Exception e) {
0 w: m9 x: s+ ] O$ v e.printStackTrace (System.err);
; l3 n/ N, i4 i' K" x" ^8 G1 c }
4 W# q& u7 Q. T * I% N6 S3 C* z. Z/ J" j) x
syncUpdateOrder ();- P+ [. n2 ?. z# M2 `
. o& ?% }4 B" [# R: T/ N
try {) `6 J% l$ }, B" q P, E1 E$ S
modelActions.createActionTo$message
" O! L6 G7 B, }, {$ V (heat, new Selector (heat.getClass (), "updateLattice", false));: X5 U% \) _7 P. o# K1 d' K
} catch (Exception e) {
- Q. K5 W K4 l: m System.err.println("Exception updateLattice: " + e.getMessage ());" A2 X7 [7 P) j
}( e( b; u i# K; g" K0 x7 G2 e& s6 D
3 i0 W4 |! H# e5 a/ H
// Then we create a schedule that executes the
. T1 d2 e/ E2 I; ~& u // modelActions. modelActions is an ActionGroup, by itself it7 p6 o' V8 s) N7 Z
// has no notion of time. In order to have it executed in
% J `# x1 W% X1 _8 k8 [ w- {% } // time, we create a Schedule that says to use the P' [; Q( V" ?
// modelActions ActionGroup at particular times. This. y! E* n f: A3 Z9 z9 b2 k, e- c
// schedule has a repeat interval of 1, it will loop every4 W$ f! f1 C" R
// time step. The action is executed at time 0 relative to
% f7 k& M% Z& ]% Z // the beginning of the loop./ g; y+ r! w3 s8 n+ a5 m
7 b6 M8 g1 [* u5 k' {' ? // This is a simple schedule, with only one action that is
/ F- } R5 `- d' s% @' v0 e3 m // just repeated every time. See jmousetrap for more
8 B3 W5 V+ q) }: A& i% B' D7 Z" X // complicated schedules.1 M1 O6 M+ C8 g3 N+ v
7 i' n# D2 B7 K5 `' i
modelSchedule = new ScheduleImpl (getZone (), 1);6 B! [7 x" P4 T
modelSchedule.at$createAction (0, modelActions);; S8 c1 H9 E. x! w& O
0 `' W% C$ j& }6 n* f) \- g0 f
return this;
. @: {" j, ^' ] B7 q7 r } |