HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' y. e# o9 n+ w* z; V j9 `9 v. y
/ t, q! W7 j; Y2 n+ q( y public Object buildActions () {
! ]6 ?$ z" Y H super.buildActions();
+ D7 k# S8 y; P/ j5 i/ |# ? P4 C$ [+ W8 B1 Q. K l- G
// Create the list of simulation actions. We put these in
/ M6 S& F9 f7 _' k7 c! t9 a // an action group, because we want these actions to be3 V0 i. a( I! q5 Z) q7 l% W6 ?
// executed in a specific order, but these steps should: ]- I" \1 C& B9 a6 }* t
// take no (simulated) time. The M(foo) means "The message
8 y% k/ C: ~& d* O: E+ Y9 z // called <foo>". You can send a message To a particular5 r9 t2 T- N8 Q. H% W- O- D: W
// object, or ForEach object in a collection.( e0 G) I9 O) D! j8 P% M q
6 G0 I! L) c U6 J9 }/ P // Note we update the heatspace in two phases: first run; B; B- [1 x6 m" U# j$ \0 m
// diffusion, then run "updateWorld" to actually enact the9 _' H4 o. |/ W' ]' l
// changes the heatbugs have made. The ordering here is
; o8 z3 U- j; V, R* O5 W# `- S // significant!
! f: A) t* Y6 D4 ?1 p% g, ^ 8 |6 v% h5 i$ ~
// Note also, that with the additional
# D2 I5 U: l$ F' B // `randomizeHeatbugUpdateOrder' Boolean flag we can+ a! \5 ]' a! x) ~* A( ]
// randomize the order in which the bugs actually run8 e% n1 ~- D4 s8 f. a
// their step rule. This has the effect of removing any9 {) `! Z8 L. H2 l$ {
// systematic bias in the iteration throught the heatbug$ I& U& T3 | |% N# v6 `% J: P
// list from timestep to timestep, M- _ F+ R; J
: }0 q S! V4 y2 t
// By default, all `createActionForEach' modelActions have
8 i7 a. r0 f* w8 v // a default order of `Sequential', which means that the. \, x. L$ C) a6 x% k# v* ?
// order of iteration through the `heatbugList' will be
- O7 @: w3 ^0 O/ q8 S9 T" k0 M% W // identical (assuming the list order is not changed, H. x* m: m' n
// indirectly by some other process).
, K4 U/ M0 y" {( i
. j" \! F v7 u- T/ r+ e/ W# P9 |. F modelActions = new ActionGroupImpl (getZone ());
- t7 d9 s' Y5 `2 s$ _; o- I# g. q: ^4 u+ N$ l3 ^) t' l) Q- @+ H
try {( ^, Y- R" k1 X9 |2 I F5 `2 b! i3 g
modelActions.createActionTo$message
# ?& ?* G$ T. H (heat, new Selector (heat.getClass (), "stepRule", false));5 T5 |0 O/ z+ t$ t5 ]3 S
} catch (Exception e) {: K; i, b+ H0 u# S' v, Q
System.err.println ("Exception stepRule: " + e.getMessage ());$ r0 i: a3 M' a6 g' w1 t! D
}/ }- ?& |- p2 o( a6 y }
% U) ]' a6 P% |0 q( L& a3 L3 u
try {
) @7 ~2 q( B1 T) ]! e, q Heatbug proto = (Heatbug) heatbugList.get (0);' t4 }) c6 C: C4 R
Selector sel =
: q& x2 {* [, A, H% Y new Selector (proto.getClass (), "heatbugStep", false);
2 g0 A2 ]3 ^3 u4 V actionForEach =
6 X% V* i _/ B/ G7 Y/ j6 F& A' ] modelActions.createFActionForEachHomogeneous$call
* \+ @/ {; h* Y5 |7 [ (heatbugList, @8 g4 ^; @/ U: O! O0 Y
new FCallImpl (this, proto, sel,
/ t7 z# a$ K" r: x/ ] new FArgumentsImpl (this, sel)));- M5 z/ p7 I" {9 n N
} catch (Exception e) {
1 T% I% O. v% G9 @( u6 A% u% t e.printStackTrace (System.err);2 R! m& W( q$ x3 o. k2 W9 P
}
- ]$ r' \2 n4 I2 @# u- ~7 v& ?4 | 6 m: O. @$ g6 E( t: J1 z0 U
syncUpdateOrder ();' I4 i" q( w: T1 G
+ V$ {- b, z9 T# q$ z( B5 N, [ try {+ [4 g6 `2 Q# [$ s/ k7 B- V9 o/ j& H
modelActions.createActionTo$message
- y* W- H! G j! f0 K, T: V (heat, new Selector (heat.getClass (), "updateLattice", false));
: s. X- s% o) B @% {7 f N/ L5 Y } catch (Exception e) {% k9 w/ D _$ @0 Z& D0 G
System.err.println("Exception updateLattice: " + e.getMessage ());
) q8 `; H2 D# _* A8 n8 G+ }' u }9 S' _( |. J3 z. }
: @1 m& u! E9 @ // Then we create a schedule that executes the
h5 P+ J# I1 E; W$ Y. x3 H c- A // modelActions. modelActions is an ActionGroup, by itself it
: q, L& @3 K! a* U3 Q, _ // has no notion of time. In order to have it executed in
2 K0 i1 ~4 w- }) r7 Q b // time, we create a Schedule that says to use the
" M) t# ]3 _/ i2 m+ B // modelActions ActionGroup at particular times. This4 a5 a8 S& y z! Z0 t0 r
// schedule has a repeat interval of 1, it will loop every; N2 m% ^! T7 c8 z& M' v
// time step. The action is executed at time 0 relative to [8 M+ ~/ o& t8 a$ U8 |( w/ F9 r3 Z
// the beginning of the loop.' a/ ?3 r9 V3 X. }% q& x( b
% O$ |, N7 ?& v+ \
// This is a simple schedule, with only one action that is4 N3 n& S; y6 u0 h; `
// just repeated every time. See jmousetrap for more
- V# B- Q8 Z, ~) r) C: T // complicated schedules.7 q( S8 h i0 n+ U
% S9 F5 t' M# t8 ~8 _/ ~+ M/ b
modelSchedule = new ScheduleImpl (getZone (), 1);
: ~: O5 H0 K( e% J$ Q) y2 j modelSchedule.at$createAction (0, modelActions); }3 Q4 {. R/ W# B$ {, w7 t
# q/ {( t1 h/ k6 V
return this;* Z3 Y/ i7 \% H7 e* t; t7 \
} |