Skip to content

Commit

Permalink
use configuration flags / map throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
cweedall committed May 1, 2024
1 parent cd5a22a commit e3d7f65
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 66 deletions.
31 changes: 10 additions & 21 deletions src/main/java/edu/isi/oba/Mapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.isi.oba;

import edu.isi.oba.config.CONFIG_FLAG;
import edu.isi.oba.config.YamlConfig;
import static edu.isi.oba.Oba.logger;

Expand Down Expand Up @@ -33,17 +34,11 @@ class Mapper {
YamlConfig config_data;

public OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
private Boolean follow_references;
private Boolean default_descriptions;
private Boolean default_properties;

public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException {
this.config_data = config_data;
this.selected_paths = config_data.getPaths();
this.mappedClasses = new ArrayList<>();
this.follow_references = config_data.getFollow_references();
this.default_descriptions = config_data.getDefault_descriptions();
this.default_properties = config_data.getDefault_properties();

List<String> config_ontologies = config_data.getOntologies();
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();
Expand Down Expand Up @@ -106,14 +101,10 @@ private void download_ontologies(List<String> config_ontologies, String destinat
* The schemas includes the properties
*
* @param destination_dir directory to write the final results
* @param config_data yaml configuration
*/
public void createSchemas(String destination_dir) {
Query query = new Query(destination_dir);
Path pathGenerator = new Path(this.config_data.getEnable_get_paths(),
this.config_data.getEnable_post_paths(),
this.config_data.getEnable_put_paths(),
this.config_data.getEnable_delete_paths(),
PathGenerator pathGenerator = new PathGenerator(this.config_data.getConfigFlags(),
this.config_data.getAuth().getEnable()
);

Expand Down Expand Up @@ -148,7 +139,7 @@ public void createSchemas(String destination_dir) {
}
}

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

private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology,
private List<OWLClass> add_owlclass_to_openapi(Query query, PathGenerator pathGenerator, OWLOntology ontology,
String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) {
List<OWLClass> ref = new ArrayList<>();
String classPrefixIRI = cls.getIRI().getNamespace();
Expand All @@ -182,7 +173,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
if(ontology.containsClassInSignature(clsToCheck.getIRI())) {
System.out.println("ADD "+ clsToCheck);
for (OWLOntology temp_ontology : this.ontologies) {
if (follow_references) {
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
this.mappedClasses.add(clsToCheck);
getMapperSchema(query, temp_ontology, clsToCheck, this.schemaDescriptions.get(clsToCheck.getIRI()));
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, clsToCheck, false);
Expand All @@ -197,11 +188,9 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
logger.info("The class " + ref_class + " exists ");
} else {
for (OWLOntology temp_ontology : this.ontologies) {
if ( follow_references ) {
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
this.mappedClasses.add(ref_class);
getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI()));
// OWLDocumentFormat format = ontology.getFormat();
// String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, ref_class, false);
}
}
Expand All @@ -219,7 +208,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,

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

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

private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
private void add_path(PathGenerator pathGenerator, MapperSchema mapperSchema) {
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
//Create the plural paths: for example: /models/
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/edu/isi/oba/MapperSchema.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.isi.oba;

import edu.isi.oba.config.CONFIG_FLAG;
import static edu.isi.oba.Oba.logger;

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

import java.util.*;

import org.openapitools.codegen.examples.ExampleGenerator;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
Expand All @@ -35,9 +35,8 @@ class MapperSchema {
private OWLOntology ontology_cls;
private OWLReasonerFactory reasonerFactory;
public List<OWLClass> properties_range;
private boolean follow_references;
private Boolean default_descriptions;
private Boolean default_properties;

private final Map<CONFIG_FLAG, Boolean> configFlags = new HashMap<>();

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

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) {
public MapperSchema(List<OWLOntology> ontologies, OWLClass cls, String clsDescription, Map<IRI, String> schemaNames, OWLOntology class_ontology, Map<CONFIG_FLAG, Boolean> configFlags) {
this.schemaNames = schemaNames;
this.follow_references = follow_references;
this.default_descriptions = default_descriptions;
this.default_properties = default_properties;
this.configFlags.putAll(configFlags);
this.cls = cls;
this.cls_description = clsDescription;
this.type = "object";
Expand Down Expand Up @@ -255,7 +252,7 @@ private Map<String, Schema> getDataProperties() {
}

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

if (this.default_properties) {
if (this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS)) {
properties.putAll(this.getDefaultProperties());
}

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

String propertyURI = odp.getIRI().toString();
propertyNameURI.put(propertyURI, propertyName);
List<String> propertyRanges = getCodeGenTypesByRangeObject(ranges, odp, owlThing, this.follow_references);
List<String> propertyRanges = getCodeGenTypesByRangeObject(ranges, odp, owlThing);

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

String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.default_descriptions);
String propertyDescription = ObaUtils.getDescription(odp, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
MapperObjectProperty mapperObjectProperty = new MapperObjectProperty(propertyName, propertyDescription, isFunctional, restrictionValues, propertyRanges);
try {
this.properties.put(mapperObjectProperty.name, mapperObjectProperty.getSchemaByObjectProperty());
Expand Down Expand Up @@ -421,10 +418,9 @@ private List<String> getCodeGenTypesByRangeData(Set<OWLDataPropertyRangeAxiom> r
* @param ranges Represents a ObjectPropertyRange
* @param odp Represents a OWLObjectProperty
* @param owlThing
* @param follow_references
* @return A list<String> with the properties
*/
private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxiom> ranges, OWLObjectProperty odp, OWLClass owlThing, boolean follow_references) {
private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxiom> ranges, OWLObjectProperty odp, OWLClass owlThing) {
List<String> objectProperty = new ArrayList<>();

for (OWLObjectPropertyRangeAxiom propertyRangeAxiom : ranges) {
Expand All @@ -436,7 +432,7 @@ private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxio
}
else {
this.properties_range.add(rangeClass.asOWLClass());
if (follow_references)
if (this.configFlags.get(CONFIG_FLAG.FOLLOW_REFERENCES))
objectProperty.add(getSchemaName(rangeClass.asOWLClass()));
}
}
Expand Down Expand Up @@ -488,7 +484,7 @@ private void getClassRestrictions(OWLClass analyzedClass) {
} else {
for (OWLObjectProperty op: this.propertiesFromObjectRestrictions) {
MapperObjectProperty mapperObjectProperty;
String propertyDescription = ObaUtils.getDescription(op, this.ontology_cls, this.default_descriptions);
String propertyDescription = ObaUtils.getDescription(op, this.ontology_cls, this.configFlags.get(CONFIG_FLAG.DEFAULT_DESCRIPTIONS));
if (!this.propertiesFromObjectRestrictions_ranges.isEmpty()) {
List<String> rangesOP = this.propertiesFromObjectRestrictions_ranges.get(this.sfp.getShortForm(op.getIRI()));
for (String j : restrictionsValuesFromClass.keySet()) {
Expand All @@ -512,7 +508,7 @@ private void getClassRestrictions(OWLClass analyzedClass) {

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

if (this.default_properties) {
if (this.configFlags.get(CONFIG_FLAG.DEFAULT_PROPERTIES)) {
this.properties.putAll(this.getDefaultProperties());
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/edu/isi/oba/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static edu.isi.oba.ObaUtils.get_yaml_data;
import edu.isi.oba.config.AuthConfig;
import edu.isi.oba.config.CONFIG_FLAG;
import edu.isi.oba.config.YamlConfig;

import java.io.File;
Expand All @@ -10,6 +11,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
Expand All @@ -18,7 +20,6 @@
import io.swagger.v3.oas.models.media.Schema;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.semanticweb.owlapi.model.OWLClass;
Expand Down Expand Up @@ -113,7 +114,7 @@ public void testComplexOntology() throws Exception{
Mapper mapper = new Mapper(config_data);
OWLClass cls = mapper.manager.getOWLDataFactory().getOWLClass("https://businessontology.com/ontology/Person");
String desc = ObaUtils.getDescription(cls, mapper.ontologies.get(0), true);
MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), true, true, true);
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)));
Schema schema = mapperSchema.getSchema();
// The person schema must not be null.
Assertions.assertNotNull(schema);
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/edu/isi/oba/ObaUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import org.json.JSONObject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

@Disabled
public class ObaUtilsTest {

@Test
Expand Down
Loading

0 comments on commit e3d7f65

Please sign in to comment.