HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 V! k0 j& \. n* p6 w/ K
$ V1 [4 T+ p) q; G& H2 q: ]$ w
public Object buildActions () {$ D) i: x' V+ e! a- q
super.buildActions();" N: J; G( q0 N& D: v% Y1 m
( m, X7 J) t% A# W6 R
// Create the list of simulation actions. We put these in
5 ]" {+ |, M) @, ]4 J% ^8 G // an action group, because we want these actions to be
t6 p& E; _* z2 r5 N, {+ w // executed in a specific order, but these steps should3 j- [ M/ `8 ]0 c
// take no (simulated) time. The M(foo) means "The message6 k" J/ g& s5 h
// called <foo>". You can send a message To a particular+ w% y, p2 y, t7 P2 \* G; G. W& M
// object, or ForEach object in a collection.) V7 R4 n' O8 R, d! p X9 r
- y! ^1 o/ b& P2 l0 G // Note we update the heatspace in two phases: first run2 H- c9 V) C# P& H1 Y, H& q
// diffusion, then run "updateWorld" to actually enact the
) ?3 d6 }) T# r+ z5 n3 P3 o6 v. G // changes the heatbugs have made. The ordering here is2 Q9 @/ y1 s$ t3 y8 r% e M4 p
// significant!1 H6 n- w4 T, x( z* \$ L
/ Y. P4 ]1 M p3 C3 q // Note also, that with the additional
7 ?! k$ Y2 E. a) F3 t v // `randomizeHeatbugUpdateOrder' Boolean flag we can
) e, w+ |9 e! k // randomize the order in which the bugs actually run7 J+ T4 E u; }7 V5 K
// their step rule. This has the effect of removing any
3 j# e+ ?2 N2 }% m3 O# t9 _ // systematic bias in the iteration throught the heatbug
5 k. B6 E7 G8 k! n, y4 H // list from timestep to timestep
4 f P& g% l, G t( v; V8 m! u( U 5 e2 Q* M1 F; O# A ], x/ ^& _
// By default, all `createActionForEach' modelActions have
8 W3 M; y/ t+ \, i // a default order of `Sequential', which means that the$ `5 N4 D. f1 R1 b- S2 A M* ^
// order of iteration through the `heatbugList' will be
) M b7 {4 M9 }; d& U* s G // identical (assuming the list order is not changed
+ |4 `+ P( y- ?- c // indirectly by some other process).
1 C: K `2 [1 B' @$ p( [' F6 J 7 X* Z; O# L( H6 X
modelActions = new ActionGroupImpl (getZone ());+ u1 g6 \2 `/ d, I7 b
) k: M3 w0 z9 E( n( a% \8 [; ]
try {
% _4 C6 u% S& R+ Z) B% H modelActions.createActionTo$message5 Z, \! k. F! d \+ i9 b0 d d
(heat, new Selector (heat.getClass (), "stepRule", false));1 e" T" t5 U9 ?' c, K
} catch (Exception e) {
$ T! o1 ~$ [# \$ r1 k System.err.println ("Exception stepRule: " + e.getMessage ());
3 X& p( c" w& Y2 F& e }1 L" Z' i$ `4 I. \% F
, f/ `. I& ]# `+ `
try {) g D9 i9 Q$ Y$ K; w! U4 D
Heatbug proto = (Heatbug) heatbugList.get (0);, M, ]' D1 X" G
Selector sel =
: r% m) @5 F" A# [/ S% t new Selector (proto.getClass (), "heatbugStep", false);
' C$ p2 d7 Q: F actionForEach =
+ X8 z, H$ H1 l7 f/ p modelActions.createFActionForEachHomogeneous$call
$ C1 }' \6 ]) Z0 e, r2 V9 _ (heatbugList,
% f2 J- |% \5 M# l4 i( D new FCallImpl (this, proto, sel,
! n3 p+ j6 L6 n new FArgumentsImpl (this, sel)));
1 K5 `5 ]& M% x4 Q# f- b } catch (Exception e) {
+ w+ @& |# E) Q e.printStackTrace (System.err);
+ m W/ m" g$ |. ]" L }- {6 z( |6 \: \" b
; ^0 d) m7 p& y4 k syncUpdateOrder ();
- \- _& B! V' v4 {/ w3 p2 V; ?) e2 V+ I$ l, F! [* Y
try {! D6 I C. B! z% |. j& R
modelActions.createActionTo$message
8 x6 n! P" t% I+ F* M0 G (heat, new Selector (heat.getClass (), "updateLattice", false));
5 ?# }* a$ O) w7 F% a1 i3 M1 e2 m# L } catch (Exception e) {
" g( w* I G" Z9 N' i, ^5 Y) G" ? System.err.println("Exception updateLattice: " + e.getMessage ());
& \/ i v2 n/ k8 g }
7 \( e3 i X6 C9 Z' z- i3 | - \( i# i4 [- i# h% f
// Then we create a schedule that executes the5 N- A( Z d6 P
// modelActions. modelActions is an ActionGroup, by itself it! l7 c, `5 x6 Z+ u, e
// has no notion of time. In order to have it executed in5 P4 n% E+ |; x0 b w/ C |
// time, we create a Schedule that says to use the7 R# C2 O% A' G6 L4 A/ E" t/ P/ g! y
// modelActions ActionGroup at particular times. This
7 O4 F* F- O# I5 `/ A // schedule has a repeat interval of 1, it will loop every9 ?% t0 M& ?- }! o; l; J
// time step. The action is executed at time 0 relative to
' {+ a9 D+ L+ L& | // the beginning of the loop.
* {3 x- v3 w+ P9 X! T7 a$ y5 u- X1 H/ J6 O
// This is a simple schedule, with only one action that is' Q7 L% g. s" l c3 T
// just repeated every time. See jmousetrap for more
8 \% {+ G" z* u# C. V" N // complicated schedules.& T2 r5 l t) ]: d2 D8 u# d6 W
' v. g9 l4 l1 \4 j) D modelSchedule = new ScheduleImpl (getZone (), 1);
X& c4 m" A! _6 a, ^" U8 C modelSchedule.at$createAction (0, modelActions);" H: p* c, c0 t
: D) r6 ? F: b. P
return this;
2 d6 W+ L& J4 ~6 L% I" Y s } |