HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 w" f, e" @0 Q& l- f6 u/ w6 \9 [* e" K7 W E8 N! L
public Object buildActions () {# `+ J$ h/ X9 b8 ~
super.buildActions();
! z3 a* q# w. k . P/ B% J# B6 D3 i8 P
// Create the list of simulation actions. We put these in' n5 [1 q+ w5 M6 J5 i8 ]8 [" \
// an action group, because we want these actions to be% }( H! k$ g$ a, x0 L7 S9 C
// executed in a specific order, but these steps should+ V7 P$ o+ Y; Z
// take no (simulated) time. The M(foo) means "The message7 H' ?$ W! M4 E3 c
// called <foo>". You can send a message To a particular4 \+ t1 Y s% y4 R3 Q- F* [
// object, or ForEach object in a collection.
7 w+ U9 ~: w: }, h: \* k : n/ _1 O5 C3 [3 G5 Z% E
// Note we update the heatspace in two phases: first run( p. Q) u1 g* P: t! h$ n) g: {% d
// diffusion, then run "updateWorld" to actually enact the- y; D U1 j4 D: G, e
// changes the heatbugs have made. The ordering here is! Y% t8 J4 q: ~1 V
// significant!
% ?( q1 x" G; Y' Y) p% Y' v0 f; U6 B * ], g$ A& F0 k; W, m
// Note also, that with the additional5 j, u* i5 h+ |. ?% |4 T0 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 W( Y1 @5 {/ I( Z6 N. d
// randomize the order in which the bugs actually run: Z9 p/ J# w7 r8 M: O
// their step rule. This has the effect of removing any& T! I4 l& D' R
// systematic bias in the iteration throught the heatbug
M7 v. J, K- u6 b // list from timestep to timestep
5 l L# S' d0 a$ |7 C6 `$ ]9 J
: C' W L |! v8 r P2 s // By default, all `createActionForEach' modelActions have% e8 w6 @7 g0 f; ~7 Q4 P: k
// a default order of `Sequential', which means that the
2 O+ O/ G$ G* k- j1 n q9 r // order of iteration through the `heatbugList' will be5 Q! |+ H8 z9 s* j4 g
// identical (assuming the list order is not changed
5 ~$ g& g: }8 Y# }6 [, l // indirectly by some other process).
$ v8 {, D i2 Z1 D9 I9 U" {* u 4 a! E/ d: S" B
modelActions = new ActionGroupImpl (getZone ());& L( e: H- `( L+ k6 o1 K9 R( t
+ x6 |) _2 \. z try {0 @" W# d0 T8 B/ Y: P6 a; [4 x
modelActions.createActionTo$message* P8 v; M o" @' j
(heat, new Selector (heat.getClass (), "stepRule", false));
$ k7 U5 J0 r( k: E* O6 ~ } catch (Exception e) {5 P, \& Y8 `% K2 q% X
System.err.println ("Exception stepRule: " + e.getMessage ());
|) t# X9 J% R! ? }3 d6 C W; G6 A' h' d7 r
z; V: m) r/ X+ N5 b
try {8 \# Y5 K# M- p' G
Heatbug proto = (Heatbug) heatbugList.get (0);9 v2 o2 k' k. r) @' k7 p8 v
Selector sel = 5 G6 \/ c# } J1 M7 k
new Selector (proto.getClass (), "heatbugStep", false);
& g9 d8 k( K3 Z) V actionForEach =
1 J1 m6 u* N2 }/ v, R9 |( @9 q) u" o modelActions.createFActionForEachHomogeneous$call
M }8 a+ j; p$ o (heatbugList,
# \2 s% j& ~$ [3 a new FCallImpl (this, proto, sel,# ^; L; o2 O; |. I% y
new FArgumentsImpl (this, sel)));
+ `6 p1 {. C, Y8 h } catch (Exception e) {1 j! i1 @7 u" H6 k: {
e.printStackTrace (System.err);1 I# j! {2 f+ K# [7 H
}
4 ]7 q2 C0 F3 n, J+ |
! K! l: C6 P9 W8 ~7 b syncUpdateOrder ();8 ?4 L! ?! M4 P
8 t5 N: j# Z9 p2 u* n# N" U& ^ try {: a) ~' }" Q: b+ t( q
modelActions.createActionTo$message
2 L* X! @8 q) x8 ?% ?) {3 K (heat, new Selector (heat.getClass (), "updateLattice", false));* p* B, t* S3 x0 g9 I' C
} catch (Exception e) {
/ ?5 X( [3 [9 U6 s" L( }1 V4 N System.err.println("Exception updateLattice: " + e.getMessage ());
8 j7 N4 `' `/ Z+ q1 D D }
/ L$ f, M$ Q" b9 ?7 c6 Y# h 5 d' P- R/ i# W7 _' ~
// Then we create a schedule that executes the9 f+ s. `$ {: J$ E* F
// modelActions. modelActions is an ActionGroup, by itself it0 ~& A6 k. E8 A9 K' C! Z% ?
// has no notion of time. In order to have it executed in
t( Z+ n; P1 u- f9 S // time, we create a Schedule that says to use the
/ ~8 q( E3 i1 @5 d // modelActions ActionGroup at particular times. This
0 ]6 K8 Z- f( A3 x7 I: C // schedule has a repeat interval of 1, it will loop every5 R# ?3 B, d: p$ r! [( D1 B; O
// time step. The action is executed at time 0 relative to
2 ^, c$ w, I/ g% _ // the beginning of the loop.
% j7 h# t* B" P2 t( F) _
: m# ?$ J6 y4 B8 ~6 X3 o: r // This is a simple schedule, with only one action that is% d3 g8 Q+ z( X, s7 S
// just repeated every time. See jmousetrap for more- s. w. t4 s4 \% m {
// complicated schedules.
& T+ v: a" S9 @( R x d # v& V" g& h4 S! a- C; t
modelSchedule = new ScheduleImpl (getZone (), 1);
2 x* @/ K( s% a9 n1 M( r6 W) W modelSchedule.at$createAction (0, modelActions);3 j B4 P+ [9 L; _
& D; R% H7 p3 F6 ~$ ~ return this;
n- D9 I; a4 T8 E" ` } |