Skip to content

Commit 704241b

Browse files
committed
Fix #155
1 parent 2f4c01b commit 704241b

5 files changed

Lines changed: 91 additions & 63 deletions

File tree

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

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ class Mapper {
2828
List<String> selected_paths;
2929
List<OWLOntology> ontologies;
3030
List<OWLClass> selected_classes;
31-
List<OWLClass> mapped_classes;
31+
List<OWLClass> mappedClasses;
3232
YamlConfig config_data;
3333

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

3737
public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException {
3838
this.config_data = config_data;
39-
List<String> paths = config_data.getPaths();
40-
this.selected_paths = paths;
41-
this.mapped_classes = new ArrayList<>();
39+
this.selected_paths = config_data.getPaths();
40+
this.mappedClasses = new ArrayList<>();
4241
this.follow_references = config_data.getFollow_references();
4342

4443
List<String> config_ontologies = config_data.getOntologies();
@@ -77,7 +76,7 @@ private void download_ontologies(List<String> config_ontologies, String destinat
7776
}
7877
else{
7978
try {
80-
//copy to right folder
79+
//copy to the right folder
8180
Files.copy(new File(ontologyPath).toPath(), ontologyFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
8281
} catch (IOException ex) {
8382
Logger.getLogger(Mapper.class.getName()).log(Level.SEVERE, "ERROR while loading file: "+ontologyPath, ex);
@@ -98,8 +97,8 @@ private void download_ontologies(List<String> config_ontologies, String destinat
9897
* Obtain Schemas using the ontology classes
9998
* The schemas includes the properties
10099
*
101-
* @param config_data
102-
* @return schemas
100+
* @param destination_dir directory to write the final results
101+
* @param config_data yaml configuration
103102
*/
104103
public void createSchemas(String destination_dir, YamlConfig config_data) {
105104
Query query = new Query(destination_dir);
@@ -117,19 +116,15 @@ public void createSchemas(String destination_dir, YamlConfig config_data) {
117116
for (OWLOntology ontology : this.ontologies) {
118117

119118
OWLDocumentFormat format = ontology.getFormat();
120-
//String defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix();
121119
String defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
122120
Set<OWLClass> classes = ontology.getClassesInSignature();
123121

124-
/**
125-
* Find the classes and return the related classes
126-
*/
127122
for (OWLClass cls : classes) {
128-
//filter if the class prefix is not the default ontology's prefix
123+
//filter if the class prefix does not have the default ontology prefix
129124
if (cls.getIRI() != null) {
130-
if (selected_classes != null && !selected_classes.contains(cls))
131-
continue;
132-
add_owlclass_to_openapi(query, pathGenerator, ontology, defaultOntologyPrefixIRI, cls, true);
125+
if (selected_classes == null || selected_classes.contains(cls)){
126+
add_owlclass_to_openapi(query, pathGenerator, ontology, defaultOntologyPrefixIRI, cls, true);
127+
}
133128
}
134129
}
135130
}
@@ -156,24 +151,42 @@ private void add_user_path(Path pathGenerator) {
156151

157152
private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology,
158153
String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) {
159-
//This method fails to parse a class when it has a some property with union of classes.
160154
List<OWLClass> ref = new ArrayList<>();
161155
String classPrefixIRI = cls.getIRI().getNamespace();
162156
if (defaultOntologyPrefixIRI.equals(classPrefixIRI)) {
163157
try{
164158
MapperSchema mapperSchema = getMapperSchema(query, ontology, cls, this.schemaDescriptions.get(cls.getIRI()));
159+
// add references to schemas in class restrictions (check selected classes to avoid conflicts)
160+
for (String classToCheck : mapperSchema.getPropertiesFromObjectRestrictions_ranges()) {
161+
OWLClass clsToCheck = manager.getOWLDataFactory().getOWLClass(IRI.create(classPrefixIRI + classToCheck));
162+
if (this.mappedClasses.contains(clsToCheck) || this.selected_classes.contains(clsToCheck)){
163+
logger.info("The class " + clsToCheck + " exists ");
164+
} else {
165+
//rare cases have instances, so we filter them out and recheck that the target is a class.
166+
if(ontology.containsClassInSignature(clsToCheck.getIRI())) {
167+
System.out.println("ADD "+ clsToCheck);
168+
for (OWLOntology temp_ontology : this.ontologies) {
169+
if (follow_references) {
170+
this.mappedClasses.add(clsToCheck);
171+
getMapperSchema(query, temp_ontology, clsToCheck, this.schemaDescriptions.get(clsToCheck.getIRI()));
172+
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, clsToCheck, false);
173+
}
174+
}
175+
}
176+
}
177+
}
178+
// add references to schemas in property ranges
165179
for (OWLClass ref_class : mapperSchema.getProperties_range()) {
166-
if (this.mapped_classes.contains(ref_class)){
180+
if (this.mappedClasses.contains(ref_class)){
167181
logger.info("The class " + ref_class + " exists ");
168182
} else {
169183
for (OWLOntology temp_ontology : this.ontologies) {
170184
if ( follow_references ) {
171-
this.mapped_classes.add(ref_class);
185+
this.mappedClasses.add(ref_class);
172186
getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI()));
173-
OWLDocumentFormat format = ontology.getFormat();
174-
//String temp_defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix();
175-
String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
176-
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, temp_defaultOntologyPrefixIRI, ref_class, false);
187+
// OWLDocumentFormat format = ontology.getFormat();
188+
// String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
189+
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, ref_class, false);
177190
}
178191
}
179192
}
@@ -236,16 +249,10 @@ private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
236249
mapperSchema.getCls().getIRI().getIRIString()));
237250
}
238251

239-
// Method not used
240-
// private void add_path_relation(Path pathGenerator, String schema_name, String predicate, String path) {
241-
// String relation = "/" + schema_name.toLowerCase() + "s/{id}/" + path;
242-
// this.paths.addPathItem(relation, pathGenerator.generate_plural(schema_name));
243-
//
244-
// }
245252

246253
public List<OWLClass> filter_classes() {
247254
List<String> selected_classes_iri = this.config_data.getClasses();
248-
List<OWLClass> filtered_classes = new ArrayList();
255+
ArrayList<OWLClass> filtered_classes = new ArrayList();
249256
for (OWLOntology ontology : this.ontologies) {
250257
for (OWLClass cls : ontology.getClassesInSignature()) {
251258
if (selected_classes_iri.contains(cls.getIRI().toString())) {

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public List<OWLClass> getProperties_range() {
4545
return properties_range;
4646
}
4747

48+
public List<String> getPropertiesFromObjectRestrictions_ranges(){
49+
List<String> aggregatedClasses = new ArrayList<>();
50+
for (List<String> l : propertiesFromObjectRestrictions_ranges.values()){
51+
aggregatedClasses.addAll(l);
52+
}
53+
return aggregatedClasses;
54+
}
55+
4856
public Schema getSchema() {
4957
return schema;
5058
}
@@ -98,12 +106,6 @@ private Schema setSchema() {
98106
return schema;
99107
}
100108

101-
private List<String> required() {
102-
return new ArrayList<String>() {{
103-
//add("id");
104-
}};
105-
}
106-
107109
/**
108110
* Check if the class cls is domain of the property dp
109111
*
@@ -197,8 +199,8 @@ private Map<String, Schema> getDataProperties() {
197199
}
198200
Map<String, Map<String,String>> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass();
199201
for (String j : restrictionsValuesFromClass.keySet()) {
200-
if (j==propertyName) {
201-
restrictionValues=restrictionsValuesFromClass.get(j);
202+
if (j.equals(propertyName)) {
203+
restrictionValues = restrictionsValuesFromClass.get(j);
202204
}
203205
}
204206
}
@@ -239,7 +241,6 @@ private void addDefaultProperties(Map<String, Schema> properties) {
239241
properties.put(labelProperty.name, labelProperty.getSchemaByDataProperty());
240242
properties.put(typeProperty.name, typeProperty.getSchemaByDataProperty());
241243
properties.put(descriptionProperty.name, descriptionProperty.getSchemaByDataProperty());
242-
243244
}
244245

245246
/**
@@ -307,7 +308,7 @@ private Map<String, Schema> getObjectProperties() {
307308
}
308309
Map<String, Map<String,String>> restrictionsValuesFromClass = restrictionVisitor.getRestrictionsValuesFromClass();
309310
for (String j : restrictionsValuesFromClass.keySet()) {
310-
if (j==propertyName) {
311+
if (j.equals(propertyName)) {
311312
restrictionValues=restrictionsValuesFromClass.get(j);
312313
}
313314
}
@@ -386,14 +387,13 @@ private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxio
386387
* @param analyzedClass Class that will be analyzed in order to get its restrictions
387388
*/
388389
private void getClassRestrictions(OWLClass analyzedClass){
389-
//this is failing when classes are subclasses of unions or intersections
390390
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
391391
OWLDataFactory dataFactory = m.getOWLDataFactory();
392392
OWLClass owlThing = dataFactory.getOWLThing();
393393
RestrictionVisitor restrictionVisitor;
394394

395395
for (OWLOntology ontology : ontologies) {
396-
restrictionVisitor = new RestrictionVisitor(analyzedClass,ontology,owlThing, "");
396+
restrictionVisitor = new RestrictionVisitor(analyzedClass, ontology, owlThing, "");
397397
for (OWLSubClassOfAxiom ax : ontology.getSubClassAxiomsForSubClass(analyzedClass)) {
398398
OWLClassExpression superCls = ax.getSuperClass();
399399
// Ask our superclass to accept a visit from the RestrictionVisitor
@@ -410,7 +410,7 @@ private void getClassRestrictions(OWLClass analyzedClass){
410410
if (restrictionsValuesFromClass.size()!=0) {
411411
// When the restriction is a ObjectComplementOf it doesn't have a object property,
412412
// thus we need to set its value at the setSchema function
413-
if (restrictionsValuesFromClass.containsKey("complementOf") && restrictionsValuesFromClass.size()==1) {
413+
if (restrictionsValuesFromClass.containsKey("complementOf") && restrictionsValuesFromClass.size() == 1) {
414414
for (String j : restrictionsValuesFromClass.keySet()) {
415415
Map<String,String> restrictionValues=restrictionsValuesFromClass.get(j);
416416
for (String restriction: restrictionValues.keySet()) {
@@ -442,7 +442,7 @@ private void getClassRestrictions(OWLClass analyzedClass){
442442
}
443443
}
444444
for (OWLDataProperty dp : propertiesFromDataRestrictions) {
445-
List<String> valuesFromDataRestrictions_ranges = new ArrayList<String>();
445+
List<String> valuesFromDataRestrictions_ranges = new ArrayList<>();
446446
String propertyDescription = ObaUtils.getDescription(dp, ontology_cls);
447447
if (propertiesFromDataRestrictions_ranges.size() != 0) {
448448
List<String> rangesDP = propertiesFromDataRestrictions_ranges.get(sfp.getShortForm(dp.getIRI()));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public static void main(String[] args) throws Exception {
8484
logger.info("OBA finished successfully. Output can be found at: "+destination_dir);
8585
}catch (Exception e){
8686
logger.severe("Error while creating the API specification: "+e.getLocalizedMessage());
87+
e.printStackTrace();
8788
System.exit(1);
8889
}
8990
}

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package edu.isi.oba;
22

3+
import edu.isi.oba.config.AuthConfig;
34
import edu.isi.oba.config.YamlConfig;
5+
import io.swagger.v3.oas.models.PathItem;
6+
import io.swagger.v3.oas.models.media.Schema;
47
import org.junit.Assert;
58
import org.junit.Test;
69
import org.semanticweb.owlapi.model.OWLClass;
710

11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.io.InputStream;
814
import java.util.ArrayList;
915
import java.util.Collections;
16+
import java.util.LinkedHashMap;
1017
import java.util.List;
18+
import java.util.logging.ConsoleHandler;
19+
import java.util.logging.Level;
20+
import java.util.logging.LogManager;
21+
import java.util.logging.Logger;
1122

1223
import static edu.isi.oba.ObaUtils.get_yaml_data;
1324

@@ -78,4 +89,33 @@ public void testMissingImportOntology() throws Exception{
7889
Mapper mapper = new Mapper(config_data);
7990
Assert.assertEquals(false, mapper.ontologies.isEmpty());
8091
}
92+
93+
/**
94+
* Test an ontology (very simple, two classes) with a missing import
95+
*/
96+
@Test
97+
public void testComplexOntology() throws Exception{
98+
InputStream stream = Oba.class.getClassLoader().getResourceAsStream("logging.properties");
99+
try {
100+
LogManager.getLogManager().readConfiguration(stream);
101+
edu.isi.oba.Oba.logger = Logger.getLogger(Oba.class.getName());
102+
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
106+
edu.isi.oba.Oba.logger.setLevel(Level.FINE);
107+
edu.isi.oba.Oba.logger.addHandler(new ConsoleHandler());
108+
String example_remote = "src/test/resources/complex_expr/config.yaml";
109+
YamlConfig config_data = get_yaml_data(example_remote);
110+
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();
111+
config_data.setAuth(new AuthConfig());
112+
Mapper mapper = new Mapper(config_data);
113+
OWLClass cls = mapper.manager.getOWLDataFactory().getOWLClass("https://businessontology.com/ontology/Person");
114+
String desc = ObaUtils.getDescription(cls, mapper.ontologies.get(0));
115+
MapperSchema mapperSchema = new MapperSchema(mapper.ontologies, cls, desc, mapper.schemaNames, mapper.ontologies.get(0), true);
116+
Schema schema = mapperSchema.getSchema();
117+
// The person schema must not be null.
118+
Assert.assertNotNull(schema);
119+
Assert.assertEquals(schema.getName(),"Person");
120+
}
81121
}

0 commit comments

Comments
 (0)