1
|
|
|
package it.cnr.istc.pst.platinum.ai.framework.time.tn; |
2
|
|
|
|
3
|
|
|
import java.util.ArrayList; |
4
|
|
|
import java.util.HashSet; |
5
|
|
|
import java.util.Iterator; |
6
|
|
|
import java.util.LinkedList; |
7
|
|
|
import java.util.List; |
8
|
|
|
import java.util.Set; |
9
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
10
|
|
|
|
11
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.microkernel.FrameworkObject; |
12
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.ex.InconsistentDistanceConstraintException; |
13
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.ex.InconsistentTpValueException; |
14
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.ex.TemporalNetworkTransactionFailureException; |
15
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.ex.TimePointNotFoundException; |
16
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.AddRelationTemporalNetworkNotification; |
17
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.AddTimePointTemporalNetworkNotification; |
18
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.DelRelationTemporalNetworkNotification; |
19
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.DelTimePointTemporalNetworkNotification; |
20
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.TemporalNetworkNotificationFactory; |
21
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.TemporalNetworkNotificationTypes; |
22
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.lang.event.TemporalNetworkObserver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @author alessandro |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
public abstract class TemporalNetwork extends FrameworkObject |
30
|
|
|
{ |
31
|
|
|
private final AtomicInteger tpCounter = new AtomicInteger(0); // time point ID counter |
32
|
|
|
protected long origin; |
33
|
|
|
protected long horizon; |
34
|
|
|
|
35
|
|
|
// origin time point |
36
|
|
|
protected TimePoint tpOrigin; |
37
|
|
|
// horizon time point |
38
|
|
|
protected TimePoint tpHorizion; |
39
|
|
|
|
40
|
|
|
// data structures |
41
|
|
|
private Set<TimePoint> recyclePoints; |
42
|
|
|
|
43
|
|
|
// temporal network observers |
44
|
|
|
private final List<TemporalNetworkObserver> observers; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates and initialize a Temporal Network instance. |
48
|
|
|
* |
49
|
|
|
* The constructor takes as input a temporal origin and a |
50
|
|
|
* temporal horizon. These values determine the lower and the |
51
|
|
|
* upper bounds of time points variables respectively. |
52
|
|
|
* |
53
|
|
|
* @param origin |
54
|
|
|
* @param horizon |
55
|
|
|
*/ |
56
|
|
|
protected TemporalNetwork(long origin, long horizon) { |
57
|
|
|
super(); |
58
|
|
|
// initialize data |
59
|
|
|
this.origin = origin; |
60
|
|
|
this.horizon = horizon; |
61
|
|
|
|
62
|
|
|
// create the origin time point |
63
|
|
|
this.tpOrigin = new TimePoint(tpCounter.getAndIncrement(), origin, origin); |
64
|
|
|
this.tpOrigin.setLowerBound(origin); |
65
|
|
|
this.tpOrigin.setUpperBound(origin); |
66
|
|
|
|
67
|
|
|
// create the horizon time point |
68
|
|
|
this.tpHorizion = new TimePoint(tpCounter.getAndIncrement(), horizon, horizon); |
69
|
|
|
this.tpHorizion.setLowerBound(horizon); |
70
|
|
|
this.tpHorizion.setUpperBound(horizon); |
71
|
|
|
|
72
|
|
|
// setup recycle points and observers |
73
|
|
|
this.recyclePoints = new HashSet<TimePoint>(); |
74
|
|
|
this.observers = new LinkedList<TemporalNetworkObserver>(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Subscribes a new observer to the Temporal Network |
79
|
|
|
* |
80
|
|
|
* @param obs |
81
|
|
|
*/ |
82
|
|
|
public final void subscribe(TemporalNetworkObserver obs) { |
83
|
|
|
synchronized (this.observers) { |
84
|
|
|
// add observer |
85
|
|
|
this.observers.add(obs); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the value of the origin |
91
|
|
|
* |
92
|
|
|
* @return |
93
|
|
|
*/ |
94
|
|
|
public final long getOrigin() { |
95
|
|
|
return origin; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the value of the horizon |
100
|
|
|
* |
101
|
|
|
* @return |
102
|
|
|
*/ |
103
|
|
|
public final long getHorizon() { |
104
|
|
|
return this.horizon; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return |
110
|
|
|
*/ |
111
|
|
|
public abstract int size(); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the temporal origin reference time point |
115
|
|
|
* |
116
|
|
|
* @return |
117
|
|
|
*/ |
118
|
|
|
public final TimePoint getOriginTimePoint() { |
119
|
|
|
return this.tpOrigin; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
* @return |
125
|
|
|
*/ |
126
|
|
|
public final TimePoint getHorizonTimePoint() { |
127
|
|
|
return this.tpHorizion; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the time point with selected id |
132
|
|
|
* |
133
|
|
|
* @param id |
134
|
|
|
* @return |
135
|
|
|
* @throws TimePointNotFoundException |
136
|
|
|
*/ |
137
|
|
|
public abstract TimePoint getTimePoint(int id) |
138
|
|
|
throws TimePointNotFoundException; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns all the time points of the temporal network |
142
|
|
|
* |
143
|
|
|
* @return |
144
|
|
|
*/ |
145
|
|
|
public abstract List<TimePoint> getTimePoints(); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns all distance constraints where the specified |
149
|
|
|
* time point is the source of the constraint |
150
|
|
|
* |
151
|
|
|
* @param tp |
152
|
|
|
* @return |
153
|
|
|
*/ |
154
|
|
|
public abstract List<TimePointDistanceConstraint> getConstraints(TimePoint tp); |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
* @return |
159
|
|
|
*/ |
160
|
|
|
public abstract List<TimePointDistanceConstraint> getContingentConstraints(); |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the list of all distance constraints of the network |
164
|
|
|
* |
165
|
|
|
* @return |
166
|
|
|
*/ |
167
|
|
|
public abstract List<TimePointDistanceConstraint> getConstraints(); |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns all distance constraints involving time points tp1 and tp2. The |
171
|
|
|
* method returns both constraints (tp1,tp2) and constraints (tp2,tp1) if |
172
|
|
|
* they exist |
173
|
|
|
* |
174
|
|
|
* @param tp1 |
175
|
|
|
* @param tp2 |
176
|
|
|
* @return |
177
|
|
|
*/ |
178
|
|
|
public abstract List<TimePointDistanceConstraint> getConstraints(TimePoint tp1, TimePoint tp2); |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the distance constraint between the origin of the network and the time point. |
182
|
|
|
* |
183
|
|
|
* The method returns null if no constraint is found |
184
|
|
|
* |
185
|
|
|
* @param point |
186
|
|
|
* @return |
187
|
|
|
*/ |
188
|
|
|
public abstract List<TimePointDistanceConstraint> getConstraintFromOrigin(TimePoint point); |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
* @param tp |
193
|
|
|
* @return |
194
|
|
|
*/ |
195
|
|
|
public abstract List<TimePointDistanceConstraint> getConstraintToHorizon(TimePoint tp); |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Creates a new flexible TimePoint |
199
|
|
|
* |
200
|
|
|
* The method can also use a previously created (and |
201
|
|
|
* deleted) time point rather than create a new instance |
202
|
|
|
* |
203
|
|
|
* @return |
204
|
|
|
* @throws InconsistentDistanceConstraintException |
205
|
|
|
*/ |
206
|
|
|
public final TimePoint addTimePoint() |
207
|
|
|
throws InconsistentDistanceConstraintException { |
208
|
|
|
|
209
|
|
|
// create a time point or use a previous created one |
210
|
|
|
TimePoint tp = null; |
211
|
|
|
// extract the first element |
212
|
|
|
Iterator<TimePoint> it = this.recyclePoints.iterator(); |
213
|
|
|
// check among recycled time points first |
214
|
|
|
if (it.hasNext()) { |
215
|
|
|
|
216
|
|
|
// extract next time point |
217
|
|
|
tp = it.next(); |
218
|
|
|
// set domain |
219
|
|
|
tp.clear(this.origin, this.horizon); |
220
|
|
|
// remove time point from the set |
221
|
|
|
it.remove(); |
222
|
|
|
} |
223
|
|
|
else { |
224
|
|
|
|
225
|
|
|
// actually create flexible time point |
226
|
|
|
tp = new TimePoint(tpCounter.getAndIncrement(), this.origin, this.horizon); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// add time point to the network |
230
|
|
|
this.doAddTimePoint(tp); |
231
|
|
|
// create notification |
232
|
|
|
AddTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory. |
233
|
|
|
createNotification(TemporalNetworkNotificationTypes.ADD_TP); |
234
|
|
|
// add time point |
235
|
|
|
info.addTimePoint(tp); |
236
|
|
|
|
237
|
|
|
// check observers |
238
|
|
|
synchronized (this.observers) { |
239
|
|
|
|
240
|
|
|
// notify observers |
241
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
242
|
|
|
// do notify |
243
|
|
|
obs.notify(info); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// return new time point |
248
|
|
|
return tp; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Creates N flexible time points |
253
|
|
|
* |
254
|
|
|
* The method can also use previously created (and |
255
|
|
|
* deleted) time points rather than create new instances |
256
|
|
|
* |
257
|
|
|
* @param n |
258
|
|
|
* @return |
259
|
|
|
* @throws TemporalNetworkTransactionFailureException |
260
|
|
|
* |
261
|
|
|
*/ |
262
|
|
|
public final List<TimePoint> addMultipleTimePoints(int n) |
263
|
|
|
throws TemporalNetworkTransactionFailureException |
264
|
|
|
{ |
265
|
|
|
// notify flag |
266
|
|
|
boolean notify = true; |
267
|
|
|
// create notification |
268
|
|
|
AddTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory. |
269
|
|
|
createNotification(TemporalNetworkNotificationTypes.ADD_TP); |
270
|
|
|
|
271
|
|
|
try { |
272
|
|
|
|
273
|
|
|
// create time points |
274
|
|
|
for (int i= 0; i < n; i++) { |
275
|
|
|
|
276
|
|
|
// create a flexible time point |
277
|
|
|
TimePoint tp = this.addTimePoint(); |
278
|
|
|
// add time point to the notification |
279
|
|
|
info.addTimePoint(tp); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
catch (InconsistentDistanceConstraintException ex) { |
283
|
|
|
|
284
|
|
|
// remove created time points |
285
|
|
|
for (TimePoint tp : info.getPoints()) { |
286
|
|
|
|
287
|
|
|
// remove created time point |
288
|
|
|
this.doRemoveTimePoint(tp); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// do not notify |
292
|
|
|
notify = false; |
293
|
|
|
// throw exception |
294
|
|
|
throw new TemporalNetworkTransactionFailureException(ex.getMessage()); |
295
|
|
|
} |
296
|
|
|
finally { |
297
|
|
|
|
298
|
|
|
// check if notification is necessary |
299
|
|
|
if (notify) { |
300
|
|
|
|
301
|
|
|
// check observers |
302
|
|
|
synchronized (this.observers) { |
303
|
|
|
// notify observers |
304
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
305
|
|
|
// do notify |
306
|
|
|
obs.notify(info); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// get time points |
313
|
|
|
return info.getPoints(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Creates a new TimePoint and fix it into the network at |
318
|
|
|
* the desired time (at) |
319
|
|
|
* |
320
|
|
|
* The method throws an exception if the specified value (at) |
321
|
|
|
* is out of the temporal network domain |
322
|
|
|
* |
323
|
|
|
* The method can also use a previously created (and deleted) time |
324
|
|
|
* point rather than create a new instance |
325
|
|
|
* |
326
|
|
|
* @param at |
327
|
|
|
* @return |
328
|
|
|
* @throws InconsistentTpValueException |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
public final TimePoint addTimePoint(long at) |
|
|
|
|
331
|
|
|
throws InconsistentTpValueException, InconsistentDistanceConstraintException { |
332
|
|
|
|
333
|
|
|
// check desired value |
334
|
|
|
if (at < this.origin || at > this.horizon) { |
335
|
|
|
// inconsistent value |
336
|
|
|
throw new InconsistentTpValueException("TimePoint value (= " + at +") out of the domain [origin= " + this.origin + ", horizon= " + this.horizon); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// create a time point or use a previous created one |
340
|
|
|
TimePoint tp = null; |
341
|
|
|
// extract the first element |
342
|
|
|
Iterator<TimePoint> it = this.recyclePoints.iterator(); |
343
|
|
|
// check recycled time points first |
344
|
|
|
if (it.hasNext()) { |
345
|
|
|
// extract next time point |
346
|
|
|
tp = it.next(); |
347
|
|
|
// set bounds |
348
|
|
|
tp.clear(at, at); |
349
|
|
|
// remove time point from the underlying set |
350
|
|
|
it.remove(); |
351
|
|
|
} |
352
|
|
|
else { |
353
|
|
|
|
354
|
|
|
// create time point |
355
|
|
|
tp = new TimePoint(tpCounter.getAndIncrement(), at, at); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// add time point to the network |
359
|
|
|
this.doAddTimePoint(tp); |
360
|
|
|
|
361
|
|
|
// create notification |
362
|
|
|
AddTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory.createNotification(TemporalNetworkNotificationTypes.ADD_TP); |
363
|
|
|
info.addTimePoint(tp); |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
// check observers |
367
|
|
|
synchronized (this.observers) { |
368
|
|
|
// notify observers |
369
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
370
|
|
|
// do notify |
371
|
|
|
obs.notify(info); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
// return new time point |
376
|
|
|
return tp; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* |
381
|
|
|
* @param lb |
382
|
|
|
* @param ub |
383
|
|
|
* @return |
384
|
|
|
* @throws InconsistentTpValueException |
385
|
|
|
* @throws InconsistentDistanceConstraintException |
386
|
|
|
*/ |
387
|
|
View Code Duplication |
public final TimePoint addTimePoint(long lb, long ub) |
|
|
|
|
388
|
|
|
throws InconsistentTpValueException, InconsistentDistanceConstraintException |
389
|
|
|
{ |
390
|
|
|
// check domain |
391
|
|
|
if (lb > ub || lb < this.origin || ub > this.horizon) { |
392
|
|
|
// inconsistent value |
393
|
|
|
throw new InconsistentTpValueException("TimePoint (lb= " + lb +", ub= " + ub + ") is inconsistent or it is out of the domain [origin= " + this.origin + ", horizon= " + this.horizon); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
// create a time point or use a previous created one |
397
|
|
|
TimePoint tp = null; |
398
|
|
|
// extract the first element |
399
|
|
|
Iterator<TimePoint> it = this.recyclePoints.iterator(); |
400
|
|
|
// check recycled time points first |
401
|
|
|
if (it.hasNext()) { |
402
|
|
|
// extract next time point |
403
|
|
|
tp = it.next(); |
404
|
|
|
// set desired bounds |
405
|
|
|
tp.clear(lb, ub); |
406
|
|
|
// remove time point from the set |
407
|
|
|
it.remove(); |
408
|
|
|
} |
409
|
|
|
else { |
410
|
|
|
|
411
|
|
|
// create time point |
412
|
|
|
tp = new TimePoint(tpCounter.getAndIncrement(), lb, ub); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// add time point to the network |
416
|
|
|
this.doAddTimePoint(tp); |
417
|
|
|
// create notification |
418
|
|
|
AddTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory.createNotification(TemporalNetworkNotificationTypes.ADD_TP); |
419
|
|
|
info.addTimePoint(tp); |
420
|
|
|
|
421
|
|
|
// check observers |
422
|
|
|
synchronized (this.observers) { |
423
|
|
|
// notify observers |
424
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
425
|
|
|
obs.notify(info); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
// return new time point |
430
|
|
|
return tp; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* |
435
|
|
|
* @param tp |
436
|
|
|
* @throws InconsistentDistanceConstraintException |
437
|
|
|
*/ |
438
|
|
|
protected abstract void doAddTimePoint(TimePoint tp) |
439
|
|
|
throws InconsistentDistanceConstraintException; |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Removes a Time Point from the network |
443
|
|
|
* |
444
|
|
|
* @param tp |
445
|
|
|
*/ |
446
|
|
|
public final void removeTimePoint(TimePoint tp) { |
447
|
|
|
|
448
|
|
|
// delete time point |
449
|
|
|
this.doRemoveTimePoint(tp); |
450
|
|
|
// add time point to recycle list |
451
|
|
|
this.recyclePoints.add(tp); |
452
|
|
|
|
453
|
|
|
// create notification |
454
|
|
|
DelTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory |
455
|
|
|
.createNotification(TemporalNetworkNotificationTypes.DEL_TP); |
456
|
|
|
info.addTimePoint(tp); |
457
|
|
|
|
458
|
|
|
// check observers |
459
|
|
|
synchronized (this.observers) { |
460
|
|
|
|
461
|
|
|
// notify observers |
462
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
463
|
|
|
// do notify |
464
|
|
|
obs.notify(info); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* |
471
|
|
|
* Remove a list of time points from the temporal network |
472
|
|
|
* |
473
|
|
|
* @param tps |
474
|
|
|
*/ |
475
|
|
|
public final synchronized void removeTimePoints(List<TimePoint> tps) { |
476
|
|
|
|
477
|
|
|
// create notification |
478
|
|
|
DelTimePointTemporalNetworkNotification info = TemporalNetworkNotificationFactory |
479
|
|
|
.createNotification(TemporalNetworkNotificationTypes.DEL_TP); |
480
|
|
|
|
481
|
|
|
// check time points to remove |
482
|
|
|
for (TimePoint tp : tps) { |
483
|
|
|
|
484
|
|
|
// delete time point |
485
|
|
|
this.doRemoveTimePoint(tp); |
486
|
|
|
// add time point to recycle list |
487
|
|
|
this.recyclePoints.add(tp); |
488
|
|
|
// add deleted time point to the notification |
489
|
|
|
info.addTimePoint(tp); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// check observers |
493
|
|
|
synchronized (this.observers) { |
494
|
|
|
// notify observers |
495
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
496
|
|
|
// do notify |
497
|
|
|
obs.notify(info); |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Actually delete the selected time point from the network |
504
|
|
|
* and all related constraints |
505
|
|
|
* |
506
|
|
|
* @param tp |
507
|
|
|
* @return |
508
|
|
|
*/ |
509
|
|
|
protected abstract List<TimePointDistanceConstraint> doRemoveTimePoint(TimePoint tp); |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* |
513
|
|
|
* @param originTimePoint |
514
|
|
|
* @param tp1 |
515
|
|
|
* @return |
516
|
|
|
*/ |
517
|
|
|
public abstract long[] getConstraintBounds(TimePoint originTimePoint, TimePoint tp1); |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Creates a distance constraint between two time points with the |
521
|
|
|
* specified lower and upper bounds |
522
|
|
|
* |
523
|
|
|
* Throws an exception when trying to create a distance constraint with |
524
|
|
|
* lower bound greater than upper bound (lb > ub) |
525
|
|
|
* |
526
|
|
|
* @param constraint |
527
|
|
|
* @return |
528
|
|
|
* @throws InconsistentTpDistanceRelation |
529
|
|
|
*/ |
530
|
|
|
public final void addDistanceConstraint(TimePointDistanceConstraint constraint) |
531
|
|
|
throws InconsistentDistanceConstraintException { |
532
|
|
|
|
533
|
|
|
// check consistency of distance bound |
534
|
|
|
if (constraint.getDistanceLowerBound() > constraint.getDistanceUpperBound()) { |
535
|
|
|
// inconsistent value |
536
|
|
|
throw new InconsistentDistanceConstraintException("DistanceConstraint " |
537
|
|
|
+ "(dmin= " + constraint.getDistanceLowerBound() +", " |
538
|
|
|
+ "dmax= " + constraint.getDistanceUpperBound() + ") " |
539
|
|
|
+ "is inconsistent or it is not compatible wih the domain [origin= " + this.origin + ", horizon= " + this.horizon + "]"); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
// add constraint to the network and check changes |
543
|
|
|
if (this.doAddConstraint(constraint)) { |
544
|
|
|
|
545
|
|
|
// create notification |
546
|
|
|
AddRelationTemporalNetworkNotification info = TemporalNetworkNotificationFactory |
547
|
|
|
.createNotification(TemporalNetworkNotificationTypes.ADD_REL); |
548
|
|
|
// set constraint |
549
|
|
|
info.addRelation(constraint); |
550
|
|
|
|
551
|
|
|
// check observers |
552
|
|
|
synchronized (this.observers) { |
553
|
|
|
// notify observers |
554
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
555
|
|
|
// do notify |
556
|
|
|
obs.notify(info); |
557
|
|
|
} |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Create N distance constraints all at once. |
564
|
|
|
* |
565
|
|
|
* The method is atomic, i.e. if a constraint creation operation fails than all the |
566
|
|
|
* constraints previously created by the method are aborted. So if something goes wrong |
567
|
|
|
* during the creation of constraints then the network is restored to the status before |
568
|
|
|
* method invocation. |
569
|
|
|
* |
570
|
|
|
* @param constraints |
571
|
|
|
* @return |
572
|
|
|
* @throws TemporalNetworkTransactionFailureException |
573
|
|
|
*/ |
574
|
|
|
public final synchronized void addDistanceConstraint(TimePointDistanceConstraint[] constraints) |
575
|
|
|
throws TemporalNetworkTransactionFailureException |
576
|
|
|
{ |
577
|
|
|
// flag of successful operation and notification notification |
578
|
|
|
boolean notify = false; |
579
|
|
|
// create notification |
580
|
|
|
AddRelationTemporalNetworkNotification info = TemporalNetworkNotificationFactory. |
581
|
|
|
createNotification(TemporalNetworkNotificationTypes.ADD_REL); |
582
|
|
|
|
583
|
|
|
// list of committed distance constraints |
584
|
|
|
List<TimePointDistanceConstraint> committed = new ArrayList<>(); |
585
|
|
|
try { |
586
|
|
|
|
587
|
|
|
// propagate constraints |
588
|
|
|
for (int i = 0; i < constraints.length; i++) { |
589
|
|
|
|
590
|
|
|
// get constraint |
591
|
|
|
TimePointDistanceConstraint constraint = constraints[i]; |
592
|
|
|
// add constraint to network |
593
|
|
|
boolean changed = this.doAddConstraint(constraint); |
594
|
|
|
// add committed constraint |
595
|
|
|
committed.add(constraint); |
596
|
|
|
// add constraint to notification |
597
|
|
|
info.addRelation(constraint); |
598
|
|
|
// check if something changes |
599
|
|
|
if (changed) { |
600
|
|
|
// set notify flag |
601
|
|
|
notify = true; |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
catch (InconsistentDistanceConstraintException ex ) { |
606
|
|
|
|
607
|
|
|
// remove all committed constraints |
608
|
|
|
for (TimePointDistanceConstraint tpd : committed) { |
609
|
|
|
// remove added constraint |
610
|
|
|
this.doRemoveDistanceConstraint(tpd); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
// do not send notification |
614
|
|
|
notify = false; |
615
|
|
|
// throw exception |
616
|
|
|
throw new TemporalNetworkTransactionFailureException("Error while creating temporal constraints - Rollback Transaction"); |
617
|
|
|
} |
618
|
|
|
finally { |
619
|
|
|
|
620
|
|
|
// check if transaction has been successfully done and a notification should be sent |
621
|
|
|
if (notify) { |
622
|
|
|
|
623
|
|
|
// check observers |
624
|
|
|
synchronized (this.observers) { |
625
|
|
|
|
626
|
|
|
// notify observers |
627
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
628
|
|
|
// do notify |
629
|
|
|
obs.notify(info); |
630
|
|
|
} |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* |
638
|
|
|
* @param rel |
639
|
|
|
* @throws InconsistentDistanceConstraintException |
640
|
|
|
*/ |
641
|
|
|
protected abstract boolean doAddConstraint(TimePointDistanceConstraint rel) |
642
|
|
|
throws InconsistentDistanceConstraintException; |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Removes the distance constraint from the network |
646
|
|
|
* |
647
|
|
|
* @param rel |
648
|
|
|
*/ |
649
|
|
|
public final void removeConstraint(TimePointDistanceConstraint rel) |
650
|
|
|
{ |
651
|
|
|
// remove temporal relation and check if a notification should be sent |
652
|
|
|
if (this.doRemoveDistanceConstraint(rel)) { |
653
|
|
|
|
654
|
|
|
// create notification |
655
|
|
|
DelRelationTemporalNetworkNotification info = TemporalNetworkNotificationFactory.createNotification(TemporalNetworkNotificationTypes.DEL_REL); |
656
|
|
|
info.addRelation(rel); |
657
|
|
|
|
658
|
|
|
// check observers |
659
|
|
|
synchronized (this.observers) { |
660
|
|
|
// notify observers |
661
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
662
|
|
|
// do notify |
663
|
|
|
obs.notify(info); |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Removes N distance constraints all at once |
671
|
|
|
* |
672
|
|
|
* @param rels |
673
|
|
|
*/ |
674
|
|
|
public final void removeDistanceConstraint(List<TimePointDistanceConstraint> rels) { |
675
|
|
|
|
676
|
|
|
// notification flag |
677
|
|
|
boolean notify = false; |
678
|
|
|
// create notification |
679
|
|
|
DelRelationTemporalNetworkNotification info = TemporalNetworkNotificationFactory |
680
|
|
|
.createNotification(TemporalNetworkNotificationTypes.DEL_REL); |
681
|
|
|
|
682
|
|
|
// get constraints to delete |
683
|
|
|
for (TimePointDistanceConstraint rel : rels) { |
684
|
|
|
// remove temporal relation |
685
|
|
|
if (this.doRemoveDistanceConstraint(rel)) { |
686
|
|
|
// set notification flag |
687
|
|
|
notify = true; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
// add constraint to notify |
691
|
|
|
info.addRelation(rel); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
// check if a notification is necessary |
695
|
|
|
if (notify) { |
696
|
|
|
|
697
|
|
|
// check observers |
698
|
|
|
synchronized (this.observers) { |
699
|
|
|
|
700
|
|
|
// notify observers |
701
|
|
|
for (TemporalNetworkObserver obs : this.observers) { |
702
|
|
|
// do notify |
703
|
|
|
obs.notify(info); |
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Actually remove constraint from network |
711
|
|
|
* |
712
|
|
|
* @param rel |
713
|
|
|
* @return |
714
|
|
|
*/ |
715
|
|
|
protected abstract boolean doRemoveDistanceConstraint(TimePointDistanceConstraint rel); |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* |
719
|
|
|
* @param lb |
720
|
|
|
* @param ub |
721
|
|
|
* @return |
722
|
|
|
*/ |
723
|
|
|
protected TimePoint createTimePoint(long lb, long ub) { |
724
|
|
|
return new TimePoint(tpCounter.getAndIncrement(), lb, ub); |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* |
729
|
|
|
* @param from |
730
|
|
|
* @param to |
731
|
|
|
* @param distance |
732
|
|
|
* @param controllable |
733
|
|
|
* @return |
734
|
|
|
*/ |
735
|
|
|
protected final TimePointDistanceConstraint createDistanceConstraint(TimePoint from, TimePoint to, long[] distance, boolean controllable) { |
736
|
|
|
|
737
|
|
|
// create constraint |
738
|
|
|
TimePointDistanceConstraint c = new TimePointDistanceConstraint(); |
739
|
|
|
c.setReference(from); |
740
|
|
|
c.setTarget(to); |
741
|
|
|
c.setDistanceLowerBound(distance[0]); |
742
|
|
|
c.setDistanceUpperBound(distance[1]); |
743
|
|
|
c.setControllable(controllable); |
744
|
|
|
// get constraint |
745
|
|
|
return c; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* |
750
|
|
|
*/ |
751
|
|
|
public abstract void printDiagnosticData(); |
752
|
|
|
|
753
|
|
|
} |