14
14
15
15
package com .github .owlcs .ontapi .internal .axioms ;
16
16
17
- import org .apache .jena .rdf .model .Property ;
18
- import org .apache .jena .rdf .model .Resource ;
19
- import org .semanticweb .owlapi .model .OWLDataPropertyExpression ;
20
- import org .semanticweb .owlapi .model .OWLDisjointDataPropertiesAxiom ;
17
+ import com .github .owlcs .ontapi .internal .InternalCache ;
21
18
import com .github .owlcs .ontapi .internal .InternalConfig ;
22
19
import com .github .owlcs .ontapi .internal .InternalObjectFactory ;
23
20
import com .github .owlcs .ontapi .internal .ONTObject ;
21
+ import com .github .owlcs .ontapi .internal .objects .FactoryAccessor ;
22
+ import com .github .owlcs .ontapi .internal .objects .ONTEntityImpl ;
23
+ import com .github .owlcs .ontapi .internal .objects .ONTStatementImpl ;
24
24
import com .github .owlcs .ontapi .jena .model .OntDisjoint ;
25
+ import com .github .owlcs .ontapi .jena .model .OntGraphModel ;
25
26
import com .github .owlcs .ontapi .jena .model .OntNDP ;
26
27
import com .github .owlcs .ontapi .jena .model .OntStatement ;
28
+ import com .github .owlcs .ontapi .jena .utils .OntModels ;
27
29
import com .github .owlcs .ontapi .jena .vocabulary .OWL ;
30
+ import org .apache .jena .graph .FrontsTriple ;
31
+ import org .apache .jena .graph .Triple ;
32
+ import org .apache .jena .rdf .model .Property ;
33
+ import org .apache .jena .rdf .model .Resource ;
34
+ import org .apache .jena .util .iterator .ExtendedIterator ;
35
+ import org .semanticweb .owlapi .model .*;
36
+
37
+ import java .util .Collection ;
38
+ import java .util .Set ;
39
+ import java .util .function .BiFunction ;
40
+ import java .util .function .Supplier ;
41
+ import java .util .stream .Stream ;
28
42
29
43
/**
30
- * see {@link AbstractTwoWayNaryTranslator}
31
- * examples :
44
+ * A translator that provides {@link OWLDisjointDataPropertiesAxiom} implementations.
45
+ * Examples :
32
46
* <ul>
33
47
* <li>{@code :objProperty1 owl:propertyDisjointWith :objProperty2}</li>
34
48
* <li>{@code [ rdf:type owl:AllDisjointProperties; owl:members ( :objProperty1 :objProperty2 :objProperty3 ) ]}</li>
35
49
* </ul>
36
50
* <p>
37
51
* Created by szuev on 12.10.2016.
38
- *
39
- * @see OWLDisjointDataPropertiesAxiom
40
52
*/
41
- public class DisjointDataPropertiesTranslator extends AbstractTwoWayNaryTranslator <OWLDisjointDataPropertiesAxiom , OWLDataPropertyExpression , OntNDP > {
53
+ public class DisjointDataPropertiesTranslator
54
+ extends AbstractTwoWayNaryTranslator <OWLDisjointDataPropertiesAxiom , OWLDataPropertyExpression , OntNDP > {
55
+
56
+ private static final Property PREDICATE = OWL .propertyDisjointWith ;
57
+
42
58
@ Override
43
59
Property getPredicate () {
44
- return OWL . propertyDisjointWith ;
60
+ return PREDICATE ;
45
61
}
46
62
47
63
@ Override
@@ -66,11 +82,211 @@ Class<OntDisjoint.DataProperties> getDisjointView() {
66
82
67
83
@ Override
68
84
public ONTObject <OWLDisjointDataPropertiesAxiom > toAxiom (OntStatement statement ,
69
- InternalObjectFactory reader ,
85
+ Supplier <OntGraphModel > model ,
86
+ InternalObjectFactory factory ,
87
+ InternalConfig config ) {
88
+ return AxiomImpl .create (statement , model , factory , config );
89
+ }
90
+
91
+ @ Override
92
+ public ONTObject <OWLDisjointDataPropertiesAxiom > toAxiom (OntStatement statement ,
93
+ InternalObjectFactory factory ,
70
94
InternalConfig config ) {
71
- return makeAxiom (statement , reader .getAnnotations (statement , config ),
72
- reader ::getProperty ,
73
- (members , annotations ) -> reader .getOWLDataFactory ()
95
+ return makeAxiom (statement , factory .getAnnotations (statement , config ),
96
+ factory ::getProperty ,
97
+ (members , annotations ) -> factory .getOWLDataFactory ()
74
98
.getOWLDisjointDataPropertiesAxiom (ONTObject .toSet (members ), ONTObject .toSet (annotations )));
75
99
}
100
+
101
+ /**
102
+ * @see com.github.owlcs.ontapi.owlapi.axioms.OWLDisjointDataPropertiesAxiomImpl
103
+ */
104
+ public abstract static class AxiomImpl extends DataPropertyNaryAxiomImpl <OWLDisjointDataPropertiesAxiom >
105
+ implements OWLDisjointDataPropertiesAxiom {
106
+
107
+ protected AxiomImpl (Triple t , Supplier <OntGraphModel > m ) {
108
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
109
+ }
110
+
111
+ protected AxiomImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
112
+ super (s , p , o , m );
113
+ }
114
+
115
+ /**
116
+ * Creates an {@link ONTObject} container, that is also {@link OWLDisjointDataPropertiesAxiom}.
117
+ *
118
+ * @param statement {@link OntStatement}, not {@code null}
119
+ * @param model {@link OntGraphModel} provider, not {@code null}
120
+ * @param factory {@link InternalObjectFactory}, not {@code null}
121
+ * @param config {@link InternalConfig}, not {@code null}
122
+ * @return {@link AxiomImpl}
123
+ */
124
+ public static AxiomImpl create (OntStatement statement ,
125
+ Supplier <OntGraphModel > model ,
126
+ InternalObjectFactory factory ,
127
+ InternalConfig config ) {
128
+ if (PREDICATE .equals (statement .getPredicate ())) {
129
+ return WithManyObjects .create (statement , model ,
130
+ SimpleImpl .FACTORY , ComplexImpl .FACTORY , SET_HASH_CODE , factory , config );
131
+ }
132
+ return WithManyObjects .create (statement , model , ComplexImpl .FACTORY , SET_HASH_CODE , factory , config );
133
+ }
134
+
135
+ @ Override
136
+ public ExtendedIterator <ONTObject <? extends OWLDataPropertyExpression >> listONTComponents (OntStatement statement ,
137
+ InternalObjectFactory factory ) {
138
+ if (PREDICATE .equals (statement .getPredicate ())) {
139
+ return super .listONTComponents (statement , factory );
140
+ }
141
+ return OntModels .listMembers (statement .getSubject (OntDisjoint .DataProperties .class ).getList ())
142
+ .mapWith (factory ::getProperty );
143
+ }
144
+
145
+ @ FactoryAccessor
146
+ @ Override
147
+ protected OWLDisjointDataPropertiesAxiom createAxiom (Collection <OWLDataPropertyExpression > members ,
148
+ Collection <OWLAnnotation > annotations ) {
149
+ return getDataFactory ().getOWLDisjointDataPropertiesAxiom (members ,
150
+ annotations == null ? NO_ANNOTATIONS : annotations );
151
+ }
152
+
153
+ /**
154
+ * An {@link OWLDisjointDataPropertiesAxiom}
155
+ * that is based on a single unannotated triple {@code s owl:propertyDisjointWith o},
156
+ * where {@code s} and {@code o} are data properties (URI {@link Resource}s).
157
+ */
158
+ protected static class SimpleImpl extends AxiomImpl implements Simple <OWLDataPropertyExpression > {
159
+
160
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, SimpleImpl > FACTORY = SimpleImpl ::new ;
161
+
162
+ protected SimpleImpl (Triple t , Supplier <OntGraphModel > m ) {
163
+ super (t , m );
164
+ }
165
+
166
+ protected SimpleImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
167
+ super (s , p , o , m );
168
+ }
169
+
170
+ @ Override
171
+ protected long count () {
172
+ return 2 ;
173
+ }
174
+
175
+ @ Override
176
+ protected boolean sameContent (ONTStatementImpl other ) {
177
+ return other instanceof SimpleImpl && isReverseTriple ((SimpleImpl ) other );
178
+ }
179
+
180
+ @ Override
181
+ protected AxiomImpl makeCopyWith (ONTObject <OWLDisjointDataPropertiesAxiom > other ) {
182
+ if (other instanceof SimpleImpl ) {
183
+ Triple t = ((SimpleImpl ) other ).asTriple ();
184
+ return new SimpleImpl (subject , predicate , object , model ) {
185
+
186
+ @ Override
187
+ public Stream <Triple > triples () {
188
+ return Stream .concat (SimpleImpl .this .triples (), Stream .of (t ));
189
+ }
190
+ };
191
+ }
192
+ return new SimpleImpl (subject , predicate , object , model ) {
193
+ @ Override
194
+ public Stream <Triple > triples () {
195
+ return Stream .concat (SimpleImpl .this .triples (), other .triples ());
196
+ }
197
+ };
198
+ }
199
+
200
+ @ SuppressWarnings ("unchecked" )
201
+ @ Override
202
+ public Set <OWLDataProperty > getDataPropertySet () {
203
+ return (Set <OWLDataProperty >) getOWLComponentsAsSet ();
204
+ }
205
+
206
+ @ SuppressWarnings ("unchecked" )
207
+ @ Override
208
+ public Set <OWLEntity > getSignatureSet () {
209
+ return (Set <OWLEntity >) getOWLComponentsAsSet ();
210
+ }
211
+
212
+ @ Override
213
+ public boolean containsDataProperty (OWLDataProperty property ) {
214
+ return hasURIResource (ONTEntityImpl .getURI (property ));
215
+ }
216
+ }
217
+
218
+ /**
219
+ * An {@link OWLDisjointDataPropertiesAxiom}
220
+ * that either has annotations or it is based on {@link OntDisjoint.DataProperties} resource.
221
+ * It has a public constructor since it is more generic then {@link SimpleImpl}.
222
+ */
223
+ public static class ComplexImpl extends AxiomImpl implements Complex <ComplexImpl , OWLDataPropertyExpression > {
224
+
225
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, ComplexImpl > FACTORY = ComplexImpl ::new ;
226
+ protected final InternalCache .Loading <ComplexImpl , Object []> content ;
227
+
228
+ public ComplexImpl (Triple t , Supplier <OntGraphModel > m ) {
229
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
230
+ }
231
+
232
+ protected ComplexImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
233
+ super (s , p , o , m );
234
+ this .content = createContentCache ();
235
+ }
236
+
237
+ @ Override
238
+ protected long count () {
239
+ return isSimple () ? 2 : members ().count ();
240
+ }
241
+
242
+ boolean isSimple () {
243
+ return predicate .equals (PREDICATE .getURI ());
244
+ }
245
+
246
+ protected OntDisjoint .DataProperties asResource () {
247
+ return getPersonalityModel ().getNodeAs (getSubjectNode (), OntDisjoint .DataProperties .class );
248
+ }
249
+
250
+ @ Override
251
+ public Stream <Triple > triples () {
252
+ if (isSimple ()) {
253
+ return super .triples ();
254
+ }
255
+ return Stream .concat (asResource ().spec ().map (FrontsTriple ::asTriple ),
256
+ objects ().flatMap (ONTObject ::triples ));
257
+ }
258
+
259
+ @ Override
260
+ public OntStatement asStatement () {
261
+ if (isSimple ()) {
262
+ return super .asStatement ();
263
+ }
264
+ return asResource ().getRoot ();
265
+ }
266
+
267
+ @ Override
268
+ public InternalCache .Loading <ComplexImpl , Object []> getContentCache () {
269
+ return content ;
270
+ }
271
+
272
+ @ Override
273
+ protected boolean sameContent (ONTStatementImpl other ) {
274
+ return testSameContent (other );
275
+ }
276
+
277
+ @ Override
278
+ protected ComplexImpl makeCopyWith (ONTObject <OWLDisjointDataPropertiesAxiom > other ) {
279
+ ComplexImpl res = new ComplexImpl (subject , predicate , object , model ) {
280
+ @ Override
281
+ public Stream <Triple > triples () {
282
+ return Stream .concat (ComplexImpl .this .triples (), other .triples ());
283
+ }
284
+ };
285
+ if (hasContent ()) {
286
+ res .putContent (getContent ());
287
+ }
288
+ return res ;
289
+ }
290
+ }
291
+ }
76
292
}
0 commit comments