HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ h% _) O% s t1 @$ o6 u% r$ Q" s% h
public Object buildActions () {; {, B4 ~4 R5 _1 f, l
super.buildActions();1 U; S: J2 j. G3 O2 x7 |
+ ~0 R* q- O1 u" @4 @8 S, o! \
// Create the list of simulation actions. We put these in
9 I* X! n' S1 o% j9 e0 F) Y // an action group, because we want these actions to be8 Y: o6 F" n. c+ ?: F
// executed in a specific order, but these steps should
7 v: z/ T3 ~ Q2 { \ // take no (simulated) time. The M(foo) means "The message
, M8 {& P, S4 w5 ?2 w // called <foo>". You can send a message To a particular6 u) \$ ~" E& Y, W0 H4 N
// object, or ForEach object in a collection.
8 Z5 U" g$ R4 A! D4 N * x( N' N5 i, f7 u" G
// Note we update the heatspace in two phases: first run
: P4 h5 H. q" U& m7 Y; p // diffusion, then run "updateWorld" to actually enact the2 x ]9 \) {' ]) u
// changes the heatbugs have made. The ordering here is5 e. x3 V4 `6 d& Y6 X, j' _
// significant!# B. W# f1 E) U
1 t) A) H3 r) l8 s // Note also, that with the additional
$ B" e; u' n$ R5 e' d // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 h% V+ F8 T: Q // randomize the order in which the bugs actually run
8 F) O9 E$ x" N8 O9 V) S // their step rule. This has the effect of removing any/ \. ?8 w5 }9 t0 O+ `' y$ f
// systematic bias in the iteration throught the heatbug4 z: a$ Q$ K+ u* [
// list from timestep to timestep, U% C( R! ^. s6 B) Z5 B
- s" k! J; P$ P' c3 [9 I // By default, all `createActionForEach' modelActions have& E! U2 y! G: V3 M8 c8 a& C
// a default order of `Sequential', which means that the
0 e* n: ^8 Y) h- _" Z3 ~4 A. L // order of iteration through the `heatbugList' will be4 ~/ ]' @: T, @1 ?* H* p# o0 |! ]
// identical (assuming the list order is not changed3 ]1 N: `2 [( v% N
// indirectly by some other process).
$ S$ v- C5 M* u P4 Y# j: J* M
5 @. G/ W4 `7 P1 x) p modelActions = new ActionGroupImpl (getZone ()); O7 }: c9 w. F& u
q: T& q0 k; Z5 N. E0 o. n$ i
try {& ^; N3 ^% v1 Y
modelActions.createActionTo$message
! K* [; j u1 ?: X9 N (heat, new Selector (heat.getClass (), "stepRule", false));
. c' U# u, ^' P1 U; N0 G } catch (Exception e) {
& }' ?3 ]0 \' F) W. Z4 | System.err.println ("Exception stepRule: " + e.getMessage ());* |/ H- g* w) }- C; P8 p9 D9 d
}! `) x8 i; P' z
3 }0 c0 d& C1 V, M Z
try {
5 k o+ T) f1 K Heatbug proto = (Heatbug) heatbugList.get (0);
7 o! a: J2 T' Z3 j1 q; k Selector sel = % O# N9 ~4 F, l" {
new Selector (proto.getClass (), "heatbugStep", false);9 M. D8 ~( K0 ]# e, h$ f% c
actionForEach =. s& [9 J, N& `; E" ^
modelActions.createFActionForEachHomogeneous$call
2 O2 K' Q( w* Q3 o' Y/ U. K (heatbugList,# a% u4 [$ D2 ?! L
new FCallImpl (this, proto, sel,
, B. A/ D' l, R2 \2 w new FArgumentsImpl (this, sel))); U' f: I( Z ~; I1 ~7 T" K) A
} catch (Exception e) {3 c _, m9 p% y8 p
e.printStackTrace (System.err);
S5 Q4 ?/ s2 v0 J. i) n$ c4 R }0 Q' c& I `7 T; U6 u o4 a9 M% Y
9 m- E, n/ Q3 c+ a9 S& h syncUpdateOrder ();$ G5 O- c: \0 w" c$ n n) j. D/ h
) e4 Y6 b0 n& k+ h1 w" M8 Q$ S/ M try {
) ^1 Z) ]- [: v' H5 i modelActions.createActionTo$message : V* K( j9 i R# T' @9 j
(heat, new Selector (heat.getClass (), "updateLattice", false));
, M2 m: C9 Z1 \9 g, E0 \- U% e4 c } catch (Exception e) {$ ?+ S0 c9 A* [, C/ {4 @" o
System.err.println("Exception updateLattice: " + e.getMessage ());- `5 V: x# u4 H. Y
}
& h* f2 k" @ E2 c4 e0 C$ o ) \+ ?' h4 l& b
// Then we create a schedule that executes the# i( w5 _8 X) c) ~6 o; {
// modelActions. modelActions is an ActionGroup, by itself it
& A$ L" q6 M/ e) u* ~" y |" n3 O2 G6 i // has no notion of time. In order to have it executed in8 v- F" e2 ~7 ~: x0 H
// time, we create a Schedule that says to use the) G# o3 l6 j3 N2 _& ~
// modelActions ActionGroup at particular times. This
" j: _6 u$ ^# N1 v% o1 h // schedule has a repeat interval of 1, it will loop every# a5 w0 \& q* p
// time step. The action is executed at time 0 relative to
# n! ]' D, }2 u Q. H5 f // the beginning of the loop.
; P- N* ?5 p% h& [0 p% R
8 c' D1 _( J1 s // This is a simple schedule, with only one action that is# b* i8 Q) j( d6 U9 _2 u3 S" M
// just repeated every time. See jmousetrap for more/ t: \- q* [, z8 f
// complicated schedules.& D8 _1 I7 }" T' v& K4 ~
- a! Z: G& i; I) H0 L8 a+ k% H" e- G
modelSchedule = new ScheduleImpl (getZone (), 1);
E" c1 g# D W* x3 B$ o" d modelSchedule.at$createAction (0, modelActions);
`' F$ S" j8 O8 _
4 q. z/ V/ v/ A6 k return this;$ _0 W" Q6 y ?0 M) p
} |