14
14
15
15
package com .github .owlcs .ontapi .internal .axioms ;
16
16
17
- import org .apache .jena .rdf .model .Property ;
18
- import org .semanticweb .owlapi .model .OWLAnnotation ;
19
- import org .semanticweb .owlapi .model .OWLIndividual ;
20
- import org .semanticweb .owlapi .model .OWLSameIndividualAxiom ;
21
- import com .github .owlcs .ontapi .internal .InternalConfig ;
22
- import com .github .owlcs .ontapi .internal .InternalObjectFactory ;
23
- import com .github .owlcs .ontapi .internal .ONTObject ;
24
- import com .github .owlcs .ontapi .internal .ONTWrapperImpl ;
17
+ import com .github .owlcs .ontapi .DataFactory ;
18
+ import com .github .owlcs .ontapi .internal .*;
19
+ import com .github .owlcs .ontapi .internal .objects .FactoryAccessor ;
20
+ import com .github .owlcs .ontapi .internal .objects .ONTEntityImpl ;
21
+ import com .github .owlcs .ontapi .internal .objects .ONTStatementImpl ;
22
+ import com .github .owlcs .ontapi .jena .model .OntGraphModel ;
25
23
import com .github .owlcs .ontapi .jena .model .OntIndividual ;
26
24
import com .github .owlcs .ontapi .jena .model .OntStatement ;
27
25
import com .github .owlcs .ontapi .jena .vocabulary .OWL ;
26
+ import org .apache .jena .graph .Triple ;
27
+ import org .apache .jena .rdf .model .Property ;
28
+ import org .semanticweb .owlapi .model .*;
28
29
30
+ import java .util .Arrays ;
29
31
import java .util .Collection ;
32
+ import java .util .Set ;
33
+ import java .util .function .BiFunction ;
34
+ import java .util .function .Supplier ;
35
+ import java .util .stream .Collectors ;
36
+ import java .util .stream .Stream ;
30
37
31
38
/**
32
- * base class {@link AbstractNaryTranslator}
33
- * example :
39
+ * A translator that provides {@link OWLSameIndividualAxiom} implementations.
40
+ * Example :
34
41
* <pre>{@code
35
42
* :indi1 owl:sameAs :indi2, :indi3 .
36
43
* }</pre>
39
46
*
40
47
* @see OWLSameIndividualAxiom
41
48
*/
42
- public class SameIndividualTranslator extends AbstractNaryTranslator <OWLSameIndividualAxiom , OWLIndividual , OntIndividual > {
49
+ public class SameIndividualTranslator
50
+ extends AbstractNaryTranslator <OWLSameIndividualAxiom , OWLIndividual , OntIndividual > {
43
51
44
52
@ Override
45
53
public Property getPredicate () {
@@ -53,14 +61,197 @@ Class<OntIndividual> getView() {
53
61
54
62
@ Override
55
63
public ONTObject <OWLSameIndividualAxiom > toAxiom (OntStatement statement ,
56
- InternalObjectFactory reader ,
64
+ Supplier <OntGraphModel > model ,
65
+ InternalObjectFactory factory ,
57
66
InternalConfig config ) {
58
- ONTObject <? extends OWLIndividual > a = reader .getIndividual (statement .getSubject (getView ()));
59
- ONTObject <? extends OWLIndividual > b = reader .getIndividual (statement .getObject ().as (getView ()));
60
- Collection <ONTObject <OWLAnnotation >> annotations = reader .getAnnotations (statement , config );
61
- OWLSameIndividualAxiom res = reader .getOWLDataFactory ()
67
+ return AxiomImpl .create (statement , model , factory , config );
68
+ }
69
+
70
+ @ Override
71
+ public ONTObject <OWLSameIndividualAxiom > toAxiom (OntStatement statement ,
72
+ InternalObjectFactory factory ,
73
+ InternalConfig config ) {
74
+ ONTObject <? extends OWLIndividual > a = factory .getIndividual (statement .getSubject (getView ()));
75
+ ONTObject <? extends OWLIndividual > b = factory .getIndividual (statement .getObject ().as (getView ()));
76
+ Collection <ONTObject <OWLAnnotation >> annotations = factory .getAnnotations (statement , config );
77
+ OWLSameIndividualAxiom res = factory .getOWLDataFactory ()
62
78
.getOWLSameIndividualAxiom (a .getOWLObject (), b .getOWLObject (), ONTObject .toSet (annotations ));
63
79
return ONTWrapperImpl .create (res , statement ).append (annotations ).append (a ).append (b );
64
80
}
65
81
82
+ /**
83
+ * @see com.github.owlcs.ontapi.owlapi.axioms.OWLSameIndividualAxiomImpl
84
+ */
85
+ public abstract static class AxiomImpl extends IndividualNaryAxiomImpl <OWLSameIndividualAxiom >
86
+ implements OWLSameIndividualAxiom {
87
+
88
+ protected AxiomImpl (Triple t , Supplier <OntGraphModel > m ) {
89
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
90
+ }
91
+
92
+ protected AxiomImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
93
+ super (s , p , o , m );
94
+ }
95
+
96
+ /**
97
+ * Creates an {@link ONTObject} container, that is also {@link OWLSameIndividualAxiom}.
98
+ *
99
+ * @param statement {@link OntStatement}, not {@code null}
100
+ * @param model {@link OntGraphModel} provider, not {@code null}
101
+ * @param factory {@link InternalObjectFactory}, not {@code null}
102
+ * @param config {@link InternalConfig}, not {@code null}
103
+ * @return {@link AxiomImpl}
104
+ */
105
+ public static AxiomImpl create (OntStatement statement ,
106
+ Supplier <OntGraphModel > model ,
107
+ InternalObjectFactory factory ,
108
+ InternalConfig config ) {
109
+ return WithManyObjects .create (statement , model ,
110
+ SimpleImpl .FACTORY , ComplexImpl .FACTORY , SET_HASH_CODE , factory , config );
111
+ }
112
+
113
+ @ Override
114
+ protected final long count () {
115
+ return 2 ;
116
+ }
117
+
118
+ @ Override
119
+ public boolean containsAnonymousIndividuals () {
120
+ return individuals ().anyMatch (OWLIndividual ::isAnonymous );
121
+ }
122
+
123
+ @ FactoryAccessor
124
+ @ Override
125
+ protected OWLSameIndividualAxiom createAxiom (Collection <OWLIndividual > members ,
126
+ Collection <OWLAnnotation > annotations ) {
127
+ return getDataFactory ().getOWLSameIndividualAxiom (members ,
128
+ annotations == null ? NO_ANNOTATIONS : annotations );
129
+ }
130
+
131
+ @ FactoryAccessor
132
+ @ Override
133
+ protected OWLSubClassOfAxiom createSubClassOf (OWLIndividual a , OWLIndividual b ) {
134
+ DataFactory df = getDataFactory ();
135
+ return df .getOWLSubClassOfAxiom (df .getOWLObjectOneOf (a ), df .getOWLObjectOneOf (b ));
136
+ }
137
+
138
+ @ FactoryAccessor
139
+ @ Override
140
+ public Collection <OWLSubClassOfAxiom > asOWLSubClassOfAxioms () { // OWL-API-impl returns a Set here
141
+ return fromPairs ((a , b ) -> createSubClassOf (eraseModel (a ), eraseModel (b ))).collect (Collectors .toSet ());
142
+ }
143
+
144
+ /**
145
+ * An {@link OWLSameIndividualAxiom} that has named classes as subject and object and has no annotations.
146
+ */
147
+ protected static class SimpleImpl extends AxiomImpl implements Simple <OWLIndividual > {
148
+
149
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, SimpleImpl > FACTORY = SimpleImpl ::new ;
150
+
151
+ protected SimpleImpl (Triple t , Supplier <OntGraphModel > m ) {
152
+ super (t , m );
153
+ }
154
+
155
+ protected SimpleImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
156
+ super (s , p , o , m );
157
+ }
158
+
159
+ @ Override
160
+ protected boolean sameContent (ONTStatementImpl other ) {
161
+ return other instanceof SimpleImpl && isReverseTriple ((SimpleImpl ) other );
162
+ }
163
+
164
+ @ Override
165
+ protected AxiomImpl makeCopyWith (ONTObject <OWLSameIndividualAxiom > other ) {
166
+ if (other instanceof SimpleImpl ) {
167
+ Triple t = ((SimpleImpl ) other ).asTriple ();
168
+ return new SimpleImpl (subject , predicate , object , model ) {
169
+
170
+ @ Override
171
+ public Stream <Triple > triples () {
172
+ return Stream .concat (SimpleImpl .this .triples (), Stream .of (t ));
173
+ }
174
+ };
175
+ }
176
+ return new SimpleImpl (subject , predicate , object , model ) {
177
+ @ Override
178
+ public Stream <Triple > triples () {
179
+ return Stream .concat (SimpleImpl .this .triples (), other .triples ());
180
+ }
181
+ };
182
+ }
183
+
184
+ @ Override
185
+ public boolean containsAnonymousIndividuals () {
186
+ return false ;
187
+ }
188
+
189
+ @ SuppressWarnings ("unchecked" )
190
+ @ Override
191
+ public Set <OWLNamedIndividual > getNamedIndividualSet () {
192
+ return (Set <OWLNamedIndividual >) getOWLComponentsAsSet ();
193
+ }
194
+
195
+ @ SuppressWarnings ("unchecked" )
196
+ @ Override
197
+ public Set <OWLEntity > getSignatureSet () {
198
+ return (Set <OWLEntity >) getOWLComponentsAsSet ();
199
+ }
200
+
201
+ @ Override
202
+ public boolean containsNamedIndividual (OWLNamedIndividual individual ) {
203
+ return hasURIResource (ONTEntityImpl .getURI (individual ));
204
+ }
205
+
206
+ @ Override
207
+ public boolean canContainAnonymousIndividuals () {
208
+ return false ;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * An {@link OWLSameIndividualAxiom}
214
+ * that either has annotations or anonymous class expressions in subject or object positions.
215
+ * It has a public constructor since it is more generic then {@link SimpleImpl}.
216
+ */
217
+ public static class ComplexImpl extends AxiomImpl
218
+ implements Complex <ComplexImpl , OWLIndividual > {
219
+
220
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, ComplexImpl > FACTORY = ComplexImpl ::new ;
221
+ protected final InternalCache .Loading <ComplexImpl , Object []> content ;
222
+
223
+ public ComplexImpl (Triple t , Supplier <OntGraphModel > m ) {
224
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
225
+ }
226
+
227
+ protected ComplexImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
228
+ super (s , p , o , m );
229
+ this .content = createContentCache ();
230
+ }
231
+
232
+ @ Override
233
+ public InternalCache .Loading <ComplexImpl , Object []> getContentCache () {
234
+ return content ;
235
+ }
236
+
237
+ @ Override
238
+ protected boolean sameContent (ONTStatementImpl other ) {
239
+ return other instanceof ComplexImpl && Arrays .equals (getContent (), ((ComplexImpl ) other ).getContent ());
240
+ }
241
+
242
+ @ Override
243
+ protected ComplexImpl makeCopyWith (ONTObject <OWLSameIndividualAxiom > other ) {
244
+ ComplexImpl res = new ComplexImpl (subject , predicate , object , model ) {
245
+ @ Override
246
+ public Stream <Triple > triples () {
247
+ return Stream .concat (ComplexImpl .this .triples (), other .triples ());
248
+ }
249
+ };
250
+ if (hasContent ()) {
251
+ res .putContent (getContent ());
252
+ }
253
+ return res ;
254
+ }
255
+ }
256
+ }
66
257
}
0 commit comments