aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java
blob: 77d416f5931be83e28533eb89c22b429b4347b93 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*******************************************************************************
 * Copyright (c) 2010-2016, Andras Szabolcs Nagy, Zoltan Ujhelyi 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.objectives.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.eclipse.viatra.dse.api.DSEException;
import org.eclipse.viatra.dse.base.ThreadContext;
import org.eclipse.viatra.dse.objectives.IObjective;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.viatra.query.runtime.api.IQuerySpecification;
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
import org.eclipse.viatra.query.runtime.exception.ViatraQueryException;

/**
 * This objective serves as soft and as hard objective at the same time by defining two lists of VIATRA Query
 * specifications.
 * 
 * As a soft objective, it collects a list of VIATRA Query specifications, which have predefined weights. Then the
 * fitness value of an arbitrary solution is calculated in the following way:
 * <p>
 * <code>fitness = sum( pattern[i].countMatches() * weight[i] )</code>
 * <p>
 * As a hard objective it collects a separate list of VIATRA Query specifications. If every one of them has a match the
 * hard constraint is considered to be fulfilled.
 * 
 * @author Andras Szabolcs Nagy
 * @see IObjective
 *
 */
public class ConstraintsObjective extends BaseObjective {

    public static final String DEFAULT_NAME = "ConstraintsObjective";

    public static class QueryConstraint {
        public final String name;
        public final IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query;
        public final Double weight;
        public final ModelQueryType type;

        public QueryConstraint(String name,
                IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, Double weight,
                ModelQueryType type) {
            this.name = name;
            this.query = query;
            this.weight = weight;
            this.type = type;
        }

        public QueryConstraint(String name,
                IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, Double weight) {
            this(name, query, weight, ModelQueryType.MUST_HAVE_MATCH);
        }

        public QueryConstraint(String name,
                IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, ModelQueryType type) {
            this(name, query, 0d, type);
        }
    }

    protected List<QueryConstraint> softConstraints;
    protected List<QueryConstraint> hardConstraints;

    protected List<ViatraQueryMatcher<? extends IPatternMatch>> softMatchers;
    protected List<ViatraQueryMatcher<? extends IPatternMatch>> hardMatchers;
    protected List<Integer> softMatches;
    protected List<Integer> hardMatches;

    public ConstraintsObjective(String name, List<QueryConstraint> softConstraints,
            List<QueryConstraint> hardConstraints) {
        super(name);
        Objects.requireNonNull(softConstraints, "The list of soft constraints cannot be null.");
        Objects.requireNonNull(hardConstraints, "The list of hard constraints cannot be null.");

        this.softConstraints = softConstraints;
        this.hardConstraints = hardConstraints;
    }

    public ConstraintsObjective(String name, List<QueryConstraint> hardConstraints) {
        this(name, new ArrayList<QueryConstraint>(), hardConstraints);
    }

    public ConstraintsObjective(List<QueryConstraint> hardConstraints) {
        this(DEFAULT_NAME, new ArrayList<QueryConstraint>(), hardConstraints);
    }

    public ConstraintsObjective(String name) {
        this(name, new ArrayList<QueryConstraint>(), new ArrayList<QueryConstraint>());
    }

    public ConstraintsObjective() {
        this(DEFAULT_NAME, new ArrayList<QueryConstraint>(), new ArrayList<QueryConstraint>());
    }

