Skip to content

Commit 1d81a9e

Browse files
committed
ont-api: fix OWLComponentType#sharedComponents() + other cosmetic changes (fix #5)
1 parent 49e4bd7 commit 1d81a9e

File tree

3 files changed

+66
-35
lines changed

3 files changed

+66
-35
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,10 @@ protected boolean isUsed(OWLContentType type, OWLObject object) {
10561056
/**
10571057
* Returns a {@code Set} of {@link Triple}s,
10581058
* that belongs to both the given and some other content component in the form of component intersection.
1059+
* This method only takes into account intersections in components.
10591060
* Almost any {@code OWLObject}-component - whatever named or anonymous -
10601061
* could be shared between different content objects.
1061-
* In this case the triples, belonging to such a component, cannot be deleted.
1062+
* In this case, the triples, belonging to such a component, cannot be deleted.
10621063
*
10631064
* @param m {@link OntGraphModel} the model to traverse over
10641065
* @param object {@link OWLObject} for which this operation is performed
@@ -1077,6 +1078,7 @@ protected Set<Triple> getUsedComponentTriples(OntGraphModel m, OWLObject object)
10771078
if (objects.isEmpty()) {
10781079
return;
10791080
}
1081+
// axioms for this component type:
10801082
selectContentContainers(type)
10811083
.forEach(x -> {
10821084
if (object.equals(x.getOWLObject())) {

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

+51-27
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* Created by @ssz on 13.07.2019.
3636
*/
3737
public enum OWLComponentType {
38-
IRI(org.semanticweb.owlapi.model.IRI.class, Resource.class, true) {
38+
IRI(org.semanticweb.owlapi.model.IRI.class, Resource.class, false, true) {
3939
@Override
4040
List<OWLComponentType> includes() {
4141
return Collections.emptyList();
@@ -70,7 +70,7 @@ ExtendedIterator<Resource> listObjects(OntGraphModel model) {
7070
}
7171

7272
},
73-
ANNOTATION_PROPERTY(OWLAnnotationProperty.class, OntNAP.class, true) {
73+
ANNOTATION_PROPERTY(OWLAnnotationProperty.class, OntNAP.class, true, true) {
7474
@Override
7575
Stream<OWLAnnotationProperty> components(OWLObject o) {
7676
return o.annotationPropertiesInSignature();
@@ -81,7 +81,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
8181
return df.getProperty(n.as(OntNAP.class));
8282
}
8383
},
84-
DATATYPE_PROPERTY(OWLDataProperty.class, OntNDP.class, true) {
84+
DATATYPE_PROPERTY(OWLDataProperty.class, OntNDP.class, true, true) {
8585
@Override
8686
Stream<OWLDataProperty> components(OWLObject o) {
8787
return o.dataPropertiesInSignature();
@@ -92,7 +92,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
9292
return df.getProperty(n.as(OntNDP.class));
9393
}
9494
},
95-
NAMED_OBJECT_PROPERTY(OWLObjectProperty.class, OntNOP.class, true) {
95+
NAMED_OBJECT_PROPERTY(OWLObjectProperty.class, OntNOP.class, true, true) {
9696
@Override
9797
Stream<OWLObjectProperty> components(OWLObject o) {
9898
return o.objectPropertiesInSignature();
@@ -103,7 +103,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
103103
return df.getProperty(n.as(OntNOP.class));
104104
}
105105
},
106-
NAMED_INDIVIDUAL(OWLNamedIndividual.class, OntIndividual.Named.class, true) {
106+
NAMED_INDIVIDUAL(OWLNamedIndividual.class, OntIndividual.Named.class, true, true) {
107107
@Override
108108
Stream<OWLNamedIndividual> components(OWLObject o) {
109109
return o.individualsInSignature();
@@ -114,7 +114,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
114114
return df.getIndividual(n.as(OntIndividual.Named.class));
115115
}
116116
},
117-
CLASS(OWLClass.class, OntClass.class, true) {
117+
CLASS(OWLClass.class, OntClass.class, true, true) {
118118
@Override
119119
Stream<OWLClass> components(OWLObject o) {
120120
return o.classesInSignature();
@@ -125,7 +125,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
125125
return df.getClass(n.as(OntClass.class));
126126
}
127127
},
128-
DATATYPE(OWLDatatype.class, OntDT.class, true) {
128+
DATATYPE(OWLDatatype.class, OntDT.class, true, true) {
129129
@Override
130130
Stream<OWLDatatype> components(OWLObject o) {
131131
return o.datatypesInSignature();
@@ -136,7 +136,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
136136
return df.getDatatype(n.as(OntDT.class));
137137
}
138138
},
139-
ENTITY(OWLEntity.class, OntEntity.class, false) {
139+
ENTITY(OWLEntity.class, OntEntity.class, true, false) {
140140
@Override
141141
List<OWLComponentType> includes() {
142142
return Arrays.asList(ANNOTATION_PROPERTY, DATATYPE_PROPERTY, NAMED_OBJECT_PROPERTY,
@@ -153,7 +153,7 @@ Stream<OWLEntity> components(OWLObject o) {
153153
return o.signature();
154154
}
155155
},
156-
LITERAL(OWLLiteral.class, Literal.class, true) {
156+
LITERAL(OWLLiteral.class, Literal.class, false, true) {
157157
@Override
158158
List<OWLComponentType> includes() {
159159
return Collections.singletonList(DATATYPE);
@@ -176,7 +176,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
176176
return df.getLiteral(n.asLiteral());
177177
}
178178
},
179-
INDIVIDUAL(OWLIndividual.class, OntIndividual.class, false) {
179+
INDIVIDUAL(OWLIndividual.class, OntIndividual.class) {
180180
@Override
181181
List<OWLComponentType> includes() {
182182
return Arrays.asList(NAMED_INDIVIDUAL, ANONYMOUS_INDIVIDUAL);
@@ -192,7 +192,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
192192
return df.getIndividual(n.as(OntIndividual.class));
193193
}
194194
},
195-
ANONYMOUS_INDIVIDUAL(OWLAnonymousIndividual.class, OntIndividual.Anonymous.class, true) {
195+
ANONYMOUS_INDIVIDUAL(OWLAnonymousIndividual.class, OntIndividual.Anonymous.class, false, true) {
196196
@Override
197197
Stream<OWLAnonymousIndividual> components(OWLObject o) {
198198
return o.anonymousIndividuals();
@@ -203,7 +203,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
203203
return df.getIndividual(n.as(OntIndividual.Anonymous.class));
204204
}
205205
},
206-
ANONYMOUS_DATA_RANGE(OWLDataRange.class, OntDR.class, false) {
206+
ANONYMOUS_DATA_RANGE(OWLDataRange.class, OntDR.class) {
207207
/**
208208
* {@inheritDoc}
209209
* + literals ({@code DataOneOf}, {@code DatatypeRestriction})
@@ -231,7 +231,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
231231
return df.getDatatype(n.as(OntDR.class));
232232
}
233233
},
234-
DATA_RANGE(OWLDataRange.class, OntDR.class, false) {
234+
DATA_RANGE(OWLDataRange.class, OntDR.class) {
235235
/**
236236
* {@inheritDoc}
237237
* + literals ({@code DataOneOf}, {@code DatatypeRestriction})
@@ -246,7 +246,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
246246
return df.getDatatype(n.as(OntDR.class));
247247
}
248248
},
249-
ANONYMOUS_CLASS_EXPRESSION(OWLAnonymousClassExpression.class, OntCE.class, false) {
249+
ANONYMOUS_CLASS_EXPRESSION(OWLAnonymousClassExpression.class, OntCE.class) {
250250
/**
251251
* {@inheritDoc}
252252
* This component may contain:
@@ -281,7 +281,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
281281
return df.getClass(n.as(OntCE.class));
282282
}
283283
},
284-
CLASS_EXPRESSION(OWLClassExpression.class, OntCE.class, false) {
284+
CLASS_EXPRESSION(OWLClassExpression.class, OntCE.class) {
285285
@Override
286286
List<OWLComponentType> includes() {
287287
return Arrays.asList(CLASS, ANONYMOUS_CLASS_EXPRESSION);
@@ -297,7 +297,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
297297
return df.getClass(n.as(OntCE.class));
298298
}
299299
},
300-
INVERSE_OBJECT_PROPERTY(OWLObjectInverseOf.class, OntOPE.Inverse.class, false) {
300+
INVERSE_OBJECT_PROPERTY(OWLObjectInverseOf.class, OntOPE.Inverse.class) {
301301
@Override
302302
List<OWLComponentType> includes() {
303303
return Collections.singletonList(NAMED_OBJECT_PROPERTY);
@@ -308,7 +308,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
308308
return df.getProperty(n.as(OntOPE.Inverse.class));
309309
}
310310
},
311-
OBJECT_PROPERTY_EXPRESSION(OWLObjectPropertyExpression.class, OntOPE.class, false) {
311+
OBJECT_PROPERTY_EXPRESSION(OWLObjectPropertyExpression.class, OntOPE.class) {
312312
@Override
313313
List<OWLComponentType> includes() {
314314
return Arrays.asList(NAMED_OBJECT_PROPERTY, INVERSE_OBJECT_PROPERTY);
@@ -319,7 +319,7 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
319319
return df.getProperty(n.as(OntOPE.class));
320320
}
321321
},
322-
FACET_RESTRICTION(OWLFacetRestriction.class, OntFR.class, false) {
322+
FACET_RESTRICTION(OWLFacetRestriction.class, OntFR.class) {
323323
@Override
324324
List<OWLComponentType> includes() {
325325
return Collections.singletonList(LITERAL);
@@ -331,13 +331,13 @@ ONTObject<OWLFacetRestriction> wrap(RDFNode n, InternalObjectFactory df) {
331331
}
332332

333333
},
334-
SWRL_VARIABLE(SWRLVariable.class, OntSWRL.Variable.class, true) {
334+
SWRL_VARIABLE(SWRLVariable.class, OntSWRL.Variable.class, false, true) {
335335
@Override
336336
ONTObject<SWRLVariable> wrap(RDFNode n, InternalObjectFactory df) {
337337
return df.getSWRLVariable(n.as(OntSWRL.Variable.class));
338338
}
339339
},
340-
SWRL_ATOM(SWRLAtom.class, OntSWRL.Atom.class, false) {
340+
SWRL_ATOM(SWRLAtom.class, OntSWRL.Atom.class) {
341341
@Override
342342
List<OWLComponentType> includes() {
343343
return Arrays.asList(INDIVIDUAL, LITERAL, SWRL_VARIABLE,
@@ -368,10 +368,22 @@ ONTObject<? extends OWLObject> wrap(RDFNode n, InternalObjectFactory df) {
368368
final Class<? extends OWLObject> owl;
369369
final Class<? extends RDFNode> jena;
370370
private final boolean primitive;
371+
private final boolean entity;
371372

372-
OWLComponentType(Class<? extends OWLObject> owl, Class<? extends RDFNode> rdf, boolean primitive) {
373+
OWLComponentType(Class<? extends OWLObject> owl, Class<? extends RDFNode> rdf) {
374+
this(owl, rdf, false, false);
375+
}
376+
377+
/**
378+
* @param owl OWL-API {@code Class}-type
379+
* @param rdf ONT-API {@code Class}-type
380+
* @param entity {@code true} if the this is an entity type
381+
* @param primitive {@code true} if the this is a primitive type
382+
*/
383+
OWLComponentType(Class<? extends OWLObject> owl, Class<? extends RDFNode> rdf, boolean entity, boolean primitive) {
373384
this.owl = owl;
374385
this.jena = rdf;
386+
this.entity = entity;
375387
this.primitive = primitive;
376388
}
377389

@@ -390,7 +402,7 @@ public static Set<OWLComponentType> toSet(OWLComponentType... values) {
390402

391403
/**
392404
* Determines and returns the most specific type for the given {@link OWLObject}.
393-
* The primitive types go first, then the composite.
405+
* The primitive types go first, then the composite or abstract.
394406
*
395407
* @param o {@link OWLObject}, not {@code null}
396408
* @return {@link OWLComponentType}
@@ -406,8 +418,7 @@ public static OWLComponentType get(OWLObject o) {
406418
}
407419

408420
/**
409-
* Lists components that can be shared,
410-
* but at the same time are not {@link OWLEntity OWL entities}.
421+
* Lists components that can be shared.
411422
*
412423
* @return {@code Stream} of {@link OWLContentType}s
413424
* @see InternalModel#getUsedComponentTriples(OntGraphModel, OWLObject)
@@ -493,17 +504,30 @@ public Stream<OWLObject> select(OWLObject container) {
493504
/**
494505
* Answers {@code true} if the given {@code container} contains the given {@code component} somewhere in its depths.
495506
*
496-
* @param container {@link OWLObject} to search in, not {@code null}
497-
* @param component {@link OWLObject} to search for, not {@code null}
507+
* @param container {@link OWLObject} the container to search in, not {@code null}
508+
* @param component {@link OWLObject} to search for, must be of this type, not {@code null}
498509
* @return boolean
499510
*/
500511
public boolean contains(OWLObject container, OWLObject component) {
501-
if (component instanceof OWLEntity) {
512+
if (entity) {
502513
return container.containsEntityInSignature((OWLEntity) component);
503514
}
504515
return components(container).anyMatch(component::equals);
505516
}
506517

518+
/**
519+
* Answers {@code true} if the given {@code container} contains
520+
* any of the given {@code components} somewhere in its depths.
521+
* Input collection must contain objects of this type only.
522+
*
523+
* @param container {@link OWLObject}, the container to search in, not {@code null}
524+
* @param components a {@code Collection} of {@link OWLObject}s of this type, not {@code null}
525+
* @return boolean, {@code true} iff any of the {@code components} in present in the {@code container}
526+
*/
527+
public boolean containsAny(OWLObject container, Collection<? extends OWLObject> components) {
528+
return components(container).anyMatch(components::contains);
529+
}
530+
507531
/**
508532
* Returns all components of this type from the specified {@link OWLObject}-container
509533
* in the form of {@code Stream} of {@link ONTObject}s.

src/test/java/com/github/owlcs/ontapi/tests/model/ModifyAxiomsTest.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,25 @@ public class ModifyAxiomsTest {
4646
public void testRemoveAllAxiomsFromLoadedOntology() {
4747
String ns = "http://x#";
4848
OntologyManager m = OntManagers.createONT();
49+
DataFactory df = m.getOWLDataFactory();
4950

5051
OntologyModel o = m.createOntology();
5152
OntGraphModel g = o.asGraphModel();
5253
g.createOntClass(ns + "X").addSuperClass(g.createOntClass(ns + "Y"));
5354
ReadWriteUtils.print(g);
55+
Assert.assertEquals(4, g.size());
56+
Assert.assertEquals(3, o.axioms().peek(x -> LOGGER.debug("1): {}", x)).count());
5457

55-
List<OWLAxiom> axioms = o.axioms().peek(x -> LOGGER.debug("(1): {}", x)).collect(Collectors.toList());
56-
Assert.assertEquals(3, axioms.size());
58+
o.remove(df.getOWLDeclarationAxiom(df.getOWLClass(ns + "X")));
59+
Assert.assertEquals(2, o.axioms().peek(x -> LOGGER.debug("2): {}", x)).count());
60+
Assert.assertEquals(4, g.size());
5761

58-
for (OWLAxiom a : axioms) {
59-
LOGGER.debug("REMOVE: {}", a);
60-
o.remove(a);
61-
}
62-
Assert.assertEquals(0, o.axioms().peek(x -> LOGGER.debug("(2): {}", x)).count());
62+
o.remove(df.getOWLDeclarationAxiom(df.getOWLClass(ns + "Y")));
63+
Assert.assertEquals(1, o.axioms().peek(x -> LOGGER.debug("3): {}", x)).count());
64+
Assert.assertEquals(4, g.size());
65+
66+
o.remove(df.getOWLSubClassOfAxiom(df.getOWLClass(ns + "X"), df.getOWLClass(ns + "Y")));
67+
Assert.assertEquals(0, o.axioms().peek(x -> LOGGER.debug("4): {}", x)).count());
6368
Assert.assertEquals(1, g.size());
6469
}
6570

0 commit comments

Comments
 (0)