-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathNegation.java
122 lines (103 loc) · 3.96 KB
/
Negation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.typeql.lang.pattern;
import com.typeql.lang.common.TypeQLToken;
import com.typeql.lang.common.TypeQLVariable;
import com.typeql.lang.common.exception.ErrorMessage;
import com.typeql.lang.common.exception.TypeQLException;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static com.typedb.common.collection.Collections.list;
import static com.typeql.lang.common.TypeQLToken.Char.CURLY_CLOSE;
import static com.typeql.lang.common.TypeQLToken.Char.CURLY_OPEN;
import static com.typeql.lang.common.TypeQLToken.Char.NEW_LINE;
import static com.typeql.lang.common.TypeQLToken.Char.SEMICOLON;
import static com.typeql.lang.common.TypeQLToken.Char.SPACE;
import static com.typeql.lang.common.util.Strings.indent;
/**
* A class representing a negation of patterns. All inner patterns must not match in a query.
*
* @param <T> the type of patterns in this negation
*/
public class Negation<T extends Pattern> implements Conjunctable {
private final T pattern;
private Negation<Disjunction<Conjunction<Conjunctable>>> normalised;
public Negation(T pattern) {
if (pattern == null) throw new NullPointerException("Null patterns");
else if (pattern.isNegation()) throw TypeQLException.of(ErrorMessage.REDUNDANT_NESTED_NEGATION);
this.pattern = pattern;
}
public T pattern() {
return pattern;
}
@Override
public List<? extends Pattern> patterns() {
return list(pattern);
}
@Override
public void validateIsBoundedBy(Set<TypeQLVariable> bounds) {
if (pattern.isNegation()) {
throw TypeQLException.of(ErrorMessage.ILLEGAL_STATE);
} else {
pattern.validateIsBoundedBy(bounds);
}
}
@Override
public Negation<Disjunction<Conjunction<Conjunctable>>> normalise() {
if (normalised == null) {
if (pattern.isNegation()) {
throw TypeQLException.of(ErrorMessage.ILLEGAL_STATE);
} else if (pattern.isStatement()) {
normalised = new Negation<>(new Disjunction<>(list(new Conjunction<>(list(pattern.asStatement())))));
} else {
if (pattern.isConjunction()) normalised = new Negation<>(pattern.asConjunction().normalise());
else normalised = new Negation<>(pattern.asDisjunction().normalise());
}
}
return normalised;
}
@Override
public boolean isNegation() {
return true;
}
@Override
public Negation<?> asNegation() {
return this;
}
@Override
public String toString(boolean pretty) {
StringBuilder negation = new StringBuilder();
negation.append(TypeQLToken.Operator.NOT).append(SPACE);
if (pattern.isConjunction()) {
negation.append(pattern.toString(pretty));
} else if (pattern.toString(pretty).lines().count() > 1) {
negation.append(CURLY_OPEN);
if (pretty) {
negation.append(NEW_LINE).append(indent(pattern.toString(pretty) + SEMICOLON)).append(NEW_LINE);
} else {
negation.append(pattern.toString(pretty)).append(SEMICOLON);
}
negation.append(CURLY_CLOSE);
} else {
negation.append(CURLY_OPEN).append(SPACE);
negation.append(pattern).append(SEMICOLON);
negation.append(SPACE).append(CURLY_CLOSE);
}
return negation.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Negation<?> negation = (Negation<?>) o;
return pattern.equals(negation.pattern);
}
@Override
public int hashCode() {
return pattern.hashCode();
}
}