HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ R4 Q9 k$ n3 L% A
( o4 B7 `$ U& o; i# G! @ public Object buildActions () {. I+ l u: D9 l! w
super.buildActions();( R0 c& a1 a4 Y$ d
) k6 |& [; b) d* K/ i |0 L; ^ // Create the list of simulation actions. We put these in- r6 K# g/ g0 x& I. p' p/ n
// an action group, because we want these actions to be
7 s' V Q% d0 S. d) U4 @4 u3 J( j0 s // executed in a specific order, but these steps should% @, ?$ t) y- P, N
// take no (simulated) time. The M(foo) means "The message
$ c' a$ b' {' w# i5 w // called <foo>". You can send a message To a particular
% r$ A7 t8 w, L* m+ Z0 ~/ e a1 S // object, or ForEach object in a collection.
' I+ q: ]1 I# o d2 z/ t. M % `% w2 t+ |8 T* {8 i, y& Q
// Note we update the heatspace in two phases: first run- M' _* w8 I# {3 }8 S8 p* G
// diffusion, then run "updateWorld" to actually enact the
& Y! I6 c1 ^5 m- J* C5 g. S/ B // changes the heatbugs have made. The ordering here is
/ J" X! `. n3 v7 {' v9 i( g4 ]1 i // significant!7 D0 j/ o& f+ ]2 s
" q b3 ]) D' C! N' D8 v& n( S // Note also, that with the additional
8 w! H ^$ k6 c; ^1 J0 O // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 W( c& l1 C( @, e! J$ k/ @/ i5 p // randomize the order in which the bugs actually run
$ X) L( ?: W8 g# {- D // their step rule. This has the effect of removing any
3 e! C# ?# {; G* O0 g. z" M // systematic bias in the iteration throught the heatbug, M6 l& x' O5 Q. _, R& W5 Y
// list from timestep to timestep
- t8 t% q+ G' M6 o, o( `" _/ E1 O * w( L) ~/ u4 {9 M3 i
// By default, all `createActionForEach' modelActions have
* W/ r( w4 H) f; ^( Q q // a default order of `Sequential', which means that the
! U2 T( j+ @" o# N y // order of iteration through the `heatbugList' will be. G$ W9 b( H2 V/ R* e
// identical (assuming the list order is not changed+ r* C3 f% v; Q1 m
// indirectly by some other process).( K, Q3 x: R+ b: x( B: l
# R& X; i9 t( e, v; _8 A+ f- a. q4 { modelActions = new ActionGroupImpl (getZone ());
x3 R( D7 k& ~- z/ _) b/ J( G: [2 N% d% ~8 b8 g& |- Q- ]0 B; x
try {! l+ w* Z- V$ L& y9 j, @
modelActions.createActionTo$message
+ w6 I+ c9 D! Y/ ?5 `# ] (heat, new Selector (heat.getClass (), "stepRule", false));
: d- ]5 A- R4 v8 G; V } catch (Exception e) {
! | W% B9 i$ P System.err.println ("Exception stepRule: " + e.getMessage ());0 X A9 \( g( Y) v! n* Y: t3 {
}& v$ I* q) _: ~/ {5 b
5 |8 A4 w9 Y! S2 U p5 |7 c u/ G try {) t/ f% \ i' s5 a
Heatbug proto = (Heatbug) heatbugList.get (0);: J9 i" v$ O% p7 Z5 z1 u, U
Selector sel =
w# u/ \8 o$ J) o% { new Selector (proto.getClass (), "heatbugStep", false);
) E& t# B( o. J, N% K9 p* N actionForEach =- X, C7 G6 p0 F" ~3 O1 r& u
modelActions.createFActionForEachHomogeneous$call; W. f4 ~* \4 L! r* L k0 \% s/ T
(heatbugList,* G! w6 B# B7 G, Y& N
new FCallImpl (this, proto, sel,
( e# q, h5 ?0 _ new FArgumentsImpl (this, sel)));5 E9 }. ~7 _3 V8 T, l" g
} catch (Exception e) {9 N1 P( }& C8 {/ `
e.printStackTrace (System.err);& w; z2 _8 }9 i* g# F
}9 A7 k$ W. A: i) d+ G( w, q
0 Q' _4 d+ X4 y9 N3 S, ~3 L9 D, ] syncUpdateOrder ();$ g. P" L7 X6 E4 |
9 i( n' z$ {; J8 Z
try {
% v% a& \6 `" v ]; x3 k7 D H. Y modelActions.createActionTo$message ! t) y* ]. W5 `& C" z! _
(heat, new Selector (heat.getClass (), "updateLattice", false));. V( D1 s7 O9 |+ F5 P
} catch (Exception e) {
) D( i {! O& w3 k System.err.println("Exception updateLattice: " + e.getMessage ());
+ A7 p6 ?1 I. N, ~7 b r }
* ]1 F2 J, H, u3 u' [3 V . j& p% Q* k: M' F' j( U
// Then we create a schedule that executes the) p4 \" Q9 b, p- o: I
// modelActions. modelActions is an ActionGroup, by itself it" ^3 m- ~; e6 ^* y! @" z
// has no notion of time. In order to have it executed in/ X) ^1 p' _- _+ K
// time, we create a Schedule that says to use the
; B# l0 q/ u* x9 G7 c7 E4 Z // modelActions ActionGroup at particular times. This
* x' V. r) _1 g // schedule has a repeat interval of 1, it will loop every$ b! [4 Q0 [+ Q5 R* K
// time step. The action is executed at time 0 relative to* w+ t" r! [" t# d6 Z2 b- | }2 [9 ?& @
// the beginning of the loop.
! k( V, r$ k9 m- {2 y2 G8 v: f) j3 d+ D1 y# }' a
// This is a simple schedule, with only one action that is
) T4 z2 m! n- h( f$ Q // just repeated every time. See jmousetrap for more( R0 b* @2 r$ S" H$ I7 }" v4 Z
// complicated schedules.. q- B. g# S5 @% D# Q5 k
2 _: }" \% H( y9 y# z3 L
modelSchedule = new ScheduleImpl (getZone (), 1);' X0 Y* s2 D' W4 o
modelSchedule.at$createAction (0, modelActions);4 v+ d) ~4 I! v5 W
# b$ ^) z3 c2 g X/ R3 ~ return this;4 C2 b0 N$ R7 M) `
} |