HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 k6 c R/ |' R. V6 \. T
7 o V2 W) r5 z+ g
public Object buildActions () {
" m. f5 r5 R' ^" {* } super.buildActions(); m$ o( @. j# K& h" G9 s
2 D7 y+ L+ G. G/ {$ O9 c // Create the list of simulation actions. We put these in
\! ~$ t* ?' k // an action group, because we want these actions to be# T4 f* r# f6 u. G7 P, m$ k
// executed in a specific order, but these steps should( q" j$ P; v- F! g8 q) ^
// take no (simulated) time. The M(foo) means "The message2 @! g, U# Z9 `3 v) x7 S
// called <foo>". You can send a message To a particular6 e! x' ?* U+ }0 N9 i s5 o
// object, or ForEach object in a collection.5 i7 L) u' S1 C. w8 w" m4 F- E1 ?
- W/ @3 W3 q2 [/ F
// Note we update the heatspace in two phases: first run& m. O0 m# _, n8 N
// diffusion, then run "updateWorld" to actually enact the- V4 m$ [: g- p* s$ r- d
// changes the heatbugs have made. The ordering here is
% n" b2 E1 M0 j( C // significant!( }! V* }4 w: y% j
: l6 ^* y- L( s. q0 L: G // Note also, that with the additional4 e4 p4 l$ R, f: k& f0 k3 U
// `randomizeHeatbugUpdateOrder' Boolean flag we can y$ [$ c6 G: o
// randomize the order in which the bugs actually run4 m/ \; C* J+ V1 I# C i9 @" x
// their step rule. This has the effect of removing any
4 Q7 S; s8 F S5 S // systematic bias in the iteration throught the heatbug8 j D3 q* _/ T9 J G9 {
// list from timestep to timestep
7 q9 R+ i5 |9 s: {2 U% d( f G # g8 k# q; u c6 h/ {, F
// By default, all `createActionForEach' modelActions have
( N1 _7 O% U1 c; K0 L0 V+ ~5 |" u* l( u // a default order of `Sequential', which means that the2 e* Z t. t; @" X/ U+ {
// order of iteration through the `heatbugList' will be3 s$ D2 @+ f% d0 k
// identical (assuming the list order is not changed; i! L: E4 X) l" ~: ^* W
// indirectly by some other process).
{0 q$ z2 ^/ ?" S$ b
! ^" C V; _3 B' m modelActions = new ActionGroupImpl (getZone ());
$ b/ }8 @2 {5 W4 p. J f+ ~) Y. q6 T- l& u, z, L
try {
- B0 }. R; H( e: O; j& } modelActions.createActionTo$message
- o s: R4 E! N3 x# j/ L( | (heat, new Selector (heat.getClass (), "stepRule", false));0 m5 m4 V' n5 y6 V$ g" C
} catch (Exception e) {0 X) ~: n# f9 L W2 Z7 H
System.err.println ("Exception stepRule: " + e.getMessage ());1 C' w! ~4 i# T7 J; k! ]2 Z8 G
}
- E% D5 l: U2 d' S* t4 F% s+ \" t8 @' e# `2 K
try {
; k0 A7 @4 O! i/ f/ m" Y/ W Heatbug proto = (Heatbug) heatbugList.get (0);9 E3 E2 m- D+ p4 ?" _ s/ F
Selector sel = * K7 Y- f( ?& h
new Selector (proto.getClass (), "heatbugStep", false);+ q. `! Y1 N! g' ^) r
actionForEach =) z h, V, _* b2 ~. P* H
modelActions.createFActionForEachHomogeneous$call
8 E5 c1 M1 k6 ?. P (heatbugList,
; V" Q2 ]4 Q0 f new FCallImpl (this, proto, sel,. x+ j1 }+ L; }' W$ U' A: ^ h
new FArgumentsImpl (this, sel)));
/ }* ^2 k' R/ b# F U } catch (Exception e) {
- l/ Y7 Z2 T1 q+ U' X4 k6 U e.printStackTrace (System.err);2 U: w/ w+ |" L+ X
}
0 k3 J1 b$ O: R$ a. ~+ A- ]+ B
' V$ B3 N! e* O9 v& ]; W" }' t: v2 _ syncUpdateOrder ();! d( [6 p) q8 s; v
8 V |. K( _( \5 F+ A try {/ F4 C' o/ h" _9 \
modelActions.createActionTo$message
3 @- \7 ~' e# q- q. L8 F: W' ? (heat, new Selector (heat.getClass (), "updateLattice", false));
% z n5 @3 h( M# b$ ]# y6 z } catch (Exception e) {
. x2 V6 Q7 W6 D# i8 e, i' u System.err.println("Exception updateLattice: " + e.getMessage ());4 s6 G- x. d1 V! z6 U
}) I$ A' z K- E+ H1 ]$ P3 r7 y0 P
! l0 G2 d0 C# [- s% q& L# i
// Then we create a schedule that executes the
8 n$ C6 P) J; s& D // modelActions. modelActions is an ActionGroup, by itself it+ ~' h! _" Q# {9 ~! Y+ a
// has no notion of time. In order to have it executed in
9 ]+ O3 Y4 J- M+ E" Y- l9 N // time, we create a Schedule that says to use the
9 z: D; o/ c4 {* v6 V5 h. e8 e // modelActions ActionGroup at particular times. This
9 j2 }& F+ ~5 a7 H+ D7 q // schedule has a repeat interval of 1, it will loop every
3 I6 ~$ v) N& J; a // time step. The action is executed at time 0 relative to
4 i, T% P& U) j: u9 l // the beginning of the loop.* u5 e6 s" t5 q, p& q
/ E' t6 F1 Y8 W6 C( d) W // This is a simple schedule, with only one action that is
$ v7 m, a1 Z# j3 ]# v // just repeated every time. See jmousetrap for more) I! L& \3 E2 T7 u9 ~
// complicated schedules.
' E3 ^! Z# K6 u( W( s& {
8 F' h$ s' o% ~ modelSchedule = new ScheduleImpl (getZone (), 1);
$ T6 m# R' Y1 R0 E modelSchedule.at$createAction (0, modelActions);& ?2 o& s$ J) D2 ~
5 D1 ^9 j3 u4 q+ [' P4 x& L9 a; Z return this;6 _! X1 R5 W% w c
} |