-
Notifications
You must be signed in to change notification settings - Fork 14
/
Assertion.java
252 lines (227 loc) · 8.85 KB
/
Assertion.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.model;
import java.net.URI;
/**
* Base assertion axiom class.
* <p>
* Defines just whether the assertion uses inferred values and existing types of assertions.
* <p>
* The usage of types may seem as not being very object-oriented, but since the hierarchy is fixed (there aren't any
* other kinds of assertions in ontologies) and since the subclasses essentially don't contain any behavior, we can use
* this way.
*/
public abstract class Assertion extends NamedResource {
protected final String language;
private final boolean hasLanguage;
private final boolean inferred;
public enum AssertionType {
/**
* PROPERTY assertion is used in cases where we don't know the property type, for instance when loading value of
* the Properties attribute
*/
CLASS, PROPERTY, OBJECT_PROPERTY, DATA_PROPERTY, ANNOTATION_PROPERTY
}
protected Assertion(URI identifier, boolean isInferred) {
super(identifier);
this.inferred = isInferred;
this.language = null;
this.hasLanguage = false;
}
protected Assertion(URI identifier, String language, boolean isInferred) {
super(identifier);
this.inferred = isInferred;
this.language = language;
// TODO hasLanguage should be false when language is set to null
this.hasLanguage = true;
}
/**
* Whether this assertion is based on inferred values.
*
* @return True if inferred, false otherwise
*/
public boolean isInferred() {
return inferred;
}
/**
* Whether this assertion is a class assertion.
* <p>
* This is a convenience method, its functionality could be emulated by retrieving this assertion's identifier and
* checking whether it equals to the rdf:type URI.
*
* @return True if this assertion is a class assertion, false otherwise.
*/
public boolean isClassAssertion() {
return getIdentifier().equals(ClassAssertion.RDF_TYPE);
}
/**
* Gets type of this assertion.
*
* @return Assertion type
*/
public abstract AssertionType getType();
/**
* Gets the language tag carried by this assertion.
* <p>
* The language tag applies only to string-based literals.
*
* @return Language tag, e.g. {@code en}, can be {@code null}
*/
public String getLanguage() {
return language;
}
/**
* Checks whether a language tag was set for this assertion.
*
* @return {@code true} if a language tag was set, {@code false} otherwise
*/
public boolean hasLanguage() {
return hasLanguage;
}
// TODO Include language in hashCode, equals
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (inferred ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Assertion other = (Assertion) obj;
return inferred == other.inferred;
}
@Override
public String toString() {
return super.toString() + (inferred ? " - inferred" : " - non-inferred");
}
/**
* Creates new class assertion.
* <p>
* Class assertions use the rdf:type identifier.
*
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createClassAssertion(boolean isInferred) {
return new ClassAssertion(isInferred);
}
/**
* Creates a property assertion without specifying the assertion identifier.
* <p>
* Note that the returned instances have the same identifier throughout one JVM - a randomly generated URI.
*
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createUnspecifiedPropertyAssertion(boolean isInferred) {
return new PropertyAssertion(isInferred);
}
/**
* Creates a property assertion without specifying the assertion identifier.
* <p>
* Note that the returned instances have the same identifier throughout one JVM - a randomly generated URI.
*
* @param language Language tag, optional
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createUnspecifiedPropertyAssertion(String language, boolean isInferred) {
return new PropertyAssertion(language, isInferred);
}
/**
* Creates new property assertion without specifying what kind of property it is.
*
* @param assertionIdentifier Assertion identifier
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createPropertyAssertion(URI assertionIdentifier, boolean isInferred) {
return new PropertyAssertion(assertionIdentifier, isInferred);
}
/**
* Creates new property assertion without specifying what kind of property it is.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createPropertyAssertion(URI assertionIdentifier, String language, boolean isInferred) {
return new PropertyAssertion(assertionIdentifier, language, isInferred);
}
/**
* Creates new object property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createObjectPropertyAssertion(URI assertionIdentifier, boolean isInferred) {
return new ObjectPropertyAssertion(assertionIdentifier, isInferred);
}
/**
* Creates new data property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createDataPropertyAssertion(URI assertionIdentifier, boolean isInferred) {
return new DataPropertyAssertion(assertionIdentifier, isInferred);
}
/**
* Creates new data property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createDataPropertyAssertion(URI assertionIdentifier, String language, boolean isInferred) {
return new DataPropertyAssertion(assertionIdentifier, language, isInferred);
}
/**
* Creates new annotation property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createAnnotationPropertyAssertion(URI assertionIdentifier, boolean isInferred) {
return new AnnotationPropertyAssertion(assertionIdentifier, isInferred);
}
/**
* Creates new annotation property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
public static Assertion createAnnotationPropertyAssertion(URI assertionIdentifier, String language,
boolean isInferred) {
return new AnnotationPropertyAssertion(assertionIdentifier, language, isInferred);
}
}