    /**
     * Adds a new soft constraint.
     * 
     * @param name
     *            A name for the soft constraint.
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @param weight
     *            The weight of the pattern.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withSoftConstraint(String name,
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> softConstraint, double weight) {
        softConstraints.add(new QueryConstraint(name, softConstraint, weight));
        return this;
    }

    /**
     * Adds a new soft constraint with the name of the query specification's fully qualified name.
     * 
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @param weight
     *            The weight of the pattern.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withSoftConstraint(
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> softConstraint, double weight) {
        return withSoftConstraint(softConstraint.getFullyQualifiedName(), softConstraint, weight);
    }

    /**
     * Adds a new hard constraint.
     * 
     * @param name
     *            A name for the hard constraint.
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @param type
     *            {@link ModelQueryType}, which determines whether the constraint should have at least one match or none
     *            at all.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withHardConstraint(String name,
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint,
            ModelQueryType type) {
        hardConstraints.add(new QueryConstraint(name, hardConstraint, type));
        return this;
    }

    /**
     * Adds a new hard constraint with the default {@link ModelQueryType#MUST_HAVE_MATCH}.
     * 
     * @param name
     *            A name for the hard constraint.
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withHardConstraint(String name,
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint) {
        hardConstraints.add(new QueryConstraint(name, hardConstraint, ModelQueryType.MUST_HAVE_MATCH));
        return this;
    }

    /**
     * Adds a new hard constraint with the name of the query specification's fully qualified name and the default
     * {@link ModelQueryType#MUST_HAVE_MATCH}.
     * 
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withHardConstraint(
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint) {
        return withHardConstraint(hardConstraint.getFullyQualifiedName(), hardConstraint,
                ModelQueryType.MUST_HAVE_MATCH);
    }

    /**
     * Adds a new hard constraint with the name of the query specification's fully qualified name.
     * 
     * @param softConstraint
     *            A VIATRA Query pattern specification.
     * @param type
     *            {@link ModelQueryType}, which determines whether the constraint should have at least one match or none
     *            at all.
     * @return The actual instance to enable builder pattern like usage.
     */
    public ConstraintsObjective withHardConstraint(
            IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint,
            ModelQueryType type) {
        return withHardConstraint(hardConstraint.getFullyQualifiedName(), hardConstraint, type);
    }

    @Override
    public Double getFitness(ThreadContext context) {

        if (softConstraints.isEmpty()) {
            return 0d;
        }

        double result = 0;

        for (int i = 0; i < softConstraints.size(); i++) {
            int countMatches = softMatchers.get(i).countMatches();
            result += countMatches * softConstraints.get(i).weight;
            softMatches.set(i, Integer.valueOf(countMatches));
        }

        return result;
    }

    @Override
    public void init(ThreadContext context) {

        super.init(context);

        softMatches = new ArrayList<Integer>(softConstraints.size());
        softMatchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>(softConstraints.size());
        hardMatches = new ArrayList<Integer>(hardConstraints.size());
        hardMatchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>(hardConstraints.size());
        for (int i = 0; i < softConstraints.size(); i++) {
            softMatches.add(0);
        }
        for (int i = 0; i < hardConstraints.size(); i++) {
            hardMatches.add(0);
        }

        try {
            ViatraQueryEngine queryEngine = context.getQueryEngine();

            for (QueryConstraint qc : softConstraints) {
                softMatchers.add(qc.query.getMatcher(queryEngine));
            }

            for (QueryConstraint qc : hardConstraints) {
                hardMatchers.add(qc.query.getMatcher(queryEngine));
            }

        } catch (ViatraQueryException e) {
            throw new DSEException("Couldn't initialize the VIATRA Query matcher, see inner exception", e);
        }
    }

    @Override
    public IObjective createNew() {
        new ArrayList<Double>(softConstraints.size());
        ConstraintsObjective result = new ConstraintsObjective(name, softConstraints, hardConstraints);
        if (isThereFitnessConstraint) {
            result.withHardConstraintOnFitness(fitnessConstraint, fitnessConstraintComparator);
        }
        return result.withComparator(comparator).withLevel(level);
    }

    @Override
    public boolean isHardObjective() {
        return !hardConstraints.isEmpty() || super.isHardObjective();
    }

    @Override
    public boolean satisifiesHardObjective(Double fitness) {

        boolean result = true;

        for (int i = 0; i < hardConstraints.size(); i++) {
            ModelQueryType type = hardConstraints.get(i).type;
            int countMatches = hardMatchers.get(i).countMatches();
            hardMatches.set(i, Integer.valueOf(countMatches));
            if ((type.equals(ModelQueryType.MUST_HAVE_MATCH) && countMatches <= 0)
                    || (type.equals(ModelQueryType.NO_MATCH) && countMatches > 0)) {
                result = false;
            }
        }
        
        result = super.satisifiesHardObjective(fitness) ? result : false;
        
        return result;
    }

    public List<QueryConstraint> getSoftConstraints() {
        return softConstraints;
    }

    public List<QueryConstraint> getHardConstraints() {
        return hardConstraints;
    }

    public String getSoftName(int index) {
        return softConstraints.get(index).name;
    }

    public String getHardName(int index) {
        return hardConstraints.get(index).name;
    }

    public List<Integer> getSoftMatches() {
        return softMatches;
    }

    public List<Integer> getHardMatches() {
        return hardMatches;
    }

    public List<String> getSoftNames() {
        List<String> softNames = new ArrayList<>(softConstraints.size());
        for (QueryConstraint qc : softConstraints) {
            softNames.add(qc.name);
        }
        return softNames;
    }

}