aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/basicdeferred/Inequality.java
blob: 5cac33dc4260c1ae4c9080aaa9f4688550a53b07 (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
/*******************************************************************************
 * Copyright (c) 2004-2010 Gabor Bergmann 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 tools.refinery.viatra.runtime.matchers.psystem.basicdeferred;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.psystem.VariableDeferredPConstraint;

/**
 * @author Gabor Bergmann
 * 
 */
public class Inequality extends VariableDeferredPConstraint {

    private PVariable who;
    private PVariable withWhom;

    /**
     * The inequality constraint is weak if it can be ignored when who is the same as withWhom, or if any if them is
     * undeducible.
     */
    private boolean weak;

    public Inequality(PBody pBody, PVariable who, PVariable withWhom) {
        this(pBody, who, withWhom, false);
    }

    public Inequality(PBody pBody, PVariable who, PVariable withWhom,
            boolean weak) {
        super(pBody, new HashSet<>(Arrays.asList(who, withWhom) ));
        this.who = who;
        this.withWhom = withWhom;
        this.weak = weak;
    }

    // private Inequality(
    // PSystem<PatternDescription, StubHandle, ?> pSystem,
    // PVariable subject, Set<PVariable> inequals)
    // {
    // super(pSystem, include(inequals, subject));
    // this.subject = subject;
    // this.inequals = inequals;
    // }

    // private static HashSet<PVariable> include(Set<PVariable> inequals, PVariable subject) {
    // HashSet<PVariable> hashSet = new HashSet<PVariable>(inequals);
    // hashSet.add(subject);
    // return hashSet;
    // }

    @Override
    public Set<PVariable> getDeferringVariables() {
        return getAffectedVariables();
    }

    // private static int[] mapIndices(Map<Object, Integer> variablesIndex, Set<PVariable> keys) {
    // int[] result = new int[keys.size()];
    // int k = 0;
    // for (PVariable key : keys) {
    // result[k++] = variablesIndex.get(key);
    // }
    // return result;
    // }

    // @Override
    // public IFoldablePConstraint getIncorporator() {
    // return incorporator;
    // }
    //
    // @Override
    // public void registerIncorporatationInto(IFoldablePConstraint incorporator) {
    // this.incorporator = incorporator;
    // }
    //
    // @Override
    // public boolean incorporate(IFoldablePConstraint other) {
    // if (other instanceof Inequality<?, ?>) {
    // Inequality other2 = (Inequality) other;
    // if (subject.equals(other2.subject)) {
    // Set<PVariable> newInequals = new HashSet<PVariable>(inequals);
    // newInequals.addAll(other2.inequals);
    // return new Inequality<PatternDescription, StubHandle>(buildable, subject, newInequals);
    // }
    // } else return false;
    // }

    @Override
    protected String toStringRest() {
        return who.toString() + (isWeak() ? "!=?" : "!=") + withWhom.toString();
    }

    @Override
    public void doReplaceVariable(PVariable obsolete, PVariable replacement) {
        if (obsolete.equals(who))
            who = replacement;
        if (obsolete.equals(withWhom))
            withWhom = replacement;
    }

    @Override
    public Set<PVariable> getDeducedVariables() {
        return Collections.emptySet();
    }

    /**
     * The inequality constraint is weak if it can be ignored when who is the same as withWhom, or if any if them is
     * undeducible.
     * 
     * @return the weak
     */
    public boolean isWeak() {
        return weak;
    }

    /**
     * A weak inequality constraint is eliminable if who is the same as withWhom, or if any if them is undeducible.
     */
    public boolean isEliminable() {
        return isWeak() && (who.equals(withWhom) || !who.isDeducable() || !withWhom.isDeducable());
    }

    /**
     * Eliminates a weak inequality constraint if it can be ignored when who is the same as withWhom, or if any if them
     * is undeducible.
     */
    public void eliminateWeak() {
        if (isEliminable())
            delete();
    }

    public PVariable getWho() {
        return who;
    }

    public PVariable getWithWhom() {
        return withWhom;
    }

}