diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-30 18:03:48 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-30 18:03:48 +0200 |
commit | e11bce7ad3e803e80883499fec0ad6e4540ffe43 (patch) | |
tree | ca3ad9d5b9137e9455485e43350a4a353f487f22 /Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java | |
parent | Disable unrepairable match scoping for now (diff) | |
download | VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.tar.gz VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.tar.zst VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.zip |
Add modified VIATRA-DSE version
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java new file mode 100644 index 00000000..d3990c23 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java | |||
@@ -0,0 +1,213 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, Andras Szabolcs Nagy and Daniel Varro | ||
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 org.eclipse.viatra.dse.base; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.HashMap; | ||
13 | import java.util.HashSet; | ||
14 | import java.util.Map; | ||
15 | import java.util.Objects; | ||
16 | import java.util.Set; | ||
17 | |||
18 | import org.eclipse.viatra.dse.statecode.IStateCoder; | ||
19 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | ||
20 | import org.eclipse.viatra.transformation.evm.api.Activation; | ||
21 | import org.eclipse.viatra.transformation.evm.api.resolver.ChangeableConflictSet; | ||
22 | import org.eclipse.viatra.transformation.evm.api.resolver.ConflictResolver; | ||
23 | import org.eclipse.viatra.transformation.evm.api.resolver.ConflictSet; | ||
24 | |||
25 | public class ActivationCodesConflictSet implements ChangeableConflictSet { | ||
26 | |||
27 | private static class ActivationCodesMultiBiMap { | ||
28 | public Map<Activation<?>, Object> activationsToCodes = new HashMap<>(); | ||
29 | public Map<Object, Set<Activation<?>>> codesToActivations = new HashMap<>(); | ||
30 | |||
31 | public void addActivation(Activation<?> activation, Object activationCode) { | ||
32 | activationsToCodes.put(activation, activationCode); | ||
33 | codesToActivations.computeIfAbsent(activationCode, k -> new HashSet<>()).add(activation); | ||
34 | } | ||
35 | |||
36 | public void removeActivaion(Activation<?> activation) { | ||
37 | Object activationCode = activationsToCodes.remove(activation); | ||
38 | Set<Activation<?>> activations = codesToActivations.get(activationCode); | ||
39 | if (activations != null) { | ||
40 | activations.remove(activation); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | public void clear() { | ||
45 | activationsToCodes.clear(); | ||
46 | codesToActivations.clear(); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | protected ActivationCodesMultiBiMap activationCodes; | ||
51 | protected IStateCoder stateCoder; | ||
52 | |||
53 | protected Set<Activation<?>> newActivations = new HashSet<>(); | ||
54 | protected Set<Activation<?>> removedActivations = new HashSet<>(); | ||
55 | // private Logger logger = Logger.getLogger(getClass()); | ||
56 | |||
57 | private boolean isIncremental = false; | ||
58 | private ConflictSet nextActivationsConflictSet; | ||
59 | |||
60 | public void setIncremental(boolean isIncremental) { | ||
61 | this.isIncremental = isIncremental; | ||
62 | } | ||
63 | |||
64 | public ActivationCodesConflictSet(ConflictSet nextActivationsConflictSet, IStateCoder stateCoder) { | ||
65 | Objects.requireNonNull(nextActivationsConflictSet); | ||
66 | this.nextActivationsConflictSet = nextActivationsConflictSet; | ||
67 | this.stateCoder = stateCoder; | ||
68 | activationCodes = new ActivationCodesMultiBiMap(); | ||
69 | } | ||
70 | |||
71 | private Object createActivationCode(Activation<?> activation) { | ||
72 | return stateCoder.createActivationCode((IPatternMatch) activation.getAtom()); | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public boolean removeActivation(Activation<?> activation) { | ||
77 | if (isIncremental) { | ||
78 | //* | ||
79 | removedActivations.add(activation); | ||
80 | newActivations.remove(activation); | ||
81 | /*/ | ||
82 | if(!removedActivations.add(activation)) { | ||
83 | logger.debug("Abnormal: already marked to remove: " + activation); | ||
84 | } else { | ||
85 | logger.debug("marked to remove: " + activation); | ||
86 | } | ||
87 | if(newActivations.remove(activation)) { | ||
88 | logger.debug("Abnormal: removed from new activations: " + activation); | ||
89 | } | ||
90 | //*/ | ||
91 | } | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public boolean addActivation(Activation<?> activation) { | ||
97 | if (isIncremental) { | ||
98 | //* | ||
99 | newActivations.add(activation); | ||
100 | removedActivations.remove(activation); | ||
101 | /*/ | ||
102 | if (activation.isEnabled()) { | ||
103 | if (!newActivations.add(activation)) { | ||
104 | logger.debug("Abnormal: already added as new: " + activation); | ||
105 | } else { | ||
106 | logger.debug("activation added: " + activation); | ||
107 | } | ||
108 | } | ||
109 | if(removedActivations.remove(activation)) { | ||
110 | logger.debug("Abnormal: was already marked to remove: " + activation); | ||
111 | } | ||
112 | //*/ | ||
113 | } | ||
114 | return false; | ||
115 | } | ||
116 | |||
117 | public Object getActivationId(Activation<?> activation) { | ||
118 | return activationCodes.activationsToCodes.get(activation); | ||
119 | } | ||
120 | |||
121 | public Activation<?> getActivation(Object activationId) { | ||
122 | Set<Activation<?>> activationsSet = activationCodes.codesToActivations.get(activationId); | ||
123 | if (activationsSet == null || activationsSet.isEmpty()) { | ||
124 | return null; | ||
125 | } else { | ||
126 | return activationsSet.iterator().next(); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | public void updateActivationCodes() { | ||
131 | // logger.debug("Updating activation codes."); | ||
132 | |||
133 | if (isIncremental) { | ||
134 | for (Activation<?> activation : removedActivations) { | ||
135 | activationCodes.removeActivaion(activation); | ||
136 | // logger.debug("removed activation: " + activationId); | ||
137 | } | ||
138 | |||
139 | for (Activation<?> activation : newActivations) { | ||
140 | if (activation.getState().isInactive()) { | ||
141 | continue; | ||
142 | } | ||
143 | Object activationCode = createActivationCode(activation); | ||
144 | activationCodes.addActivation(activation, activationCode); | ||
145 | // logger.debug("new activation: " + activationId); | ||
146 | // Activation<?> similarActivation = activationIds.inverse().get(activationId); | ||
147 | // if (similarActivation != null) { | ||
148 | // logger.debug("Activation " + toStringAct(activation) + " is already present with id: " + activationId); | ||
149 | // if (similarActivation.isEnabled()) { | ||
150 | // logger.warn("Duplicate activation code: " + activationId); | ||
151 | // } else { | ||
152 | // logger.debug("Force put: " + activationId); | ||
153 | // } | ||
154 | // continue; | ||
155 | // } | ||
156 | // activationIds.put(activation, activationId); | ||
157 | } | ||
158 | removedActivations.clear(); | ||
159 | newActivations.clear(); | ||
160 | } else { | ||
161 | activationCodes.clear(); | ||
162 | for (Activation<?> activation : nextActivationsConflictSet.getNextActivations()) { | ||
163 | Object activationCode = createActivationCode(activation); | ||
164 | activationCodes.addActivation(activation, activationCode); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | |||
169 | } | ||
170 | |||
171 | protected void reinitWithActivations(ConflictSet nextActivationsConflictSet) { | ||
172 | this.nextActivationsConflictSet = nextActivationsConflictSet; | ||
173 | activationCodes.clear(); | ||
174 | for (Activation<?> activation : nextActivationsConflictSet.getNextActivations()) { | ||
175 | Object activationCode = createActivationCode(activation); | ||
176 | activationCodes.addActivation(activation, activationCode); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | @Override | ||
181 | public Activation<?> getNextActivation() { | ||
182 | throw new UnsupportedOperationException(); | ||
183 | } | ||
184 | |||
185 | @Override | ||
186 | public Set<Activation<?>> getNextActivations() { | ||
187 | throw new UnsupportedOperationException(); | ||
188 | } | ||
189 | |||
190 | @Override | ||
191 | public Set<Activation<?>> getConflictingActivations() { | ||
192 | throw new UnsupportedOperationException(); | ||
193 | } | ||
194 | |||
195 | @Override | ||
196 | public ConflictResolver getConflictResolver() { | ||
197 | throw new UnsupportedOperationException(); | ||
198 | } | ||
199 | |||
200 | @Override | ||
201 | public String toString() { | ||
202 | StringBuilder sb = new StringBuilder(); | ||
203 | for (Object activationCode : activationCodes.activationsToCodes.values()) { | ||
204 | sb.append(activationCode); | ||
205 | sb.append(" | "); | ||
206 | } | ||
207 | return sb.toString(); | ||
208 | } | ||
209 | |||
210 | public Collection<Object> getCurrentActivationCodes() { | ||
211 | return activationCodes.activationsToCodes.values(); | ||
212 | } | ||
213 | } | ||