Skip to content

Commit b65f89d

Browse files
authored
Merge pull request #104 from KnowledgeCaptureAndDiscovery/Issues#98&#27
Issues#98&#27
2 parents c627b40 + f565fdd commit b65f89d

15 files changed

Lines changed: 301 additions & 108 deletions

File tree

examples/pplan/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ openapi:
1111
version: v1.0.0
1212
externalDocs:
1313
description: P-Plan
14-
url: https://w3id.org/okn/o/sdm
14+
url: http://purl.org/net/p-plan
1515

1616
firebase:
1717
key: "test"
1818

19+
#endpoint not used for this example.
1920
endpoint:
2021
url: http://endpoint.mint.isi.edu/example
2122
prefix: https://w3id.org/okn/i/example

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@
66
import io.swagger.v3.oas.models.media.StringSchema;
77
import io.swagger.v3.oas.models.media.XML;
88
import org.semanticweb.owlapi.apibinding.OWLManager;
9-
import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
109
import org.semanticweb.owlapi.model.*;
1110

1211
import static edu.isi.oba.Oba.logger;
1312

1413
import java.io.File;
1514
import java.io.IOException;
16-
import java.io.InputStream;
1715
import java.nio.file.Files;
1816
import java.nio.file.StandardCopyOption;
1917
import java.util.*;
2018
import java.util.logging.Level;
2119
import java.util.logging.Logger;
2220
import java.util.stream.Collectors;
23-
import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
2421

2522
class Mapper {
2623
public static final String DEFAULT_DIR_QUERY = "_default_";
27-
public final Map<IRI, String> schemaNames = new HashMap<>();
24+
public final Map<IRI, String> schemaNames = new HashMap<>(); //URI-names of the schemas
25+
public final Map<IRI, String> schemaDescriptions = new HashMap<>(); //URI-description of the schemas
2826
public Map<String, Schema> schemas = new HashMap<>();
2927
final Paths paths = new Paths();
3028
List<String> selected_paths;
@@ -61,6 +59,7 @@ public Mapper(YamlConfig config_data) throws OWLOntologyCreationException {
6159
for (OWLOntology ontology : ontologies) {
6260
Set<OWLClass> classes = ontology.getClassesInSignature();
6361
setSchemaNames(classes);
62+
setSchemaDrescriptions(classes,ontology);
6463
}
6564
if (config_data.getClasses() != null)
6665
this.selected_classes = filter_classes();
@@ -158,7 +157,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
158157
List<OWLClass> ref = new ArrayList<>();
159158
String classPrefixIRI = cls.getIRI().getNamespace();
160159
if (defaultOntologyPrefixIRI.equals(classPrefixIRI)) {
161-
MapperSchema mapperSchema = getMapperSchema(query, ontology, cls);
160+
MapperSchema mapperSchema = getMapperSchema(query, ontology, cls, this.schemaDescriptions.get(cls.getIRI()));
162161

163162
for (OWLClass ref_class : mapperSchema.getProperties_range()) {
164163
if (this.mapped_classes.contains(ref_class)){
@@ -167,7 +166,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
167166
for (OWLOntology temp_ontology : this.ontologies) {
168167
if ( follow_references ) {
169168
this.mapped_classes.add(ref_class);
170-
getMapperSchema(query, temp_ontology, ref_class);
169+
getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI()));
171170
OWLDocumentFormat format = ontology.getFormat();
172171
//String temp_defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix();
173172
String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
@@ -184,9 +183,9 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
184183
return ref;
185184
}
186185

187-
private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass cls) {
186+
private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass cls, String cls_description) {
188187
//Convert from OWL Class to OpenAPI Schema.
189-
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, schemaNames, ontology, follow_references);
188+
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, cls_description, schemaNames, ontology, follow_references);
190189
//Write queries
191190
query.write_readme(mapperSchema.name);
192191
//Create the OpenAPI schema
@@ -207,21 +206,37 @@ private void setSchemaNames(Set<OWLClass> classes) {
207206
schemaNames.put(cls.getIRI(), cls.getIRI().getShortForm());
208207
}
209208
}
209+
210+
/**
211+
* Given a set of classes from an ontology, this method initializes
212+
* schemaDescriptions with the definitions used to describe an ontology (if provided)
213+
* @param classes the classes you want the description for
214+
* @param ontology the ontology from where we will extract the descriptions
215+
*/
216+
private void setSchemaDrescriptions(Set<OWLClass> classes,OWLOntology ontology){
217+
for (OWLClass cls : classes) {
218+
System.out.println(cls);
219+
schemaDescriptions.put(cls.getIRI(), ObaUtils.getDescription(cls, ontology));
220+
}
221+
}
210222

211223
private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
212224
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
213225
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
214226
//Create the plural paths: for example: /models/
215-
this.paths.addPathItem(plural_name, pathGenerator.generate_plural(mapperSchema.name));
227+
this.paths.addPathItem(plural_name, pathGenerator.generate_plural(mapperSchema.name,
228+
mapperSchema.getCls().getIRI().getIRIString()));
216229
//Create the plural paths: for example: /models/id
217-
this.paths.addPathItem(singular_name, pathGenerator.generate_singular(mapperSchema.name));
230+
this.paths.addPathItem(singular_name, pathGenerator.generate_singular(mapperSchema.name,
231+
mapperSchema.getCls().getIRI().getIRIString()));
218232
}
219233

