HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! S7 y5 ]' U, h8 `% h: ?- t, g! r
9 q, J% G7 @+ q, W) Y1 D( g B public Object buildActions () {6 Y4 o. D' ]3 _0 A# }* q
super.buildActions();! e7 m/ |: d- o; g' Q
0 M1 _4 l% X1 \6 b( l // Create the list of simulation actions. We put these in/ p& A8 M) }8 }4 }1 W2 f
// an action group, because we want these actions to be1 Q) ^, ^9 c% M& E9 h9 l- e
// executed in a specific order, but these steps should8 e+ |6 e* c' E) D- Y& S
// take no (simulated) time. The M(foo) means "The message% W0 l" K7 D/ b6 a$ J
// called <foo>". You can send a message To a particular0 {, z" F' [' t Z" c, F+ i7 _
// object, or ForEach object in a collection.7 L+ I7 T, M9 v7 M2 d6 s
4 K7 [( e; ~" O+ m
// Note we update the heatspace in two phases: first run
% v: P1 S; t3 V. h; }0 a, M // diffusion, then run "updateWorld" to actually enact the1 j. u$ q$ m0 Z, _0 w+ R
// changes the heatbugs have made. The ordering here is' n$ d5 `0 O0 q9 k M" C
// significant!+ e2 T. P' G" S! x
- @- T! U# v; ~, {2 v
// Note also, that with the additional
6 A; ~* F; f- E+ f // `randomizeHeatbugUpdateOrder' Boolean flag we can" u- c- h5 \- n5 o: E1 C; R& M7 x
// randomize the order in which the bugs actually run
5 ?. b/ A2 @! V; F/ ^ // their step rule. This has the effect of removing any
# W' D8 Y% l! e+ L // systematic bias in the iteration throught the heatbug2 t! O+ n0 b; q+ |
// list from timestep to timestep
' P1 ^- o* y# a* `' ]
* x- V2 }/ L \0 J1 `2 q3 h // By default, all `createActionForEach' modelActions have6 r" Z4 N0 ^. f, X, w& g/ D
// a default order of `Sequential', which means that the$ R) X# c3 z" L8 c1 W
// order of iteration through the `heatbugList' will be7 s* ~% V# D; i8 L- ?1 w
// identical (assuming the list order is not changed
5 d3 W2 m( v0 m E: A. ~2 q5 z5 Y! o2 \ // indirectly by some other process).
! r% f! X4 b0 y/ |
+ L# |9 Z: s, d6 k modelActions = new ActionGroupImpl (getZone ());' r0 B1 V7 {/ B- M
7 }5 c/ L7 _: w* J
try {* K% u ^- {9 ?) S% }" F
modelActions.createActionTo$message
# p* K8 Y% @: J! [$ Y1 H4 H (heat, new Selector (heat.getClass (), "stepRule", false));
) m& A; p& ^& K8 @3 [! ~6 _ } catch (Exception e) {# S/ `! E* z( c ]$ L
System.err.println ("Exception stepRule: " + e.getMessage ());
2 D6 ?5 h6 L) l- S }
, S& H! P3 U; M! N0 {4 v5 F$ i* T' b1 H
try {- p& X8 |: L: j
Heatbug proto = (Heatbug) heatbugList.get (0);
; e) Z/ u* G& u# u4 k3 i$ | Selector sel = 1 k* c3 V. @: u6 P$ F$ f# B- M: c
new Selector (proto.getClass (), "heatbugStep", false);
5 [% d. ^( D; L8 e( |) }# |/ u actionForEach =- l) x0 P; t6 I5 I+ A
modelActions.createFActionForEachHomogeneous$call
1 r' ]9 S6 J ^) N J7 E (heatbugList,
7 L" h3 T' @1 _ new FCallImpl (this, proto, sel, f# x" O" R- C8 D
new FArgumentsImpl (this, sel)));0 N: X, c; D L
} catch (Exception e) {5 f9 n- v* [6 c- `! n; X
e.printStackTrace (System.err);* ^1 p( Y1 F& z
}5 t6 t( \$ R3 D$ } Y
9 u! P$ I8 y8 _% P9 f: \) d0 G syncUpdateOrder ();
# g3 ]+ `2 `+ M7 q/ z9 H4 L k6 Q, P- B
try {+ k; x/ c h0 i: m( h
modelActions.createActionTo$message 2 i1 U# d! l3 F, Z! S* `7 r' m
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 a3 y3 z5 _. Y( @( o } catch (Exception e) {1 t+ i: L# u' d' q
System.err.println("Exception updateLattice: " + e.getMessage ());6 h4 v, s( G8 X
}
" |: G3 O ?4 l$ ?0 S, g # M# ?' t; J: r: ?! ?4 `$ b; s
// Then we create a schedule that executes the
/ i6 K4 R M! i# s7 V8 r // modelActions. modelActions is an ActionGroup, by itself it
* M0 ^ Q3 V7 ~& k2 ^9 f // has no notion of time. In order to have it executed in3 c4 e5 ?8 i( l/ E( m2 |
// time, we create a Schedule that says to use the& {. g! ]3 I. H
// modelActions ActionGroup at particular times. This- e* F& U' o( g
// schedule has a repeat interval of 1, it will loop every6 D) Z( E' p- Z( v: k* x
// time step. The action is executed at time 0 relative to: N5 g/ e% V1 A7 E! L; O
// the beginning of the loop.
o$ d6 h. T+ n1 M( e6 U+ T6 D R0 h8 u; z
// This is a simple schedule, with only one action that is
+ p, O5 A! ?# v' w: ] // just repeated every time. See jmousetrap for more
! B) H( ?5 [% y( x8 k7 B // complicated schedules.& |- p4 C! p9 ?: ?
; ?6 B4 i q) C5 j) u
modelSchedule = new ScheduleImpl (getZone (), 1);
- i* g: l! H7 Y4 c0 Y* @# L modelSchedule.at$createAction (0, modelActions);4 }4 Z z+ }. b3 z5 D
4 {' L. a3 B( V2 l1 ]& A$ D7 f" D return this;) g; N5 \! v3 R' C# h7 v; z1 u
} |