Skip to content

Commit

Permalink
Merge branch 'main' into apacheGH-2809-Integration-tests_for_Jena_Sys…
Browse files Browse the repository at this point in the history
…tem_init
  • Loading branch information
arne-bdt committed Oct 29, 2024
2 parents 38b0874 + 08209ae commit 1065280
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 29 deletions.
17 changes: 11 additions & 6 deletions jena-core/src/main/java/org/apache/jena/datatypes/TypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,16 @@ public RDFDatatype getSafeTypeByName(final String uri) {
// Plain literal
return null;
}
RDFDatatype dtype = uriToDT.get(uri);
if (dtype == null) {
return uriToDT.computeIfAbsent(uri, u -> {
// Unknown datatype
if (JenaParameters.enableSilentAcceptanceOfUnknownDatatypes) {
dtype = new BaseDatatype(uri);
registerDatatype(dtype);
// No need to update classToDT because BaseDatatype.getJavaClass is always null
return new BaseDatatype(u);
} else {
throw new DatatypeFormatException(
"Attempted to created typed literal using an unknown datatype - " + uri);
}
}
return dtype;
});
}

/**
Expand Down Expand Up @@ -183,6 +181,9 @@ public RDFDatatype getTypeByClass(final Class<?> clazz) {

/**
* Register a new datatype
* This will overwrite any existing registration for this datatype IRI.
* Comparisons of literals with different datatype instances will fail, so be careful
* if you are using this outside of initialization code.
*/
public void registerDatatype(final RDFDatatype type) {
uriToDT.put(type.getURI(), type);
Expand All @@ -194,6 +195,10 @@ public void registerDatatype(final RDFDatatype type) {

/**
* Remove a datatype registration.
* <p>
* WARNING: This method may cause unexpected behavior if the datatype is still in use.
* If you unregister a datatype that is still used somewhere on the heap, literal comparisons with
* that datatype will fail.
*/
public void unregisterDatatype(final RDFDatatype type) {
uriToDT.remove(type.getURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
/**
* Implementation of the ValidationContext interface. Used to establish an
* environment for simple type validation.
* <p>
* This class is not thread-safe.
*
* {@literal @xerces.internal}
*
Expand All @@ -50,9 +52,10 @@ public class ValidationState implements ValidationContext {
private SymbolTable fSymbolTable = null;
private Locale fLocale = null;

//REVISIT: Should replace with a lighter structure.
private final HashMap fIdTable = new HashMap();
private final HashMap fIdRefTable = new HashMap();
// REVISIT: Should replace with a lighter structure.
// These tables are initialized only on demand to avoid unneeded allocations.
private HashMap fIdTable = null;
private HashMap fIdRefTable = null;
private final static Object fNullValue = new Object();

//
Expand Down Expand Up @@ -91,11 +94,12 @@ public void setSymbolTable(SymbolTable sTable) {
* otherwise return the first IDREF value without a matching ID value.
*/
public String checkIDRefID () {
if (fIdRefTable == null) return null;
Iterator iter = fIdRefTable.keySet().iterator();
String key;
while (iter.hasNext()) {
key = (String) iter.next();
if (!fIdTable.containsKey(key)) {
if (fIdTable == null || !fIdTable.containsKey(key)) {
return key;
}
}
Expand All @@ -106,8 +110,8 @@ public void reset () {
fExtraChecking = true;
fFacetChecking = true;
fNamespaces = true;
fIdTable.clear();
fIdRefTable.clear();
fIdTable = null;
fIdRefTable = null;
fEntityState = null;
fNamespaceContext = null;
fSymbolTable = null;
Expand All @@ -120,8 +124,8 @@ public void reset () {
* the two tables.
*/
public void resetIDTables() {
fIdTable.clear();
fIdRefTable.clear();
fIdTable = null;
fIdRefTable = null;
}

//
Expand Down Expand Up @@ -169,16 +173,25 @@ public boolean isEntityUnparsed (String name) {
// id
@Override
public boolean isIdDeclared(String name) {
if (fIdTable == null) {
return false;
}
return fIdTable.containsKey(name);
}
@Override
public void addId(String name) {
if (fIdTable == null) {
fIdTable = new HashMap();
}
fIdTable.put(name, fNullValue);
}

// idref
@Override
public void addIdRef(String name) {
if (fIdRefTable == null) {
fIdRefTable = new HashMap();
}
fIdRefTable.put(name, fNullValue);
}
// get symbols
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,15 +965,20 @@ public OntGraphModelImpl deleteOntList(OntObject subject, Property predicate, On

@Override
public OntDisjoint.Classes createDisjointClasses(Collection<OntClass> classes) {
if (classes.isEmpty()) {
throw new IllegalArgumentException("Empty list is specified");
}
checkType(OntDisjoint.Classes.class);
checkType(OntClass.IntersectionOf.class);
return checkCreate(model ->
OntDisjointImpl.createDisjointClasses(model, classes.stream()), OntDisjoint.Classes.class
);
}

@Override
public OntDisjoint.Individuals createDifferentIndividuals(Collection<OntIndividual> individuals) {
if (individuals.isEmpty()) {
throw new IllegalArgumentException("Empty list is specified");
}
checkType(OntDisjoint.Individuals.class);
return checkCreate(model ->
OntDisjointImpl.createDifferentIndividuals(model, individuals.stream()), OntDisjoint.Individuals.class
Expand All @@ -982,6 +987,9 @@ public OntDisjoint.Individuals createDifferentIndividuals(Collection<OntIndividu

@Override
public OntDisjoint.ObjectProperties createDisjointObjectProperties(Collection<OntObjectProperty> properties) {
if (properties.isEmpty()) {
throw new IllegalArgumentException("Empty list is specified");
}
checkType(OntDisjoint.ObjectProperties.class);
return checkCreate(model ->
OntDisjointImpl.createDisjointObjectProperties(model, properties.stream()), OntDisjoint.ObjectProperties.class
Expand All @@ -990,6 +998,9 @@ public OntDisjoint.ObjectProperties createDisjointObjectProperties(Collection<On

@Override
public OntDisjoint.DataProperties createDisjointDataProperties(Collection<OntDataProperty> properties) {
if (properties.isEmpty()) {
throw new IllegalArgumentException("Empty list is specified");
}
checkType(OntDisjoint.DataProperties.class);
return checkCreate(model ->
OntDisjointImpl.createDisjointDataProperties(model, properties.stream()), OntDisjoint.DataProperties.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,16 +785,16 @@ public final class OWL2ObjectFactories {
OntFacetRestriction.LangRange.class
);

public static final EnhNodeFactory CLASSES_DISJOINT = OntDisjoints.createDisjointClassesFactory(0);
public static final EnhNodeFactory CLASSES_DISJOINT = OntDisjoints.createDisjointClassesFactory(1);
public static final EnhNodeFactory EL_CLASSES_DISJOINT = OntDisjoints.createDisjointClassesFactory(2);
public static final EnhNodeFactory QL_RL_CLASSES_DISJOINT = OntDisjoints.createQLRLDisjointClassesFactory();
public static final Function<OntConfig, EnhNodeFactory> DIFFERENT_INDIVIDUALS_DISJOINT =
OntDisjoints::createDLFullDifferentIndividualsFactory;
public static final EnhNodeFactory EL_QL_RL_DIFFERENT_INDIVIDUALS_DISJOINT =
OntDisjoints.createELQLRLDifferentIndividualsFactory();
public static final EnhNodeFactory OBJECT_PROPERTIES_DISJOINT = OntDisjoints.createDisjointObjectPropertiesFactory(0);
public static final EnhNodeFactory OBJECT_PROPERTIES_DISJOINT = OntDisjoints.createDisjointObjectPropertiesFactory(1);
public static final EnhNodeFactory QL_RL_OBJECT_PROPERTIES_DISJOINT = OntDisjoints.createDisjointObjectPropertiesFactory(2);
public static final EnhNodeFactory DATA_PROPERTIES_DISJOINT = OntDisjoints.createDisjointDataPropertiesFactory(0);
public static final EnhNodeFactory DATA_PROPERTIES_DISJOINT = OntDisjoints.createDisjointDataPropertiesFactory(1);
public static final EnhNodeFactory QL_RL_DATA_PROPERTIES_DISJOINT = OntDisjoints.createDisjointDataPropertiesFactory(2);

public static final EnhNodeFactory ANY_PROPERTIES_DISJOINT = OntEnhNodeFactories.createFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static EnhNodeFactory createDLFullDifferentIndividualsFactory(OntConfig c
OWL2.AllDifferent,
OntIndividual.class,
it -> true,
0,
1,
predicates
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
import org.apache.jena.graph.Triple;
import org.apache.jena.ontapi.impl.GraphListenerBase;
import org.apache.jena.ontapi.model.OntClass;
import org.apache.jena.ontapi.model.OntDisjoint;
import org.apache.jena.ontapi.model.OntModel;
import org.apache.jena.ontapi.utils.OntModels;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.OWL2;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -99,4 +103,55 @@ public void testWriteAll() {
Assertions.assertTrue(d.contains(d.createResource("http://ex2/B"), RDF.type, OWL2.Class));
}

@Test
public void testOntDisjointProperties() {
var m = ModelFactory.createDefaultModel().setNsPrefixes(PrefixMapping.Standard);
var p1 = m.createResource("p1", OWL.DatatypeProperty);
var p2 = m.createResource("p2", OWL.DatatypeProperty);
var d = m.createResource().addProperty(RDF.type, OWL.AllDisjointProperties);
var list = m.createList(p1, p2);
d.addProperty(OWL.members, list);

var ont = OntModelFactory.createModel(m.getGraph());
var actual1 = ont.ontObjects(OntDisjoint.DataProperties.class).toList();
Assertions.assertEquals(1, actual1.size());

var actual2 = ont.ontObjects(OntDisjoint.ObjectProperties.class).toList();
Assertions.assertEquals(0, actual2.size());

var actual3 = ont.ontObjects(OntDisjoint.class).toList();
Assertions.assertEquals(1, actual3.size());
Assertions.assertEquals(OntDisjoint.DataProperties.class, OntModels.getOntType(actual3.get(0)));

Assertions.assertThrows(IllegalArgumentException.class, ont::createDisjointObjectProperties);
Assertions.assertThrows(IllegalArgumentException.class, ont::createDisjointDataProperties);
}

@Test
public void testOntDisjointClasses() {
var m = ModelFactory.createDefaultModel().setNsPrefixes(PrefixMapping.Standard);
var d = m.createResource().addProperty(RDF.type, OWL.AllDisjointClasses);
var list = m.createList();
d.addProperty(OWL.members, list);

var ont = OntModelFactory.createModel(m.getGraph());
var actual1 = ont.ontObjects(OntDisjoint.Classes.class).toList();
Assertions.assertEquals(0, actual1.size());

Assertions.assertThrows(IllegalArgumentException.class, ont::createDisjointClasses);
}

@Test
public void testOntDisjointIndividuals() {
var m = ModelFactory.createDefaultModel().setNsPrefixes(PrefixMapping.Standard);
var d = m.createResource().addProperty(RDF.type, OWL.AllDifferent);
var list = m.createList();
d.addProperty(OWL.members, list);

var ont = OntModelFactory.createModel(m.getGraph());
var actual1 = ont.ontObjects(OntDisjoint.Individuals.class).toList();
Assertions.assertEquals(0, actual1.size());

Assertions.assertThrows(IllegalArgumentException.class, ont::createDifferentIndividuals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,24 @@ public void testDisjointIndividualsForOWL1(TestSpec spec) {
OntModel m = OntModelFactory.createModel(spec.inst);
OntIndividual i1 = m.getOWLThing().createIndividual("A");
OntIndividual i2 = m.getOWLThing().createIndividual("B");
OntClass c1 = m.createOntClass("C");
OntDataProperty p1 = m.createDataProperty("p1");
OntObjectProperty p2 = m.createObjectProperty("p2");
OntDisjoint.Individuals d = m.createDifferentIndividuals(i1, i2);
Assertions.assertEquals(
List.of("A", "B"),
d.members().map(Resource::getURI).sorted().collect(Collectors.toList())
);

Assertions.assertEquals(8, m.statements().count());
Assertions.assertEquals(11, m.statements().count());
Assertions.assertEquals(1, m.ontObjects(OntDisjoint.Individuals.class).count());
Assertions.assertEquals(1, m.ontObjects(OntDisjoint.class).count());

Assertions.assertThrows(OntJenaException.Unsupported.class, m::createDisjointClasses);
Assertions.assertThrows(OntJenaException.Unsupported.class, m::createDisjointDataProperties);
Assertions.assertThrows(OntJenaException.Unsupported.class, m::createDisjointObjectProperties);
Assertions.assertThrows(OntJenaException.Unsupported.class, () -> m.createDisjointClasses(c1));
Assertions.assertThrows(OntJenaException.Unsupported.class, () -> m.createDisjointDataProperties(p1));
Assertions.assertThrows(OntJenaException.Unsupported.class, () -> m.createDisjointObjectProperties(p2));

Assertions.assertEquals(8, m.statements().count());
Assertions.assertEquals(11, m.statements().count());
Assertions.assertEquals(1, m.ontObjects(OntDisjoint.Individuals.class).count());
Assertions.assertEquals(1, m.ontObjects(OntDisjoint.class).count());
}
Expand Down Expand Up @@ -244,6 +247,7 @@ public void testOntClassCastOWL1(TestSpec spec) {
@ParameterizedTest
@EnumSource(names = {
"OWL1_MEM",
"OWL1_DL_MEM",
"OWL1_LITE_MEM",
})
public void testDisabledFeaturesOWL1(TestSpec spec) {
Expand All @@ -259,7 +263,7 @@ public void testDisabledFeaturesOWL1(TestSpec spec) {
);
d.createDataProperty("dp1").addEquivalentProperty(d.createDataProperty("dp2"));

OntModel m = OntModelFactory.createModel(d.getGraph(), OntSpecification.OWL1_DL_MEM);
OntModel m = OntModelFactory.createModel(d.getGraph(), spec.inst);
OntClass.Named x = m.getOntClass("X");
OntClass.Named q = m.createOntClass("Q");
OntDataProperty dp1 = m.createDataProperty("dp1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ public void testHasKey(TestSpec spec) {
public void testDifferentIndividuals(TestSpec spec) {
OntModel data = OntModelFactory.createModel();
data.createDifferentIndividuals(data.createIndividual("a"));
data.createDifferentIndividuals();
data.createResource()
.addProperty(RDF.type, OWL2.AllDifferent)
.addProperty(OWL2.members, data.createList());
data.createDifferentIndividuals(data.createIndividual("b"), data.createIndividual("c"));

OntModel m = OntModelFactory.createModel(data.getGraph(), spec.inst);
Expand Down Expand Up @@ -348,7 +350,9 @@ public void testDifferentIndividuals(TestSpec spec) {
public void testDisjointClasses(TestSpec spec) {
OntModel data = OntModelFactory.createModel();
data.createDisjointClasses(data.createOntClass("a"));
data.createDisjointClasses();
data.createResource()
.addProperty(RDF.type, OWL2.AllDisjointClasses)
.addProperty(OWL2.members, data.createList());
data.createDisjointClasses(data.createOntClass("b"), data.createOntClass("c"));

OntModel m = OntModelFactory.createModel(data.getGraph(), spec.inst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ public void testHasKey(TestSpec spec) {
public void testDisjointDataProperties(TestSpec spec) {
OntModel data = OntModelFactory.createModel();
data.createDisjointDataProperties(data.createDataProperty("a"));
data.createDisjointDataProperties();
data.createResource()
.addProperty(RDF.type, OWL2.AllDisjointProperties)
.addProperty(OWL2.members, data.createList());
data.createDisjointDataProperties(data.createDataProperty("b"), data.createDataProperty("c"));

OntModel m = OntModelFactory.createModel(data.getGraph(), spec.inst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ public void testHasKey(TestSpec spec) {
public void testDisjointObjectProperties(TestSpec spec) {
OntModel data = OntModelFactory.createModel();
data.createDisjointObjectProperties(data.createObjectProperty("a"));
data.createDisjointObjectProperties();
data.createResource()
.addProperty(RDF.type, OWL2.AllDisjointProperties)
.addProperty(OWL2.members, data.createList());
data.createDisjointObjectProperties(data.createObjectProperty("b"), data.createObjectProperty("c"));

OntModel m = OntModelFactory.createModel(data.getGraph(), spec.inst);
Expand Down

0 comments on commit 1065280

Please sign in to comment.