220-
private void add_path_relation(Path pathGenerator, String schema_name, String predicate, String path) {
221-
String relation = "/" + schema_name.toLowerCase() + "s/{id}/" + path;
222-
this.paths.addPathItem(relation, pathGenerator.generate_plural(schema_name));
223-
224-
}
234+
// Method not used
235+
// private void add_path_relation(Path pathGenerator, String schema_name, String predicate, String path) {
236+
// String relation = "/" + schema_name.toLowerCase() + "s/{id}/" + path;
237+
// this.paths.addPathItem(relation, pathGenerator.generate_plural(schema_name));
238+
//
239+
// }
225240

226241
public List<OWLClass> filter_classes() {
227242
List<String> selected_classes_iri = this.config_data.getClasses();

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

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.swagger.v3.oas.models.media.*;
44

5-
import java.util.ArrayList;
65
import java.util.HashMap;
76
import java.util.List;
87
import static edu.isi.oba.Oba.logger;
@@ -72,15 +71,17 @@ private String getDataType(String key){
7271
private static final String BOOLEAN_TYPE = "boolean";
7372

7473
final String name;
74+
final String description;
7575
private List<String> type;
7676
private Boolean array;
7777
private Boolean nullable;
7878

7979

80-
public MapperDataProperty(String name, List<String> type, Boolean array, Boolean nullable) {
80+
public MapperDataProperty(String name, String description, List<String> type, Boolean array, Boolean nullable) {
8181
this.dataTypes = new HashMap<>();
8282
this.setDataTypes();
8383
this.name = name;
84+
this.description = description;
8485
this.type = type;
8586
this.array = array;
8687
this.nullable = nullable;
@@ -93,10 +94,10 @@ public Schema getSchemaByDataProperty(){
9394
////}
9495

9596
if (this.type.size() == 0) {
96-
return (array) ? arraySchema(new StringSchema(), nullable) : new StringSchema().nullable(nullable);
97+
return (array) ? arraySchema(new StringSchema(), nullable) : new StringSchema().nullable(nullable).description(description);
9798
}
9899
else if (this.type.size() > 1){
99-
return (array) ? arraySchema(new Schema(), nullable) : new Schema().nullable(nullable);
100+
return (array) ? arraySchema(new Schema(), nullable) : new Schema().nullable(nullable).description(description);
100101
}
101102

102103
String schemaType = getDataType(this.type.get(0));
@@ -105,66 +106,69 @@ else if (this.type.size() > 1){
105106
}
106107
switch (schemaType) {
107108
case STRING_TYPE:
108-
return (array) ? arraySchema(new StringSchema(), nullable) : new StringSchema().nullable(nullable);
109+
return (array) ? arraySchema(new StringSchema(), nullable) : new StringSchema().nullable(nullable).description(description);
109110
case NUMBER_TYPE:
110-
return (array) ? arraySchema(new NumberSchema(), nullable) : new NumberSchema().nullable(nullable);
111+
return (array) ? arraySchema(new NumberSchema(), nullable) : new NumberSchema().nullable(nullable).description(description);
111112
case INTEGER_TYPE:
112-
return (array) ? arraySchema(new IntegerSchema(), nullable) : new IntegerSchema().nullable(nullable);
113+
return (array) ? arraySchema(new IntegerSchema(), nullable) : new IntegerSchema().nullable(nullable).description(description);
113114
case BOOLEAN_TYPE:
114-
return (array) ? arraySchema(new BooleanSchema(), nullable) : new BooleanSchema().nullable(nullable);
115+
return (array) ? arraySchema(new BooleanSchema(), nullable) : new BooleanSchema().nullable(nullable).description(description);
115116
default:
116117
logger.warning("datatype mapping failed " + this.type.get(0));
117-
return (array) ? arraySchema(new Schema(), nullable) : new Schema().nullable(nullable);
118+
return (array) ? arraySchema(new Schema(), nullable) : new Schema().nullable(nullable).description(description);
118119
}
119120
}
120121

121-
private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nullable){
122-
Schema object = new ObjectSchema();
123-
object.set$ref(ref);
124-
125-
if (array) {
126-
ArraySchema objects = new ArraySchema();
127-
objects.setNullable(nullable);
128-
objects.setItems(object);
129-
return objects;
130-
}
131-
else {
132-
return object;
133-
}
134-
135-
136-
}
137-
private Schema getComposedSchemaObject(List<String> refs, boolean array, boolean nullable){
138-
Schema object = new ObjectSchema();
139-
object.setType("object");
140-
141-
if (array) {
142-
ArraySchema objects = new ArraySchema();
143-
objects.setNullable(nullable);
144-
objects.setItems(object);
145-
return objects;
146-
}
147-
else {
148-
return object;
149-
}
150-
151-
/*ComposedSchema composedSchema = new ComposedSchema();
152-
List<Schema> items = new ArrayList<>();
153-
for (String ref : refs){
154-
Schema item = getObjectPropertiesByRef(ref, array, nullable);
155-
items.add(item);
156-
}
157-
composedSchema.setAnyOf(items);
158-
return composedSchema;
159-
*/
160-
161-
}
122+
//Not used
123+
// private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nullable){
124+
// Schema object = new ObjectSchema();
125+
// object.set$ref(ref);
126+
//
127+
// if (array) {
128+
// ArraySchema objects = new ArraySchema();
129+
// objects.setNullable(nullable);
130+
// objects.setItems(object);
131+
// return objects;
132+
// }
133+
// else {
134+
// return object;
135+
// }
136+
//
137+
//
138+
// }
139+
// not used
140+
// private Schema getComposedSchemaObject(List<String> refs, boolean array, boolean nullable){
141+
// Schema object = new ObjectSchema();
142+
// object.setType("object");
143+
//
144+
// if (array) {
145+
// ArraySchema objects = new ArraySchema();
146+
// objects.setNullable(nullable);
147+
// objects.setItems(object);
148+
// return objects;
149+
// }
150+
// else {
151+
// return object;
152+
// }
153+
//
154+
// /*ComposedSchema composedSchema = new ComposedSchema();
155+
// List<Schema> items = new ArrayList<>();
156+
// for (String ref : refs){
157+
// Schema item = getObjectPropertiesByRef(ref, array, nullable);
158+
// items.add(item);
159+
// }
160+
// composedSchema.setAnyOf(items);
161+
// return composedSchema;
162+
// */
163+
//
164+
// }
162165

163166

164167

165168

166169
private ArraySchema arraySchema(Schema base, boolean nullable) {
167170
ArraySchema array = new ArraySchema();
171+
array.setDescription(description);
168172
array.setNullable(nullable);
169173
array.setItems(base);
170174
return array;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
import io.swagger.v3.oas.models.media.*;
44

5-
import java.util.ArrayList;
6-
import java.util.HashMap;
75
import java.util.List;
86

97
class MapperObjectProperty {
108
final String name;
9+
final String description;
1110
private List<String> ref;
1211
private Boolean array;
1312
private Boolean nullable;
1413

15-
public MapperObjectProperty(String name, List<String> ref) {
14+
public MapperObjectProperty(String name, String description, List<String> ref) {
1615
this.name = name;
16+
this.description = description;
1717
this.ref = ref;
1818
this.array = true;
1919
this.nullable = true;
2020
}
2121

22-
public MapperObjectProperty(String name, List<String> ref, Boolean array, Boolean nullable) {
22+
public MapperObjectProperty(String name, String description, List<String> ref, Boolean array, Boolean nullable) {
2323
this.name = name;
24+
this.description = description;
2425
this.ref = ref;
2526
this.array = array;
2627
this.nullable = nullable;
@@ -42,9 +43,11 @@ public Schema getSchemaByObjectProperty(){
4243
private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nullable){
4344
Schema object = new ObjectSchema();
4445
object.set$ref(ref);
46+
object.setDescription(description);
4547

4648
if (array) {
4749
ArraySchema objects = new ArraySchema();
50+
objects.setDescription(description);
4851
objects.setNullable(nullable);
4952
objects.setItems(object);
5053
return objects;
@@ -58,9 +61,11 @@ private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nulla
5861
private Schema getComposedSchemaObject(List<String> refs, boolean array, boolean nullable){
5962
Schema object = new ObjectSchema();
6063
object.setType("object");
64+
object.setDescription(description);
6165

6266
if (array) {
6367
ArraySchema objects = new ArraySchema();
68+
objects.setDescription(description);
6469
objects.setNullable(nullable);
6570
objects.setItems(object);
6671
return objects;

0 commit comments

Comments
 (0)