-
Notifications
You must be signed in to change notification settings - Fork 0
/
OntPersonality.java
315 lines (280 loc) · 11.2 KB
/
OntPersonality.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.github.sszuev.jena.ontapi.common;
import com.github.sszuev.jena.ontapi.OntJenaException;
import com.github.sszuev.jena.ontapi.model.OntAnnotationProperty;
import com.github.sszuev.jena.ontapi.model.OntClass;
import com.github.sszuev.jena.ontapi.model.OntDataProperty;
import com.github.sszuev.jena.ontapi.model.OntDataRange;
import com.github.sszuev.jena.ontapi.model.OntEntity;
import com.github.sszuev.jena.ontapi.model.OntIndividual;
import com.github.sszuev.jena.ontapi.model.OntModel;
import com.github.sszuev.jena.ontapi.model.OntObject;
import com.github.sszuev.jena.ontapi.model.OntObjectProperty;
import com.github.sszuev.jena.ontapi.model.OntProperty;
import com.github.sszuev.jena.ontapi.vocabulary.OWL;
import com.github.sszuev.jena.ontapi.vocabulary.RDF;
import org.apache.jena.enhanced.EnhGraph;
import org.apache.jena.enhanced.Personality;
import org.apache.jena.graph.Node;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A {@link OntModel Ontology RDF Model} configuration object
* that serves for the following purposes:
* <ul>
* <li>Defines a set of permitted mappings from [interface] Class objects
* to {@link EnhNodeFactory OWL Ontology Object factory}
* that can generate instances of the facet represented by the Class.</li>
* <li>Defines a set of builtin {@link OntEntity OWL entities}</li>
* <li>Defines a set of OWL punnings</li>
* <li>Defines a set of reserved {@link Resource}s and {@link Property}s, that cannot be used as OWL Entities</li>
* </ul>
* <b>NOTE</b>: The instance of this interface must also extend {@link Personality Jena Personality}
* with a generic type {@link RDFNode RDFNode}.
* <p>
* Instances of this class must be unmodifiable and
* the {@link OntObjectPersonalityBuilder builder} should be used to create instances with different settings.
* <p>
* Created by @szz on 15.01.2019.
*/
@SuppressWarnings("unused")
public interface OntPersonality {
/**
* Represents the given {@code OntPersonality} configuration as
* a {@link Personality Jena Personality} with a generic type {@link RDFNode}.
*
* @param p {@link OntPersonality}
* @return {@link Personality}
* @throws OntJenaException in case the conversion is not possible
* @see EnhNodeFactory#asJenaImplementation(EnhNodeFactory)
* @see OntEnhGraph#asPersonalityModel(EnhGraph)
*/
@SuppressWarnings("unchecked")
static Personality<RDFNode> asJenaPersonality(OntPersonality p) throws OntJenaException {
if (p instanceof Personality) {
return (Personality<RDFNode>) p;
}
throw new OntJenaException.IllegalArgument("The given OntPersonality is not an instance of Jena Personality.");
}
/**
* Lists all object-types, which are supported by this personality configuration.
* Each of the class-types are associated with either {@link EnhNodeFactory} (if it is {@link OntObject} type)
* or with {@link org.apache.jena.enhanced.Implementation} (if it is a standard jena resource object).
*
* @return Stream of {@code Class}es, subclasses of {@link RDFNode}.
*/
Stream<Class<? extends RDFNode>> types();
/**
* Gets the implementation-factory for the specified object type,
* returning {@code null} if there isn't one available.
*
* @param type a class-type of {@link OntObject}
* @return {@link EnhNodeFactory} a factory to create an instance of the given type, not {@code null}
*/
EnhNodeFactory getObjectFactory(Class<? extends RDFNode> type);
/**
* Answers if a type is supported.
*
* @param type {@code Class}
* @return {@code boolean}
*/
boolean supports(Class<? extends RDFNode> type);
/**
* Makes a full copy of this configuration.
*
* @return {@link OntPersonality} a new instance identical to this
*/
OntPersonality copy();
/**
* Returns personality name, language profile.
*
* @return String
*/
String getName();
/**
* Returns the config.
* Config holds various settings to control model behaviour.
*
* @return {@link OntConfig}
*/
OntConfig getConfig();
/**
* Returns a punnings' vocabulary.
*
* @return {@link Punnings}
*/
Punnings getPunnings();
/**
* Returns a builtins vocabulary.
*
* @return {@link Builtins}
*/
Builtins getBuiltins();
/**
* Returns a reserved vocabulary.
*
* @return {@link Reserved}
*/
Reserved getReserved();
/**
* Gets system resources for the specified type.
* An entity of the given {@code type} cannot be created with any of the URIs, which this method returns.
*
* @param type {@link OntObject}
* @return Set of URIs
*/
Set<String> forbidden(Class<? extends OntObject> type);
/**
* Lists all object-types encapsulated by this config, that extend the specified object-type.
*
* @param type {@code Class}-type of {@link OntObject}
* @param <T> any subtype of {@link OntObject}
* @return Stream of all types where each element extends {@link T} inclusive
*/
@SuppressWarnings("unchecked")
default <T extends OntObject> Stream<Class<? extends T>> types(Class<T> type) {
Objects.requireNonNull(type);
return types()
.filter(c -> c == type || Arrays.stream(c.getInterfaces())
.anyMatch(type::isAssignableFrom))
.map(x -> (Class<? extends T>) x);
}
/**
* A punnings' vocabulary.
* For a given {@link OntEntity} type it returns a {@code Set} of forbidden types
* (the right part of SPO with the (@code rdf:type) predicate).
* A {@link OntModel model}, that holds this configuration,
* cannot contain entities which have intersection in {@link RDF#type rdf:type}
* that are determined by this vocabulary.
* <p>
* For example, for the {@link PunningsMode#DL_WEAK} and for the {@link PunningsMode#DL2}
* configurations, the expression {@code voc.get(OntClass.class)}, where {@code voc} is an instance of this class,
* should return a {@code Set}
* containing {@link org.apache.jena.vocabulary.RDFS#Datatype rdfs:Datatype} in the form of {@link Node},
* since {@code OntDT <-> OntClass} is illegal punning.
* <p>
* Each node obtained from this class must be IRI (i.e. {@code node.isURI() = true}).
*
* @see <a href='https://www.w3.org/TR/owl2-new-features/#F12:_Punning'>Punnings</a>
* @see OntEntity#types()
*/
interface Punnings extends ResourceVocabulary<OntObject> {
default Set<Node> getNamedClasses() {
return get(OntClass.Named.class);
}
default Set<Node> getDatatypes() {
return get(OntDataRange.Named.class);
}
default Set<Node> getObjectProperties() {
return get(OntObjectProperty.Named.class);
}
default Set<Node> getDatatypeProperties() {
return get(OntDataProperty.class);
}
default Set<Node> getAnnotationProperties() {
return get(OntAnnotationProperty.class);
}
default Set<Node> getNamedIndividuals() {
return get(OntIndividual.Named.class);
}
}
/**
* A vocabulary of reserved IRIs.
* A {@link OntModel model}, that holds this configuration,
* cannot contain entities with the IRIs from this vocabulary.
* <p>
* Example of such a forbidden {@link Property} returned by this vocabulary is
* {@link OWL#sameAs owl:sameAs},
* since it is used by a model to build individual equality and, therefore, it cannot be used in other cases.
* An {@link OntModel ontology model} cannot contain an OWL entity with this IRI.
* All these things are customizable, and vocabularies may contain more or less restrictions.
* <p>
* Each node obtained from this class must be IRI (i.e. {@code node.isURI() = true}).
*/
interface Reserved extends ResourceVocabulary<Resource> {
/**
* Gets all reserved IRIs, which are most likely used as an object in SPO of some schema graph.
*
* @return Set of IRI-{@link Node node}s
*/
default Set<Node> getResources() {
return get(Resource.class);
}
/**
* Gets all reserved IRIs, which are most likely used as a predicate in SPO of some schema graph.
*
* @return Set of IRI-{@link Node node}s
*/
default Set<Node> getProperties() {
return get(Property.class);
}
/**
* Resources + Properties
* @return Set of IRI-{@link Node node}s
*/
default Set<Node> getAllResources() {
return Stream.of(getResources(), getProperties()).flatMap(Collection::stream).collect(Collectors.toUnmodifiableSet());
}
}
/**
* A vocabulary of built-in {@link OntEntity ONT Entities}.
* A {@link OntModel model}, that holds this configuration,
* can contain entities without explicit declarations, if their IRIs are determined by this vocabulary.
* <p>
* For example, the OWL standard vocabulary determines
* {@link OWL#Thing owl:Thing} as a built-in OWL class.
* To describe this case the expression {@code voc.get(OntClass.class)},
* where {@code voc} is an instance of this class,
* should return a {@code Set} containing {@code owl:Thing} in the form of {@link Node}.
* <p>
* Each node obtained from this class must be IRI (i.e. {@code node.isURI() = true}).
*
* @see OntEntity#types()
*/
interface Builtins extends ResourceVocabulary<OntObject> {
default Set<Class<? extends OntObject>> supportedTypes() {
return Set.of(
OntClass.Named.class,
OntDataRange.Named.class,
OntObjectProperty.Named.class,
OntDataProperty.class,
OntAnnotationProperty.class,
OntProperty.class,
OntEntity.class
);
}
default Set<Node> getNamedClasses() {
return get(OntClass.Named.class);
}
default Set<Node> getDatatypes() {
return get(OntDataRange.Named.class);
}
default Set<Node> getObjectProperties() {
return get(OntObjectProperty.Named.class);
}
default Set<Node> getDatatypeProperties() {
return get(OntDataProperty.class);
}
default Set<Node> getAnnotationProperties() {
return get(OntAnnotationProperty.class);
}
default Set<Node> getNamedIndividuals() {
return get(OntIndividual.Named.class);
}
/**
* Returns a {@code Set} of all OWL builtin properties
* (annotation, datatype and object named property expressions)
*
* @return Set of IRI-{@link Node node}s
*/
default Set<Node> getOntProperties() {
return get(OntProperty.class);
}
}
}