aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java')
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java
new file mode 100644
index 00000000..7616b4a2
--- /dev/null
+++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java
@@ -0,0 +1,116 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Objects;
14
15import org.eclipse.viatra.dse.api.DSEException;
16import org.eclipse.viatra.dse.base.ThreadContext;
17import org.eclipse.viatra.dse.objectives.IGlobalConstraint;
18import org.eclipse.viatra.query.runtime.api.IPatternMatch;
19import org.eclipse.viatra.query.runtime.api.IQuerySpecification;
20import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
21import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
22import org.eclipse.viatra.query.runtime.exception.ViatraQueryException;
23
24/**
25 * This global constraint collects a list of VIATRA Query pattern and checks if any of them has a match on along a trajectory.
26 * If any of the patterns has a match then it is unsatisfied and the exploration should backtrack.
27 *
28 * @author Andras Szabolcs Nagy
29 *
30 */
31public class ModelQueriesGlobalConstraint implements IGlobalConstraint {
32
33 public static final String GLOBAL_CONSTRAINT = "GlobalConstraint";
34 protected String name;
35 protected List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints;
36 protected List<ViatraQueryMatcher<? extends IPatternMatch>> matchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>();
37 protected ModelQueryType type = ModelQueryType.NO_MATCH;
38
39 public ModelQueriesGlobalConstraint(String name,
40 List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints) {
41 Objects.requireNonNull(name, "Name of the global constraint cannot be null.");
42 Objects.requireNonNull(constraints, "The list of constraints cannot be null.");
43
44 this.name = name;
45 this.constraints = constraints;
46 }
47
48 public ModelQueriesGlobalConstraint(
49 List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints) {
50 this(GLOBAL_CONSTRAINT, constraints);
51 }
52
53 public ModelQueriesGlobalConstraint(String name) {
54 this(name, new ArrayList<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>>());
55 }
56
57 public ModelQueriesGlobalConstraint() {
58 this(GLOBAL_CONSTRAINT,
59 new ArrayList<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>>());
60 }
61
62 /**
63 * Adds a new VIATRA Query pattern.
64 *
65 * @param constraint
66 * A VIATRA Query pattern.
67 * @return The actual instance to enable builder pattern like usage.
68 */
69 public ModelQueriesGlobalConstraint withConstraint(
70 IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> constraint) {
71 constraints.add(constraint);
72 return this;
73 }
74
75 public ModelQueriesGlobalConstraint withType(ModelQueryType type) {
76 this.type = type;
77 return this;
78 }
79
80 @Override
81 public String getName() {
82 return name;
83 }
84
85 @Override
86 public boolean checkGlobalConstraint(ThreadContext context) {
87 for (ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) {
88 if ((type.equals(ModelQueryType.NO_MATCH) && matcher.countMatches() > 0)
89 || (type.equals(ModelQueryType.MUST_HAVE_MATCH) && matcher.countMatches() == 0)) {
90 return false;
91 }
92 }
93 return true;
94 }
95
96 @Override
97 public void init(ThreadContext context) {
98 try {
99 ViatraQueryEngine queryEngine = context.getQueryEngine();
100
101 for (IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> querySpecification : constraints) {
102 ViatraQueryMatcher<? extends IPatternMatch> matcher = querySpecification.getMatcher(queryEngine);
103 matchers.add(matcher);
104 }
105
106 } catch (ViatraQueryException e) {
107 throw new DSEException("Couldn't get the VIATRA Query matcher, see inner exception", e);
108 }
109 }
110
111 @Override
112 public IGlobalConstraint createNew() {
113 return new ModelQueriesGlobalConstraint(name, constraints);
114 }
115
116}