HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ Q% q5 \* R8 l [$ q7 P! y+ A6 B
7 H S9 g, K- n& U) A3 S, n
public Object buildActions () {
. b6 d+ `9 E( X% K$ V0 p super.buildActions();
2 z! |+ ]6 F2 |! W7 B ( m8 C( F, C a7 a- N$ J! f
// Create the list of simulation actions. We put these in
( \, V9 V* T# L$ H; T/ b' m // an action group, because we want these actions to be. m$ ?# v+ b& }3 g8 ]
// executed in a specific order, but these steps should
3 g9 W; ~* b- p6 ]7 V. p2 P6 d& b // take no (simulated) time. The M(foo) means "The message
% q5 C5 n9 `1 [8 w/ Y$ Z4 F // called <foo>". You can send a message To a particular Q3 w6 f9 D0 F# z) w
// object, or ForEach object in a collection.
* {( F) w7 p% E) h- _9 b
) U; \ a% D6 F) G // Note we update the heatspace in two phases: first run
. x- {, p! X! g2 e // diffusion, then run "updateWorld" to actually enact the
9 R9 d. q& G, m7 n9 Y // changes the heatbugs have made. The ordering here is8 }& @+ [; `; f6 t; F+ D) \
// significant!. |! E$ _- `' k$ Q- ~2 ] y
5 `% F2 r/ s# j1 w" C6 q1 ?# D
// Note also, that with the additional
6 g0 E S, H. r // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ w/ g' Q5 S. Q" m // randomize the order in which the bugs actually run
! V$ W- Q' t% S9 w; G // their step rule. This has the effect of removing any
2 E7 M* k. s9 S1 f8 o // systematic bias in the iteration throught the heatbug8 e) a- X# Y- M* D$ \7 d
// list from timestep to timestep$ x" u, i1 J- M# K
2 w. X$ R" C' r5 I( a! R% ~, d+ h // By default, all `createActionForEach' modelActions have) R2 K1 r0 } O8 {& H# R4 W2 G
// a default order of `Sequential', which means that the# H6 L- ^9 U( X% ]& m
// order of iteration through the `heatbugList' will be
K2 D, r# P2 g3 ? // identical (assuming the list order is not changed
+ v( q8 r# x) O! {- T' a" I // indirectly by some other process).- _; p2 t3 p! L2 ^0 t
3 w9 A# R }9 d+ G/ Y modelActions = new ActionGroupImpl (getZone ());- [% x$ J4 i6 p% K
; @" R4 ^7 D. O6 p% m2 }- v4 i
try {: x F- Y: [- P1 g
modelActions.createActionTo$message
8 K: q2 [' y' x) g& e# K5 v (heat, new Selector (heat.getClass (), "stepRule", false));
' L0 h* X* A P6 m } catch (Exception e) {7 U! _ V* B9 O+ I, o
System.err.println ("Exception stepRule: " + e.getMessage ());0 m+ I, D+ ]/ ]6 B+ r; x
}+ F- T( s6 o# r( y4 U) J
: \% l& i3 P6 z try {( |) ]$ [; v: d% Y/ Y% y% i
Heatbug proto = (Heatbug) heatbugList.get (0);' D |8 \: _! v s! P3 X) }" j
Selector sel =
) x# d" j/ P% l b" `* R( Y( E new Selector (proto.getClass (), "heatbugStep", false);
, {# _' v; b, T" o; O2 S$ U% l actionForEach =
$ n' A9 H9 Z# }! A3 t) ^ modelActions.createFActionForEachHomogeneous$call' W/ R) ?/ b. \. ]" d1 @6 G, P W9 X
(heatbugList,
% I) a( t, G! O3 q" C new FCallImpl (this, proto, sel,
8 M! }1 l2 I! K9 T5 h. I l7 R5 f new FArgumentsImpl (this, sel)));
9 k% O- K3 @- z1 t3 x } catch (Exception e) {( h. Z a6 u- c
e.printStackTrace (System.err);
2 L- K+ s0 l, y$ p5 Z Y* M }
+ R+ Q+ f8 h8 J8 _8 i
+ A/ U: D8 Z. B2 U+ w( M9 { syncUpdateOrder ();/ ~+ z+ k% v T2 V. G1 C+ s
0 q8 w2 R+ Q4 p' D) w6 f7 ] try {+ |$ B) e" h4 M# T
modelActions.createActionTo$message
/ e0 L$ k4 f9 o+ Y (heat, new Selector (heat.getClass (), "updateLattice", false));) Q9 j- N7 F% D4 X! `8 F( I" l
} catch (Exception e) {
5 f$ m3 H; o3 z System.err.println("Exception updateLattice: " + e.getMessage ());; N/ }* y/ b; l* r) ~+ K) Z
}. u8 l# E' b) z& l& c& q7 }" ?* N
* _5 Q' q' z, G P3 a6 i/ {2 f* q
// Then we create a schedule that executes the
. F& z/ `$ o5 J" X; `! ? // modelActions. modelActions is an ActionGroup, by itself it
' j! B" M' R2 b // has no notion of time. In order to have it executed in L. B" A( k/ i' l! b
// time, we create a Schedule that says to use the9 z* j$ Y' _2 n1 U
// modelActions ActionGroup at particular times. This
- h, I' h( z. F9 s. [ // schedule has a repeat interval of 1, it will loop every9 B& L7 B5 |: B4 x$ a! P0 x
// time step. The action is executed at time 0 relative to
+ Z% K8 u% L- C // the beginning of the loop.
. C0 Y$ u {" f1 A A W1 M. c* D% G: T2 M. ?1 {* W) I+ g2 o
// This is a simple schedule, with only one action that is) q+ ~ N1 f4 A$ Q) o$ U' k5 J
// just repeated every time. See jmousetrap for more+ W1 c9 y% e, y
// complicated schedules.) s/ o2 Y: @+ |, @ K. W4 H
6 V" l! }/ _: k4 d* p
modelSchedule = new ScheduleImpl (getZone (), 1);0 m8 C, l7 w" V B: [. h. T4 f* b2 {
modelSchedule.at$createAction (0, modelActions);# m1 n6 G' E8 z- o# F- {
9 `% N( W# Y+ z) G1 ^
return this;
; }$ D y, D1 D+ l1 d } |