HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 W9 M, i# g8 q6 H6 [& G% ?$ w2 w. c+ v9 e9 A& y, q$ z# [1 X0 j
public Object buildActions () {
4 d; _0 J/ ]# |2 L super.buildActions();
: g: {0 F# {& [; |1 i4 j4 _4 d7 ^+ | 9 U2 P. H- k$ E& U& p# L; |3 q! @' [
// Create the list of simulation actions. We put these in
$ _3 K0 q$ [9 E+ W2 Q1 n- Q // an action group, because we want these actions to be
) j' M5 H" x7 [) a // executed in a specific order, but these steps should
9 o: P' C9 R6 R* p5 q* U. O( m // take no (simulated) time. The M(foo) means "The message
6 E; D# O. q0 x1 u7 ~' Q8 X // called <foo>". You can send a message To a particular3 E# A' |; Z. \% n9 F
// object, or ForEach object in a collection.. N& G7 G0 b5 b* @- y
' b/ S/ G2 J1 d( Y4 ` // Note we update the heatspace in two phases: first run! G9 j6 g: a8 e: C6 K
// diffusion, then run "updateWorld" to actually enact the
9 S" P3 O# @ X# O4 g // changes the heatbugs have made. The ordering here is
( Y( ?7 B5 g) [4 k+ G // significant!# o( F* a9 X* e7 n
. Y3 D7 b$ n4 }# _
// Note also, that with the additional$ B# u3 k5 O( g+ v) X4 k4 n
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, J" c8 U! S" U. z% M+ } // randomize the order in which the bugs actually run- v0 C: G P: g+ Z% Z2 I. l; ]+ r2 W" o
// their step rule. This has the effect of removing any5 c; B' R+ i" _, A4 O- P3 @8 x/ @$ r
// systematic bias in the iteration throught the heatbug3 j+ r- Z, i9 K+ e, K& f
// list from timestep to timestep
. E- i9 S& _! x0 ^* _' |" j 6 [8 H. y; v: d/ U3 Z
// By default, all `createActionForEach' modelActions have
: D) j& G |5 D" f# e: r) Q // a default order of `Sequential', which means that the' M: j) T4 }, E' i5 O# h5 ]0 B, j+ L
// order of iteration through the `heatbugList' will be- U$ A' P+ Y* Z1 h# p; C
// identical (assuming the list order is not changed
: P% A* @, ?. Z0 J1 q // indirectly by some other process).
5 w, b# S5 V+ w! a$ P , E/ ]5 L- n$ d9 h. ~$ s0 }- }
modelActions = new ActionGroupImpl (getZone ());# \" h5 s8 H. q6 e6 F7 w/ u
: J7 ]/ j% Z- j/ X) Y4 a try {; @7 S" I4 y( j6 x% n6 I' {
modelActions.createActionTo$message- `! P4 R% t4 ?8 M1 b
(heat, new Selector (heat.getClass (), "stepRule", false));6 n* M/ O' @' u' w* a- P/ |
} catch (Exception e) {( M/ i x! |0 j7 d' D% h/ H9 T
System.err.println ("Exception stepRule: " + e.getMessage ());/ y. I8 M8 R! ]) B7 ]
}
/ J1 A: m$ p' H) ^- |0 R/ ^3 h! f1 ?9 ^1 c1 Q2 H8 ^
try {
* Z! G8 @, u# b- _9 X Heatbug proto = (Heatbug) heatbugList.get (0);# [) I' K0 k) Y6 D. y
Selector sel =
3 Q( A, e2 G% Y; \4 K- k new Selector (proto.getClass (), "heatbugStep", false);
2 f; G9 @. C; I1 X, A |' e actionForEach =
4 F: Y1 V1 s: Z7 X0 V$ H modelActions.createFActionForEachHomogeneous$call9 I% c2 W; j" a% x0 j! R
(heatbugList,
* ]7 L, \: Z9 o* R! [ new FCallImpl (this, proto, sel,
& W$ y. n: K* Z new FArgumentsImpl (this, sel)));
$ Q9 Y5 a9 H3 x0 `0 n7 Y5 K e } catch (Exception e) {
+ u* l9 H6 u/ c0 h& O7 N e.printStackTrace (System.err);1 Z8 ~+ J" ^5 k H0 P
}
; o, J0 I) R5 C5 T5 ^. [ N
6 J/ W- u2 n" G syncUpdateOrder ();
; r6 u& Z$ v7 W; |4 j& A' k5 y
1 x6 _8 f* J/ X+ r3 O try {8 ~' C- q' g2 P0 u6 S% w
modelActions.createActionTo$message
' {0 I6 S# g6 G8 I (heat, new Selector (heat.getClass (), "updateLattice", false));+ O% {. ^9 ^% q, ]8 t
} catch (Exception e) {
' H/ \5 F: H- h4 ~; M System.err.println("Exception updateLattice: " + e.getMessage ());( O# N1 G& g: X a) G1 O/ @
}$ J$ |8 I7 {) y) o9 c# j8 y( v/ l
2 t$ ^$ L# J" Y$ o" Z( h ?
// Then we create a schedule that executes the g2 g7 F! T; H) v, L1 B- C
// modelActions. modelActions is an ActionGroup, by itself it
, q( v0 ^+ ~2 e$ r* S% s7 b/ e ~ // has no notion of time. In order to have it executed in
4 J6 ?, R/ F2 I0 O // time, we create a Schedule that says to use the# ]: V L" C0 a# H" e% p5 |. K1 z
// modelActions ActionGroup at particular times. This- a7 j4 {5 n- y% D6 V9 x$ F1 n
// schedule has a repeat interval of 1, it will loop every4 h: v5 x, J- q2 F4 f
// time step. The action is executed at time 0 relative to9 L$ J* D9 a3 ?% X: d3 g6 K( d) D2 ?
// the beginning of the loop.) P1 |' v1 G1 n! `% r) t* {
o' F7 s4 E- X! c* @! b4 I# {
// This is a simple schedule, with only one action that is4 s: k( G" V6 I
// just repeated every time. See jmousetrap for more0 V8 |% q9 Y/ i% o
// complicated schedules.
) i$ _" K4 o3 b6 s( ^
9 i& c! `9 y% ^7 F modelSchedule = new ScheduleImpl (getZone (), 1);
% r; c6 z$ c$ V" Y6 d' H. h modelSchedule.at$createAction (0, modelActions);; D% m& u! f' c' l
1 s; I4 b4 r* i8 ]" ? return this; c: u; r9 |+ u( ]# _
} |