diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java index 5e1ae94d..091b4e04 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java | |||
@@ -1,11 +1,11 @@ | |||
1 | package tools.refinery.store.query.literal; | 1 | package tools.refinery.store.query.literal; |
2 | 2 | ||
3 | import tools.refinery.store.query.DnfUtils; | ||
4 | import tools.refinery.store.query.RelationLike; | 3 | import tools.refinery.store.query.RelationLike; |
5 | import tools.refinery.store.query.Variable; | 4 | import tools.refinery.store.query.Variable; |
5 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | ||
6 | import tools.refinery.store.query.substitution.Substitution; | ||
6 | 7 | ||
7 | import java.util.List; | 8 | import java.util.List; |
8 | import java.util.Map; | ||
9 | import java.util.Objects; | 9 | import java.util.Objects; |
10 | import java.util.Set; | 10 | import java.util.Set; |
11 | 11 | ||
@@ -31,6 +31,8 @@ public abstract class CallLiteral<T extends RelationLike> implements Literal { | |||
31 | return polarity; | 31 | return polarity; |
32 | } | 32 | } |
33 | 33 | ||
34 | public abstract Class<T> getTargetType(); | ||
35 | |||
34 | public T getTarget() { | 36 | public T getTarget() { |
35 | return target; | 37 | return target; |
36 | } | 38 | } |
@@ -46,8 +48,44 @@ public abstract class CallLiteral<T extends RelationLike> implements Literal { | |||
46 | } | 48 | } |
47 | } | 49 | } |
48 | 50 | ||
49 | protected List<Variable> substituteArguments(Map<Variable, Variable> substitution) { | 51 | protected List<Variable> substituteArguments(Substitution substitution) { |
50 | return arguments.stream().map(variable -> DnfUtils.maybeSubstitute(variable, substitution)).toList(); | 52 | return arguments.stream().map(substitution::getSubstitute).toList(); |
53 | } | ||
54 | |||
55 | /** | ||
56 | * Compares the target of this call literal with another object. | ||
57 | * | ||
58 | * @param helper Equality helper for comparing {@link Variable} and {@link tools.refinery.store.query.Dnf} | ||
59 | * instances. | ||
60 | * @param otherTarget The object to compare the target to. | ||
61 | * @return {@code true} if {@code otherTarget} is equal to the return value of {@link #getTarget()} according to | ||
62 | * {@code helper}, {@code false} otherwise. | ||
63 | */ | ||
64 | protected boolean targetEquals(LiteralEqualityHelper helper, T otherTarget) { | ||
65 | return target.equals(otherTarget); | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) { | ||
70 | if (other.getClass() != getClass()) { | ||
71 | return false; | ||
72 | } | ||
73 | var otherCallLiteral = (CallLiteral<?>) other; | ||
74 | if (getTargetType() != otherCallLiteral.getTargetType() || polarity != otherCallLiteral.polarity) { | ||
75 | return false; | ||
76 | } | ||
77 | var arity = arguments.size(); | ||
78 | if (arity != otherCallLiteral.arguments.size()) { | ||
79 | return false; | ||
80 | } | ||
81 | for (int i = 0; i < arity; i++) { | ||
82 | if (!helper.variableEqual(arguments.get(i), otherCallLiteral.arguments.get(i))) { | ||
83 | return false; | ||
84 | } | ||
85 | } | ||
86 | @SuppressWarnings("unchecked") | ||
87 | var otherTarget = (T) otherCallLiteral.target; | ||
88 | return targetEquals(helper, otherTarget); | ||
51 | } | 89 | } |
52 | 90 | ||
53 | @Override | 91 | @Override |
@@ -63,4 +101,33 @@ public abstract class CallLiteral<T extends RelationLike> implements Literal { | |||
63 | public int hashCode() { | 101 | public int hashCode() { |
64 | return Objects.hash(polarity, target, arguments); | 102 | return Objects.hash(polarity, target, arguments); |
65 | } | 103 | } |
104 | |||
105 | protected String targetToString() { | ||
106 | return "@%s %s".formatted(getTargetType().getSimpleName(), target.name()); | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public String toString() { | ||
111 | var builder = new StringBuilder(); | ||
112 | if (!polarity.isPositive()) { | ||
113 | builder.append("!("); | ||
114 | } | ||
115 | builder.append(targetToString()); | ||
116 | if (polarity.isTransitive()) { | ||
117 | builder.append("+"); | ||
118 | } | ||
119 | builder.append("("); | ||
120 | var argumentIterator = arguments.iterator(); | ||
121 | if (argumentIterator.hasNext()) { | ||
122 | builder.append(argumentIterator.next()); | ||
123 | while (argumentIterator.hasNext()) { | ||
124 | builder.append(", ").append(argumentIterator.next()); | ||
125 | } | ||
126 | } | ||
127 | builder.append(")"); | ||
128 | if (!polarity.isPositive()) { | ||
129 | builder.append(")"); | ||
130 | } | ||
131 | return builder.toString(); | ||
132 | } | ||
66 | } | 133 | } |