diff --git a/docs/figs/Untitled Diagram.drawio b/docs/figs/Untitled Diagram.drawio new file mode 100644 index 0000000..b23a612 --- /dev/null +++ b/docs/figs/Untitled Diagram.drawio @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/docs/figs/dlite_cuds.svg b/docs/figs/dlite_cuds.svg new file mode 100644 index 0000000..56e3bbd --- /dev/null +++ b/docs/figs/dlite_cuds.svg @@ -0,0 +1,4 @@ + + + +Shared knowledge baseShared knowle...queryquerydlite_cudsPython libraryfrom_cuds_metadata()from_cuds()to_cuds()dlite_cuds...usesusesDLite plugincudsDLite plugin...usesusesOTEAPI pluginUse existing oteapi-dlite strategies:parse, generateOTEAPI plugin...queryqueryosp-coreosp-coreusesusesMODSMODSStandalone library only depending on tripper and DLite.Accessing the knowledge base via Tripper.Standalone library only dependi...Shared knowledge baseShared knowle...queryquerydlite_cudsPython libraryfrom_cuds_metadata()from_cuds()to_cuds()dlite_cuds...usesusesDLite plugincudsMaybe not needed??DLite plugin...usesusesOTEAPI pluginBased on existing plugins in dlite_cudsOTEAPI plugin...queryqueryosp-coreosp-coreusesusesMODSMODSStandalone library only depending on tripper and DLite.Accessing the knowledge base via Tripper.Standalone library only dependi...Alternative designAlternative designViewer does not support full SVG 1.1 diff --git a/docs/figs/example_data.svg b/docs/figs/example_data.svg new file mode 100644 index 0000000..46e78de --- /dev/null +++ b/docs/figs/example_data.svg @@ -0,0 +1,4 @@ + + + +ontology.example.ymlontology.example.ymlstoredInstoredInex.ttlex.ttlstoredInstoredInexample_ABox.ttlexample_ABox.ttlgenerategenerategenerate_example_a_box.pygenerate_example_a_box.pyinstances.jsoninstances.jsongenerategeneratemkcoll.pymkcoll.pyhasInputhasInputTypeOne.jsonTypeOne.jsonhasInputhasInputTypeTwo.jsonTypeTwo.jsonhasInputhasInputTypeThree.jsonTypeThree.jsongenerategeneratehasInputhasInputconvert_ontology_to_ttl.pyconvert_ontology_to_ttl.pyFiles in the example folderFiles in the example folderKnowledge baseKnowledge basegenerategeneratehasInputhasInputfrom_cuds_metadata()from_cuds_metadata()generategeneratehasInputhasInputfrom_cuds()from_cuds()hasInputhasInputhasInputhasInputpopulatepopulateto_cuds()to_cuds()Colour codingColour codingAdded by SeanAdded by SeanAdded by SINTEFAdded by SINTEFPlanned stuffPlanned stuffText is not SVG - cannot display \ No newline at end of file diff --git a/example/TypeOne.json b/example/TypeOne.json new file mode 100644 index 0000000..366bdcb --- /dev/null +++ b/example/TypeOne.json @@ -0,0 +1,15 @@ +{ + "uri": "http://onto-ns.com/meta/0.1/TypeOne", + "description": "", + "dimensions": {}, + "properties": { + "dpOne": { + "type": "string", + "description": "" + }, + "dpTwo": { + "type": "int", + "description": "" + } + } +} diff --git a/example/TypeThree.json b/example/TypeThree.json new file mode 100644 index 0000000..71c5b1d --- /dev/null +++ b/example/TypeThree.json @@ -0,0 +1,11 @@ +{ + "uri": "http://onto-ns.com/meta/0.1/TypeThree", + "description": "", + "dimensions": {}, + "properties": { + "dpTwo": { + "type": "int", + "description": "" + } + } +} diff --git a/example/TypeTwo.json b/example/TypeTwo.json new file mode 100644 index 0000000..fd743ab --- /dev/null +++ b/example/TypeTwo.json @@ -0,0 +1,19 @@ +{ + "uri": "http://onto-ns.com/meta/0.1/TypeTwo", + "description": "", + "dimensions": {}, + "properties": { + "dpOne": { + "type": "string", + "description": "" + }, + "dpTwo": { + "type": "int", + "description": "" + }, + "dpThree": { + "type": "float", + "description": "" + } + } +} diff --git a/example/convert_ontology_to_ttl.py b/example/convert_ontology_to_ttl.py new file mode 100644 index 0000000..88dcd8a --- /dev/null +++ b/example/convert_ontology_to_ttl.py @@ -0,0 +1,33 @@ +"""Generates `ex` ontology ttl file""" +import os + +import rdflib +from osp.core.ontology import Ontology +from osp.core.ontology.parser import OntologyParser + + +def export_ontology(ontology_namespace): + """Converts ex ontology to a ttl file and exports as ex.ttl""" + ontology_graph = rdflib.graph.Graph() + packages_path = os.path.expanduser(os.path.join("~", ".osp_ontologies")) + package_path = os.path.join(packages_path, ontology_namespace) + + export_file_path = os.path.join(os.getcwd(), f"{ontology_namespace}.ttl") + + if os.path.exists(f"{package_path}.yml"): + ontology = Ontology(from_parser=OntologyParser.get_parser(package_path)) + ontology_graph += ontology.graph + else: + raise FileNotFoundError( + f"Package {ontology_namespace} not " + f"found in {packages_path}. Are " + f"you sure it is installed?" + ) + + result = ontology_graph.serialize(format="ttl", encoding="UTF-8").decode("UTF-8") + with open(export_file_path, "w+", encoding="utf-8") as file_handle: + file_handle.write(result) + + +if __name__ == "__main__": + export_ontology("ex") diff --git a/example/ex.ttl b/example/ex.ttl new file mode 100644 index 0000000..68a49c4 --- /dev/null +++ b/example/ex.ttl @@ -0,0 +1,73 @@ +@prefix ns1: . +@prefix ns2: . +@prefix owl: . +@prefix rdfs: . +@prefix xsd: . + + ns2:_default_rel . + + a owl:Class ; + rdfs:subClassOf [ a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty ], + ns2:Entity ; + ns1:prefLabel "TypeThree"@en . + + a owl:Class ; + rdfs:subClassOf [ a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty ], + ns2:Entity, + ; + ns1:prefLabel "TypeTwo"@en . + + a owl:ObjectProperty ; + rdfs:isDefinedBy "inverse of second object property"@en ; + rdfs:subPropertyOf ns2:passiveRelationship ; + owl:inverseOf ; + ns1:prefLabel "iOPTwo"@en . + + a owl:Class ; + rdfs:subClassOf [ a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty ], + [ a owl:Restriction ; + owl:cardinality 1 ; + owl:onProperty ], + ns2:Entity ; + ns1:prefLabel "TypeOne"@en . + + a owl:DatatypeProperty, + owl:FunctionalProperty ; + rdfs:range xsd:string ; + rdfs:subPropertyOf ns2:attribute ; + ns1:prefLabel "dpOne"@en . + + a owl:DatatypeProperty, + owl:FunctionalProperty ; + rdfs:range xsd:float ; + rdfs:subPropertyOf ns2:attribute ; + ns1:prefLabel "dpThree"@en . + + a owl:ObjectProperty ; + rdfs:isDefinedBy "inverse of the default relationship"@en ; + rdfs:subPropertyOf ns2:passiveRelationship ; + owl:inverseOf ; + ns1:prefLabel "iOPOne"@en . + + a owl:ObjectProperty ; + rdfs:isDefinedBy "second object property"@en ; + rdfs:subPropertyOf ns2:activeRelationship ; + ns1:prefLabel "opTwo"@en . + + a owl:DatatypeProperty, + owl:FunctionalProperty ; + rdfs:range xsd:integer ; + rdfs:subPropertyOf ns2:attribute ; + ns1:prefLabel "dpTwo"@en . + + a owl:ObjectProperty ; + rdfs:isDefinedBy "default relationship"@en ; + rdfs:subPropertyOf ns2:activeRelationship ; + owl:inverseOf ; + ns1:prefLabel "opOne"@en . diff --git a/example/example_ABox.ttl b/example/example_ABox.ttl new file mode 100644 index 0000000..3517524 --- /dev/null +++ b/example/example_ABox.ttl @@ -0,0 +1,29 @@ +@prefix cuba: . +@prefix ex: . +@prefix rdf: . +@prefix xsd: . + +cuba:_serialization rdf:first "f817bd09-3945-4325-9a60-62ecca19ea3c" . + + a ex:TypeThree ; + ex:dpTwo 3 ; + ex:iOPTwo . + + a ex:TypeTwo ; + ex:dpOne "g"^^xsd:string ; + ex:dpThree "8.9"^^xsd:float ; + ex:dpTwo 4 ; + ex:iOPTwo . + + a ex:TypeTwo ; + ex:dpOne "b"^^xsd:string ; + ex:dpThree "2.5"^^xsd:float ; + ex:dpTwo 5 ; + ex:iOPOne ; + ex:opTwo . + + a ex:TypeOne ; + ex:dpOne "a"^^xsd:string ; + ex:dpTwo 1 ; + ex:opOne ; + ex:opTwo . diff --git a/example/generate_example_a_box.py b/example/generate_example_a_box.py new file mode 100644 index 0000000..d0f748b --- /dev/null +++ b/example/generate_example_a_box.py @@ -0,0 +1,21 @@ +"""Generates an example ABox""" +from osp.core.namespaces import ex +from osp.core.utils import export_cuds + + +def generate_example(): + """Generates and exports example ABox""" + inst1 = ex.TypeOne(dpOne="a", dpTwo=1) + inst2 = ex.TypeTwo(dpOne="b", dpTwo=5, dpThree=2.5) + inst3 = ex.TypeThree(dpTwo=3) + inst4 = ex.TypeTwo(dpOne="g", dpTwo=4, dpThree=8.9) + + inst2.add(inst4, rel=ex.opTwo) + inst1.add(inst2, rel=ex.opOne) + inst1.add(inst3, rel=ex.opTwo) + + export_cuds(inst1, file="example_ABox.ttl") + + +if __name__ == "__main__": + generate_example() diff --git a/example/instances.json b/example/instances.json new file mode 100644 index 0000000..701bd6d --- /dev/null +++ b/example/instances.json @@ -0,0 +1,71 @@ +{ + "f8e99b4f-4e0a-5bf5-be29-248964aa1f51": { + "uri": "http://www.osp-core.com/cuds#db11661e-e815-4f36-b5a6-2bafe25f09b6", + "meta": "http://onto-ns.com/meta/0.1/TypeTwo", + "dimensions": { + }, + "properties": { + "dpOne": "b", + "dpTwo": 5, + "dpThree": 2.5 + } + }, + "9cb54433-ba6d-4d8b-9fab-1cb4aba27dae": { + "meta": "http://onto-ns.com/meta/0.1/Collection", + "dimensions": { + "nrelations": 18 + }, + "properties": { + "relations": [ + ["inst1", "_is-a", "Instance"], + ["inst1", "_has-uuid", "2a917046-a0ba-5714-a9bf-f7f610f11451"], + ["inst1", "_has-meta", "http://onto-ns.com/meta/0.1/TypeOne"], + ["inst2a", "_is-a", "Instance"], + ["inst2a", "_has-uuid", "91eb70b8-5d79-54bc-b96c-fc9ee126672a"], + ["inst2a", "_has-meta", "http://onto-ns.com/meta/0.1/TypeTwo"], + ["inst2b", "_is-a", "Instance"], + ["inst2b", "_has-uuid", "f8e99b4f-4e0a-5bf5-be29-248964aa1f51"], + ["inst2b", "_has-meta", "http://onto-ns.com/meta/0.1/TypeTwo"], + ["inst3", "_is-a", "Instance"], + ["inst3", "_has-uuid", "cbc76b74-195c-5e77-855e-a1e434ce18c9"], + ["inst3", "_has-meta", "http://onto-ns.com/meta/0.1/TypeThree"], + ["inst1", "ex:opOne", "inst2b"], + ["inst1", "ex:opTwo", "inst3"], + ["inst2a", "ex:iOPTwo", "inst2b"], + ["inst2b", "ex:iOPOne", "inst1"], + ["inst2b", "ex:opTwo", "inst2a"], + ["inst3", "ex:iOPTwo", "inst1"] + ] + } + }, + "2a917046-a0ba-5714-a9bf-f7f610f11451": { + "uri": "http://www.osp-core.com/cuds#f817bd09-3945-4325-9a60-62ecca19ea3c", + "meta": "http://onto-ns.com/meta/0.1/TypeOne", + "dimensions": { + }, + "properties": { + "dpOne": "a", + "dpTwo": 1 + } + }, + "91eb70b8-5d79-54bc-b96c-fc9ee126672a": { + "uri": "http://www.osp-core.com/cuds#7dc3081f-72cb-4754-9623-e821b352fd14", + "meta": "http://onto-ns.com/meta/0.1/TypeTwo", + "dimensions": { + }, + "properties": { + "dpOne": "g", + "dpTwo": 4, + "dpThree": 8.9 + } + }, + "cbc76b74-195c-5e77-855e-a1e434ce18c9": { + "uri": "http://www.osp-core.com/cuds#5c29896e-b51f-4994-903f-090a2dedbac3", + "meta": "http://onto-ns.com/meta/0.1/TypeThree", + "dimensions": { + }, + "properties": { + "dpTwo": 3 + } + } +} diff --git a/example/mkcoll.py b/example/mkcoll.py new file mode 100644 index 0000000..c73c4b4 --- /dev/null +++ b/example/mkcoll.py @@ -0,0 +1,54 @@ +from pathlib import Path + +import dlite + +thisdir = Path(__file__).resolve().parent +dlite.storage_path.append(thisdir) + +TypeOne = dlite.get_instance("http://onto-ns.com/meta/0.1/TypeOne") +TypeTwo = dlite.get_instance("http://onto-ns.com/meta/0.1/TypeTwo") +TypeThree = dlite.get_instance("http://onto-ns.com/meta/0.1/TypeThree") + + +inst1 = TypeOne(id="http://www.osp-core.com/cuds#f817bd09-3945-4325-9a60-62ecca19ea3c") +inst1.dpOne = "a" +inst1.dpTwo = 1 + +inst2a = TypeTwo(id="http://www.osp-core.com/cuds#7dc3081f-72cb-4754-9623-e821b352fd14") +inst2a.dpOne = "g" +inst2a.dpTwo = 4 +inst2a.dpThree = 8.9 + +inst2b = TypeTwo(id="http://www.osp-core.com/cuds#db11661e-e815-4f36-b5a6-2bafe25f09b6") +inst2b.dpOne = "b" +inst2b.dpTwo = 5 +inst2b.dpThree = 2.5 + +inst3 = TypeThree( + id="http://www.osp-core.com/cuds#5c29896e-b51f-4994-903f-090a2dedbac3" +) +inst3.dpTwo = 3 + + +coll = dlite.Collection() +coll.add("inst1", inst1) +coll.add("inst2a", inst2a) +coll.add("inst2b", inst2b) +coll.add("inst3", inst3) + +# coll.add_relation(inst1.uri, "ex:opOne", inst2b.uri) +# coll.add_relation(inst1.uri, "ex:opTwo", inst3.uri) +# coll.add_relation(inst2a.uri, "ex:iOPTwo", inst2b.uri) +# coll.add_relation(inst2b.uri, "ex:iOPOne", inst1.uri) +# coll.add_relation(inst2b.uri, "ex:opTwo", inst2a.uri) +# coll.add_relation(inst3.uri, "ex:iOPTwo", inst1.uri) + +coll.add_relation("inst1", "ex:opOne", "inst2b") +coll.add_relation("inst1", "ex:opTwo", "inst3") +coll.add_relation("inst2a", "ex:iOPTwo", "inst2b") +coll.add_relation("inst2b", "ex:iOPOne", "inst1") +coll.add_relation("inst2b", "ex:opTwo", "inst2a") +coll.add_relation("inst3", "ex:iOPTwo", "inst1") + +# coll.save("yaml", "instances.yaml", "mode=w") +coll.save("json", "instances.json", "mode=w") diff --git a/example/ontology.example.yml b/example/ontology.example.yml new file mode 100644 index 0000000..c6f39c1 --- /dev/null +++ b/example/ontology.example.yml @@ -0,0 +1,67 @@ +--- +version: "0.0.1" +namespace: "ex" +author: "CMCL Innovations" + +ontology: + ################ RELATIONSHIPS ################ + opOne: + default_rel: true + description: "default relationship" + inverse: ex.iOPOne + subclass_of: + - cuba.activeRelationship + + iOPOne: + description: "inverse of the default relationship" + inverse: ex.opOne + subclass_of: + - cuba.passiveRelationship + + opTwo: + description: "second object property" + subclass_of: + - cuba.activeRelationship + + iOPTwo: + description: "inverse of second object property" + inverse: ex.opTwo + subclass_of: + - cuba.passiveRelationship + + ################ MAIN ENTITIES ################ + #================================================# + TypeOne: + subclass_of: + - cuba.Entity + attributes: + ex.dpOne: + ex.dpTwo: + + TypeTwo: + subclass_of: + - cuba.Entity + - ex.TypeOne + attributes: + ex.dpThree: + + TypeThree: + subclass_of: + - cuba.Entity + attributes: + ex.dpTwo: + + dpOne: + subclass_of: + - cuba.attribute + datatype: STRING + + dpTwo: + subclass_of: + - cuba.attribute + datatype: INT + + dpThree: + subclass_of: + - cuba.attribute + datatype: FLOAT
from_cuds_metadata()
from_cuds()
to_cuds()