From eedc0ac8c20710e20095e0c19269601c2719543a Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 6 Dec 2022 16:13:55 +0100 Subject: refactor(store): remove CountPolarity We will have to implement counting in DNF queries in another way. --- .../query/viatra/ViatraQueryableModelStore.java | 7 ---- .../internal/pquery/CountExpressionEvaluator.java | 38 ------------------ .../pquery/CountNotEqualsExpressionEvaluator.java | 30 --------------- .../query/viatra/internal/pquery/DNF2PQuery.java | 37 +++--------------- .../refinery/store/query/viatra/QueryTest.java | 45 +--------------------- .../refinery/store/query/atom/CallPolarity.java | 27 ++++++++++--- .../store/query/atom/ComparisonOperator.java | 22 ----------- .../store/query/atom/CountNotEqualsAtom.java | 31 --------------- .../store/query/atom/CountingPolarity.java | 13 ------- .../refinery/store/query/atom/SimplePolarity.java | 26 ------------- 10 files changed, 28 insertions(+), 248 deletions(-) delete mode 100644 subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountExpressionEvaluator.java delete mode 100644 subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/CountNotEqualsExpressionEvaluator.java delete mode 100644 subprojects/store/src/main/java/tools/refinery/store/query/atom/ComparisonOperator.java delete mode 100644 subprojects/store/src/main/java/tools/refinery/store/query/atom/CountNotEqualsAtom.java delete mode 100644 subprojects/store/src/main/java/tools/refinery/store/query/atom/CountingPolarity.java delete mode 100644 subprojects/store/src/main/java/tools/refinery/store/query/atom/SimplePolarity.java (limited to 'subprojects') 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 { validateRelationAtom(relationViews, dnfPredicate, relationViewAtom); } else if (atom instanceof CallAtom queryCallAtom) { validatePredicateAtom(predicates, dnfPredicate, queryCallAtom); - } else if (atom instanceof CountNotEqualsAtom countNotEqualsAtom) { - validateCountNotEqualsAtom(predicates, dnfPredicate, countNotEqualsAtom); } else if (!(atom instanceof EquivalenceAtom || atom instanceof ConstantAtom)) { throw new IllegalArgumentException("Unknown constraint: " + atom.toString()); } @@ -89,11 +87,6 @@ public class ViatraQueryableModelStore implements QueryableModelStore { validatePredicateReference(predicates, dnfPredicate, queryCallAtom.getTarget()); } - private void validateCountNotEqualsAtom(Set predicates, DNF dnfPredicate, - CountNotEqualsAtom countNotEqualsAtom) { - validatePredicateReference(predicates, dnfPredicate, countNotEqualsAtom.mayTarget()); - validatePredicateReference(predicates, dnfPredicate, countNotEqualsAtom.mustTarget()); - } private Map> initPredicates(Set predicates) { Map> 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 @@ -package tools.refinery.store.query.viatra.internal.pquery; - -import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator; -import org.eclipse.viatra.query.runtime.matchers.psystem.IValueProvider; -import tools.refinery.store.query.atom.ComparisonOperator; -import tools.refinery.store.query.atom.CountingPolarity; - -import java.util.List; - -public record CountExpressionEvaluator(String variableName, ComparisonOperator operator, - int threshold) implements IExpressionEvaluator { - public CountExpressionEvaluator(String variableName, CountingPolarity polarity) { - this(variableName, polarity.operator(), polarity.threshold()); - } - - @Override - public String getShortDescription() { - return "%s %s %d".formatted(variableName, operator, threshold); - } - - @Override - public Iterable getInputParameterNames() { - return List.of(variableName); - } - - @Override - public Object evaluateExpression(IValueProvider provider) { - int value = (Integer) provider.getValue(variableName); - return switch (operator) { - case EQUALS -> value == threshold; - case NOT_EQUALS -> value != threshold; - case LESS -> value < threshold; - case LESS_EQUALS -> value <= threshold; - case GREATER -> value > threshold; - case GREATER_EQUALS -> value >= threshold; - }; - } -} 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 @@ -package tools.refinery.store.query.viatra.internal.pquery; - -import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator; -import org.eclipse.viatra.query.runtime.matchers.psystem.IValueProvider; - -import java.util.List; - -public record CountNotEqualsExpressionEvaluator(boolean must, int threshold, String mayVariableName, - String mustVariableName) implements IExpressionEvaluator { - @Override - public String getShortDescription() { - return "%d %s not in [%s; %s]".formatted(threshold, must ? "must" : "may", mustVariableName, mayVariableName); - } - - @Override - public Iterable getInputParameterNames() { - return List.of(mayVariableName, mustVariableName); - } - - @Override - public Object evaluateExpression(IValueProvider provider) throws Exception { - int mayCount = (Integer) provider.getValue(mayVariableName); - int mustCount = (Integer) provider.getValue(mustVariableName); - if (must) { - return mayCount < threshold || mustCount > threshold; - } else { - return mayCount > threshold || mustCount < threshold; - } - } -} 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 { translateCallAtom(callAtom, body); } else if (constraint instanceof ConstantAtom constantAtom) { translateConstantAtom(constantAtom, body); - } else if (constraint instanceof CountNotEqualsAtom countNotEqualsAtom) { - translateCountNotEqualsAtom(countNotEqualsAtom, body); } else { throw new IllegalArgumentException("Unknown constraint: " + constraint.toString()); } @@ -128,20 +126,11 @@ public class DNF2PQuery { var variablesTuple = translateSubstitution(callAtom.getSubstitution(), body); var translatedReferred = translate(target); var polarity = callAtom.getPolarity(); - if (polarity instanceof SimplePolarity simplePolarity) { - switch (simplePolarity) { - case POSITIVE -> new PositivePatternCall(body, variablesTuple, translatedReferred); - case TRANSITIVE -> new BinaryTransitiveClosure(body, variablesTuple, translatedReferred); - case NEGATIVE -> new NegativePatternCall(body, variablesTuple, translatedReferred); - default -> throw new IllegalArgumentException("Unknown BasicCallKind: " + simplePolarity); - } - } else if (polarity instanceof CountingPolarity countingPolarity) { - var countVariableName = DNFUtils.generateUniqueName("count"); - var countPVariable = body.getOrCreateVariableByName(countVariableName); - new PatternMatchCounter(body, variablesTuple, translatedReferred, countPVariable); - new ExpressionEvaluation(body, new CountExpressionEvaluator(countVariableName, countingPolarity), null); - } else { - throw new IllegalArgumentException("Unknown CallKind: " + polarity); + switch (polarity) { + case POSITIVE -> new PositivePatternCall(body, variablesTuple, translatedReferred); + case TRANSITIVE -> new BinaryTransitiveClosure(body, variablesTuple, translatedReferred); + case NEGATIVE -> new NegativePatternCall(body, variablesTuple, translatedReferred); + default -> throw new IllegalArgumentException("Unknown polarity: " + polarity); } } @@ -149,20 +138,4 @@ public class DNF2PQuery { var variable = body.getOrCreateVariableByName(constantAtom.variable().getUniqueName()); new ConstantValue(body, variable, constantAtom.nodeId()); } - - private void translateCountNotEqualsAtom(CountNotEqualsAtom countNotEqualsAtom, PBody body) { - if (!(countNotEqualsAtom.mayTarget() instanceof DNF mayTarget) || - !(countNotEqualsAtom.mustTarget() instanceof DNF mustTarget)) { - throw new IllegalArgumentException("Only calls to DNF are supported"); - } - var variablesTuple = translateSubstitution(countNotEqualsAtom.substitution(), body); - var mayCountName = DNFUtils.generateUniqueName("countMay"); - var mayCountVariable = body.getOrCreateVariableByName(mayCountName); - new PatternMatchCounter(body, variablesTuple, translate(mayTarget), mayCountVariable); - var mustCountName = DNFUtils.generateUniqueName("countMust"); - var mustCountVariable = body.getOrCreateVariableByName(mustCountName); - new PatternMatchCounter(body, variablesTuple, translate(mustTarget), mustCountVariable); - new ExpressionEvaluation(body, new CountNotEqualsExpressionEvaluator(countNotEqualsAtom.must(), - countNotEqualsAtom.threshold(), mayCountName, mustCountName), null); - } } diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java index d18187e5..e82e006c 100644 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java +++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java @@ -5,7 +5,6 @@ import tools.refinery.store.model.representation.Relation; import tools.refinery.store.model.representation.TruthValue; import tools.refinery.store.query.*; import tools.refinery.store.query.atom.*; -import tools.refinery.store.query.viatra.ViatraQueryableModelStore; import tools.refinery.store.query.view.FilteredRelationView; import tools.refinery.store.query.view.KeyOnlyRelationView; import tools.refinery.store.query.view.RelationView; @@ -438,7 +437,7 @@ class QueryTest { .clause( new RelationViewAtom(personView, p3), new RelationViewAtom(personView, p4), - new CallAtom<>(SimplePolarity.TRANSITIVE, friendPredicate, p3, p4) + new CallAtom<>(CallPolarity.TRANSITIVE, friendPredicate, p3, p4) ) .build(); @@ -456,48 +455,6 @@ class QueryTest { assertEquals(3, model.countResults(predicate)); } - @Test - void countMatchTest() { - Relation person = new Relation<>("Person", 1, Boolean.class, false); - Relation friend = new Relation<>("friend", 2, TruthValue.class, TruthValue.FALSE); - RelationView personView = new KeyOnlyRelationView(person); - RelationView friendMustView = new FilteredRelationView<>(friend, "must", - TruthValue::must); - - Variable p1 = new Variable("p1"); - Variable p2 = new Variable("p2"); - - DNF called = DNF.builder("Called") - .parameters(p1, p2) - .clause( - new RelationViewAtom(personView, p1), - new RelationViewAtom(personView, p2), - new RelationViewAtom(friendMustView, p1, p2) - ) - .build(); - - DNF predicate = DNF.builder("Count") - .parameters(p1) - .clause( - new RelationViewAtom(personView, p1), - new CallAtom<>(new CountingPolarity(ComparisonOperator.EQUALS, 2), called, p1, p2) - ) - .build(); - - QueryableModelStore store = new ViatraQueryableModelStore(Set.of(person, friend), - Set.of(personView, friendMustView), Set.of(called, predicate)); - QueryableModel model = store.createModel(); - - model.put(person, Tuple.of(0), true); - model.put(person, Tuple.of(1), true); - model.put(person, Tuple.of(2), true); - model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); - model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); - - model.flushChanges(); - assertEquals(1, model.countResults(predicate)); - } - static void compareMatchSets(Stream matchSet, Set expected) { Set translatedMatchSet = new HashSet<>(); var iterator = matchSet.iterator(); diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallPolarity.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallPolarity.java index 2ab2b6d2..957e9b7b 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallPolarity.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallPolarity.java @@ -1,11 +1,28 @@ package tools.refinery.store.query.atom; -public sealed interface CallPolarity permits SimplePolarity, CountingPolarity { - boolean isPositive(); +public enum CallPolarity { + POSITIVE(true, false), + NEGATIVE(false, false), + TRANSITIVE(true, true); - boolean isTransitive(); + private final boolean positive; - static CallPolarity fromBoolean(boolean positive) { - return positive ? SimplePolarity.POSITIVE : SimplePolarity.NEGATIVE; + private final boolean transitive; + + CallPolarity(boolean positive, boolean transitive) { + this.positive = positive; + this.transitive = transitive; + } + + public boolean isPositive() { + return positive; + } + + public boolean isTransitive() { + return transitive; + } + + public static CallPolarity fromBoolean(boolean positive) { + return positive ? POSITIVE : NEGATIVE; } } diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/ComparisonOperator.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/ComparisonOperator.java deleted file mode 100644 index ca113181..00000000 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/ComparisonOperator.java +++ /dev/null @@ -1,22 +0,0 @@ -package tools.refinery.store.query.atom; - -public enum ComparisonOperator { - EQUALS, - NOT_EQUALS, - LESS, - LESS_EQUALS, - GREATER, - GREATER_EQUALS; - - @Override - public String toString() { - return switch (this) { - case EQUALS -> "=="; - case NOT_EQUALS -> "!="; - case LESS -> "<"; - case LESS_EQUALS -> "<="; - case GREATER -> ">"; - case GREATER_EQUALS -> ">="; - }; - } -} diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountNotEqualsAtom.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountNotEqualsAtom.java deleted file mode 100644 index 312e5fb8..00000000 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountNotEqualsAtom.java +++ /dev/null @@ -1,31 +0,0 @@ -package tools.refinery.store.query.atom; - -import tools.refinery.store.model.RelationLike; -import tools.refinery.store.query.Variable; - -import java.util.List; -import java.util.Set; - -public record CountNotEqualsAtom(boolean must, int threshold, T mayTarget, T mustTarget, - List substitution) implements DNFAtom { - public CountNotEqualsAtom { - if (substitution.size() != mayTarget.getArity()) { - throw new IllegalArgumentException("%s needs %d arguments, but got %s".formatted(mayTarget.getName(), - mayTarget.getArity(), substitution.size())); - } - if (substitution.size() != mustTarget.getArity()) { - throw new IllegalArgumentException("%s needs %d arguments, but got %s".formatted(mustTarget.getName(), - mustTarget.getArity(), substitution.size())); - } - } - - public CountNotEqualsAtom(boolean must, int threshold, T mayTarget, T mustTarget, Variable... substitution) { - this(must, threshold, mayTarget, mustTarget, List.of(substitution)); - } - - @Override - public void collectAllVariables(Set variables) { - // No variables to collect, because all variables should either appear in other clauses, - // or are quantified by this clause. - } -} diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountingPolarity.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountingPolarity.java deleted file mode 100644 index 08a62f4f..00000000 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CountingPolarity.java +++ /dev/null @@ -1,13 +0,0 @@ -package tools.refinery.store.query.atom; - -public record CountingPolarity(ComparisonOperator operator, int threshold) implements CallPolarity { - @Override - public boolean isPositive() { - return false; - } - - @Override - public boolean isTransitive() { - return false; - } -} diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/SimplePolarity.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/SimplePolarity.java deleted file mode 100644 index d243bf26..00000000 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/SimplePolarity.java +++ /dev/null @@ -1,26 +0,0 @@ -package tools.refinery.store.query.atom; - -public enum SimplePolarity implements CallPolarity { - POSITIVE(true, false), - NEGATIVE(false, false), - TRANSITIVE(true, true); - - private final boolean positive; - - private final boolean transitive; - - SimplePolarity(boolean positive, boolean transitive) { - this.positive = positive; - this.transitive = transitive; - } - - @Override - public boolean isPositive() { - return positive; - } - - @Override - public boolean isTransitive() { - return transitive; - } -} -- cgit v1.2.3-54-g00ecf