Skip to content

Commit f4ee853

Browse files
committed
ont-api: add OWLDisjointDataPropertiesAxiom model-impl (issue #2) + add/change tests
1 parent 4ab015e commit f4ee853

File tree

8 files changed

+352
-56
lines changed

8 files changed

+352
-56
lines changed

src/main/java/com/github/owlcs/ontapi/internal/AxiomTranslator.java

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ protected abstract ONTObject<Axiom> toAxiom(OntStatement statement,
276276
* @param config {@link InternalConfig} to control process
277277
* @return {@link ONTObject} around {@link OWLAxiom}
278278
* @throws JenaException if no possible to get axiom from the statement
279+
* @since 2.0.0
279280
*/
280281
protected ONTObject<Axiom> toAxiom(OntStatement statement,
281282
Supplier<OntGraphModel> model,

src/main/java/com/github/owlcs/ontapi/internal/axioms/AbstractNaryTranslator.java

+31
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,37 @@ public final boolean canContainDataProperties() {
388388
}
389389
}
390390

391+
/**
392+
* An abstract {@link OWLNaryPropertyAxiom} implementation for member-type {@link OWLDataPropertyExpression}.
393+
*
394+
* @param <A> either {@link OWLEquivalentDataPropertiesAxiom} or {@link OWLDisjointDataPropertiesAxiom}
395+
*/
396+
@SuppressWarnings("WeakerAccess")
397+
protected abstract static class DataPropertyNaryAxiomImpl<A extends OWLNaryPropertyAxiom<OWLDataPropertyExpression>>
398+
extends PropertyNaryAxiomImpl<A, OWLDataPropertyExpression> {
399+
400+
protected DataPropertyNaryAxiomImpl(Object subject, String predicate, Object object, Supplier<OntGraphModel> m) {
401+
super(subject, predicate, object, m);
402+
}
403+
404+
@Override
405+
public ExtendedIterator<ONTObject<? extends OWLDataPropertyExpression>> listONTComponents(OntStatement statement,
406+
InternalObjectFactory factory) {
407+
return Iter.of(factory.getProperty(statement.getSubject(OntNDP.class)),
408+
factory.getProperty(statement.getObject(OntNDP.class)));
409+
}
410+
411+
@Override
412+
public ONTObject<? extends OWLDataPropertyExpression> findByURI(String uri, InternalObjectFactory factory) {
413+
return ONTDataPropertyImpl.find(uri, factory, model);
414+
}
415+
416+
@Override
417+
public final boolean canContainObjectProperties() {
418+
return false;
419+
}
420+
}
421+
391422
/**
392423
* An abstraction, that combines common properties for {@link OWLNaryIndividualAxiom} and {@link OWLNaryClassAxiom}.
393424
*

src/main/java/com/github/owlcs/ontapi/internal/axioms/DisjointDataPropertiesTranslator.java

+230-14
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,50 @@
1414

1515
package com.github.owlcs.ontapi.internal.axioms;
1616

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;
2118
import com.github.owlcs.ontapi.internal.InternalConfig;
2219
import com.github.owlcs.ontapi.internal.InternalObjectFactory;
2320
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;
2424
import com.github.owlcs.ontapi.jena.model.OntDisjoint;
25+
import com.github.owlcs.ontapi.jena.model.OntGraphModel;
2526
import com.github.owlcs.ontapi.jena.model.OntNDP;
2627
import com.github.owlcs.ontapi.jena.model.OntStatement;
28+
import com.github.owlcs.ontapi.jena.utils.OntModels;
2729
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;
2842

2943
/**
30-
* see {@link AbstractTwoWayNaryTranslator}
31-
* examples:
44+
* A translator that provides {@link OWLDisjointDataPropertiesAxiom} implementations.
45+
* Examples:
3246
* <ul>
3347
* <li>{@code :objProperty1 owl:propertyDisjointWith :objProperty2}</li>
3448
* <li>{@code [ rdf:type owl:AllDisjointProperties; owl:members ( :objProperty1 :objProperty2 :objProperty3 ) ]}</li>
3549
* </ul>
3650
* <p>
3751
* Created by szuev on 12.10.2016.
38-
*
39-
* @see OWLDisjointDataPropertiesAxiom
4052
*/
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+
4258
@Override
4359
Property getPredicate() {
44-
return OWL.propertyDisjointWith;
60+
return PREDICATE;
4561
}
4662

4763
@Override
@@ -66,11 +82,211 @@ Class<OntDisjoint.DataProperties> getDisjointView() {
6682

6783
@Override
6884
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,
7094
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()
7498
.getOWLDisjointDataPropertiesAxiom(ONTObject.toSet(members), ONTObject.toSet(annotations)));
7599
}
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+
}
76292
}

src/test/java/com/github/owlcs/ontapi/tests/TestFactory.java

+37
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,43 @@ public String toString() {
25142514
"IRI.create(\"https://github.com/owlcs\"))))";
25152515
}
25162516
}
2517+
, new AxiomData() {
2518+
@Override
2519+
public AxiomType getType() {
2520+
return AxiomType.DISJOINT_DATA_PROPERTIES;
2521+
}
2522+
2523+
@Override
2524+
public OWLObject create(OWLDataFactory df) {
2525+
return df.getOWLDisjointDataPropertiesAxiom(df.getOWLDataProperty("A"),
2526+
df.getOWLDataProperty("B"));
2527+
}
2528+
2529+
@Override
2530+
public String toString() {
2531+
return "df.getOWLDisjointDataPropertiesAxiom(df.getOWLDataProperty(\"A\"), " +
2532+
"df.getOWLDataProperty(\"B\"))";
2533+
}
2534+
}
2535+
, new AxiomData() {
2536+
@Override
2537+
public AxiomType getType() {
2538+
return AxiomType.DISJOINT_DATA_PROPERTIES;
2539+
}
2540+
2541+
@Override
2542+
public OWLObject create(OWLDataFactory df) {
2543+
return df.getOWLDisjointDataPropertiesAxiom(Collections.singleton(df.getOWLDataProperty("P")),
2544+
Collections.singleton(df.getRDFSLabel(IRI.create("https://github.com/owlcs"))));
2545+
}
2546+
2547+
@Override
2548+
public String toString() {
2549+
return "df.getOWLDisjointDataPropertiesAxiom(Collections.singleton(" +
2550+
"df.getOWLDataProperty(\"P\")), " +
2551+
"Collections.singleton(df.getRDFSLabel(IRI.create(\"https://github.com/owlcs\"))))";
2552+
}
2553+
}
25172554
);
25182555
}
25192556

src/test/java/com/github/owlcs/ontapi/tests/internal/AxiomPropertiesTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public void testEquivalentObjectProperties() {
7878
testSplitNaryAxioms(data, (OWLNaryPropertyAxiom<?> a) -> a.properties().collect(Collectors.toList()));
7979
}
8080

81+
@Test
82+
public void testDisjointDataProperties() {
83+
List<TestFactory.AxiomData> data = CommonAxiomsTest.getAxiomData(AxiomType.DISJOINT_DATA_PROPERTIES);
84+
testAxiom(data, (OWLNaryPropertyAxiom<?> a) -> a.properties().collect(Collectors.toList()));
85+
}
86+
8187
@SuppressWarnings("unchecked")
8288
@SafeVarargs
8389
private static <X extends OWLAxiom> void testAxiom(List<TestFactory.AxiomData> data,

0 commit comments

Comments
 (0)