diff --git a/src/main/java/edu/isi/oba/Mapper.java b/src/main/java/edu/isi/oba/Mapper.java index 1ab2b92..97bb892 100644 --- a/src/main/java/edu/isi/oba/Mapper.java +++ b/src/main/java/edu/isi/oba/Mapper.java @@ -28,7 +28,7 @@ class Mapper { List selected_paths; List ontologies; List selected_classes; - List mapped_classes; + List mappedClasses; YamlConfig config_data; public OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); @@ -36,9 +36,8 @@ class Mapper { public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException { this.config_data = config_data; - List paths = config_data.getPaths(); - this.selected_paths = paths; - this.mapped_classes = new ArrayList<>(); + this.selected_paths = config_data.getPaths(); + this.mappedClasses = new ArrayList<>(); this.follow_references = config_data.getFollow_references(); List config_ontologies = config_data.getOntologies(); @@ -77,7 +76,7 @@ private void download_ontologies(List config_ontologies, String destinat } else{ try { - //copy to right folder + //copy to the right folder Files.copy(new File(ontologyPath).toPath(), ontologyFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { Logger.getLogger(Mapper.class.getName()).log(Level.SEVERE, "ERROR while loading file: "+ontologyPath, ex); @@ -98,8 +97,8 @@ private void download_ontologies(List config_ontologies, String destinat * Obtain Schemas using the ontology classes * The schemas includes the properties * - * @param config_data - * @return schemas + * @param destination_dir directory to write the final results + * @param config_data yaml configuration */ public void createSchemas(String destination_dir, YamlConfig config_data) { Query query = new Query(destination_dir); @@ -117,19 +116,15 @@ public void createSchemas(String destination_dir, YamlConfig config_data) { for (OWLOntology ontology : this.ontologies) { OWLDocumentFormat format = ontology.getFormat(); - //String defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix(); String defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix(); Set classes = ontology.getClassesInSignature(); - /** - * Find the classes and return the related classes - */ for (OWLClass cls : classes) { - //filter if the class prefix is not the default ontology's prefix + //filter if the class prefix does not have the default ontology prefix if (cls.getIRI() != null) { - if (selected_classes != null && !selected_classes.contains(cls)) - continue; - add_owlclass_to_openapi(query, pathGenerator, ontology, defaultOntologyPrefixIRI, cls, true); + if (selected_classes == null || selected_classes.contains(cls)){ + add_owlclass_to_openapi(query, pathGenerator, ontology, defaultOntologyPrefixIRI, cls, true); + } } } } @@ -156,24 +151,42 @@ private void add_user_path(Path pathGenerator) { private List add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology, String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) { - //This method fails to parse a class when it has a some property with union of classes. List ref = new ArrayList<>(); String classPrefixIRI = cls.getIRI().getNamespace(); if (defaultOntologyPrefixIRI.equals(classPrefixIRI)) { try{ MapperSchema mapperSchema = getMapperSchema(query, ontology, cls, this.schemaDescriptions.get(cls.getIRI())); + // add references to schemas in class restrictions (check selected classes to avoid conflicts) + for (String classToCheck : mapperSchema.getPropertiesFromObjectRestrictions_ranges()) { + OWLClass clsToCheck = manager.getOWLDataFactory().getOWLClass(IRI.create(classPrefixIRI + classToCheck)); + if (this.mappedClasses.contains(clsToCheck) || this.selected_classes.contains(clsToCheck)){ + logger.info("The class " + clsToCheck + " exists "); + } else { + //rare cases have instances, so we filter them out and recheck that the target is a class. + if(ontology.containsClassInSignature(clsToCheck.getIRI())) { + System.out.println("ADD "+ clsToCheck); + for (OWLOntology temp_ontology : this.ontologies) { + if (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); + } + } + } + } + } + // add references to schemas in property ranges for (OWLClass ref_class : mapperSchema.getProperties_range()) { - if (this.mapped_classes.contains(ref_class)){ + if (this.mappedClasses.contains(ref_class)){ logger.info("The class " + ref_class + " exists "); } else { for (OWLOntology temp_ontology : this.ontologies) { if ( follow_references ) { - this.mapped_classes.add(ref_class); + this.mappedClasses.add(ref_class); getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI())); - OWLDocumentFormat format = ontology.getFormat(); - //String temp_defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix(); - String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix(); - add_owlclass_to_openapi(query, pathGenerator, temp_ontology, temp_defaultOntologyPrefixIRI, ref_class, false); +// OWLDocumentFormat format = ontology.getFormat(); +// String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix(); + add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, ref_class, false); } } } @@ -236,16 +249,10 @@ private void add_path(Path pathGenerator, MapperSchema mapperSchema) { mapperSchema.getCls().getIRI().getIRIString())); } -// Method not used -// private void add_path_relation(Path pathGenerator, String schema_name, String predicate, String path) { -// String relation = "/" + schema_name.toLowerCase() + "s/{id}/" + path; -// this.paths.addPathItem(relation, pathGenerator.generate_plural(schema_name)); -// -// } public List filter_classes() { List selected_classes_iri = this.config_data.getClasses(); - List filtered_classes = new ArrayList(); + ArrayList filtered_classes = new ArrayList(); for (OWLOntology ontology : this.ontologies) { for (OWLClass cls : ontology.getClassesInSignature()) { if (selected_classes_iri.contains(cls.getIRI().toString())) { diff --git a/src/main/java/edu/isi/oba/MapperSchema.java b/src/main/java/edu/isi/oba/MapperSchema.java index ad8fec7..75d784f 100644 --- a/src/main/java/edu/isi/oba/MapperSchema.java +++ b/src/main/java/edu/isi/oba/MapperSchema.java @@ -45,6 +45,14 @@ public List getProperties_range() { return properties_range; } + public List getPropertiesFromObjectRestrictions_ranges(){ + List aggregatedClasses = new ArrayList<>(); + for (List l : propertiesFromObjectRestrictions_ranges.values()){ + aggregatedClasses.addAll(l); + } + return aggregatedClasses; + } + public Schema getSchema() { return schema; } @@ -98,12 +106,6 @@ private Schema setSchema() { return schema; } - private List required() { - return new ArrayList() {{ - //add("id"); - }}; - } - /** * Check if the class cls is domain of the property dp * @@ -197,8 +199,8 @@ private Map getDataProperties() { } Map> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass(); for (String j : restrictionsValuesFromClass.keySet()) { - if (j==propertyName) { - restrictionValues=restrictionsValuesFromClass.get(j); + if (j.equals(propertyName)) { + restrictionValues = restrictionsValuesFromClass.get(j); } } } @@ -239,7 +241,6 @@ private void addDefaultProperties(Map properties) { properties.put(labelProperty.name, labelProperty.getSchemaByDataProperty()); properties.put(typeProperty.name, typeProperty.getSchemaByDataProperty()); properties.put(descriptionProperty.name, descriptionProperty.getSchemaByDataProperty()); - } /** @@ -307,7 +308,7 @@ private Map getObjectProperties() { } Map> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass(); for (String j : restrictionsValuesFromClass.keySet()) { - if (j==propertyName) { + if (j.equals(propertyName)) { restrictionValues=restrictionsValuesFromClass.get(j); } } @@ -386,14 +387,13 @@ private List getCodeGenTypesByRangeObject(Set restrictionValues=restrictionsValuesFromClass.get(j); for (String restriction: restrictionValues.keySet()) { @@ -442,7 +442,7 @@ private void getClassRestrictions(OWLClass analyzedClass){ } } for (OWLDataProperty dp : propertiesFromDataRestrictions) { - List valuesFromDataRestrictions_ranges = new ArrayList(); + List valuesFromDataRestrictions_ranges = new ArrayList<>(); String propertyDescription = ObaUtils.getDescription(dp, ontology_cls); if (propertiesFromDataRestrictions_ranges.size() != 0) { List rangesDP = propertiesFromDataRestrictions_ranges.get(sfp.getShortForm(dp.getIRI())); diff --git a/src/main/java/edu/isi/oba/Oba.java b/src/main/java/edu/isi/oba/Oba.java index d420d31..f99fcc2 100644 --- a/src/main/java/edu/isi/oba/Oba.java +++ b/src/main/java/edu/isi/oba/Oba.java @@ -84,6 +84,7 @@ public static void main(String[] args) throws Exception { logger.info("OBA finished successfully. Output can be found at: "+destination_dir); }catch (Exception e){ logger.severe("Error while creating the API specification: "+e.getLocalizedMessage()); + e.printStackTrace(); System.exit(1); } } diff --git a/src/test/java/edu/isi/oba/AppTest.java b/src/test/java/edu/isi/oba/AppTest.java deleted file mode 100644 index 72e4b71..0000000 --- a/src/test/java/edu/isi/oba/AppTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package edu.isi.oba; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -/** - * Unit test for simple App. - */ -public class AppTest -{ - /** - * Rigorous Test :-) - */ - @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); - } -} diff --git a/src/test/java/edu/isi/oba/MapperTest.java b/src/test/java/edu/isi/oba/MapperTest.java index d07a21f..9048d5a 100644 --- a/src/test/java/edu/isi/oba/MapperTest.java +++ b/src/test/java/edu/isi/oba/MapperTest.java @@ -1,13 +1,24 @@ package edu.isi.oba; +import edu.isi.oba.config.AuthConfig; import edu.isi.oba.config.YamlConfig; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.media.Schema; import org.junit.Assert; import org.junit.Test; import org.semanticweb.owlapi.model.OWLClass; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.logging.ConsoleHandler; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; import static edu.isi.oba.ObaUtils.get_yaml_data; @@ -78,4 +89,33 @@ public void testMissingImportOntology() throws Exception{ Mapper mapper = new Mapper(config_data); Assert.assertEquals(false, mapper.ontologies.isEmpty()); } + + /** + * Test an ontology (very simple, two classes) with a missing import + */ + @Test + public void testComplexOntology() throws Exception{ + InputStream stream = Oba.class.getClassLoader().getResourceAsStream("logging.properties"); + try { + LogManager.getLogManager().readConfiguration(stream); + edu.isi.oba.Oba.logger = Logger.getLogger(Oba.class.getName()); + + } catch (IOException e) { + e.printStackTrace(); + } + edu.isi.oba.Oba.logger.setLevel(Level.FINE); + edu.isi.oba.Oba.logger.addHandler(new ConsoleHandler()); + String example_remote = "src/test/resources/complex_expr/config.yaml"; + YamlConfig config_data = get_yaml_data(example_remote); + String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName(); + config_data.setAuth(new AuthConfig()); + 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)); + MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), true); + Schema schema = mapperSchema.getSchema(); + // The person schema must not be null. + Assert.assertNotNull(schema); + Assert.assertEquals(schema.getName(),"Person"); + } }