-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPattern.java
91 lines (70 loc) · 2.91 KB
/
Pattern.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
/*
* 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.TypeQLVariable;
import com.typeql.lang.common.exception.TypeQLException;
import com.typeql.lang.pattern.statement.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import static com.typedb.common.util.Objects.className;
import static com.typeql.lang.common.exception.ErrorMessage.INVALID_CASTING;
import static com.typeql.lang.common.exception.ErrorMessage.VARIABLE_NAME_CONFLICT;
public interface Pattern {
Pattern normalise();
List<? extends Pattern> patterns();
void validateIsBoundedBy(Set<TypeQLVariable> bounds);
default boolean isStatement() {
return false;
}
default boolean isConjunction() {
return false;
}
default boolean isDisjunction() {
return false;
}
default boolean isNegation() {
return false;
}
default boolean isConjunctable() {
return false;
}
default Statement asStatement() {
throw TypeQLException.of(INVALID_CASTING.message(className(this.getClass()), className(Statement.class)));
}
default Conjunction<? extends Pattern> asConjunction() {
throw TypeQLException.of(INVALID_CASTING.message(className(this.getClass()), className(Conjunction.class)));
}
default Disjunction<? extends Pattern> asDisjunction() {
throw TypeQLException.of(INVALID_CASTING.message(className(this.getClass()), className(Disjunction.class)));
}
default Negation<? extends Pattern> asNegation() {
throw TypeQLException.of(INVALID_CASTING.message(className(this.getClass()), className(Negation.class)));
}
default Conjunctable asConjunctable() {
throw TypeQLException.of(INVALID_CASTING.message(className(this.getClass()), className(Negation.class)));
}
static void validateNamesUnique(Stream<? extends Pattern> patterns) {
Set<String> conceptNames = new HashSet<>();
Set<String> valueNames = new HashSet<>();
patterns.flatMap(p -> {
if (p.isStatement()) return Stream.of(p);
else if (p.isConjunction()) return p.asConjunction().statements();
else return Stream.empty();
}).filter(Pattern::isStatement).flatMap(p -> p.asStatement().variables()).forEach(v -> {
if (v.isNamedValue()) valueNames.add(v.name());
else if (v.isNamedConcept()) conceptNames.add(v.name());
});
conceptNames.retainAll(valueNames);
if (!conceptNames.isEmpty()) {
throw TypeQLException.of(VARIABLE_NAME_CONFLICT.message(String.join(",", conceptNames)));
}
}
@Override
String toString();
String toString(boolean pretty);
}