HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 U8 G6 p) A, _/ H0 r% q6 A+ g5 W4 @' j& N' G2 r% J9 n/ c5 A
public Object buildActions () {6 M9 o+ s# o3 Z
super.buildActions();, ~6 D/ p5 C' T' `- U+ T$ V- M1 S
( ? T/ x8 U, Z# M( B# m. T // Create the list of simulation actions. We put these in0 I7 y4 G3 u- [8 K0 Y
// an action group, because we want these actions to be
( v& g. v. \) a // executed in a specific order, but these steps should
$ _! {9 Q) q- X2 n9 l, H6 l& w8 A // take no (simulated) time. The M(foo) means "The message1 r/ C0 }1 Y0 v( }9 c+ e! I
// called <foo>". You can send a message To a particular% Y; x: B7 g7 L$ P$ t y0 N/ [
// object, or ForEach object in a collection.
& D* L; \7 D: ^+ W+ Y
! K' f) a) q( G // Note we update the heatspace in two phases: first run
6 N4 P s6 B6 _- z3 A2 o2 ? // diffusion, then run "updateWorld" to actually enact the
/ y% b2 e, t% z7 h7 g) z // changes the heatbugs have made. The ordering here is; o* |) Y3 p8 r4 R8 r
// significant!% w1 o' W/ [# t: u, R
# v" X! D3 ?' u& K3 n$ K/ f
// Note also, that with the additional
. J( [' U1 c9 H5 ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
* J% ]7 \% j9 A0 O( @) u& H6 @ // randomize the order in which the bugs actually run
@! f: ~0 q; w // their step rule. This has the effect of removing any
8 a$ d& }3 j1 v // systematic bias in the iteration throught the heatbug0 k& D3 u/ u, S- I& O, v7 E
// list from timestep to timestep* `( M$ R3 i& n
/ _) y6 ]3 [& S // By default, all `createActionForEach' modelActions have
/ \4 }. a2 \" d6 a1 j9 U8 Y- _6 C // a default order of `Sequential', which means that the2 O' v* }, B# N. s( C# x
// order of iteration through the `heatbugList' will be9 F9 q) i9 S* ^) z* g2 D' o" F# M
// identical (assuming the list order is not changed
; j, b; K1 z* _! b$ j // indirectly by some other process).
4 Z* K$ _- ]; Z( f+ x/ ~
9 W1 { U& V* U3 Z+ h* C3 c modelActions = new ActionGroupImpl (getZone ());
+ {( R# i8 s( L
" @' O" _0 t& n try {+ s# A& x. }$ M6 q' j a2 K
modelActions.createActionTo$message
/ [3 d: A6 V! m& i+ Y% ^+ _ (heat, new Selector (heat.getClass (), "stepRule", false));4 K0 X: o$ n( Q
} catch (Exception e) {! |# t) z. ]/ z' E+ F
System.err.println ("Exception stepRule: " + e.getMessage ());% a+ I# `0 @7 n6 C9 d2 x5 Y
}: ^4 A8 b" y6 T
* ^& T5 Y! N9 E/ P/ B* W try {
9 G! ?0 C% E5 ]- ^0 d0 b2 n Heatbug proto = (Heatbug) heatbugList.get (0);# O; d5 A. ^% D; k
Selector sel =
8 u C2 y; V1 r9 x# H new Selector (proto.getClass (), "heatbugStep", false);
, v+ y+ P1 d) f. i, |- L* D actionForEach =
l# t: q( h, E) x modelActions.createFActionForEachHomogeneous$call
( s7 j l+ P4 {8 ~- a (heatbugList,: F+ l! G$ X" g0 C" I
new FCallImpl (this, proto, sel,
5 K/ K2 M) R3 B# j3 ?) E1 E new FArgumentsImpl (this, sel)));
" A: D: ~6 }( r3 p: L6 f$ _ } catch (Exception e) {. P& v2 x5 S1 @( Y% ?/ ]
e.printStackTrace (System.err);
5 N3 c, y+ Z! F- r8 \6 D1 o& ] }( P! G3 ]! x5 v; p4 O' ^/ Q
# P, x* J: Q+ O. x) S1 ` syncUpdateOrder ();2 w/ }' A0 o. k6 {0 F2 }
: g( q* S3 H9 c7 K$ B) S3 k: P try {
8 M+ V2 S' V+ a- ?# l) S modelActions.createActionTo$message / N; s/ U- _/ E5 T1 ~0 O2 K$ K
(heat, new Selector (heat.getClass (), "updateLattice", false));7 p- z" q* ^) i- g) [9 F
} catch (Exception e) {
% H \0 f5 ]) k5 a U% v System.err.println("Exception updateLattice: " + e.getMessage ());
, }3 h- ~* n n6 z' g }
& w( i4 A# ^9 k* x) \
' N D$ c6 V* w" o2 {8 C3 l* S8 U // Then we create a schedule that executes the
4 [3 [; p$ ?7 ]; [/ ` // modelActions. modelActions is an ActionGroup, by itself it8 j. g d- @( h! i. t
// has no notion of time. In order to have it executed in
. c! @0 J5 U* O; q // time, we create a Schedule that says to use the
* Y9 `5 l- ?1 o3 L4 j$ A$ P, z // modelActions ActionGroup at particular times. This5 _' A3 H9 ~- j k5 L
// schedule has a repeat interval of 1, it will loop every
( Z* ]$ N& F9 f- e# C# J // time step. The action is executed at time 0 relative to
1 d( A. U8 c; z9 _& q9 q // the beginning of the loop.
; B; w9 x3 p4 n+ }. J6 p O$ c2 Y
7 V. ~4 k7 G9 U. }1 ^% K // This is a simple schedule, with only one action that is
2 v1 B* y' w- J* w4 U W // just repeated every time. See jmousetrap for more
5 H) h- r3 q( n" A // complicated schedules.
; H4 S. c9 L: x- ^
, Q0 c5 T( x( b/ Z1 Q: y! y5 U! p modelSchedule = new ScheduleImpl (getZone (), 1);4 K% s/ y1 X' V- C6 p0 E$ D1 f
modelSchedule.at$createAction (0, modelActions);
# z* `" c6 `; w/ L4 i; \ m 7 f% Q5 \6 t+ O/ p2 _' g
return this;
; g* R3 E2 b- e5 v4 @7 W2 i4 F" l } |