Skip to content

Commit

Permalink
Fix #155
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarijo committed Feb 18, 2021
1 parent 2f4c01b commit 704241b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 63 deletions.
65 changes: 36 additions & 29 deletions src/main/java/edu/isi/oba/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ class Mapper {
List<String> selected_paths;
List<OWLOntology> ontologies;
List<OWLClass> selected_classes;
List<OWLClass> mapped_classes;
List<OWLClass> mappedClasses;
YamlConfig config_data;

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

public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException {
this.config_data = config_data;
List<String> 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<String> config_ontologies = config_data.getOntologies();
Expand Down Expand Up @@ -77,7 +76,7 @@ private void download_ontologies(List<String> 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);
Expand All @@ -98,8 +97,8 @@ private void download_ontologies(List<String> 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);
Expand All @@ -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<OWLClass> 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);
}
}
}
}
Expand All @@ -156,24 +151,42 @@ private void add_user_path(Path pathGenerator) {

private List<OWLClass> 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<OWLClass> 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);
}
}
}
Expand Down Expand Up @@ -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<OWLClass> filter_classes() {
List<String> selected_classes_iri = this.config_data.getClasses();
List<OWLClass> filtered_classes = new ArrayList();
ArrayList<OWLClass> filtered_classes = new ArrayList();
for (OWLOntology ontology : this.ontologies) {
for (OWLClass cls : ontology.getClassesInSignature()) {
if (selected_classes_iri.contains(cls.getIRI().toString())) {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/edu/isi/oba/MapperSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public List<OWLClass> getProperties_range() {
return properties_range;
}

public List<String> getPropertiesFromObjectRestrictions_ranges(){
List<String> aggregatedClasses = new ArrayList<>();
for (List<String> l : propertiesFromObjectRestrictions_ranges.values()){
aggregatedClasses.addAll(l);
}
return aggregatedClasses;
}

public Schema getSchema() {
return schema;
}
Expand Down Expand Up @@ -98,12 +106,6 @@ private Schema setSchema() {
return schema;
}

private List<String> required() {
return new ArrayList<String>() {{
//add("id");
}};
}

/**
* Check if the class cls is domain of the property dp
*
Expand Down Expand Up @@ -197,8 +199,8 @@ private Map<String, Schema> getDataProperties() {
}
Map<String, Map<String,String>> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass();
for (String j : restrictionsValuesFromClass.keySet()) {
if (j==propertyName) {
restrictionValues=restrictionsValuesFromClass.get(j);
if (j.equals(propertyName)) {
restrictionValues = restrictionsValuesFromClass.get(j);
}
}
}
Expand Down Expand Up @@ -239,7 +241,6 @@ private void addDefaultProperties(Map<String, Schema> properties) {
properties.put(labelProperty.name, labelProperty.getSchemaByDataProperty());
properties.put(typeProperty.name, typeProperty.getSchemaByDataProperty());
properties.put(descriptionProperty.name, descriptionProperty.getSchemaByDataProperty());

}

/**
Expand Down Expand Up @@ -307,7 +308,7 @@ private Map<String, Schema> getObjectProperties() {
}
Map<String, Map<String,String>> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass();
for (String j : restrictionsValuesFromClass.keySet()) {
if (j==propertyName) {
if (j.equals(propertyName)) {
restrictionValues=restrictionsValuesFromClass.get(j);
}
}
Expand Down Expand Up @@ -386,14 +387,13 @@ private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxio
* @param analyzedClass Class that will be analyzed in order to get its restrictions
*/
private void getClassRestrictions(OWLClass analyzedClass){
//this is failing when classes are subclasses of unions or intersections
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLDataFactory dataFactory = m.getOWLDataFactory();
OWLClass owlThing = dataFactory.getOWLThing();
RestrictionVisitor restrictionVisitor;

for (OWLOntology ontology : ontologies) {
restrictionVisitor = new RestrictionVisitor(analyzedClass,ontology,owlThing, "");
restrictionVisitor = new RestrictionVisitor(analyzedClass, ontology, owlThing, "");
for (OWLSubClassOfAxiom ax : ontology.getSubClassAxiomsForSubClass(analyzedClass)) {
OWLClassExpression superCls = ax.getSuperClass();
// Ask our superclass to accept a visit from the RestrictionVisitor
Expand All @@ -410,7 +410,7 @@ private void getClassRestrictions(OWLClass analyzedClass){
if (restrictionsValuesFromClass.size()!=0) {
// When the restriction is a ObjectComplementOf it doesn't have a object property,
// thus we need to set its value at the setSchema function
if (restrictionsValuesFromClass.containsKey("complementOf") && restrictionsValuesFromClass.size()==1) {
if (restrictionsValuesFromClass.containsKey("complementOf") && restrictionsValuesFromClass.size() == 1) {
for (String j : restrictionsValuesFromClass.keySet()) {
Map<String,String> restrictionValues=restrictionsValuesFromClass.get(j);
for (String restriction: restrictionValues.keySet()) {
Expand Down Expand Up @@ -442,7 +442,7 @@ private void getClassRestrictions(OWLClass analyzedClass){
}
}
for (OWLDataProperty dp : propertiesFromDataRestrictions) {
List<String> valuesFromDataRestrictions_ranges = new ArrayList<String>();
List<String> valuesFromDataRestrictions_ranges = new ArrayList<>();
String propertyDescription = ObaUtils.getDescription(dp, ontology_cls);
if (propertiesFromDataRestrictions_ranges.size() != 0) {
List<String> rangesDP = propertiesFromDataRestrictions_ranges.get(sfp.getShortForm(dp.getIRI()));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/isi/oba/Oba.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
20 changes: 0 additions & 20 deletions src/test/java/edu/isi/oba/AppTest.java

This file was deleted.

40 changes: 40 additions & 0 deletions src/test/java/edu/isi/oba/MapperTest.java
Original file line number Diff line number Diff line change
@@ -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;

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

0 comments on commit 704241b

Please sign in to comment.