aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ActivationCodesConflictSet.java
blob: d3990c23591c18d5be6f14452c1d9bcef87fdd83 (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
/*******************************************************************************
 * Copyright (c) 2010-2017, Andras Szabolcs Nagy and Daniel Varro
 * 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 org.eclipse.viatra.dse.base;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.viatra.dse.statecode.IStateCoder;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.viatra.transformation.evm.api.Activation;
import org.eclipse.viatra.transformation.evm.api.resolver.ChangeableConflictSet;
import org.eclipse.viatra.transformation.evm.api.resolver.ConflictResolver;
import org.eclipse.viatra.transformation.evm.api.resolver.ConflictSet;

public class ActivationCodesConflictSet implements ChangeableConflictSet {

    private static class ActivationCodesMultiBiMap {
        public Map<Activation<?>, Object> activationsToCodes = new HashMap<>();
        public Map<Object, Set<Activation<?>>> codesToActivations = new HashMap<>();

        public void addActivation(Activation<?> activation, Object activationCode) {
            activationsToCodes.put(activation, activationCode);
            codesToActivations.computeIfAbsent(activationCode, k -> new HashSet<>()).add(activation);
        }

        public void removeActivaion(Activation<?> activation) {
            Object activationCode = activationsToCodes.remove(activation);
            Set<Activation<?>> activations = codesToActivations.get(activationCode);
            if (activations != null) {
                activations.remove(activation);
            }
        }

        public void clear() {
            activationsToCodes.clear();
            codesToActivations.clear();
        }
    }
    
    protected ActivationCodesMultiBiMap activationCodes;
    protected IStateCoder stateCoder;

    protected Set<Activation<?>> newActivations = new HashSet<>();
    protected Set<Activation<?>> removedActivations = new HashSet<>();
//    private Logger logger = Logger.getLogger(getClass());

    private boolean isIncremental = false;
    private ConflictSet nextActivationsConflictSet;

    public void setIncremental(boolean isIncremental) {
        this.isIncremental = isIncremental;
    }

    public ActivationCodesConflictSet(ConflictSet nextActivationsConflictSet, IStateCoder stateCoder) {
        Objects.requireNonNull(nextActivationsConflictSet);
        this.nextActivationsConflictSet = nextActivationsConflictSet;
        this.stateCoder = stateCoder;
        activationCodes = new ActivationCodesMultiBiMap();
    }

    private Object createActivationCode(Activation<?> activation) {
        return stateCoder.createActivationCode((IPatternMatch) activation.getAtom());
    }

    @Override
    public boolean removeActivation(Activation<?> activation) {
        if (isIncremental) {
//*
            removedActivations.add(activation);
            newActivations.remove(activation);
/*/
            if(!removedActivations.add(activation)) {
                logger.debug("Abnormal: already marked to remove: " + activation);
            } else {
                logger.debug("marked to remove: " + activation);
            }
            if(newActivations.remove(activation)) {
                logger.debug("Abnormal: removed from new activations: " + activation);
            }
//*/
        }
        return false;
    }

    @Override
    public boolean addActivation(Activation<?> activation) {
        if (isIncremental) {
//*
            newActivations.add(activation);
            removedActivations.remove(activation);
            /*/
            if (activation.isEnabled()) {
                if (!newActivations.add(activation)) {
                    logger.debug("Abnormal: already added as new: " + activation);
                } else {
                    logger.debug("activation added: " + activation);
                }
            }
            if(removedActivations.remove(activation)) {
                logger.debug("Abnormal: was already marked to remove: " + activation);
            }
//*/
        }
        return false;
    }

    public Object getActivationId(Activation<?> activation) {
        return activationCodes.activationsToCodes.get(activation);
    }

    public Activation<?> getActivation(Object activationId) {
        Set<Activation<?>> activationsSet = activationCodes.codesToActivations.get(activationId);
        if (activationsSet == null || activationsSet.isEmpty()) {
            return null;
        } else {
            return activationsSet.iterator().next();
        }
    }

    public void updateActivationCodes() {
//        logger.debug("Updating activation codes.");

        if (isIncremental) {
          for (Activation<?> activation : removedActivations) {
              activationCodes.removeActivaion(activation);
//              logger.debug("removed activation: " + activationId);
          }
    
          for (Activation<?> activation : newActivations) {
              if (activation.getState().isInactive()) {
                  continue;
              }
              Object activationCode = createActivationCode(activation);
              activationCodes.addActivation(activation, activationCode);
//              logger.debug("new activation: " + activationId);
//              Activation<?> similarActivation = activationIds.inverse().get(activationId);
//              if (similarActivation != null) {
//                  logger.debug("Activation " + toStringAct(activation) + " is already present with id: " + activationId);
//                  if (similarActivation.isEnabled()) {
//                      logger.warn("Duplicate activation code: " + activationId);
//                  } else {
//                      logger.debug("Force put: " + activationId);
//                  }
//                  continue;
//              }
//              activationIds.put(activation, activationId);
          }
          removedActivations.clear();
          newActivations.clear();
        } else {
            activationCodes.clear();
            for (Activation<?> activation : nextActivationsConflictSet.getNextActivations()) {
                Object activationCode = createActivationCode(activation);
                activationCodes.addActivation(activation, activationCode);
            }
        }


    }

    protected void reinitWithActivations(ConflictSet nextActivationsConflictSet) {
        this.nextActivationsConflictSet = nextActivationsConflictSet;
        activationCodes.clear();
        for (Activation<?> activation : nextActivationsConflictSet.getNextActivations()) {
            Object activationCode = createActivationCode(activation);
            activationCodes.addActivation(activation, activationCode);
        }
    }

    @Override
    public Activation<?> getNextActivation() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<Activation<?>> getNextActivations() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<Activation<?>> getConflictingActivations() {
        throw new UnsupportedOperationException();
    }

    @Override
    public ConflictResolver getConflictResolver() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Object activationCode : activationCodes.activationsToCodes.values()) {
            sb.append(activationCode);
            sb.append(" | ");
        }
        return sb.toString();
    }

    public Collection<Object> getCurrentActivationCodes() {
        return activationCodes.activationsToCodes.values();
    }
}