HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: e4 o8 D z) |$ U" D
9 o% H: W) X* Y2 n$ u- O6 Q0 [
public Object buildActions () {
( Q/ x, b" [5 S% e3 I5 S! k super.buildActions();
) d7 k/ O H+ [
/ \) A- m6 ]9 ` // Create the list of simulation actions. We put these in
) _5 Q9 G9 q+ _6 {0 l // an action group, because we want these actions to be/ T: H/ ~$ u: c, f$ M. G4 j4 v+ U
// executed in a specific order, but these steps should
) R8 {- s# U/ y; Z4 @% d // take no (simulated) time. The M(foo) means "The message
% T0 _" N% a- K) ]3 b0 h8 @8 x' O // called <foo>". You can send a message To a particular) q' p: a4 }! y* u
// object, or ForEach object in a collection.
0 b. Y) f. _# t5 G/ L 6 k$ F& E% Z: M
// Note we update the heatspace in two phases: first run
7 h0 l1 P0 P# ^: O: }; D // diffusion, then run "updateWorld" to actually enact the" V) e+ H; b+ U& A" J3 G
// changes the heatbugs have made. The ordering here is
4 k7 h4 u+ L8 F. K5 x // significant!% e, S+ k* m9 `$ t; F1 o9 t
& K' E7 y; l7 L
// Note also, that with the additional7 b$ M7 z$ |' Y( D0 J9 C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( q: H. E9 r, D3 i // randomize the order in which the bugs actually run
5 @+ D ?' l- ~( B5 O9 r8 j- A // their step rule. This has the effect of removing any `6 C) ^* g5 h: F
// systematic bias in the iteration throught the heatbug
% @! A; e a! r! r' Q9 l // list from timestep to timestep$ o7 x1 r- z& A4 x6 g
$ O* ?% S5 S3 w' I& u0 U // By default, all `createActionForEach' modelActions have
3 Y* |1 h; M- R' |7 ^: d // a default order of `Sequential', which means that the
' ^0 z: I9 w3 T! r1 G% ? // order of iteration through the `heatbugList' will be
$ g/ P) M( l; F8 i6 ^ // identical (assuming the list order is not changed
( O7 {* I' @, z% M8 K o5 H // indirectly by some other process).3 G8 ~! P) y$ J( o
1 R0 }0 s" l k1 Y/ n4 }
modelActions = new ActionGroupImpl (getZone ());/ I9 M9 i# u# Q E
+ x" ^9 y& C, Z: s1 z | try {" E1 k0 Y/ _: Y6 p( l" y; x
modelActions.createActionTo$message
$ b$ \' g0 |$ R, O$ j* @$ Z (heat, new Selector (heat.getClass (), "stepRule", false));
- c5 U4 v" T6 v* @8 E H d } catch (Exception e) {
. O, d& M$ O6 J* u! z. C8 l1 k# P System.err.println ("Exception stepRule: " + e.getMessage ());
5 z; g1 P- |9 }% @; B# {4 k8 C }
6 Z% N8 C: B! a3 L5 z
1 p: F4 N6 d9 u# k# w% z- U7 O- u try {# v* l, J& f# J/ j( e
Heatbug proto = (Heatbug) heatbugList.get (0);
+ V, x% j: t% {/ {7 Z9 f Selector sel =
# Y* b: C/ S& } new Selector (proto.getClass (), "heatbugStep", false);
5 Z6 o; ~+ P, O+ N actionForEach =
$ d# C) b; H& M. \ modelActions.createFActionForEachHomogeneous$call
5 Q/ Y' s1 @) X' w (heatbugList,
I. E! I7 [, W8 j5 S new FCallImpl (this, proto, sel,. C3 i+ v5 G. D
new FArgumentsImpl (this, sel)));; P9 q$ r9 V" v" `4 C4 f# p( \
} catch (Exception e) {6 _: j' T: w' c3 L6 t
e.printStackTrace (System.err);& W; @; X$ d3 f
}
4 p3 m! P' _# D! W+ V0 W " w; q3 Y; V5 n# y5 a
syncUpdateOrder ();* b$ ]" c7 [$ t
% O& K& p9 h3 x
try {
o8 e Z0 i( g5 }4 m! u7 @ modelActions.createActionTo$message # L) v/ x8 |* C" m
(heat, new Selector (heat.getClass (), "updateLattice", false));
& b) j& G- H# Z9 s9 g } catch (Exception e) {
7 @' I0 n/ ]& I System.err.println("Exception updateLattice: " + e.getMessage ());
" O( p6 r6 F' S8 Z8 x }% g$ o6 Z( g: t2 o# F* x: ?
6 m. Z6 x) {8 Z // Then we create a schedule that executes the& V% [- |. t! {1 U
// modelActions. modelActions is an ActionGroup, by itself it7 V L2 s f# y7 P
// has no notion of time. In order to have it executed in. \: y7 a L4 D& ^# H
// time, we create a Schedule that says to use the
- H: G/ c0 ^" e, c% a) ?! e // modelActions ActionGroup at particular times. This `- D0 ~! c. r2 g' P5 k
// schedule has a repeat interval of 1, it will loop every4 e9 E0 x6 n- T4 N! y
// time step. The action is executed at time 0 relative to& {+ r' [+ s9 G, E3 }
// the beginning of the loop.
0 }# g* U' j2 S& C6 G# G4 l/ G) @# j7 c" K0 B/ n6 Y7 x
// This is a simple schedule, with only one action that is5 k) `9 z3 I7 T* ?& N
// just repeated every time. See jmousetrap for more. p( \- t- k2 @0 p' [2 N/ |
// complicated schedules.1 W& ~0 C! z/ n* O3 t
* ]9 B& K& N" |) c
modelSchedule = new ScheduleImpl (getZone (), 1);
. f. `% _' @5 {5 f8 d4 i modelSchedule.at$createAction (0, modelActions);/ c8 l( Y" s% @7 T
9 c; W9 |+ D- W
return this;
1 }- C! V7 s9 q$ d+ n' Q } |