HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 Q g. A4 k+ g: u2 I
( A: ~- t5 S% ]' a$ c& m/ h9 x; C public Object buildActions () {
+ J) F) }0 |4 A. y' m: R+ G super.buildActions();
& Y1 s" ^- j6 e
. v/ m; G$ `; X // Create the list of simulation actions. We put these in
' J5 d8 i- C4 w0 M6 U6 E7 A: {, t/ E // an action group, because we want these actions to be
, W% @3 u) p( G1 b/ k' M$ d/ x // executed in a specific order, but these steps should6 @- X- M. B# }! U$ O: w1 ?% J
// take no (simulated) time. The M(foo) means "The message, u( A; |) J+ G- m% z6 K' D
// called <foo>". You can send a message To a particular9 }6 {5 C+ K% S2 F( r4 b$ j, `
// object, or ForEach object in a collection.
& m7 ^3 q' y# D& q' W- _- G( e ! x/ p( i4 A$ l1 B( a" z4 p/ I: Z
// Note we update the heatspace in two phases: first run7 Z/ A3 k9 T8 s, v( z
// diffusion, then run "updateWorld" to actually enact the
) y* _" @# K+ q- O# n! h' A+ J // changes the heatbugs have made. The ordering here is2 k* E5 \, ^: n8 B* Y& o# D: C% b
// significant!
8 X: Y2 j# q: ]6 {+ x
( Y% B: m" B( L" U2 l: N' u& X // Note also, that with the additional
7 e1 D; i4 t7 @5 s! l- ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 w9 g. S4 v( b5 R" t8 N# h2 N$ H // randomize the order in which the bugs actually run: o) r2 E3 C( t, m C! _ |
// their step rule. This has the effect of removing any% g* {6 W1 g* Q) P
// systematic bias in the iteration throught the heatbug! |0 v* T" i c0 I% N- L, ^: t
// list from timestep to timestep
8 b. ^/ t% _. Y% _
. x# j% U* e1 |& w- z5 K* B // By default, all `createActionForEach' modelActions have0 e! L3 W/ J( _! N- W c
// a default order of `Sequential', which means that the
, s( [3 c1 U9 B9 r. [ // order of iteration through the `heatbugList' will be1 |- `) B" u8 G0 D1 H
// identical (assuming the list order is not changed! g/ K" j/ N4 g' Z" }+ j
// indirectly by some other process).# i8 S5 S9 f. n' i, r) W
: x2 {) r( [$ l. Q5 v( _ modelActions = new ActionGroupImpl (getZone ());
! ]& I( R# ~, x* S' V6 U6 _1 I2 ]' @7 F; o- m3 ]
try {6 [1 d" l" p$ o" \# E3 {
modelActions.createActionTo$message4 u) f# s' r$ J: j' G9 G9 w1 L
(heat, new Selector (heat.getClass (), "stepRule", false));
2 y; M( e) s d t* `1 u) D. ? } catch (Exception e) {' u* m6 x% z( x$ N. W
System.err.println ("Exception stepRule: " + e.getMessage ());
" d. b& J" }4 G }1 n: c7 G9 A# H. U
7 x8 M5 {" _4 ~0 f
try {2 q8 ?5 ^' ~' P& |
Heatbug proto = (Heatbug) heatbugList.get (0);
5 L: O g1 g- A Selector sel = ! q9 g3 Y- c) k, V+ ?: S
new Selector (proto.getClass (), "heatbugStep", false);0 E+ y9 ]6 t; X5 f
actionForEach =6 D- Z( j2 ]! f) f' i) l* {: D
modelActions.createFActionForEachHomogeneous$call5 u) H; }( D. I
(heatbugList,. z1 R) B4 }7 \* D: c
new FCallImpl (this, proto, sel,2 }6 x4 o c9 q0 B
new FArgumentsImpl (this, sel)));
5 _4 M6 P. g% H* Q) H } catch (Exception e) {% |( U9 F& U9 g5 |
e.printStackTrace (System.err);, _6 [/ l7 L& F% ^; o1 u
}
' Z) u3 c4 t/ |4 j, f2 { 1 Z% ^8 o) v: j7 M
syncUpdateOrder ();; _* E& i' c- a: c
* P. e' m* s0 t try {
3 Q; ]$ U/ Z# t2 [: {1 D% c modelActions.createActionTo$message & K% [1 S& ?6 @, G6 n+ |
(heat, new Selector (heat.getClass (), "updateLattice", false));$ @) a2 z: ?6 U$ L
} catch (Exception e) {; }8 \# x+ E* F/ ~' @2 Q# r; l
System.err.println("Exception updateLattice: " + e.getMessage ());; x. o- ? B, T. w
}7 o6 i/ A5 I2 H$ i
7 [* q5 l' K$ S
// Then we create a schedule that executes the0 F9 l9 N' [2 d
// modelActions. modelActions is an ActionGroup, by itself it
/ A2 Q# `" }0 C9 c- ] // has no notion of time. In order to have it executed in
% @$ q m" t- } // time, we create a Schedule that says to use the7 R5 _, ?- {% R! L7 _) z$ M+ V1 [0 `
// modelActions ActionGroup at particular times. This8 j2 F+ Q4 G3 _% ~: _ J" ^6 l! o+ Z
// schedule has a repeat interval of 1, it will loop every
! z* H/ b" m2 z* t // time step. The action is executed at time 0 relative to# n a+ i% z4 [) s6 O" M
// the beginning of the loop.
, X% s& q( x/ F! ?
" q2 v- K2 f0 A' z // This is a simple schedule, with only one action that is
1 q! b( w9 p: j4 x6 }* { // just repeated every time. See jmousetrap for more- L6 l4 o* k/ u- W& x- g
// complicated schedules.
# o7 d3 `3 Z) \# m# ?! {5 ]! h% E" C
) c3 v4 A5 v" s5 s modelSchedule = new ScheduleImpl (getZone (), 1);- t( `8 N7 F: w
modelSchedule.at$createAction (0, modelActions);. E3 h) B" H% [' ^' }
* U* h5 x) ?1 y' F return this;! N% s G* e& A. ?; S
} |