diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/MappingTraceCollector.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/MappingTraceCollector.java | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/MappingTraceCollector.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/MappingTraceCollector.java new file mode 100644 index 00000000..7429fc60 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/MappingTraceCollector.java | |||
@@ -0,0 +1,135 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, Grill Balázs, IncQueryLabs | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.psystem.rewriters; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.HashMap; | ||
13 | import java.util.HashSet; | ||
14 | import java.util.LinkedList; | ||
15 | import java.util.Map; | ||
16 | import java.util.Queue; | ||
17 | import java.util.Set; | ||
18 | import java.util.function.Predicate; | ||
19 | import java.util.stream.Stream; | ||
20 | |||
21 | import tools.refinery.viatra.runtime.matchers.psystem.PTraceable; | ||
22 | import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.MemoryType; | ||
25 | import tools.refinery.viatra.runtime.matchers.util.IMemoryView; | ||
26 | import tools.refinery.viatra.runtime.matchers.util.IMultiLookup; | ||
27 | import tools.refinery.viatra.runtime.matchers.util.Preconditions; | ||
28 | |||
29 | /** | ||
30 | * Multimap-based implementation to contain and query traces | ||
31 | * | ||
32 | * @since 1.6 | ||
33 | * | ||
34 | */ | ||
35 | public class MappingTraceCollector implements IRewriterTraceCollector { | ||
36 | |||
37 | /** | ||
38 | * Traces from derivative to original | ||
39 | */ | ||
40 | private final IMultiLookup<PTraceable, PTraceable> traces = CollectionsFactory.createMultiLookup(Object.class, MemoryType.SETS, Object.class); | ||
41 | |||
42 | /** | ||
43 | * Traces from original to derivative | ||
44 | */ | ||
45 | private final IMultiLookup<PTraceable, PTraceable> inverseTraces = CollectionsFactory.createMultiLookup(Object.class, MemoryType.SETS, Object.class); | ||
46 | |||
47 | /** | ||
48 | * Reasons for removing {@link PTraceable}s | ||
49 | */ | ||
50 | private final Map<PTraceable, IDerivativeModificationReason> removals = new HashMap<>(); | ||
51 | |||
52 | /** | ||
53 | * Decides whether {@link PTraceable} is removed | ||
54 | */ | ||
55 | private final Predicate<PTraceable> removed = removals::containsKey; | ||
56 | |||
57 | /** | ||
58 | * @since 2.0 | ||
59 | */ | ||
60 | @Override | ||
61 | public Stream<PTraceable> getCanonicalTraceables(PTraceable derivative) { | ||
62 | return findTraceEnds(derivative, traces).stream(); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * @since 2.0 | ||
67 | */ | ||
68 | @Override | ||
69 | public Stream<PTraceable> getRewrittenTraceables(PTraceable source) { | ||
70 | return findTraceEnds(source, inverseTraces).stream(); | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Returns the end of trace chains starting from the given {@link PTraceable} along the given trace edges. | ||
75 | */ | ||
76 | private Set<PTraceable> findTraceEnds(PTraceable traceable, IMultiLookup<PTraceable, PTraceable> traceRecords) { | ||
77 | if (traceable instanceof PQuery) { // PQueries are preserved | ||
78 | return Collections.singleton(traceable); | ||
79 | } | ||
80 | Set<PTraceable> visited = new HashSet<>(); | ||
81 | Set<PTraceable> result = new HashSet<>(); | ||
82 | Queue<PTraceable> queue = new LinkedList<>(); | ||
83 | queue.add(traceable); | ||
84 | while(!queue.isEmpty()){ | ||
85 | PTraceable aDerivative = queue.poll(); | ||
86 | // Track visited elements to avoid infinite loop via directed cycles in traces | ||
87 | visited.add(aDerivative); | ||
88 | IMemoryView<PTraceable> nextOrigins = traceRecords.lookup(aDerivative); | ||
89 | if (nextOrigins == null){ | ||
90 | // End of trace chain | ||
91 | result.add(aDerivative); | ||
92 | } else { | ||
93 | // Follow traces | ||
94 | for(PTraceable nextOrigin : nextOrigins){ | ||
95 | if (!visited.contains(nextOrigin)){ | ||
96 | queue.add(nextOrigin); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | return result; | ||
102 | } | ||
103 | |||
104 | @Override | ||
105 | public void addTrace(PTraceable original, PTraceable derivative){ | ||
106 | traces.addPairOrNop(derivative, original); | ||
107 | inverseTraces.addPairOrNop(original, derivative); | ||
108 | // Even if this element was marked as removed earlier, now we replace it with another constraint! | ||
109 | removals.remove(original); | ||
110 | } | ||
111 | |||
112 | @Override | ||
113 | public void derivativeRemoved(PTraceable derivative, IDerivativeModificationReason reason){ | ||
114 | Preconditions.checkState(!removals.containsKey(derivative), "Traceable %s removed multiple times", derivative); | ||
115 | // XXX the derivative must not be removed from the trace chain, as some rewriters, e.g. the normalizer keeps trace links to deleted elements | ||
116 | if (!inverseTraces.lookupExists(derivative)) { | ||
117 | // If there already exists a trace link, this removal means an update | ||
118 | removals.put(derivative, reason); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | @Override | ||
123 | public boolean isRemoved(PTraceable traceable) { | ||
124 | return getRewrittenTraceables(traceable).allMatch(removed); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * @since 2.0 | ||
129 | */ | ||
130 | @Override | ||
131 | public Stream<IDerivativeModificationReason> getRemovalReasons(PTraceable traceable) { | ||
132 | return getRewrittenTraceables(traceable).filter(removed).map(removals::get); | ||
133 | } | ||
134 | |||
135 | } | ||