diff --git a/src/main/java/edu/isi/oba/MapperSchema.java b/src/main/java/edu/isi/oba/MapperSchema.java index fa23274..5858eac 100644 --- a/src/main/java/edu/isi/oba/MapperSchema.java +++ b/src/main/java/edu/isi/oba/MapperSchema.java @@ -110,7 +110,10 @@ private Schema setSchema() { schema.setType(this.type); schema.setProperties(this.getProperties()); - schema.setRequired(this.required_properties); + if (this.configFlags.containsKey(CONFIG_FLAG.REQUIRED_PROPERTIES_FROM_CARDINALITY) + && this.configFlags.get(CONFIG_FLAG.REQUIRED_PROPERTIES_FROM_CARDINALITY)) { + schema.setRequired(this.required_properties); + } HashMap exampleMap = new HashMap<>(); exampleMap.put("id", "some_id"); @@ -505,8 +508,11 @@ private void getClassRestrictions(OWLClass analyzedClass) { // If cardinality is exactly 1, then we can remove the min/max property constraints // and set the property to be required for the class. if (exactCardinality == 1 || (minCardinality == 1 && maxCardinality == 1)) { - opSchema.setMinItems(null); - opSchema.setMaxItems(null); + if (!this.configFlags.containsKey(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS) || !this.configFlags.get(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS)) { + opSchema.setMinItems(null); + opSchema.setMaxItems(null); + } + is_required = true; } @@ -555,6 +561,9 @@ private void getClassRestrictions(OWLClass analyzedClass) { || minCardinality > 0 || maxCardinality > 1; + // If config flag to generate arrays is set, use it to override current setting. + isArray |= (this.configFlags.containsKey(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS) && this.configFlags.get(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS)); + // If cardinality is exactly 1 OR a minimum of 1, then not nullable. boolean isNullable = (exactCardinality == -1 && minCardinality == -1) ? true : exactCardinality != 1 && minCardinality < 1; diff --git a/src/test/java/edu/isi/oba/RestrictionsTest.java b/src/test/java/edu/isi/oba/RestrictionsTest.java index 41f9c05..4ecbefd 100644 --- a/src/test/java/edu/isi/oba/RestrictionsTest.java +++ b/src/test/java/edu/isi/oba/RestrictionsTest.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.ConsoleHandler; @@ -30,10 +31,13 @@ public class RestrictionsTest { static Logger logger = null; // Convenience variable so we don't need to retype this for each MapperSchema constructor. - private final Map configFlags = Map.ofEntries( - Map.entry(CONFIG_FLAG.DEFAULT_DESCRIPTIONS, true), - Map.entry(CONFIG_FLAG.DEFAULT_PROPERTIES, true), - Map.entry(CONFIG_FLAG.FOLLOW_REFERENCES, true)); + private Map configFlags = new HashMap<>(){{ + put(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS, true); + put(CONFIG_FLAG.DEFAULT_DESCRIPTIONS, true); + put(CONFIG_FLAG.DEFAULT_PROPERTIES, true); + put(CONFIG_FLAG.FOLLOW_REFERENCES, true); + put(CONFIG_FLAG.REQUIRED_PROPERTIES_FROM_CARDINALITY, true); + }}; /** * This method allows you to configure the logger variable that is required to print several @@ -204,22 +208,59 @@ public void testObjectSomeValuesFrom_ComposedByRestriction() throws OWLOntologyC } } + + /** + * This test attempts to get the OAS representation of the exact cardinality of an ObjectProperty, + * when arrays are set to always be generated for properties. + */ + @Test + public void testObjectExactCardinalityWithArraysGenerated() throws OWLOntologyCreationException, Exception { + try { + this.initializeLogger(); + YamlConfig config_data = get_yaml_data("examples/restrictions/config.yaml"); + Mapper mapper = new Mapper(config_data); + OWLClass cls = mapper.manager.getOWLDataFactory().getOWLClass("https://w3id.org/example#AmericanStudent"); + String desc = ObaUtils.getDescription(cls, mapper.ontologies.get(0), true); + + this.configFlags.put(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS, true); + MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), this.configFlags); + Schema schema = mapperSchema.getSchema(); + Object property= schema.getProperties().get("hasRecord"); + if (property instanceof ArraySchema) { + Integer maxItems = ((ArraySchema) property).getMaxItems(); + Integer minItems = ((ArraySchema) property).getMinItems(); + if (maxItems != null && minItems != null) { + if (maxItems == minItems) + // "Exact cardinality configured" -- does this really need to be output for the test? + return; + else + Assertions.fail("Error in exact cardinality restriction."); + } else + Assertions.fail("Null values in exact cardinality restriction."); + } + } catch (OWLOntologyCreationException e) { + Assertions.fail("Error in ontology creation: ", e); + } + } /** - * This test attempts to get the OAS representation of the exact cardinality of an ObjectProperty. + * This test attempts to get the OAS representation of the exact cardinality of an ObjectProperty, + * when properties may or may not be arrays, depending on cardinality. */ @Test - public void testObjectExactCardinality() throws OWLOntologyCreationException, Exception { + public void testObjectExactCardinalityWithoutArraysGenerated() throws OWLOntologyCreationException, Exception { try { this.initializeLogger(); YamlConfig config_data = get_yaml_data("examples/restrictions/config.yaml"); Mapper mapper = new Mapper(config_data); OWLClass cls = mapper.manager.getOWLDataFactory().getOWLClass("https://w3id.org/example#AmericanStudent"); String desc = ObaUtils.getDescription(cls, mapper.ontologies.get(0), true); + + this.configFlags.put(CONFIG_FLAG.ALWAYS_GENERATE_ARRAYS, false); MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), this.configFlags); Schema schema = mapperSchema.getSchema(); - boolean isRequired = schema.getRequired().contains("hasRecord"); + boolean isRequired = schema.getRequired() != null && schema.getRequired().contains("hasRecord"); // For exact cardinality, the class schema should have it marked as required. if (isRequired) {