Skip to content

Commit

Permalink
use flags for (non-)arrays + required props
Browse files Browse the repository at this point in the history
  • Loading branch information
cweedall committed May 6, 2024
1 parent fb0c34a commit e654474
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/main/java/edu/isi/oba/MapperSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> exampleMap = new HashMap<>();
exampleMap.put("id", "some_id");
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down
55 changes: 48 additions & 7 deletions src/test/java/edu/isi/oba/RestrictionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CONFIG_FLAG, Boolean> 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<CONFIG_FLAG, Boolean> 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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e654474

Please sign in to comment.