HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 o* Y3 H+ R! D% E5 ~
R# E5 }: i3 V* c
public Object buildActions () {) k$ k" E4 d% B! r' ^
super.buildActions();
4 D% K: B T" p3 v) T5 d& a 3 \7 t. E3 D8 i/ ]8 ]' V) F) E
// Create the list of simulation actions. We put these in
! T, m0 `; V: w: `1 Z6 |# R- s1 `; I // an action group, because we want these actions to be
6 n) V$ p, E" i+ a% [1 S% R // executed in a specific order, but these steps should
+ o; o$ B& _' E' g1 ^8 X# O- V0 c" E // take no (simulated) time. The M(foo) means "The message! r) _( |6 P1 |. C( E
// called <foo>". You can send a message To a particular
+ s5 \9 d3 b+ e- c7 ^ b/ l7 u // object, or ForEach object in a collection.0 }) L/ P/ j5 L* O' A6 `
# ]8 v, [+ Z! E: o+ C // Note we update the heatspace in two phases: first run3 A# n! x: A* |9 ~( @) Y$ Q: k
// diffusion, then run "updateWorld" to actually enact the" J' Y# M2 ?6 s7 }
// changes the heatbugs have made. The ordering here is
- }/ @; E* P" p/ w3 m // significant!( Q3 G( O8 I' `# z2 D
) H0 t( g+ b" @8 \% s- Z
// Note also, that with the additional
5 \3 Y& w* G5 i8 r" P/ k) c* E // `randomizeHeatbugUpdateOrder' Boolean flag we can
* T1 E9 D/ l! e) A0 a7 F // randomize the order in which the bugs actually run
" M2 c. X0 F$ s& z: G( l' p; X // their step rule. This has the effect of removing any, Y' Z8 x( V% ~( c) ]7 s9 U' H
// systematic bias in the iteration throught the heatbug) g% V# b- I. [* ^/ x: o/ K
// list from timestep to timestep) i- q/ a& W$ m& r3 m! q
! h a1 P* N* E4 f5 `+ z. G // By default, all `createActionForEach' modelActions have0 Y) T2 }; A6 p, X1 l: ]# b$ B! ?" U
// a default order of `Sequential', which means that the
- M, e& m& m6 h3 b9 q/ Z // order of iteration through the `heatbugList' will be
+ n- l7 g- x# h: V1 `) N) `1 E // identical (assuming the list order is not changed
: O8 F4 ]8 b9 W- Q. ] // indirectly by some other process).
. S4 b9 A) L4 C: t* y
6 z/ }. A6 A; X& h" m modelActions = new ActionGroupImpl (getZone ());
4 C4 t6 F7 c* O! p6 K; ]2 E4 g# K1 P
0 O; Z6 ]) q1 f6 c0 r9 t try {
% a' i2 }* U. J$ H3 N modelActions.createActionTo$message4 v8 d! B$ B" `
(heat, new Selector (heat.getClass (), "stepRule", false));: n$ u' F7 A8 ^( }# H
} catch (Exception e) {+ H2 Y9 {# F, j/ {2 M6 m. I/ i
System.err.println ("Exception stepRule: " + e.getMessage ());$ _* W$ G1 w$ i8 G
}% e' b# b% J& x6 H8 v0 e' G( u
0 ?( [; Z+ [# e' W7 V: r- X0 `- Y9 v try {
* `3 Z/ M" [6 I) e2 r o6 ~ Heatbug proto = (Heatbug) heatbugList.get (0);
8 e3 t5 B" F p4 `5 u! J/ E Selector sel =
' y% H# |% [! k. C! j new Selector (proto.getClass (), "heatbugStep", false);
" t8 `. X) [+ W' L# ^ actionForEach =7 u8 v; G6 G' T0 j3 ^6 a, J) F/ v$ S
modelActions.createFActionForEachHomogeneous$call. e) Z" L& C3 S/ V- R' S/ z2 l1 S( q
(heatbugList," ]. S" b# h8 d+ J) t
new FCallImpl (this, proto, sel,
/ v$ [( }0 ~1 d7 W7 `& n' j( { new FArgumentsImpl (this, sel)));2 h+ R. |4 r, @3 i: Z5 n# J. t
} catch (Exception e) {7 ~) e8 ^5 Q; D: v8 N4 p1 r: |
e.printStackTrace (System.err);
5 u# i( F% X# g }2 ]! j' L% [3 e
5 {, u" s4 x: f) T3 j6 ]$ f syncUpdateOrder ();
# D. L+ \, ~9 I9 x$ }, M
6 G+ T. d- s: [" j. Z try {
; w: m8 l/ ~. `3 a/ ]9 V, h modelActions.createActionTo$message
; |6 L" Q" {3 p4 x6 J. @ (heat, new Selector (heat.getClass (), "updateLattice", false));) V& n" p! O) z
} catch (Exception e) {" Q, j! |/ K/ w! U, r0 b& [
System.err.println("Exception updateLattice: " + e.getMessage ());( j# Q" T. X+ q: @
}! _% v/ W/ d5 B
9 D) X3 S! s2 Z% `6 Z // Then we create a schedule that executes the; M( G$ |; S/ ?$ H$ f
// modelActions. modelActions is an ActionGroup, by itself it* @' {# N6 b- f Z( X. g* q( l
// has no notion of time. In order to have it executed in
' Y* A9 A$ F, F: X) Q // time, we create a Schedule that says to use the9 w! \/ M4 \8 ?/ p1 a
// modelActions ActionGroup at particular times. This6 E! I- P7 b3 H+ x- t! n; c
// schedule has a repeat interval of 1, it will loop every
0 X: b' z& m) r8 `' W7 k; G // time step. The action is executed at time 0 relative to
2 Q" ]* z5 X( a' _4 B // the beginning of the loop.( S" q# o$ t k4 S; i
1 G; Q! r! f$ M, m+ g
// This is a simple schedule, with only one action that is
, W. W) W% c) Q1 t5 q* z // just repeated every time. See jmousetrap for more7 ?! m) V _) S2 T
// complicated schedules.
5 l6 @ D- v1 ^' ~ J ) i( \; B9 [ L/ L4 S
modelSchedule = new ScheduleImpl (getZone (), 1);
! A: g/ L9 a) [3 }, l modelSchedule.at$createAction (0, modelActions);" e' p4 s, p& y4 |' n# ]. J
& ~( `8 m0 C3 N# Y% Z return this;
" U! v& Q( _. B( W9 n } |