HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 o! y/ o, u' k# S, D: p9 n4 B( Q& W# k1 r
public Object buildActions () {* W+ z* v S+ a B* s" P
super.buildActions();6 r8 `9 J C& w+ h2 ?
( g, A; |0 h& f& y R, \
// Create the list of simulation actions. We put these in4 r( S1 H$ m! f: t. ^, d
// an action group, because we want these actions to be4 c) z: P( T: h9 C& Z# L
// executed in a specific order, but these steps should
7 n0 ^' s$ ]" P& E9 ?2 { // take no (simulated) time. The M(foo) means "The message
5 J: d- Y# V/ } // called <foo>". You can send a message To a particular. L6 K$ [# v/ I) w
// object, or ForEach object in a collection. u/ r4 U1 r$ @! h6 h: j
+ z7 ?. L0 r0 h6 ] // Note we update the heatspace in two phases: first run3 k: U- {/ w7 D: ~
// diffusion, then run "updateWorld" to actually enact the
' z6 K- L3 x& F4 c // changes the heatbugs have made. The ordering here is+ K8 w, _0 b7 O# D* u) H
// significant!
, l4 t' e$ M% | " z, F$ f+ V7 B0 e7 v( Z/ L
// Note also, that with the additional# Y$ f+ H. @/ U1 i; n! d# g- S! U* N+ H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 T% Y+ p) c) ?# Y% R! O$ m // randomize the order in which the bugs actually run3 Q6 c5 g% ^' }' ~4 H7 \9 i, ~
// their step rule. This has the effect of removing any
$ C. q3 x; N4 u" y" X) m // systematic bias in the iteration throught the heatbug" ~: B$ g+ \( J) O; H
// list from timestep to timestep& a7 L$ G7 m6 d v* {4 j P. ^
3 r3 V7 C, @) [9 v& x3 `6 n // By default, all `createActionForEach' modelActions have
- q7 b" q8 e" r // a default order of `Sequential', which means that the
6 ], g0 s' y; r7 a$ ^1 v // order of iteration through the `heatbugList' will be1 P) l& b$ n) O7 M. l8 g# N! y
// identical (assuming the list order is not changed
; z7 k2 l2 o6 W7 | // indirectly by some other process).
8 `, f9 x5 r3 D: ]. D5 g" r
8 ~ v, w! u1 l- j; n modelActions = new ActionGroupImpl (getZone ());
$ V! h A# f$ n6 S! R) E- S0 F+ T0 G2 B1 y$ u' {# V' O
try {) [8 e B- J% H2 J6 H
modelActions.createActionTo$message; \9 I& b- F' u& W5 S- E4 v8 b
(heat, new Selector (heat.getClass (), "stepRule", false));( R+ M/ Q) n2 u
} catch (Exception e) {: k) g3 }0 Q& S, C7 O5 q0 P
System.err.println ("Exception stepRule: " + e.getMessage ());
% x3 b- J- K! ? }2 h/ j2 Y: [. j3 ?& {, T
8 M% g8 s' |! @' s+ o
try {
# a6 z5 X: Z- h/ b8 L# S Heatbug proto = (Heatbug) heatbugList.get (0);
4 f+ e v' S0 I" |/ e Selector sel =
0 F6 J. ?* t- Z2 w1 W: g new Selector (proto.getClass (), "heatbugStep", false);
6 k L6 k# a5 r actionForEach =
# }9 [5 R8 n6 a! N& D& N modelActions.createFActionForEachHomogeneous$call
# K2 W) g8 B8 _ (heatbugList,' f# e3 o. d# j+ t
new FCallImpl (this, proto, sel,4 }- T/ n g- E
new FArgumentsImpl (this, sel)));3 E& u4 u c+ h$ J8 O
} catch (Exception e) {1 _ d+ |6 s! A. f$ L4 W
e.printStackTrace (System.err);3 q* z% t2 y0 j" c; [
}
+ J, s, ?4 f z 9 Y/ Z! m ?1 y+ U' B
syncUpdateOrder ();/ X% o$ `# B* x2 L# b i8 o
8 b5 r% ~# S X* ]/ ?; ~
try {" v9 k& G9 g- J: y- O8 k8 t
modelActions.createActionTo$message / ]) O+ |. s0 J/ T& A
(heat, new Selector (heat.getClass (), "updateLattice", false));7 M; `! G. l3 r3 G% \
} catch (Exception e) {
( Q' ^2 c5 P0 }, y- ]7 J$ w System.err.println("Exception updateLattice: " + e.getMessage ());
% H9 ]1 e' a* g: f* O }- {$ r! j& I; F; p5 Y) z5 a# E# r6 C+ u
& l3 c3 t8 l3 w% U
// Then we create a schedule that executes the0 X$ g: O# y; V2 Q
// modelActions. modelActions is an ActionGroup, by itself it
* c4 Y* `) w# Q1 L // has no notion of time. In order to have it executed in
* P% K- O5 o9 u2 x$ ~ // time, we create a Schedule that says to use the
; g4 b: x, ~9 L+ n0 n- t- x // modelActions ActionGroup at particular times. This
- m- C0 O7 J3 t. F' C8 M // schedule has a repeat interval of 1, it will loop every" n# _1 B8 P( G7 p7 t
// time step. The action is executed at time 0 relative to
# n" z* }; B5 w% I/ V // the beginning of the loop.
8 y. X+ w4 O- r# D! Z/ W, J, P+ q, G2 U9 K. T; U/ U0 J
// This is a simple schedule, with only one action that is
' G- k( }3 ^! S, O" [ // just repeated every time. See jmousetrap for more
9 N$ f) f4 I8 J% l" X" o u K // complicated schedules.9 L" ?1 b/ b/ o; n& C" ^
. E" a4 o- D D' O8 {! T modelSchedule = new ScheduleImpl (getZone (), 1);
' `8 ?8 ~, P. L/ t2 _ modelSchedule.at$createAction (0, modelActions);' u2 D( ?# t7 y" s
3 X: N$ z+ ?# O: g+ Q return this;
1 ^: K8 g" H" p: T! r2 q } |