BaseOntology
Here we provide base data models that can be used to define new ontologies. The base data models are classes which inherit from pydantic.BaseModel and define fields as annotated attributes.
T
module-attribute
¶
T = TypeVar('T')
A type variable to represent any type in Python. This is used as placeholder for any concept in the ontologies.
KnowledgeGraph ¶
Bases: BaseModel
This class is used to represent a knowledge graph consists of Pydantic objects in the Python memory.
Attributes:
Name | Type | Description |
---|---|---|
ontology_lookup |
Dict[str, BaseOntology]
|
A class variable to store the lookup dictionary of ontologies |
class_lookup |
Dict[str, BaseClass]
|
A class variable to store the lookup dictionary of classes |
property_lookup |
Dict[str, BaseProperty]
|
A class variable to store the lookup dictionary of properties |
graph
classmethod
¶
This method is used to retrieve the knowledge graph in Python memory.
Returns:
Name | Type | Description |
---|---|---|
Graph |
Graph
|
The rdflib.Graph object of the knowledge graph |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
all_triples_of_nodes
classmethod
¶
This method is used to retrieve all (in-coming and out-going) triples of the given nodes in the knowledge graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iris
|
str or list
|
The IRI of the nodes to be retrieved |
required |
Returns:
Name | Type | Description |
---|---|---|
Graph |
Graph
|
The rdflib.Graph object of the triples of the given nodes |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
construct_object_lookup
classmethod
¶
This method is used to retrieve all BaseClass (pydantic) objects created in Python memory.
Returns:
Type | Description |
---|---|
Dict[str, BaseClass]
|
A dictionary of BaseClass (pydantic) objects with their IRIs as keys |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
get_object_from_lookup
classmethod
¶
This method is used to retrieve an object from Python memory given its IRI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iri
|
str
|
IRI of the object to be retrieved |
required |
Returns:
Type | Description |
---|---|
Union[BaseClass, None]
|
The pydantic object of the given IRI if exist, otherwise return None. |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
clear_object_lookup
classmethod
¶
This method is used to clear the object lookup dictionary in Python memory.
BaseOntology ¶
Bases: BaseModel
This class is used to represent an ontology which consists of a list of BaseClass and ObjectProperty/DatatypeProperty.
Attributes:
Name | Type | Description |
---|---|---|
base_url |
str
|
The base URL to be used to construct the namespace IRI, the default value is 'https://www.theworldavatar.com/kg/' |
namespace |
str
|
The namespace of the ontology, e.g. 'ontolab' |
namespace_iri |
str
|
The namespace IRI of the ontology, e.g. 'https://www.theworldavatar.com/kg/ontolab' |
class_lookup |
Dict[str, BaseClass]
|
A dictionary of BaseClass classes with their rdf:type as keys |
object_property_lookup |
Dict[str, ObjectProperty]
|
A dictionary of ObjectProperty classes with their predicate IRI as keys |
data_property_lookup |
Dict[str, DatatypeProperty]
|
A dictionary of DatatypeProperty classes with their predicate IRI as keys |
rdfs_comment |
Set[str]
|
The comment of the ontology |
owl_versionInfo |
str
|
The version of the ontology |
is_dev_mode
classmethod
¶
set_dev_mode
classmethod
¶
This method sets the KnowledgeGraph to development mode, where duplicate class or property registration will be allowed that the existing ones will be overwritten.
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
set_prod_mode
classmethod
¶
This method sets the KnowledgeGraph to production mode, where duplicate class or property registration will raise an error.
export_to_graph
classmethod
¶
This method is used to export the ontology to a rdflib.Graph object. It operates at the TBox level, i.e. it only exports the classes and properties of the ontology.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g
|
Graph
|
The rdflib.Graph object to which the ontology will be exported |
None
|
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
export_to_triple_store
classmethod
¶
export_to_triple_store(sparql_client: PySparqlClient)
This method is used to export the ontology to a triplestore. It operates at the TBox level, i.e. it only exports the classes and properties of the ontology.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparql_client
|
PySparqlClient
|
The PySparqlClient object that connects to the triplestore |
required |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
export_to_owl
classmethod
¶
This method is used to export the ontology to an ontology file. It operates at the TBox level, i.e. it only exports the classes and properties of the ontology.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The path of the ontology file to be exported to |
required |
format
|
str
|
The format of the ontology file, the default value is 'ttl' |
'ttl'
|
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
BaseProperty ¶
Base class that is inherited by ObjectProperty and DatatypeProperty.
Attributes:
Name | Type | Description |
---|---|---|
rdfs_isDefinedBy |
Type[BaseOntology]
|
The ontology that defines the property |
predicate_iri |
str
|
The predicate IRI of the property |
owl_minQualifiedCardinality |
int
|
The minimum qualified cardinality of the property (default is 0) |
owl_maxQualifiedCardinality |
int
|
The maximum qualified cardinality of the property (default is None, meaning infinite) |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
retrieve_cardinality
classmethod
¶
create_from_base
classmethod
¶
create_from_base(class_name: str, ontology: Type[BaseOntology], min_cardinality: Optional[int] = 0, max_cardinality: Optional[int] = None) -> Type[BaseProperty]
This method is used to create a new property class from the calling class. The new property class will inherit the min and max cardinality from the calling class if not specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the new property class |
required |
ontology
|
Type[BaseOntology]
|
The ontology that defines the property |
required |
min_cardinality
|
Optional[int]
|
The minimum qualified cardinality of the property (defaults to 0) |
0
|
max_cardinality
|
Optional[int]
|
The maximum qualified cardinality of the property (defaults to None meaning infinite) |
None
|
Returns:
Type | Description |
---|---|
Type[BaseProperty]
|
Type[BaseProperty]: The new property class |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
BaseClass ¶
Bases: BaseModel
Base class for all the Python classes that are used to define the classes in ontology.
Attributes:
Name | Type | Description |
---|---|---|
rdfs_isDefinedBy |
BaseOntology
|
The ontology that defines the class |
rdf_type |
str
|
The rdf:type of the class |
object_lookup |
Dict[str, BaseClass]
|
A dictionary that maps the IRI of the object to the object |
rdfs_comment |
str
|
The comment of the instance |
rdfs_label |
str
|
The label of the instance |
instance_iri |
str
|
The IRI of the instance |
Example: class MyClass(BaseClass): myObjectProperty: MyObjectProperty[MyOtherClass] myDatatypeProperty: MyDatatypeProperty[str]
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
rdfs_isDefinedBy
class-attribute
¶
rdfs_isDefinedBy: BaseOntology = None
NOTE for all subclasses, one can just use
rdfs_isDefinedBy = MyOntology
, see this discussion in Pydantic
rdf_type
class-attribute
¶
rdf_type: str = OWL_BASE_URL + 'Class'
NOTE rdf_type is the automatically generated IRI of the class which can also be accessed at the instance level.
model_post_init ¶
model_post_init(__context: Any) -> None
The post init process of the BaseClass. It sets the instance_iri if it is not set. It also registers the object to the lookup dictionary of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__context
|
Any
|
Any other context that is needed for the post init process |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
It calls the super().model_post_init(__context) to finish the post init process |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
retrieve_subclass
classmethod
¶
This function retrieves the subclass of the current class based on the IRI. If the IRI is the same as the rdf:type of the current class, it will return the current class itself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iri
|
str
|
The IRI of the subclass |
required |
Returns:
Type | Description |
---|---|
Type[BaseClass]
|
Type[BaseClass]: The subclass of the BaseClass |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
construct_subclass_dictionary
classmethod
¶
This function constructs a dictionary that maps the rdf:type to the subclass of the BaseClass.
Returns:
Type | Description |
---|---|
Dict[str, Type[BaseClass]]
|
Dict[str, Type[BaseClass]]: The dictionary that maps the rdf:type to the subclass of the BaseClass |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
push_all_instances_to_kg
classmethod
¶
push_all_instances_to_kg(sparql_client: PySparqlClient, recursive_depth: int = 0, force_overwrite_local: bool = False)
This function pushes all the instances of the class to the knowledge graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparql_client
|
PySparqlClient
|
The SPARQL client that is used to push the data to the KG |
required |
recursive_depth
|
int
|
The depth of the recursion, 0 means no recursion, -1 means infinite recursion, n means n-level recursion |
0
|
force_overwrite_local
|
bool
|
Whether to force overwrite the local values with the remote values |
False
|
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
clear_object_lookup
classmethod
¶
This function clears the lookup dictionary of the class.
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
pull_from_kg
classmethod
¶
pull_from_kg(iris: List[str], sparql_client: PySparqlClient, recursive_depth: int = 0, force_overwrite_local: bool = False) -> List[BaseClass]
This function pulls the objects from the KG based on the given IRIs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iris
|
List[str]
|
The list of IRIs of the objects that one wants to pull from the KG |
required |
sparql_client
|
PySparqlClient
|
The SPARQL client that is used to pull the data from the KG |
required |
recursive_depth
|
int
|
The depth of the recursion, 0 means no recursion, -1 means infinite recursion, n means n-level recursion |
0
|
force_overwrite_local
|
bool
|
Whether to force overwrite the local values with the remote values |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
The rdf:type of the IRI provided does not match the calling class |
Returns:
Type | Description |
---|---|
List[BaseClass]
|
List[BaseClass]: A list of objects that are pulled from the KG |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 |
|
pull_all_instances_from_kg
classmethod
¶
pull_all_instances_from_kg(sparql_client: PySparqlClient, recursive_depth: int = 0, force_overwrite_local: bool = False) -> Set[BaseClass]
This function pulls all instances of the calling class from the knowledge graph (triplestore). It calls the pull_from_kg function with the IRIs of all instances of the calling class. By default, it pulls the instances with no recursion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparql_client
|
PySparqlClient
|
The SPARQL client that is used to pull the data from the KG |
required |
recursive_depth
|
int
|
The depth of the recursion, 0 means no recursion, -1 means infinite recursion, n means n-level recursion |
0
|
force_overwrite_local
|
bool
|
Whether to force overwrite the local values with the remote values |
False
|
Returns:
Type | Description |
---|---|
Set[BaseClass]
|
Set[BaseClass]: A set of objects that are pulled from the KG |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
get_object_and_data_properties
classmethod
¶
This function returns the object and data properties of the calling class. This method calls the get_object_properties and get_data_properties functions and returns the combined dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Union[str, Type[BaseProperty]]]]
|
Dict[str, Dict[str, Union[str, Type[BaseProperty]]]]: A dictionary containing the object and data properties of the calling class |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
get_object_properties
classmethod
¶
This function returns the object properties of the calling class.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Union[str, Type[ObjectProperty]]]]
|
Dict[str, Union[str, Type[ObjectProperty]]]]: A dictionary containing the object properties of the calling class in the format of {predicate_iri: {'field': field_name, 'type': field_clz}} e.g. {'https://twa.com/myObjectProperty': {'field': 'myObjectProperty', 'type': MyObjectProperty}} |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
get_data_properties
classmethod
¶
This function returns the data properties of the calling class.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Union[str, Type[DatatypeProperty]]]]
|
Dict[str, Dict[str, Union[str, Type[DatatypeProperty]]]]: A dictionary containing the data properties of the calling class in the format of {predicate_iri: {'field': field_name, 'type': field_clz}} e.g. {'https://twa.com/myDatatypeProperty': {'field': 'myDatatypeProperty', 'type': MyDatatypeProperty}} |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
revert_local_changes ¶
This function reverts the local changes made to the python object to cached values.
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
get_object_property_by_iri ¶
get_object_property_by_iri(iri: str) -> ObjectProperty
This function returns the object property by the IRI of the property.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iri
|
str
|
IRI of the object property |
required |
Returns:
Name | Type | Description |
---|---|---|
ObjectProperty |
ObjectProperty
|
The object property |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
push_to_kg ¶
push_to_kg(sparql_client: PySparqlClient, recursive_depth: int = 0, pull_first: bool = False, maximum_retry: int = 0, force_overwrite_if_pull_first: bool = False) -> Tuple[Graph, Graph]
This function pushes the triples of the calling object to the knowledge graph (triplestore).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparql_client
|
PySparqlClient
|
The SPARQL client object to be used to push the triples |
required |
recursive_depth
|
int
|
The depth of the recursion, 0 means no recursion, -1 means infinite recursion, n means n-level recursion |
0
|
pull_first
|
bool
|
Whether to pull the latest triples from the KG before pushing the triples |
False
|
maximum_retry
|
int
|
The number of retries if any exception was raised during SPARQL update |
0
|
force_overwrite_if_pull_first
|
bool
|
Whether to force overwrite the local values with the remote values if |
False
|
Returns:
Type | Description |
---|---|
Tuple[Graph, Graph]
|
Tuple[Graph, Graph]: A tuple of two rdflib.Graph objects containing the triples to be removed and added |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
graph ¶
This method adds all the outgoing triples of the calling object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g
|
Graph
|
The rdflib.Graph object to which the triples should be added |
None
|
Returns:
Name | Type | Description |
---|---|---|
Graph |
Graph
|
The rdflib.Graph object containing the triples added |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
ObjectProperty ¶
Bases: BaseProperty
Base class for object properties. It inherits the BaseProperty class.
Attributes:
Name | Type | Description |
---|---|---|
rdfs_isDefinedBy |
The ontology that defines the property |
|
predicate_iri |
The predicate IRI of the property |
|
owl_minQualifiedCardinality |
The minimum qualified cardinality of the property (default is 0) |
|
owl_maxQualifiedCardinality |
The maximum qualified cardinality of the property (default is None, meaning infinite) |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
retrieve_cardinality
classmethod
¶
create_from_base
classmethod
¶
create_from_base(class_name: str, ontology: Type[BaseOntology], min_cardinality: Optional[int] = 0, max_cardinality: Optional[int] = None) -> Type[ObjectProperty]
This method is used to create a new property class from the calling property class. The new property class will inherit the min and max cardinality from the calling class if not specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the new property class |
required |
ontology
|
Type[BaseOntology]
|
The ontology that defines the property |
required |
min_cardinality
|
Optional[int]
|
The minimum qualified cardinality of the property (defaults to 0) |
0
|
max_cardinality
|
Optional[int]
|
The maximum qualified cardinality of the property (defaults to None meaning infinite) |
None
|
Returns:
Type | Description |
---|---|
Type[ObjectProperty]
|
Type[ObjectProperty]: The new property class |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
TransitiveProperty ¶
Bases: ObjectProperty
Base class for transitive object properties. It inherits the ObjectProperty class.
Attributes:
Name | Type | Description |
---|---|---|
rdfs_isDefinedBy |
The ontology that defines the property |
|
predicate_iri |
The predicate IRI of the property |
|
owl_minQualifiedCardinality |
The minimum qualified cardinality of the property (default is 0) |
|
owl_maxQualifiedCardinality |
The maximum qualified cardinality of the property (default is None, meaning infinite) |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
obtain_transitive_objects
classmethod
¶
This function obtains the transitive objects of the instance for the transitive object property.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Union[BaseClass, str]
|
The instance for which the transitive objects are to be obtained |
required |
Returns:
Name | Type | Description |
---|---|---|
Set |
Set
|
The set that contains the transitive objects |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
DatatypeProperty ¶
Bases: BaseProperty
Base class for data properties. It inherits the BaseProperty class.
Attributes:
Name | Type | Description |
---|---|---|
rdfs_isDefinedBy |
The ontology that defines the property |
|
predicate_iri |
The predicate IRI of the property |
|
owl_minQualifiedCardinality |
The minimum qualified cardinality of the property (default is 0) |
|
owl_maxQualifiedCardinality |
The maximum qualified cardinality of the property (default is None, meaning infinite) |
Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/base_ontology.py
retrieve_cardinality
classmethod
¶
create_from_base
classmethod
¶
create_from_base(class_name: str, ontology: Type[BaseOntology], min_cardinality: Optional[int] = 0, max_cardinality: Optional[int] = None) -> Type[DatatypeProperty]
This method is used to create a new property class from the calling property class. The new property class will inherit the min and max cardinality from the calling class if not specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
str
|
The name of the new property class |
required |
ontology
|
Type[BaseOntology]
|
The ontology that defines the property |
required |
min_cardinality
|
Optional[int]
|
The minimum qualified cardinality of the property (defaults to 0) |
0
|
max_cardinality
|
Optional[int]
|
The maximum qualified cardinality of the property (defaults to None meaning infinite) |
None
|
Returns:
Type | Description |
---|---|
Type[DatatypeProperty]
|
Type[DatatypeProperty]: The new property class |