Skip to content

Commit

Permalink
add: filter by class
Browse files Browse the repository at this point in the history
  • Loading branch information
mosoriob committed Mar 22, 2020
1 parent c32fbe5 commit afbe890
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 167 deletions.
16 changes: 8 additions & 8 deletions examples/dbpedia/config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
ontologies:
- https://gist.githubusercontent.com/sirspock/cec147b24bd241295584dfcc21c21b93/raw/b6fa41ddf93212d967f35da20278f54d2ae2d40d/gistfile1.txt
- https://gist.githubusercontent.com/sirspock/cec147b24bd241295584dfcc21c21b93/raw/b6fa41ddf93212d967f35da20278f54d2ae2d40d/gistfile1.txt
name: dbpedia
openapi:
openapi: 3.0.1
info:
description: This is the API of the Software Description Ontology
at [https://mintproject.github.io/Mint-ModelCatalog-Ontology/release/1.3.0/index-en.html](https://w3id.org/okn/o/sdm)
title: Model Catalog
description: This is the API of the DBpedia Ontology
title: DBpedia
version: v1.3.0
externalDocs:
description: Model Catalog
description: DBpedia
url: https://w3id.org/okn/o/sdm
servers:
- url: https://api.models.mint.isi.edu/v1.3.0
- url: https://dev.api.models.mint.isi.edu/v1.3.0
- url: https:///dbpedia.dbpedia.oba.isi.edu/v1.3.0
- url: http://localhost:8080/v1.3.0


enable_get_paths: true
enable_post_paths: false
enable_delete_paths: false
enable_put_paths: false

classes:
- http://dbpedia.org/ontology/Genre
- http://dbpedia.org/ontology/Band
firebase:
key:

Expand Down
6 changes: 3 additions & 3 deletions examples/modelcatalog/config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
ontologies:
- https://mintproject.github.io/Mint-ModelCatalog-Ontology/release/1.3.0/ontology.xml
- https://knowledgecaptureanddiscovery.github.io/SoftwareDescriptionOntology/release/1.4.0/ontology.xml
- http://shorturl.at/fmsvM
- http://shorturl.at/nRYZ2
name: modelcatalog
output_dir: outputs

openapi:
openapi: 3.0.1
info:
description: This is the API of the Software Description Ontology at [https://w3id.org/okn/o/sdm](https://w3id.org/okn/o/sdm)
description: This is the API of the Software Description Ontology at [https://w3id.org/okn/o/sdm](https://w3id.org/okn/o/sdm)
title: Model Catalog
version: v1.4.0
externalDocs:
Expand Down
261 changes: 150 additions & 111 deletions src/main/java/edu/isi/oba/Mapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.isi.oba;

import edu.isi.oba.config.RelationConfig;
import edu.isi.oba.config.YamlConfig;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.media.Schema;
Expand All @@ -9,138 +8,178 @@
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.rdf.rdfxml.renderer.OWLOntologyXMLNamespaceManager;

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

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

class Mapper {
public static final String DEFAULT_DIR_QUERY = "_default_";
public final Map<IRI, String> schemaNames = new HashMap<>();
public Map<String, Schema> schemas = new HashMap<>();
final Paths paths = new Paths();
List<String> selected_paths;
List<OWLOntology> ontologies;

public OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

public Mapper(YamlConfig config_data) throws OWLOntologyCreationException {
List<String> paths = config_data.getPaths();

this.selected_paths = paths;
List<String> config_ontologies = config_data.getOntologies();
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();
public static final String DEFAULT_DIR_QUERY = "_default_";
public final Map<IRI, String> schemaNames = new HashMap<>();
public Map<String, Schema> schemas = new HashMap<>();
final Paths paths = new Paths();
List<String> selected_paths;
List<OWLOntology> ontologies;
List<OWLClass> selected_classes;
List<OWLClass> mapped_classes;
YamlConfig config_data;

public OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

public Mapper(YamlConfig config_data) throws OWLOntologyCreationException {
this.config_data = config_data;
List<String> paths = config_data.getPaths();
this.selected_paths = paths;
this.mapped_classes = new ArrayList<>();

List<String> config_ontologies = config_data.getOntologies();
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();

//Load the ontology into the manager
for (String ontologyURL : config_ontologies) {
this.manager.loadOntology(IRI.create(ontologyURL));
}
ontologies = this.manager.ontologies().collect(Collectors.toList());

//Load the ontology into the manager
for (String ontologyURL : config_ontologies) {
this.manager.loadOntology(IRI.create(ontologyURL));
//Create a temporal Map<IRI, String> schemaNames with the classes
for (OWLOntology ontology : ontologies) {
Set<OWLClass> classes = ontology.getClassesInSignature();
setSchemaNames(classes);
}
this.selected_classes = filter_classes();
}
ontologies = this.manager.ontologies().collect(Collectors.toList());

//Create a temporal Map<IRI, String> schemaNames with the classes
for (OWLOntology ontology : ontologies) {
Set<OWLClass> classes = ontology.getClassesInSignature();
setSchemaNames(classes);
}
//Create OpenAPI spec
this.createSchemas(destination_dir, config_data);
}

/**
* Obtain Schemas using the ontology classes
* The schemas includes the properties
*
* @param config_data
* @return schemas
*/
private void createSchemas(String destination_dir, YamlConfig config_data) {
Query query = new Query(destination_dir);
Path pathGenerator = new Path(config_data.getEnable_get_paths(),
config_data.getEnable_post_paths(),
config_data.getEnable_put_paths(),
config_data.getEnable_delete_paths()
);
query.get_all(DEFAULT_DIR_QUERY);

for (OWLOntology ontology : this.ontologies){
OWLDocumentFormat format = ontology.getFormat();
String defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix();
Set<OWLClass> classes = ontology.getClassesInSignature();
for (OWLClass cls : classes) {
//filter if the class prefix is not the default ontology's prefix
if (cls.getIRI() != null) {
String classPrefixIRI = cls.getIRI().getNamespace();
if (defaultOntologyPrefixIRI.equals(classPrefixIRI)) {
//Convert from OWL Class to OpenAPI Schema.
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, schemaNames, ontology);
//Write the query
query.write_readme(mapperSchema.name);

//Create the schema
Schema schema = mapperSchema.getSchema();
schemas.put(schema.getName(), schema);


//Add the paths
if (this.selected_paths == null){
add_path(pathGenerator, mapperSchema);
} else {
for (String str : this.selected_paths) {
String search = str.trim().toLowerCase();
String schemaName = mapperSchema.name.toLowerCase();
if (search.trim().toLowerCase().equals(schemaName)) {
add_path(pathGenerator, mapperSchema);
/**
* Obtain Schemas using the ontology classes
* The schemas includes the properties
*
* @param config_data
* @return schemas
*/
public void createSchemas(String destination_dir, YamlConfig config_data) {
Query query = new Query(destination_dir);
Path pathGenerator = new Path(config_data.getEnable_get_paths(),
config_data.getEnable_post_paths(),
config_data.getEnable_put_paths(),
config_data.getEnable_delete_paths()
);
query.get_all(DEFAULT_DIR_QUERY);
for (OWLOntology ontology : this.ontologies) {

OWLDocumentFormat format = ontology.getFormat();
String defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).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
if (cls.getIRI() != null) {
if (selected_classes != null && !selected_classes.contains(cls))
continue;
add_owlclass_to_openapi(query, pathGenerator, ontology, defaultOntologyPrefixIRI, cls, true);
}
}
}
}
}

//User schema
Map<String, Schema> userProperties = new HashMap<>();
StringSchema username = new StringSchema();
StringSchema password = new StringSchema();
userProperties.put("username", username);
userProperties.put("password", password);

Schema userSchema = new Schema();
userSchema.setName("User");
userSchema.setType("object");
userSchema.setProperties(userProperties);
userSchema.setXml(new XML().name("User"));
schemas.put("User", userSchema);

this.paths.addPathItem("/user/login", pathGenerator.user_login());
}
}

private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology,
String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) {
List<OWLClass> ref = new ArrayList<>();
String classPrefixIRI = cls.getIRI().getNamespace();
if (defaultOntologyPrefixIRI.equals(classPrefixIRI)) {
MapperSchema mapperSchema = getMapperSchema(query, ontology, cls);

for (OWLClass ref_class : mapperSchema.getProperties_range()) {
if (this.mapped_classes.contains(ref_class)){
logger.info("The class " + ref_class + " exists ");
} else {
for (OWLOntology temp_ontology : this.ontologies) {
this.mapped_classes.add(ref_class);
getMapperSchema(query, temp_ontology, ref_class);

OWLDocumentFormat format = ontology.getFormat();
String temp_defaultOntologyPrefixIRI = ((RDFXMLDocumentFormat) format).getDefaultPrefix();
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, temp_defaultOntologyPrefixIRI, ref_class, false);
}
}
}

//Add the OpenAPI paths
if (topLevel)
addOpenAPIPaths(pathGenerator, mapperSchema, cls);
}
return ref;
}



//User schema
Map<String, Schema> userProperties = new HashMap<>();
StringSchema username = new StringSchema();
StringSchema password = new StringSchema();
userProperties.put("username", username);
userProperties.put("password", password);

Schema userSchema = new Schema();
userSchema.setName("User");
userSchema.setType("object");
userSchema.setProperties(userProperties);
userSchema.setXml(new XML().name("User"));
schemas.put("User", userSchema);

this.paths.addPathItem("/user/login", pathGenerator.user_login());
private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass cls) {
//Convert from OWL Class to OpenAPI Schema.
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, schemaNames, ontology);
//Write queries
query.write_readme(mapperSchema.name);
//Create the OpenAPI schema
Schema schema = mapperSchema.getSchema();
schemas.put(schema.getName(), schema);
return mapperSchema;
}

}
private void addOpenAPIPaths(Path pathGenerator, MapperSchema mapperSchema, OWLClass cls) {
if (selected_classes != null && !selected_classes.contains(cls))
logger.info("Ignoring class " + cls.toString());
else
add_path(pathGenerator, mapperSchema);
}

private void setSchemaNames(Set<OWLClass> classes) {
for (OWLClass cls : classes) {
schemaNames.put(cls.getIRI(), cls.getIRI().getShortForm());
private void setSchemaNames(Set<OWLClass> classes) {
for (OWLClass cls : classes) {
schemaNames.put(cls.getIRI(), cls.getIRI().getShortForm());
}
}
}

private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
//Create the plural paths: for example: /models/
this.paths.addPathItem(plural_name, pathGenerator.generate_plural(mapperSchema.name));
//Create the plural paths: for example: /models/id
this.paths.addPathItem(singular_name, pathGenerator.generate_singular(mapperSchema.name));
}
private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
//Create the plural paths: for example: /models/
this.paths.addPathItem(plural_name, pathGenerator.generate_plural(mapperSchema.name));
//Create the plural paths: for example: /models/id
this.paths.addPathItem(singular_name, pathGenerator.generate_singular(mapperSchema.name));
}

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));
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();
for (OWLOntology ontology : this.ontologies) {
for (OWLClass cls : ontology.getClassesInSignature()) {
if (selected_classes_iri.contains(cls.getIRI().toString())) {
filtered_classes.add(cls);
}
}
}
return filtered_classes;
}
}
8 changes: 7 additions & 1 deletion src/main/java/edu/isi/oba/MapperSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class MapperSchema {
private final Schema schema;
private OWLOntology ontology_cls;
private OWLReasonerFactory reasonerFactory;
public List<OWLClass> properties_range;

public List<OWLClass> getProperties_range() {
return properties_range;
}

public Schema getSchema() {
return schema;
Expand All @@ -43,6 +47,8 @@ public MapperSchema(List<OWLOntology> ontologies, OWLClass cls, Map<IRI, String>
reasonerFactory = new StructuralReasonerFactory();
this.reasoner = reasonerFactory.createReasoner(this.ontology_cls);

properties_range = new ArrayList<>();

this.name = getSchemaName(cls);
this.properties = setProperties();
this.schema = setSchema();
Expand Down Expand Up @@ -237,14 +243,14 @@ private List<String> getCodeGenTypesByRangeData(Set<OWLDataPropertyRangeAxiom> r
private List<String> getCodeGenTypesByRangeObject(Set<OWLObjectPropertyRangeAxiom> ranges, OWLObjectProperty odp, OWLClass owlThing) {
List<String> objectProperty = new ArrayList<>();


for (OWLObjectPropertyAxiom propertyRangeAxiom : ranges) {
for (OWLEntity rangeClass : propertyRangeAxiom.getSignature()) {
if (!rangeClass.containsEntityInSignature(odp)) {
if (rangeClass.asOWLClass().equals(owlThing)) {
logger.info("Ignoring owl:Thing" + odp);
}
else {
this.properties_range.add(rangeClass.asOWLClass());
objectProperty.add(getSchemaName(rangeClass.asOWLClass()));
}
}
Expand Down
Loading

0 comments on commit afbe890

Please sign in to comment.