Skip to content

Commit e3d7f65

Browse files
committed
use configuration flags / map throughout
1 parent cd5a22a commit e3d7f65

File tree

5 files changed

+58
-66
lines changed

5 files changed

+58
-66
lines changed

src/main/java/edu/isi/oba/Mapper.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.isi.oba;
22

3+
import edu.isi.oba.config.CONFIG_FLAG;
34
import edu.isi.oba.config.YamlConfig;
45
import static edu.isi.oba.Oba.logger;
56

@@ -33,17 +34,11 @@ class Mapper {
3334
YamlConfig config_data;
3435

3536
public OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
36-
private Boolean follow_references;
37-
private Boolean default_descriptions;
38-
private Boolean default_properties;
3937

4038
public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException {
4139
this.config_data = config_data;
4240
this.selected_paths = config_data.getPaths();
4341
this.mappedClasses = new ArrayList<>();
44-
this.follow_references = config_data.getFollow_references();
45-
this.default_descriptions = config_data.getDefault_descriptions();
46-
this.default_properties = config_data.getDefault_properties();
4742

4843
List<String> config_ontologies = config_data.getOntologies();
4944
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();
@@ -106,14 +101,10 @@ private void download_ontologies(List<String> config_ontologies, String destinat
106101
* The schemas includes the properties
107102
*
108103
* @param destination_dir directory to write the final results
109-
* @param config_data yaml configuration
110104
*/
111105
public void createSchemas(String destination_dir) {
112106
Query query = new Query(destination_dir);
113-
Path pathGenerator = new Path(this.config_data.getEnable_get_paths(),
114-
this.config_data.getEnable_post_paths(),
115-
this.config_data.getEnable_put_paths(),
116-
this.config_data.getEnable_delete_paths(),
107+
PathGenerator pathGenerator = new PathGenerator(this.config_data.getConfigFlags(),
117108
this.config_data.getAuth().getEnable()
118109
);
119110

@@ -148,7 +139,7 @@ public void createSchemas(String destination_dir) {
148139
}
149140
}
150141

151-
private void add_user_path(Path pathGenerator) {
142+
private void add_user_path(PathGenerator pathGenerator) {
152143
//User schema
153144
Map<String, Schema> userProperties = new HashMap<>();
154145
StringSchema username = new StringSchema();
@@ -165,7 +156,7 @@ private void add_user_path(Path pathGenerator) {
165156
this.paths.addPathItem("/user/login", pathGenerator.user_login(userSchema.getName()));
166157
}
167158

168-
private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology,
159+
private List<OWLClass> add_owlclass_to_openapi(Query query, PathGenerator pathGenerator, OWLOntology ontology,
169160
String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) {
170161
List<OWLClass> ref = new ArrayList<>();
171162
String classPrefixIRI = cls.getIRI().getNamespace();
@@ -182,7 +173,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
182173
if(ontology.containsClassInSignature(clsToCheck.getIRI())) {
183174
System.out.println("ADD "+ clsToCheck);
184175
for (OWLOntology temp_ontology : this.ontologies) {
185-
if (follow_references) {
176+
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
186177
this.mappedClasses.add(clsToCheck);
187178
getMapperSchema(query, temp_ontology, clsToCheck, this.schemaDescriptions.get(clsToCheck.getIRI()));
188179
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, clsToCheck, false);
@@ -197,11 +188,9 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
197188
logger.info("The class " + ref_class + " exists ");
198189
} else {
199190
for (OWLOntology temp_ontology : this.ontologies) {
200-
if ( follow_references ) {
191+
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
201192
this.mappedClasses.add(ref_class);
202193
getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI()));
203-
// OWLDocumentFormat format = ontology.getFormat();
204-
// String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
205194
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, ref_class, false);
206195
}
207196
}
@@ -219,7 +208,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
219208

220209
private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass cls, String cls_description) {
221210
//Convert from OWL Class to OpenAPI Schema.
222-
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, cls_description, schemaNames, ontology, this.follow_references, this.default_descriptions, this.default_properties);
211+
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, cls_description, schemaNames, ontology, this.config_data.getConfigFlags());
223212
//Write queries
224213
query.write_readme(mapperSchema.name);
225214
//Create the OpenAPI schema
@@ -228,7 +217,7 @@ private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass
228217
return mapperSchema;
229218
}
230219

231-
private void addOpenAPIPaths(Path pathGenerator, MapperSchema mapperSchema, OWLClass cls) {
220+
private void addOpenAPIPaths(PathGenerator pathGenerator, MapperSchema mapperSchema, OWLClass cls) {
232221
if (selected_classes != null && !selected_classes.contains(cls)) {
233222
logger.info("Ignoring class " + cls.toString());
234223
} else {
@@ -251,11 +240,11 @@ private void setSchemaNames(Set<OWLClass> classes) {
251240
private void setSchemaDrescriptions(Set<OWLClass> classes, OWLOntology ontology){
252241
for (OWLClass cls: classes) {
253242
System.out.println(cls);
254-
schemaDescriptions.put(cls.getIRI(), ObaUtils.getDescription(cls, ontology, this.default_descriptions));
243+
schemaDescriptions.put(cls.getIRI(), ObaUtils.getDescription(cls, ontology, this.config_data.getConfigFlagValue(CONFIG_FLAG.DEFAULT_DESCRIPTIONS)));
255244
}
256245
}
257246

258-
private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
247+
private void add_path(PathGenerator pathGenerator, MapperSchema mapperSchema) {
259248
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
260249
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
261250
//Create the plural paths: for example: /models/

src/main/java/edu/isi/oba/MapperSchema.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.isi.oba;
22

3+
import edu.isi.oba.config.CONFIG_FLAG;
34
import static edu.isi.oba.Oba.logger;
45

56
import io.swagger.v3.oas.models.examples.Example;
@@ -8,7 +9,6 @@
89

910
import java.util.*;
1011

11-
import org.openapitools.codegen.examples.ExampleGenerator;
1212
import org.semanticweb.owlapi.apibinding.OWLManager;
1313
import org.semanticweb.owlapi.model.*;
1414
import org.semanticweb.owlapi.reasoner.OWLReasoner;
@@ -35,9 +35,8 @@ class MapperSchema {
3535
private OWLOntology ontology_cls;
3636
private OWLReasonerFactory reasonerFactory;
3737
public List<OWLClass> properties_range;
38-
private boolean follow_references;
39-
private Boolean default_descriptions;
40-
private Boolean default_properties;
38+
39+
private final Map<CONFIG_FLAG, Boolean> configFlags = new HashMap<>();
4140

4241
public List<OWLObjectProperty> propertiesFromObjectRestrictions;
4342
public Map<String, List<String>> propertiesFromObjectRestrictions_ranges;
@@ -62,11 +61,9 @@ public Schema getSchema() {
6261
return this.schema;
6362
}
6463

65-
public MapperSchema(List<OWLOntology> ontologies, OWLClass cls, String clsDescription, Map<IRI, String> schemaNames, OWLOntology class_ontology, Boolean follow_references, Boolean default_descriptions, Boolean default_properties) {
64+
public MapperSchema(List<OWLOntology> ontologies, OWLClass cls, String clsDescription, Map<IRI, String> schemaNames, OWLOntology class_ontology, Map<CONFIG_FLAG, Boolean> configFlags) {
6665
this.schemaNames = schemaNames;
67-
this.follow_references = follow_references;
68-
this.default_descriptions = default_descriptions;
69-
this.default_properties = default_properties;
66+
this.configFlags.putAll(configFlags);
7067
this.cls = cls;
7168
this.cls_description = clsDescription;
7269
this.type = "object";
@@ -255,7 +252,7 @@ private Map<String, Schema> getDataProperties() {
255252
}
256253

257254
List<String> propertyRanges = getCodeGenTypesByRangeData(ranges, odp);
258-
String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.default_descriptions);
255+
String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
259256
MapperDataProperty mapperProperty = new MapperDataProperty(propertyName, propertyDescription, isFunctional, restrictionValues, valuesFromDataRestrictions_ranges, propertyRanges, array, nullable);
260257
try {
261258
this.properties.put(mapperProperty.name, mapperProperty.getSchemaByDataProperty());
@@ -267,7 +264,7 @@ private Map<String, Schema> getDataProperties() {
267264
}
268265
}
269266

270-
if (this.default_properties) {
267+
if (this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS)) {
271268
properties.putAll(this.getDefaultProperties());
272269
}
273270

@@ -364,7 +361,7 @@ private Map<String, Schema> getObjectProperties() {
364361

365362
String propertyURI = odp.getIRI().toString();
366363
propertyNameURI.put(propertyURI, propertyName);
367-
List<String> propertyRanges = getCodeGenTypesByRangeObject(ranges, odp, owlThing, this.follow_references);
364+
List<String> propertyRanges = getCodeGenTypesByRangeObject(ranges, odp, owlThing);
368365

369366
Map<String,String> restrictionValues = new HashMap<String, String>() ;
370367
for (OWLOntology ontology: this.ontologies) {
@@ -383,7 +380,7 @@ private Map<String, Schema> getObjectProperties() {
383380
propertyRanges.clear();
384381
}
385382

386-
String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.default_descriptions);
383+
String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
387384
MapperObjectProperty mapperObjectProperty = new MapperObjectProperty(propertyName, propertyDescription, isFunctional, restrictionValues, propertyRanges);
388385
try {
389386
this.properties.put(mapperObjectProperty.name, mapperObjectProperty.getSchemaByObjectProperty());
@@ -421,10 +418,9 @@ private List<String> getCodeGenTypesByRangeData(Set<OWLDataPropertyRangeAxiom> r
421418
* @param ranges Represents a ObjectPropertyRange
422419
* @param odp Represents a OWLObjectProperty
423420
* @param owlThing
424-
* @param follow_references
425421
* @return A list<String> with the properties
426422
*/
427-
private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxiom> ranges, OWLObjectProperty odp, OWLClass owlThing, boolean follow_references) {
423+
private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxiom> ranges, OWLObjectProperty odp, OWLClass owlThing) {
428424
List<String> objectProperty = new ArrayList<>();
429425

430426
for (OWLObjectPropertyRangeAxiom propertyRangeAxiom : ranges) {
@@ -436,7 +432,7 @@ private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxio
436432
}
437433
else {
438434
this.properties_range.add(rangeClass.asOWLClass());
439-
if (follow_references)
435+
if (this.configFlags.get(CONFIG_FLAG.FOLLOW_REFERENCES))
440436
objectProperty.add(getSchemaName(rangeClass.asOWLClass()));
441437
}
442438
}
@@ -488,7 +484,7 @@ private void getClassRestrictions(OWLClass analyzedClass) {
488484
} else {
489485
for (OWLObjectProperty op: this.propertiesFromObjectRestrictions) {
490486
MapperObjectProperty mapperObjectProperty;
491-
String propertyDescription = ObaUtils.getDescription(op, this.ontology_cls, this.default_descriptions);
487+
String propertyDescription = ObaUtils.getDescription(op, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
492488
if (!this.propertiesFromObjectRestrictions_ranges.isEmpty()) {
493489
List<String> rangesOP = this.propertiesFromObjectRestrictions_ranges.get(this.sfp.getShortForm(op.getIRI()));
494490
for (String j : restrictionsValuesFromClass.keySet()) {
@@ -512,7 +508,7 @@ private void getClassRestrictions(OWLClass analyzedClass) {
512508

513509
for (OWLDataProperty dp: this.propertiesFromDataRestrictions) {
514510
List<String> valuesFromDataRestrictions_ranges = new ArrayList<>();
515-
String propertyDescription = ObaUtils.getDescription(dp, this.ontology_cls, this.default_descriptions);
511+
String propertyDescription = ObaUtils.getDescription(dp, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
516512
if (!this.propertiesFromDataRestrictions_ranges.isEmpty()) {
517513
List<String> rangesDP = this.propertiesFromDataRestrictions_ranges.get(this.sfp.getShortForm(dp.getIRI()));
518514
for (String j: restrictionsValuesFromClass.keySet()) {
@@ -537,7 +533,7 @@ private void getClassRestrictions(OWLClass analyzedClass) {
537533
}
538534
}
539535

540-
if (this.default_properties) {
536+
if (this.configFlags.get(CONFIG_FLAG.DEFAULT_PROPERTIES)) {
541537
this.properties.putAll(this.getDefaultProperties());
542538
}
543539
}

src/test/java/edu/isi/oba/MapperTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static edu.isi.oba.ObaUtils.get_yaml_data;
44
import edu.isi.oba.config.AuthConfig;
5+
import edu.isi.oba.config.CONFIG_FLAG;
56
import edu.isi.oba.config.YamlConfig;
67

78
import java.io.File;
@@ -10,6 +11,7 @@
1011
import java.util.ArrayList;
1112
import java.util.Collections;
1213
import java.util.List;
14+
import java.util.Map;
1315
import java.util.logging.ConsoleHandler;
1416
import java.util.logging.Level;
1517
import java.util.logging.LogManager;
@@ -18,7 +20,6 @@
1820
import io.swagger.v3.oas.models.media.Schema;
1921

2022
import org.junit.jupiter.api.Assertions;
21-
import org.junit.jupiter.api.Disabled;
2223
import org.junit.jupiter.api.Test;
2324

2425
import org.semanticweb.owlapi.model.OWLClass;
@@ -113,7 +114,7 @@ public void testComplexOntology() throws Exception{
113114
Mapper mapper = new Mapper(config_data);
114115
OWLClass cls = mapper.manager.getOWLDataFactory().getOWLClass("https://businessontology.com/ontology/Person");
115116
String desc = ObaUtils.getDescription(cls, mapper.ontologies.get(0), true);
116-
MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), true, true, true);
117+
MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), Map.ofEntries(Map.entry(CONFIG_FLAG.DEFAULT_DESCRIPTIONS, true), Map.entry(CONFIG_FLAG.DEFAULT_PROPERTIES, true), Map.entry(CONFIG_FLAG.FOLLOW_REFERENCES, true)));
117118
Schema schema = mapperSchema.getSchema();
118119
// The person schema must not be null.
119120
Assertions.assertNotNull(schema);

src/test/java/edu/isi/oba/ObaUtilsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import org.json.JSONObject;
1010

1111
import org.junit.jupiter.api.Assertions;
12-
import org.junit.jupiter.api.Disabled;
1312
import org.junit.jupiter.api.Test;
1413

1514
import org.semanticweb.owlapi.model.OWLClass;
1615
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
1716

18-
@Disabled
1917
public class ObaUtilsTest {
2018

2119
@Test

0 commit comments

Comments
 (0)