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 .OWLEquivalentObjectPropertiesAxiom ;
20
- import org .semanticweb .owlapi .model .OWLObjectPropertyExpression ;
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 .OntOPE ;
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 .Stream ;
30
36
31
37
/**
32
- * example:
33
- * <pre>
34
- * {@code
38
+ * A translator that provides {@link OWLEquivalentObjectPropertiesAxiom} implementations.
39
+ * Example:
40
+ * <pre> {@code
35
41
* <http://schema.org/image> rdf:type owl:ObjectProperty ; owl:equivalentProperty foaf:depiction .
36
- * }
37
- * </pre>
42
+ * }</pre>
38
43
* Created by @szuev on 01.10.2016.
39
- *
40
- * @see OWLEquivalentObjectPropertiesAxiom
41
44
*/
42
45
public class EquivalentObjectPropertiesTranslator
43
46
extends AbstractNaryTranslator <OWLEquivalentObjectPropertiesAxiom , OWLObjectPropertyExpression , OntOPE > {
@@ -54,14 +57,182 @@ Class<OntOPE> getView() {
54
57
55
58
@ Override
56
59
public ONTObject <OWLEquivalentObjectPropertiesAxiom > toAxiom (OntStatement statement ,
57
- InternalObjectFactory reader ,
60
+ Supplier <OntGraphModel > model ,
61
+ InternalObjectFactory factory ,
58
62
InternalConfig config ) {
59
- ONTObject <? extends OWLObjectPropertyExpression > a = reader .getProperty (statement .getSubject (getView ()));
60
- ONTObject <? extends OWLObjectPropertyExpression > b = reader .getProperty (statement .getObject ().as (getView ()));
61
- Collection <ONTObject <OWLAnnotation >> annotations = reader .getAnnotations (statement , config );
62
- OWLEquivalentObjectPropertiesAxiom res = reader .getOWLDataFactory ()
63
+ return AxiomImpl .create (statement , model , factory , config );
64
+ }
65
+
66
+ @ Override
67
+ public ONTObject <OWLEquivalentObjectPropertiesAxiom > toAxiom (OntStatement statement ,
68
+ InternalObjectFactory factory ,
69
+ InternalConfig config ) {
70
+ ONTObject <? extends OWLObjectPropertyExpression > a = factory .getProperty (statement .getSubject (getView ()));
71
+ ONTObject <? extends OWLObjectPropertyExpression > b = factory .getProperty (statement .getObject ().as (getView ()));
72
+ Collection <ONTObject <OWLAnnotation >> annotations = factory .getAnnotations (statement , config );
73
+ OWLEquivalentObjectPropertiesAxiom res = factory .getOWLDataFactory ()
63
74
.getOWLEquivalentObjectPropertiesAxiom (a .getOWLObject (), b .getOWLObject (), ONTObject .toSet (annotations ));
64
75
return ONTWrapperImpl .create (res , statement ).append (annotations ).append (a ).append (b );
65
76
}
66
77
78
+ /**
79
+ * @see com.github.owlcs.ontapi.owlapi.axioms.OWLEquivalentObjectPropertiesAxiomImpl
80
+ */
81
+ public abstract static class AxiomImpl extends ObjectPropertyNaryAxiomImpl <OWLEquivalentObjectPropertiesAxiom >
82
+ implements OWLEquivalentObjectPropertiesAxiom {
83
+
84
+ protected AxiomImpl (Triple t , Supplier <OntGraphModel > m ) {
85
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
86
+ }
87
+
88
+ protected AxiomImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
89
+ super (s , p , o , m );
90
+ }
91
+
92
+ /**
93
+ * Creates an {@link ONTObject} container, that is also {@link OWLEquivalentObjectPropertiesAxiom}.
94
+ *
95
+ * @param statement {@link OntStatement}, not {@code null}
96
+ * @param model {@link OntGraphModel} provider, not {@code null}
97
+ * @param factory {@link InternalObjectFactory}, not {@code null}
98
+ * @param config {@link InternalConfig}, not {@code null}
99
+ * @return {@link AxiomImpl}
100
+ */
101
+ public static AxiomImpl create (OntStatement statement ,
102
+ Supplier <OntGraphModel > model ,
103
+ InternalObjectFactory factory ,
104
+ InternalConfig config ) {
105
+ return WithManyObjects .create (statement , model ,
106
+ SimpleImpl .FACTORY , ComplexImpl .FACTORY , SET_HASH_CODE , factory , config );
107
+ }
108
+
109
+ @ Override
110
+ protected final long count () {
111
+ return 2 ;
112
+ }
113
+
114
+ @ FactoryAccessor
115
+ @ Override
116
+ protected OWLEquivalentObjectPropertiesAxiom createAxiom (Collection <OWLObjectPropertyExpression > members ,
117
+ Collection <OWLAnnotation > annotations ) {
118
+ return getDataFactory ().getOWLEquivalentObjectPropertiesAxiom (members ,
119
+ annotations == null ? NO_ANNOTATIONS : annotations );
120
+ }
121
+
122
+ @ FactoryAccessor
123
+ OWLSubObjectPropertyOfAxiom createSubPropertyOf (OWLObjectPropertyExpression a ,
124
+ OWLObjectPropertyExpression b ) {
125
+ DataFactory df = getDataFactory ();
126
+ return df .getOWLSubObjectPropertyOfAxiom (a , b );
127
+ }
128
+
129
+ @ FactoryAccessor
130
+ @ Override
131
+ public Collection <OWLSubObjectPropertyOfAxiom > asSubObjectPropertyOfAxioms () {
132
+ return walkAllPairwise ((a , b ) -> createSubPropertyOf (eraseModel (a ), eraseModel (b )));
133
+ }
134
+
135
+ /**
136
+ * An {@link OWLEquivalentObjectPropertiesAxiom} that has named object property expressions
137
+ * as subject and object and has no annotations.
138
+ */
139
+ protected static class SimpleImpl extends AxiomImpl implements Simple <OWLObjectPropertyExpression > {
140
+
141
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, SimpleImpl > FACTORY = SimpleImpl ::new ;
142
+
143
+ protected SimpleImpl (Triple t , Supplier <OntGraphModel > m ) {
144
+ super (t , m );
145
+ }
146
+
147
+ protected SimpleImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
148
+ super (s , p , o , m );
149
+ }
150
+
151
+ @ Override
152
+ protected boolean sameContent (ONTStatementImpl other ) {
153
+ return other instanceof SimpleImpl && isReverseTriple ((SimpleImpl ) other );
154
+ }
155
+
156
+ @ Override
157
+ protected AxiomImpl makeCopyWith (ONTObject <OWLEquivalentObjectPropertiesAxiom > other ) {
158
+ if (other instanceof SimpleImpl ) {
159
+ Triple t = ((SimpleImpl ) other ).asTriple ();
160
+ return new SimpleImpl (subject , predicate , object , model ) {
161
+
162
+ @ Override
163
+ public Stream <Triple > triples () {
164
+ return Stream .concat (SimpleImpl .this .triples (), Stream .of (t ));
165
+ }
166
+ };
167
+ }
168
+ return new SimpleImpl (subject , predicate , object , model ) {
169
+ @ Override
170
+ public Stream <Triple > triples () {
171
+ return Stream .concat (SimpleImpl .this .triples (), other .triples ());
172
+ }
173
+ };
174
+ }
175
+
176
+ @ SuppressWarnings ("unchecked" )
177
+ @ Override
178
+ public Set <OWLObjectProperty > getObjectPropertySet () {
179
+ return (Set <OWLObjectProperty >) getOWLComponentsAsSet ();
180
+ }
181
+
182
+ @ SuppressWarnings ("unchecked" )
183
+ @ Override
184
+ public Set <OWLEntity > getSignatureSet () {
185
+ return (Set <OWLEntity >) getOWLComponentsAsSet ();
186
+ }
187
+
188
+ @ Override
189
+ public boolean containsObjectProperty (OWLObjectProperty property ) {
190
+ return hasURIResource (ONTEntityImpl .getURI (property ));
191
+ }
192
+ }
193
+
194
+ /**
195
+ * An {@link OWLEquivalentObjectPropertiesAxiom}
196
+ * that either has annotations or anonymous object property expressions in the subject or the object positions.
197
+ * It has a public constructor since it is more generic then {@link SimpleImpl}.
198
+ */
199
+ public static class ComplexImpl extends AxiomImpl implements Complex <ComplexImpl , OWLObjectPropertyExpression > {
200
+
201
+ private static final BiFunction <Triple , Supplier <OntGraphModel >, ComplexImpl > FACTORY = ComplexImpl ::new ;
202
+ protected final InternalCache .Loading <ComplexImpl , Object []> content ;
203
+
204
+ public ComplexImpl (Triple t , Supplier <OntGraphModel > m ) {
205
+ this (strip (t .getSubject ()), t .getPredicate ().getURI (), strip (t .getObject ()), m );
206
+ }
207
+
208
+ protected ComplexImpl (Object s , String p , Object o , Supplier <OntGraphModel > m ) {
209
+ super (s , p , o , m );
210
+ this .content = createContentCache ();
211
+ }
212
+
213
+ @ Override
214
+ public InternalCache .Loading <ComplexImpl , Object []> getContentCache () {
215
+ return content ;
216
+ }
217
+
218
+ @ Override
219
+ protected boolean sameContent (ONTStatementImpl other ) {
220
+ return other instanceof ComplexImpl && Arrays .equals (getContent (), ((ComplexImpl ) other ).getContent ());
221
+ }
222
+
223
+ @ Override
224
+ protected ComplexImpl makeCopyWith (ONTObject <OWLEquivalentObjectPropertiesAxiom > other ) {
225
+ ComplexImpl res = new ComplexImpl (subject , predicate , object , model ) {
226
+ @ Override
227
+ public Stream <Triple > triples () {
228
+ return Stream .concat (ComplexImpl .this .triples (), other .triples ());
229
+ }
230
+ };
231
+ if (hasContent ()) {
232
+ res .putContent (getContent ());
233
+ }
234
+ return res ;
235
+ }
236
+ }
237
+ }
67
238
}
0 commit comments