diff options
author | Kristóf Marussy <kristof@marussy.com> | 2023-01-29 02:06:57 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2023-01-29 02:06:57 +0100 |
commit | 1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef (patch) | |
tree | ee5cfb52db47c10de7e86bb44651e815526acb0b /subprojects/store/src | |
parent | refactor: Model store and query API (diff) | |
download | refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.tar.gz refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.tar.zst refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.zip |
feat: negative and transitive RelationViewAtom
Use PVisibility.EMBEDDED helper patterns to avoid superfluous production
nodes in the Rete net.
Diffstat (limited to 'subprojects/store/src')
3 files changed, 40 insertions, 60 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/AbstractSubstitutionAtom.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/AbstractSubstitutionAtom.java deleted file mode 100644 index af6a8f5a..00000000 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/AbstractSubstitutionAtom.java +++ /dev/null | |||
@@ -1,35 +0,0 @@ | |||
1 | package tools.refinery.store.query.atom; | ||
2 | |||
3 | import tools.refinery.store.representation.SymbolLike; | ||
4 | import tools.refinery.store.query.Variable; | ||
5 | |||
6 | import java.util.List; | ||
7 | import java.util.Set; | ||
8 | |||
9 | public abstract class AbstractSubstitutionAtom<T extends SymbolLike> implements DNFAtom { | ||
10 | private final T target; | ||
11 | |||
12 | private final List<Variable> substitution; | ||
13 | |||
14 | protected AbstractSubstitutionAtom(T target, List<Variable> substitution) { | ||
15 | if (substitution.size() != target.arity()) { | ||
16 | throw new IllegalArgumentException("%s needs %d arguments, but got %s".formatted(target.name(), | ||
17 | target.arity(), substitution.size())); | ||
18 | } | ||
19 | this.target = target; | ||
20 | this.substitution = substitution; | ||
21 | } | ||
22 | |||
23 | public T getTarget() { | ||
24 | return target; | ||
25 | } | ||
26 | |||
27 | public List<Variable> getSubstitution() { | ||
28 | return substitution; | ||
29 | } | ||
30 | |||
31 | @Override | ||
32 | public void collectAllVariables(Set<Variable> variables) { | ||
33 | variables.addAll(substitution); | ||
34 | } | ||
35 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallAtom.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallAtom.java index 2e855246..6de5cd08 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallAtom.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/atom/CallAtom.java | |||
@@ -1,21 +1,28 @@ | |||
1 | package tools.refinery.store.query.atom; | 1 | package tools.refinery.store.query.atom; |
2 | 2 | ||
3 | import tools.refinery.store.representation.SymbolLike; | ||
4 | import tools.refinery.store.query.Variable; | 3 | import tools.refinery.store.query.Variable; |
4 | import tools.refinery.store.representation.SymbolLike; | ||
5 | 5 | ||
6 | import java.util.List; | 6 | import java.util.List; |
7 | import java.util.Objects; | 7 | import java.util.Objects; |
8 | import java.util.Set; | 8 | import java.util.Set; |
9 | 9 | ||
10 | public abstract class CallAtom<T extends SymbolLike> extends AbstractSubstitutionAtom<T> { | 10 | public abstract class CallAtom<T extends SymbolLike> implements DNFAtom { |
11 | private final CallPolarity polarity; | 11 | private final CallPolarity polarity; |
12 | private final T target; | ||
13 | private final List<Variable> substitution; | ||
12 | 14 | ||
13 | protected CallAtom(CallPolarity polarity, T target, List<Variable> substitution) { | 15 | protected CallAtom(CallPolarity polarity, T target, List<Variable> substitution) { |
14 | super(target, substitution); | 16 | if (substitution.size() != target.arity()) { |
17 | throw new IllegalArgumentException("%s needs %d arguments, but got %s".formatted(target.name(), | ||
18 | target.arity(), substitution.size())); | ||
19 | } | ||
15 | if (polarity.isTransitive() && target.arity() != 2) { | 20 | if (polarity.isTransitive() && target.arity() != 2) { |
16 | throw new IllegalArgumentException("Transitive closures can only take binary relations"); | 21 | throw new IllegalArgumentException("Transitive closures can only take binary relations"); |
17 | } | 22 | } |
18 | this.polarity = polarity; | 23 | this.polarity = polarity; |
24 | this.target = target; | ||
25 | this.substitution = substitution; | ||
19 | } | 26 | } |
20 | 27 | ||
21 | protected CallAtom(CallPolarity polarity, T target, Variable... substitution) { | 28 | protected CallAtom(CallPolarity polarity, T target, Variable... substitution) { |
@@ -42,10 +49,18 @@ public abstract class CallAtom<T extends SymbolLike> extends AbstractSubstitutio | |||
42 | return polarity; | 49 | return polarity; |
43 | } | 50 | } |
44 | 51 | ||
52 | public T getTarget() { | ||
53 | return target; | ||
54 | } | ||
55 | |||
56 | public List<Variable> getSubstitution() { | ||
57 | return substitution; | ||
58 | } | ||
59 | |||
45 | @Override | 60 | @Override |
46 | public void collectAllVariables(Set<Variable> variables) { | 61 | public void collectAllVariables(Set<Variable> variables) { |
47 | if (polarity.isPositive()) { | 62 | if (polarity.isPositive()) { |
48 | super.collectAllVariables(variables); | 63 | variables.addAll(substitution); |
49 | } | 64 | } |
50 | } | 65 | } |
51 | 66 | ||
@@ -53,14 +68,13 @@ public abstract class CallAtom<T extends SymbolLike> extends AbstractSubstitutio | |||
53 | public boolean equals(Object o) { | 68 | public boolean equals(Object o) { |
54 | if (this == o) return true; | 69 | if (this == o) return true; |
55 | if (o == null || getClass() != o.getClass()) return false; | 70 | if (o == null || getClass() != o.getClass()) return false; |
56 | CallAtom<?> that = (CallAtom<?>) o; | 71 | CallAtom<?> callAtom = (CallAtom<?>) o; |
57 | return Objects.equals(polarity, that.polarity) | 72 | return polarity == callAtom.polarity && Objects.equals(target, callAtom.target) && Objects.equals(substitution |
58 | && Objects.equals(getTarget(), that.getTarget()) | 73 | , callAtom.substitution); |
59 | && Objects.equals(getSubstitution(), that.getSubstitution()); | ||
60 | } | 74 | } |
61 | 75 | ||
62 | @Override | 76 | @Override |
63 | public int hashCode() { | 77 | public int hashCode() { |
64 | return Objects.hash(polarity, getTarget(), getSubstitution()); | 78 | return Objects.hash(polarity, target, substitution); |
65 | } | 79 | } |
66 | } | 80 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/atom/RelationViewAtom.java b/subprojects/store/src/main/java/tools/refinery/store/query/atom/RelationViewAtom.java index cf836541..a2b176c4 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/atom/RelationViewAtom.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/atom/RelationViewAtom.java | |||
@@ -4,28 +4,29 @@ import tools.refinery.store.query.Variable; | |||
4 | import tools.refinery.store.query.view.AnyRelationView; | 4 | import tools.refinery.store.query.view.AnyRelationView; |
5 | 5 | ||
6 | import java.util.List; | 6 | import java.util.List; |
7 | import java.util.Objects; | ||
8 | 7 | ||
9 | public final class RelationViewAtom extends AbstractSubstitutionAtom<AnyRelationView> { | 8 | public final class RelationViewAtom extends CallAtom<AnyRelationView> { |
10 | public RelationViewAtom(AnyRelationView target, List<Variable> substitution) { | 9 | public RelationViewAtom(CallPolarity polarity, AnyRelationView target, List<Variable> substitution) { |
11 | super(target, substitution); | 10 | super(polarity, target, substitution); |
12 | } | 11 | } |
13 | 12 | ||
14 | public RelationViewAtom(AnyRelationView target, Variable... substitution) { | 13 | public RelationViewAtom(CallPolarity polarity, AnyRelationView target, Variable... substitution) { |
15 | this(target, List.of(substitution)); | 14 | super(polarity, target, substitution); |
16 | } | 15 | } |
17 | 16 | ||
18 | @Override | 17 | public RelationViewAtom(boolean positive, AnyRelationView target, List<Variable> substitution) { |
19 | public boolean equals(Object o) { | 18 | super(positive, target, substitution); |
20 | if (this == o) return true; | ||
21 | if (o == null || getClass() != o.getClass()) return false; | ||
22 | RelationViewAtom relationViewAtom = (RelationViewAtom) o; | ||
23 | return Objects.equals(getTarget(), relationViewAtom.getTarget()) | ||
24 | && Objects.equals(getSubstitution(), relationViewAtom.getSubstitution()); | ||
25 | } | 19 | } |
26 | 20 | ||
27 | @Override | 21 | public RelationViewAtom(boolean positive, AnyRelationView target, Variable... substitution) { |
28 | public int hashCode() { | 22 | super(positive, target, substitution); |
29 | return Objects.hash(getTarget(), getSubstitution()); | 23 | } |
24 | |||
25 | public RelationViewAtom(AnyRelationView target, List<Variable> substitution) { | ||
26 | super(target, substitution); | ||
27 | } | ||
28 | |||
29 | public RelationViewAtom(AnyRelationView target, Variable... substitution) { | ||
30 | super(target, substitution); | ||
30 | } | 31 | } |
31 | } | 32 | } |