summaryrefslogtreecommitdiff
path: root/src/main/java/events/implementations/VSTimestampTriggeredEvent.java
blob: c5e5386a2587da50f5c2cd61fb50da37bc3ad4e5 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package events.implementations;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import core.VSInternalProcess;
import core.time.VSLamportTime;
import core.time.VSVectorTime;
import events.VSAbstractEvent;
import events.VSCopyableEvent;
import serialize.VSSerialize;

/**
 * Abstract base class for timestamp-triggered events that fire when specific
 * Lamport or vector clock conditions are met.
 * 
 * <p>This class provides the foundation for creating events that trigger based on
 * timestamp conditions. Subclasses can define events that fire when:</p>
 * <ul>
 *   <li>Lamport time reaches a specific value</li>
 *   <li>Vector clock matches certain conditions</li>
 *   <li>Custom timestamp comparisons are satisfied</li>
 * </ul>
 * 
 * <p>Events can use various comparison operators (equal, greater than, less than, etc.)
 * and will only trigger once when their condition is first met.</p>
 * 
 * @see VSLamportTimestampEvent
 * @see VSVectorTimestampEvent
 * @see VSTimestampMonitorEvent
 * @author Paul C. Buetow
 */
public abstract class VSTimestampTriggeredEvent extends VSAbstractEvent implements VSCopyableEvent {
    
    public enum TimestampType {
        LAMPORT,
        VECTOR
    }
    
    public enum ComparisonOperator {
        EQUAL,
        GREATER_THAN,
        LESS_THAN,
        GREATER_EQUAL,
        LESS_EQUAL
    }
    
    protected TimestampType timestampType;
    protected ComparisonOperator operator;
    protected boolean hasTriggered;
    
    protected long targetLamportTime;
    protected VSVectorTime targetVectorTime;
    
    /**
     * Constructor for Lamport timestamp events
     */
    public VSTimestampTriggeredEvent(long targetLamport, ComparisonOperator op) {
        this.timestampType = TimestampType.LAMPORT;
        this.targetLamportTime = targetLamport;
        this.operator = op;
        this.hasTriggered = false;
    }
    
    /**
     * Constructor for Vector timestamp events
     */
    public VSTimestampTriggeredEvent(VSVectorTime targetVector, ComparisonOperator op) {
        this.timestampType = TimestampType.VECTOR;
        this.targetVectorTime = targetVector.getCopy();
        this.operator = op;
        this.hasTriggered = false;
    }
    
    /**
     * Default constructor for serialization
     */
    public VSTimestampTriggeredEvent() {
        this.hasTriggered = false;
    }
    
    @Override
    public void onInit() {
        setClassname(getClass().getName());
    }
    
    @Override
    public void onStart() {
        if (hasTriggered) {
            return;
        }
        
        VSInternalProcess internalProcess = (VSInternalProcess) process;
        boolean conditionMet = false;
        
        if (timestampType == TimestampType.LAMPORT) {
            conditionMet = checkLamportCondition(internalProcess);
        } else if (timestampType == TimestampType.VECTOR) {
            conditionMet = checkVectorCondition(internalProcess);
        }
        
        if (conditionMet) {
            hasTriggered = true;
            onTimestampReached();
        }
    }
    
    /**
     * Check timestamp condition without triggering the event.
     * Used by monitoring systems to test conditions.
     */
    public boolean checkCondition(VSInternalProcess process) {
        if (hasTriggered) {
            return false;
        }
        
        if (timestampType == TimestampType.LAMPORT) {
            return checkLamportCondition(process);
        } else if (timestampType == TimestampType.VECTOR) {
            return checkVectorCondition(process);
        }
        
        return false;
    }
    
    /**
     * Check if Lamport timestamp condition is met
     */
    protected boolean checkLamportCondition(VSInternalProcess process) {
        long currentLamport = process.getLamportTime();
        
        switch (operator) {
            case EQUAL:
                return currentLamport == targetLamportTime;
            case GREATER_THAN:
                return currentLamport > targetLamportTime;
            case LESS_THAN:
                return currentLamport < targetLamportTime;
            case GREATER_EQUAL:
                return currentLamport >= targetLamportTime;
            case LESS_EQUAL:
                return currentLamport <= targetLamportTime;
            default:
                return false;
        }
    }
    
    /**
     * Check if Vector timestamp condition is met
     */
    protected boolean checkVectorCondition(VSInternalProcess process) {
        VSVectorTime currentVector = process.getVectorTime();
        
        if (currentVector == null || targetVectorTime == null) {
            return false;
        }
        
        switch (operator) {
            case EQUAL:
                return vectorTimesEqual(currentVector, targetVectorTime);
            case GREATER_THAN:
                return vectorTimeGreater(currentVector, targetVectorTime, false);
            case LESS_THAN:
                return vectorTimeGreater(targetVectorTime, currentVector, false);
            case GREATER_EQUAL:
                return vectorTimeGreater(currentVector, targetVectorTime, true);
            case LESS_EQUAL:
                return vectorTimeGreater(targetVectorTime, currentVector, true);
            default:
                return false;
        }
    }
    
