Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owl -> Jsonld automation script created. #19

Merged
merged 12 commits into from
Jul 9, 2021
214 changes: 99 additions & 115 deletions creditrisk_poc/api_doc/ApiDoc.jsonld

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions creditrisk_poc/api_doc/api_docwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hydra_python_core.doc_writer import (HydraDoc, HydraClass,
HydraClassProp, HydraClassOp, HydraStatus, HydraCollection)
import logging
import NPLVocab_parser as parser
import nplvocab_parser as parser

logging.basicConfig(filename="docwriter_log.log", format='%(asctime)s %(message)s', filemode='a')
logger = logging.getLogger()
Expand All @@ -29,14 +29,14 @@
hydra_classes = parser.create_hydra_classes(classes)
classes = {class_.title: class_ for class_ in hydra_classes}

loan_foriegnkey_uri = classes['CounterParty'].id_
loan_foriegnkey_uri = classes['Counterparty'].id_
loan_foriegnkey_title = "CounterpartyId"
CounterpartyId_prop = HydraClassProp(loan_foriegnkey_uri, loan_foriegnkey_title,
required=True, read=True, write=True)
classes['Loan'].add_supported_prop(CounterpartyId_prop)

Loan_operations = parser.add_operations_to_class(hydra_classes, "Loan", ["GET", "PUT", "POST", "DELETE"])
CounterParty_operations = parser.add_operations_to_class(hydra_classes, "CounterParty",
Counterparty_operations = parser.add_operations_to_class(hydra_classes, "Counterparty",
["GET", "PUT", "POST", "DELETE"])
Collateral_operations = parser.add_operations_to_class(hydra_classes, "Collateral", ["GET", "PUT", "POST", "DELETE"])

Expand All @@ -60,7 +60,7 @@
counterparty_collection_description = "Collection for Borrower class"
counterparty_collection_managed_by = {
"property": "rdf:type",
"object": parser.get_class_id("CounterParty", hydra_classes),
"object": parser.get_class_id("Counterparty", hydra_classes),
}
counterparty_collection = HydraCollection(collection_name=counterparty_collection_name,
collection_description=counterparty_collection_description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def get_all_classes(vocab: dict) -> list:
Get all the classes from the given Vocabulary.
"""
classes = list()
defines = vocab['defines']
defines = vocab['@graph']
for obj in defines:
if obj['@type'] == 'rdfs:Class':
if obj['@type'] == 'owl:Class':
classes.append(obj)
return classes

Expand All @@ -30,32 +30,40 @@ def create_hydra_classes(vocab_classes: list) -> list:
"""
hydra_classes = list()
for class_ in vocab_classes:
hydra_class = HydraClass(class_['rdfs:label'], class_['rdfs:comment'], endpoint=True)
class_name = (class_['rdfs:comment'].split('Class')[0]).replace(" ", "")
if "class" in class_name:
class_name = (class_['rdfs:comment'].split('class')[0]).replace(" ", "")
hydra_class = HydraClass(class_name, class_['rdfs:comment'], endpoint=True)
hydra_classes.append(hydra_class)
return hydra_classes


def get_class_properties(class_ : str, vocab : dict) -> list:
def get_class_properties(class_name: str, vocab: dict) -> list:
"""
Return all the properties of the given class.
"""
properties = list()
defines = vocab['defines']
defines = vocab['@graph']
for obj in defines:
if obj.get('propertyOf'):
propertyof = obj['propertyOf']['@id'].split('#')[1]
if propertyof == class_ or obj['@type'] == 'owl:DataProperty' and obj['@type'] == 'owl:ObjectProperty':
propertyof = obj['propertyOf'].split('#')[1]
if propertyof == class_name or obj['@type'] == 'owl:DataProperty' and obj['@type'] == 'owl:ObjectProperty':
properties.append(obj)
return properties


def create_hydra_properties(property_: dict, hydra_classes: dict) -> HydraClassProp:
if property_.get('propertyOn') and isinstance(property_['propertyOn'], dict):
property_on_class = property_.get("propertyOn")['@id'].split('#')[1]
property_uri = hydra_classes[property_on_class].id_
else:
property_uri = None
property_name = None
if property_['@type'] == 'owl:DatatypeProperty':
property_uri = property_['@id']
hydra_property = HydraClassProp(property_uri, property_['rdfs:label'],
property_name = property_['rdfs:label']
elif 'owl:ObjectProperty' in property_['@type']:
property_on_class = property_.get("propertyOn").split('#')[1]
property_uri = hydra_classes[property_on_class].id_
property_name = property_['@id'].split('#')[1]

hydra_property = HydraClassProp(property_uri, property_name,
required=True, read=True, write=True)
return hydra_property

Expand Down Expand Up @@ -87,7 +95,8 @@ def add_operations_to_class(hydra_classes: list, class_name: str, operations: li
hydra_operations.append(op)
if operation == "POST":
put_operation_status = [HydraStatus(code=200, desc=class_name + " class updated.")]
op = HydraClassOp(class_name + operation, operation, class_id, None, [], ["Content-Type", "Content-Length"],
op = HydraClassOp(class_name + operation, operation, class_id, None, [],
["Content-Type", "Content-Length"],
put_operation_status)
hydra_operations.append(op)
if operation == "DELETE":
Expand All @@ -96,9 +105,3 @@ def add_operations_to_class(hydra_classes: list, class_name: str, operations: li
put_operation_status)
hydra_operations.append(op)
return hydra_operations






Loading