-
Notifications
You must be signed in to change notification settings - Fork 4
/
Entity.java
308 lines (253 loc) · 8.36 KB
/
Entity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package cn.softeng.basicsim;
import cn.softeng.events.EventHandle;
import cn.softeng.events.EventManager;
import cn.softeng.events.ProcessTarget;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
/**
* 封装创建模拟对象所需的方法和数据的基类。封装基本系统对象以实现离散事件执行
* @date: 12/15/2020 11:41 PM
*/
public class Entity {
/**
* 所有创建的实体数量
*/
private static AtomicLong entityCount = new AtomicLong(0);
/**
* 所有已创建实体的集合
*/
private static final ArrayList<Entity> allInstances;
/**
* 所有命名实体的集合,每一个实体都有一个唯一的名字
*/
private static final HashMap<String, Entity> namedEntities;
/**
* 实体名称
*/
private String entityName;
/**
* 实体唯一id
*/
private final long entityNumber;
/**
* 用于表示实体当前状态,实体同一时间可能有多个状态,所以用位运算来表示
*/
int flags;
static final int FLAG_ADDED = 0x20; // entity was defined after the 'RecordEdits' flag
static final int FLAG_GENERATED = 0x80; // entity was created during the execution of the simulation
static final int FLAG_DEAD = 0x0100; // entity has been deleted
static final int FLAG_REGISTERED = 0x0200; // entity is included in the namedEntities HashMap
static final int FLAG_RETAINED = 0x0400; // entity is retained when the model is reset between runs
static {
allInstances = new ArrayList<>(100);
namedEntities = new HashMap<>(100);
}
public Entity() {
entityNumber = entityCount.incrementAndGet();
synchronized (allInstances) {
allInstances.add(this);
}
// 初始化实体状态为空
flags = 0;
}
/**
* 模型运行前初始化实体
* 该方法中操作只能依赖于自身,这样才可以在任何序列中初始化每个实体
* (相当于命令模式中的接收者,是真正执行命令操作的功能代码)
*/
public void earlyInit() {
// 将实体的属性重置为初始值
}
/**
* 初始化实体
* (相当于命令模式中的接收者,是真正执行命令操作的功能代码)
*/
public void lateInit() {
}
/**
* 启动该实体的模型运行
* (相当于命令模式中的接收者,是真正执行命令操作的功能代码)
*/
public void startUp() {
}
/**
* 仿真运行结束后的一些操作,如生成报告
*/
public void doEnd() {
}
/**
* 获取当前仿真ticks(时刻)值
* @return
*/
public final long getSimTicks() {
return EventManager.simTicks();
}
public final double getSimTime() {
return EventManager.simSeconds();
}
/**
* 返回实体名称
* @return
*/
public final String getName() {
return entityName;
}
/**
* 封装异常信息并抛出
* @param fmt
* @param args
* @throws ErrorException
*/
public void error(String fmt, Object... args) throws ErrorException {
final StringBuilder sb = new StringBuilder(this.getName());
sb.append(": ");
sb.append(String.format(fmt, args));
throw new ErrorException(sb.toString());
}
/**
* 返回所有创建过个的实体
* @return
*/
public static ArrayList<? extends Entity> getAll() {
synchronized (allInstances) {
return allInstances;
}
}
public void kill() {
synchronized (allInstances) {
allInstances.remove(this);
}
if (!testFlag(FLAG_GENERATED)) {
synchronized (namedEntities) {
if (namedEntities.get(entityName) == this) {
namedEntities.remove(entityName);
}
entityName = null;
}
}
setFlag(FLAG_DEAD);
}
public static <T extends Entity> T fastCopy(T entity, String name) {
T ret = (T) generateEntityWithName(entity.getClass(), name);
return ret;
}
private static <T extends Entity> T generateEntityWithName(Class<T> proto, String name) {
T ent = null;
try {
ent = proto.newInstance();
if (name != null) {
ent.setName(name);
} else {
ent.setName(proto.getSimpleName() + "-" + ent.getEntityNumber());
}
} catch (Exception e) {
e.printStackTrace();
}
return ent;
}
public long getEntityNumber() {
return entityNumber;
}
public void setName(String newName) {
if (testFlag(FLAG_GENERATED)) {
entityName = newName;
return;
}
synchronized (namedEntities) {
namedEntities.remove(entityName);
entityName = newName;
namedEntities.put(entityName, this);
}
}
// *******************************
// 实体状态变量flags的一系列未操作运算符
// *******************************
/**
* 给当前状态变量flags添加新的状态
* 利用或运算的性质,向flags添加新的转态
* @param flag 新添加的转态
*/
public void setFlag(int flag) {
flags |= flag;
}
/**
* 从状态变量flags中清除指定状态flag
* @param flag 要清除的状态
*/
public void clearFlag(int flag) {
// 先对flag取反,在将flag与flags进行与操作
// 从而从flags中移除flag
flags &= ~flag;
}
/**
* 测试状态变量中flags中是否包含flag状态
* @param flag 判断是否包含该状态
* @return
*/
public boolean testFlag(int flag) {
return (flags & flag) != 0;
}
public static <T extends Entity> InstanceIterable<T> getInstanceIterator(Class<T> proto) {
return new InstanceIterable<>(proto);
}
public static <T extends Entity> ClonesOfIterable<T> getClonesOfIterable(Class<T> proto) {
return new ClonesOfIterable<>(proto);
}
public static <T extends Entity> ClonesOfIterable<T> getClonesOfIterator(Class<T> proto){
return new ClonesOfIterable<>(proto);
}
public final void scheduleProcess(double secs, int priority, ProcessTarget t) {
EventManager.scheduleSeconds(secs, priority, false, t, null);
}
public final void scheduleProcess(double secs, int priority, ProcessTarget t, EventHandle handle) {
EventManager.scheduleSeconds(secs, priority, false, t, handle);
}
public final void scheduleProcess(double secs, int priority, boolean fifo, ProcessTarget t, EventHandle handle) {
EventManager.scheduleSeconds(secs, priority, fifo, t, handle);
}
public final void scheduleProcessTicks(long ticks, int priority, boolean fifo, ProcessTarget t, EventHandle h) {
EventManager.scheduleTicks(ticks, priority, fifo, t, h);
}
public final void scheduleProcessTicks(long ticks, int priority, ProcessTarget t, EventHandle handle) {
EventManager.scheduleTicks(ticks, priority, false, t, handle);
}
public final void scheduleProcessTicks(long ticks, int priority, ProcessTarget t) {
EventManager.scheduleTicks(ticks, priority, false, t, null);
}
/**
* 更具实体名称,获取指定实体
* @param name
* @return
*/
public static Entity getNamedEntity(String name) {
synchronized (namedEntities) {
return namedEntities.get(name);
}
}
/**
* 重置实体收集的统计数据
*/
public void clearStatistics() {}
/**
* 更新实体的统计数据
*/
public void updateStatistics() {}
/**
* !慎用,用于DesSim.initModel 重置实体集合,避免用户不合法输入
*/
public static void resetAllInstance(List<Entity> entities) {
allInstances.clear();
allInstances.addAll(entities);
}
/**
* !慎用,用于DesSim.initModel 重置实体集合,避免用户不合法输入
*/
public static void resetNamedEntities(List<Entity> entities) {
namedEntities.clear();
for (Entity entity : entities) {
namedEntities.put(entity.getName(), entity);
}
}
}