aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query-viatra/src/main
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-12-06 16:13:55 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-12-09 00:07:39 +0100
commiteedc0ac8c20710e20095e0c19269601c2719543a (patch)
tree86341ae1eb585eb41167670ab970d2aa6dc16609 /subprojects/store-query-viatra/src/main
parentrefactor(frontend): theme improvements (diff)
downloadrefinery-eedc0ac8c20710e20095e0c19269601c2719543a.tar.gz
refinery-eedc0ac8c20710e20095e0c19269601c2719543a.tar.zst
refinery-eedc0ac8c20710e20095e0c19269601c2719543a.zip
refactor(store): remove CountPolarity
We will have to implement counting in DNF queries in another way.
Diffstat (limited to 'subprojects/store-query-viatra/src/main')
-rw-r--r--subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/ViatraQueryableModelStore.java7
-rw-r--r--subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountExpressionEvaluator.java38
-rw-r--r--subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountNotEqualsExpressionEvaluator.java30
-rw-r--r--subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/DNF2PQuery.java37
4 files changed, 5 insertions, 107 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/ViatraQueryableModelStore.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/ViatraQueryableModelStore.java
index 59fb1171..37cd91a6 100644
--- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/ViatraQueryableModelStore.java
+++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/ViatraQueryableModelStore.java
@@ -57,8 +57,6 @@ public class ViatraQueryableModelStore implements QueryableModelStore {
57 validateRelationAtom(relationViews, dnfPredicate, relationViewAtom); 57 validateRelationAtom(relationViews, dnfPredicate, relationViewAtom);
58 } else if (atom instanceof CallAtom<?> queryCallAtom) { 58 } else if (atom instanceof CallAtom<?> queryCallAtom) {
59 validatePredicateAtom(predicates, dnfPredicate, queryCallAtom); 59 validatePredicateAtom(predicates, dnfPredicate, queryCallAtom);
60 } else if (atom instanceof CountNotEqualsAtom<?> countNotEqualsAtom) {
61 validateCountNotEqualsAtom(predicates, dnfPredicate, countNotEqualsAtom);
62 } else if (!(atom instanceof EquivalenceAtom || atom instanceof ConstantAtom)) { 60 } else if (!(atom instanceof EquivalenceAtom || atom instanceof ConstantAtom)) {
63 throw new IllegalArgumentException("Unknown constraint: " + atom.toString()); 61 throw new IllegalArgumentException("Unknown constraint: " + atom.toString());
64 } 62 }
@@ -89,11 +87,6 @@ public class ViatraQueryableModelStore implements QueryableModelStore {
89 validatePredicateReference(predicates, dnfPredicate, queryCallAtom.getTarget()); 87 validatePredicateReference(predicates, dnfPredicate, queryCallAtom.getTarget());
90 } 88 }
91 89
92 private void validateCountNotEqualsAtom(Set<DNF> predicates, DNF dnfPredicate,
93 CountNotEqualsAtom<?> countNotEqualsAtom) {
94 validatePredicateReference(predicates, dnfPredicate, countNotEqualsAtom.mayTarget());
95 validatePredicateReference(predicates, dnfPredicate, countNotEqualsAtom.mustTarget());
96 }
97 90
98 private Map<DNF, GenericQuerySpecification<RawPatternMatcher>> initPredicates(Set<DNF> predicates) { 91 private Map<DNF, GenericQuerySpecification<RawPatternMatcher>> initPredicates(Set<DNF> predicates) {
99 Map<DNF, GenericQuerySpecification<RawPatternMatcher>> result = new HashMap<>(); 92 Map<DNF, GenericQuerySpecification<RawPatternMatcher>> result = new HashMap<>();
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountExpressionEvaluator.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountExpressionEvaluator.java
deleted file mode 100644
index e583e187..00000000
--- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountExpressionEvaluator.java
+++ /dev/null
@@ -1,38 +0,0 @@
1package tools.refinery.store.query.viatra.internal.pquery;
2
3import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator;
4import org.eclipse.viatra.query.runtime.matchers.psystem.IValueProvider;
5import tools.refinery.store.query.atom.ComparisonOperator;
6import tools.refinery.store.query.atom.CountingPolarity;
7
8import java.util.List;
9
10public record CountExpressionEvaluator(String variableName, ComparisonOperator operator,
11 int threshold) implements IExpressionEvaluator {
12 public CountExpressionEvaluator(String variableName, CountingPolarity polarity) {
13 this(variableName, polarity.operator(), polarity.threshold());
14 }
15
16 @Override
17 public String getShortDescription() {
18 return "%s %s %d".formatted(variableName, operator, threshold);
19 }
20
21 @Override
22 public Iterable<String> getInputParameterNames() {
23 return List.of(variableName);
24 }
25
26 @Override
27 public Object evaluateExpression(IValueProvider provider) {
28 int value = (Integer) provider.getValue(variableName);
29 return switch (operator) {
30 case EQUALS -> value == threshold;
31 case NOT_EQUALS -> value != threshold;
32 case LESS -> value < threshold;
33 case LESS_EQUALS -> value <= threshold;
34 case GREATER -> value > threshold;
35 case GREATER_EQUALS -> value >= threshold;
36 };
37 }
38}
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountNotEqualsExpressionEvaluator.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountNotEqualsExpressionEvaluator.java
deleted file mode 100644
index 6f333a06..00000000
--- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountNotEqualsExpressionEvaluator.java
+++ /dev/null
@@ -1,30 +0,0 @@
1package tools.refinery.store.query.viatra.internal.pquery;
2
3import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator;
4import org.eclipse.viatra.query.runtime.matchers.psystem.IValueProvider;
5
6import java.util.List;
7
8public record CountNotEqualsExpressionEvaluator(boolean must, int threshold, String mayVariableName,
9 String mustVariableName) implements IExpressionEvaluator {
10 @Override
11 public String getShortDescription() {
12 return "%d %s not in [%s; %s]".formatted(threshold, must ? "must" : "may", mustVariableName, mayVariableName);
13 }
14
15 @Override
16 public Iterable<String> getInputParameterNames() {
17 return List.of(mayVariableName, mustVariableName);
18 }
19
20 @Override
21 public Object evaluateExpression(IValueProvider provider) throws Exception {
22 int mayCount = (Integer) provider.getValue(mayVariableName);
23 int mustCount = (Integer) provider.getValue(mustVariableName);
24 if (must) {
25 return mayCount < threshold || mustCount > threshold;
26 } else {
27 return mayCount > threshold || mustCount < threshold;
28 }
29 }
30}
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/DNF2PQuery.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/DNF2PQuery.java
index ae99365d..98054161 100644
--- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/DNF2PQuery.java
+++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/DNF2PQuery.java
@@ -85,8 +85,6 @@ public class DNF2PQuery {
85 translateCallAtom(callAtom, body); 85 translateCallAtom(callAtom, body);
86 } else if (constraint instanceof ConstantAtom constantAtom) { 86 } else if (constraint instanceof ConstantAtom constantAtom) {
87 translateConstantAtom(constantAtom, body); 87 translateConstantAtom(constantAtom, body);
88 } else if (constraint instanceof CountNotEqualsAtom<?> countNotEqualsAtom) {
89 translateCountNotEqualsAtom(countNotEqualsAtom, body);
90 } else { 88 } else {
91 throw new IllegalArgumentException("Unknown constraint: " + constraint.toString()); 89 throw new IllegalArgumentException("Unknown constraint: " + constraint.toString());
92 } 90 }
@@ -128,20 +126,11 @@ public class DNF2PQuery {
128 var variablesTuple = translateSubstitution(callAtom.getSubstitution(), body); 126 var variablesTuple = translateSubstitution(callAtom.getSubstitution(), body);
129 var translatedReferred = translate(target); 127 var translatedReferred = translate(target);
130 var polarity = callAtom.getPolarity(); 128 var polarity = callAtom.getPolarity();
131 if (polarity instanceof SimplePolarity simplePolarity) { 129 switch (polarity) {
132 switch (simplePolarity) { 130 case POSITIVE -> new PositivePatternCall(body, variablesTuple, translatedReferred);
133 case POSITIVE -> new PositivePatternCall(body, variablesTuple, translatedReferred); 131 case TRANSITIVE -> new BinaryTransitiveClosure(body, variablesTuple, translatedReferred);
134 case TRANSITIVE -> new BinaryTransitiveClosure(body, variablesTuple, translatedReferred); 132 case NEGATIVE -> new NegativePatternCall(body, variablesTuple, translatedReferred);
135 case NEGATIVE -> new NegativePatternCall(body, variablesTuple, translatedReferred); 133 default -> throw new IllegalArgumentException("Unknown polarity: " + polarity);
136 default -> throw new IllegalArgumentException("Unknown BasicCallKind: " + simplePolarity);
137 }
138 } else if (polarity instanceof CountingPolarity countingPolarity) {
139 var countVariableName = DNFUtils.generateUniqueName("count");
140 var countPVariable = body.getOrCreateVariableByName(countVariableName);
141 new PatternMatchCounter(body, variablesTuple, translatedReferred, countPVariable);
142 new ExpressionEvaluation(body, new CountExpressionEvaluator(countVariableName, countingPolarity), null);
143 } else {
144 throw new IllegalArgumentException("Unknown CallKind: " + polarity);
145 } 134 }
146 } 135 }
147 136
@@ -149,20 +138,4 @@ public class DNF2PQuery {
149 var variable = body.getOrCreateVariableByName(constantAtom.variable().getUniqueName()); 138 var variable = body.getOrCreateVariableByName(constantAtom.variable().getUniqueName());
150 new ConstantValue(body, variable, constantAtom.nodeId()); 139 new ConstantValue(body, variable, constantAtom.nodeId());
151 } 140 }
152
153 private void translateCountNotEqualsAtom(CountNotEqualsAtom<?> countNotEqualsAtom, PBody body) {
154 if (!(countNotEqualsAtom.mayTarget() instanceof DNF mayTarget) ||
155 !(countNotEqualsAtom.mustTarget() instanceof DNF mustTarget)) {
156 throw new IllegalArgumentException("Only calls to DNF are supported");
157 }
158 var variablesTuple = translateSubstitution(countNotEqualsAtom.substitution(), body);
159 var mayCountName = DNFUtils.generateUniqueName("countMay");
160 var mayCountVariable = body.getOrCreateVariableByName(mayCountName);
161 new PatternMatchCounter(body, variablesTuple, translate(mayTarget), mayCountVariable);
162 var mustCountName = DNFUtils.generateUniqueName("countMust");
163 var mustCountVariable = body.getOrCreateVariableByName(mustCountName);
164 new PatternMatchCounter(body, variablesTuple, translate(mustTarget), mustCountVariable);
165 new ExpressionEvaluation(body, new CountNotEqualsExpressionEvaluator(countNotEqualsAtom.must(),
166 countNotEqualsAtom.threshold(), mayCountName, mustCountName), null);
167 }
168} 141}