    /**
     * Check if two vector times are equal
     */
    protected boolean vectorTimesEqual(VSVectorTime v1, VSVectorTime v2) {
        int maxSize = Math.max(v1.size(), v2.size());
        
        for (int i = 0; i < maxSize; i++) {
            long val1 = i < v1.size() ? v1.get(i) : 0;
            long val2 = i < v2.size() ? v2.get(i) : 0;
            
            if (val1 != val2) {
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * Check if v1 > v2 (or >= if allowEqual is true) using vector clock ordering
     */
    protected boolean vectorTimeGreater(VSVectorTime v1, VSVectorTime v2, boolean allowEqual) {
        int maxSize = Math.max(v1.size(), v2.size());
        boolean hasGreater = false;
        
        for (int i = 0; i < maxSize; i++) {
            long val1 = i < v1.size() ? v1.get(i) : 0;
            long val2 = i < v2.size() ? v2.get(i) : 0;
            
            if (val1 < val2) {
                return false;
            } else if (val1 > val2) {
                hasGreater = true;
            }
        }
        
        return hasGreater || (allowEqual && vectorTimesEqual(v1, v2));
    }
    
    /**
     * This method is called when the timestamp condition is met.
     * Subclasses should override this to define the actual behavior.
     */
    protected abstract void onTimestampReached();
    
    @Override
    protected String createShortname(String savedShortname) {
        return "TimestampTrigger";
    }
    
    @Override
    public void initCopy(VSAbstractEvent copy) {
        if (copy instanceof VSTimestampTriggeredEvent) {
            VSTimestampTriggeredEvent copyEvent = (VSTimestampTriggeredEvent) copy;
            copyEvent.timestampType = this.timestampType;
            copyEvent.operator = this.operator;
            copyEvent.targetLamportTime = this.targetLamportTime;
            copyEvent.hasTriggered = this.hasTriggered;
            
            if (this.targetVectorTime != null) {
                copyEvent.targetVectorTime = this.targetVectorTime.getCopy();
            }
        }
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(" [TimestampTrigger: ");
        
        if (timestampType == TimestampType.LAMPORT) {
            sb.append("Lamport ").append(operator).append(" ").append(targetLamportTime);
        } else {
            sb.append("Vector ").append(operator).append(" ").append(targetVectorTime);
        }
        
        if (hasTriggered) {
            sb.append(" (TRIGGERED)");
        }
        
        sb.append("]");
        return sb.toString();
    }
    
    // Getters and setters
    public TimestampType getTimestampType() {
        return timestampType;
    }
    
    public ComparisonOperator getOperator() {
        return operator;
    }
    
    public long getTargetLamportTime() {
        return targetLamportTime;
    }
    
    public VSVectorTime getTargetVectorTime() {
        return targetVectorTime != null ? targetVectorTime.getCopy() : null;
    }
    
    public boolean hasTriggered() {
        return hasTriggered;
    }
    
    public void reset() {
        hasTriggered = false;
    }
    
    @Override
    public synchronized void serialize(VSSerialize serialize,
                                     ObjectOutputStream objectOutputStream)
    throws IOException {
        super.serialize(serialize, objectOutputStream);
        
        if (VSSerialize.DEBUG)
            System.out.println("Serializing: VSTimestampTriggeredEvent; id="+getID());
        
        /** For later backwards compatibility, to add more stuff */
        objectOutputStream.writeObject(Boolean.valueOf(false));
        
        objectOutputStream.writeObject(timestampType);
        objectOutputStream.writeObject(operator);
        objectOutputStream.writeObject(Long.valueOf(targetLamportTime));
        objectOutputStream.writeObject(Boolean.valueOf(hasTriggered));
        
        // Serialize vector time if present
        boolean hasVectorTime = (targetVectorTime != null);
        objectOutputStream.writeObject(Boolean.valueOf(hasVectorTime));
        if (hasVectorTime) {
            objectOutputStream.writeObject(targetVectorTime);
        }
        
        /** For later backwards compatibility, to add more stuff */
        objectOutputStream.writeObject(Boolean.valueOf(false));
    }
    
    @Override
    public synchronized void deserialize(VSSerialize serialize,
                                       ObjectInputStream objectInputStream)
    throws IOException, ClassNotFoundException {
        super.deserialize(serialize, objectInputStream);
        
        if (VSSerialize.DEBUG)
            System.out.print("Deserializing: VSTimestampTriggeredEvent ");
        
        /** For later backwards compatibility, to add more stuff */
        objectInputStream.readObject();
        
        timestampType = (TimestampType) objectInputStream.readObject();
        operator = (ComparisonOperator) objectInputStream.readObject();
        targetLamportTime = ((Long) objectInputStream.readObject()).longValue();
        hasTriggered = ((Boolean) objectInputStream.readObject()).booleanValue();
        
        // Deserialize vector time if present
        boolean hasVectorTime = ((Boolean) objectInputStream.readObject()).booleanValue();
        if (hasVectorTime) {
            targetVectorTime = (VSVectorTime) objectInputStream.readObject();
        }
        
        /** For later backwards compatibility, to add more stuff */
        objectInputStream.readObject();
    }
}