aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyMaskedMemory.java
blob: 45ce3a4eb04e6584242a43f47cf3d96e30523605 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs Ltd.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.matchers.memories.timely;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;

import tools.refinery.viatra.runtime.matchers.memories.MaskedTupleMemory;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.matchers.util.Signed;
import tools.refinery.viatra.runtime.matchers.util.TimelyMemory;
import tools.refinery.viatra.runtime.matchers.util.timeline.Diff;
import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline;

/**
 * Common parts of timely default and timely unary implementations.
 * 
 * @noextend This class is not intended to be subclassed by clients.
 * @author Tamas Szabo
 * @since 2.3
 */
abstract class AbstractTimelyMaskedMemory<Timestamp extends Comparable<Timestamp>, KeyType>
        extends MaskedTupleMemory<Timestamp> {

    protected final TreeMap<Timestamp, Set<KeyType>> foldingStates;
    protected final Map<KeyType, TimelyMemory<Timestamp>> memoryMap;
    protected final boolean isLazy;

    public AbstractTimelyMaskedMemory(final TupleMask mask, final Object owner, final boolean isLazy) {
        super(mask, owner);
        this.isLazy = isLazy;
        this.memoryMap = CollectionsFactory.createMap();
        this.foldingStates = this.isLazy ? CollectionsFactory.createTreeMap() : null;
    }

    @Override
    public void initializeWith(final MaskedTupleMemory<Timestamp> other, final Timestamp defaultValue) {
        final Iterable<Tuple> signatures = other.getSignatures();
        for (final Tuple signature : signatures) {
            if (other.isTimely()) {
                final Map<Tuple, Timeline<Timestamp>> tupleMap = other.getWithTimeline(signature);
                for (final Entry<Tuple, Timeline<Timestamp>> entry : tupleMap.entrySet()) {
                    for (final Signed<Timestamp> signed : entry.getValue().asChangeSequence()) {
                        if (signed.getDirection() == Direction.DELETE) {
                            this.removeWithTimestamp(entry.getKey(), signed.getPayload());
                        } else {
                            this.addWithTimestamp(entry.getKey(), signed.getPayload());
                        }
                    }
                }
            } else {
                final Collection<Tuple> tuples = other.get(signature);
                for (final Tuple tuple : tuples) {
                    this.addWithTimestamp(tuple, defaultValue);
                }
            }
        }
    }

    public boolean isPresentAtInfinityInteral(KeyType key) {
        final TimelyMemory<Timestamp> values = this.memoryMap.get(key);
        if (values == null) {
            return false;
        } else {
            return values.getCountAtInfinity() != 0;
        }
    }

    @Override
    public void clear() {
        this.memoryMap.clear();
    }

    @Override
    public int getKeysetSize() {
        return this.memoryMap.keySet().size();
    }

    @Override
    public int getTotalSize() {
        int i = 0;
        for (final Entry<KeyType, TimelyMemory<Timestamp>> entry : this.memoryMap.entrySet()) {
            i += entry.getValue().size();
        }
        return i;
    }

    @Override
    public Iterator<Tuple> iterator() {
        return this.memoryMap.values().stream().flatMap(e -> e.keySet().stream()).iterator();
    }

    protected Collection<Tuple> getInternal(final KeyType key) {
        final TimelyMemory<Timestamp> memory = this.memoryMap.get(key);
        if (memory == null) {
            return null;
        } else {
            return memory.getTuplesAtInfinity();
        }
    }

    public Map<Tuple, Timeline<Timestamp>> getWithTimestampInternal(final KeyType key) {
        final TimelyMemory<Timestamp> memory = this.memoryMap.get(key);
        if (memory == null) {
            return null;
        } else {
            return memory.asMap();
        }
    }

    protected Diff<Timestamp> removeInternal(final KeyType key, final Tuple tuple, final Timestamp timestamp) {
        Timestamp oldResumableTimestamp = null;
        Timestamp newResumableTimestamp = null;
        
        final TimelyMemory<Timestamp> keyMemory = this.memoryMap.get(key);
        if (keyMemory == null) {
            throw raiseDuplicateDeletion(tuple);
        }
        
        if (this.isLazy) {            
            oldResumableTimestamp = keyMemory.getResumableTimestamp();
        }
        
        Diff<Timestamp> diff = null;
        try {
            diff = keyMemory.remove(tuple, timestamp);
        } catch (final IllegalStateException e) {
            throw raiseDuplicateDeletion(tuple);
        }
        if (keyMemory.isEmpty()) {
            this.memoryMap.remove(key);
        }
        
        if (this.isLazy) {            
            newResumableTimestamp = keyMemory.getResumableTimestamp();
            if (!Objects.equals(oldResumableTimestamp, newResumableTimestamp)) {
                unregisterFoldingState(oldResumableTimestamp, key);
                registerFoldingState(newResumableTimestamp, key);
            }
        }
        
        return diff;
    }

    protected Diff<Timestamp> addInternal(final KeyType key, final Tuple tuple, final Timestamp timestamp) {
        Timestamp oldResumableTimestamp = null;
        Timestamp newResumableTimestamp = null;
                
        final TimelyMemory<Timestamp> keyMemory = this.memoryMap.computeIfAbsent(key,
                k -> new TimelyMemory<Timestamp>(this.isLazy));
        
        if (this.isLazy) {            
            oldResumableTimestamp = keyMemory.getResumableTimestamp();
        }
        
        final Diff<Timestamp> diff = keyMemory.put(tuple, timestamp);
        
        if (this.isLazy) {            
            newResumableTimestamp = keyMemory.getResumableTimestamp();
            if (!Objects.equals(oldResumableTimestamp, newResumableTimestamp)) {
                unregisterFoldingState(oldResumableTimestamp, key);
                registerFoldingState(newResumableTimestamp, key);
            }
        }
        
        return diff;
    }
    
    @Override
    public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Timestamp timestamp) {
        return removeWithTimestamp(tuple, null, timestamp);
    }

    @Override
    public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Timestamp timestamp) {
        return addWithTimestamp(tuple, null, timestamp);
    }

    @Override
    public boolean isTimely() {
        return true;
    }
    
    protected void registerFoldingState(final Timestamp timestamp, final KeyType key) {
        if (timestamp != null) {
            this.foldingStates.compute(timestamp, (k, v) -> {
                if (v == null) {
                    v = CollectionsFactory.createSet();
                }
                v.add(key);
                return v;
            });
        }
    }
    
    protected void unregisterFoldingState(final Timestamp timestamp, final KeyType key) {
        if (timestamp != null) {
            this.foldingStates.compute(timestamp, (k, v) -> {
                v.remove(key);
                return v.isEmpty() ? null : v;
            });
        }
    }
    
    @Override
    public Timestamp getResumableTimestamp() {
        if (this.foldingStates == null || this.foldingStates.isEmpty()) {
            return null;
        } else {
            return this.foldingStates.firstKey();
        }
    }

}