@@ -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 ())) {
0 commit comments