:py:mod:`ontolearn.knowledge_base` ================================== .. py:module:: ontolearn.knowledge_base .. autoapi-nested-parse:: Knowledge Base. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: ontolearn.knowledge_base.KnowledgeBase Functions ~~~~~~~~~ .. autoapisummary:: ontolearn.knowledge_base.depth_Default_ReasonerFactory Attributes ~~~~~~~~~~ .. autoapisummary:: ontolearn.knowledge_base.logger .. py:data:: logger .. py:function:: depth_Default_ReasonerFactory(onto: owlapy.owl_ontology.OWLOntology) -> owlapy.owl_reasoner.OWLReasoner .. py:class:: KnowledgeBase(*, path: str, ontologymanager_factory: Callable[[], owlapy.owl_ontology_manager.OWLOntologyManager] = OWLOntologyManager_Owlready2(world_store=None), reasoner_factory: Callable[[owlapy.owl_ontology.OWLOntology], owlapy.owl_reasoner.OWLReasoner] = None, length_metric: Optional[ontolearn.base.owl.utils.OWLClassExpressionLengthMetric] = None, length_metric_factory: Optional[Callable[[], ontolearn.base.owl.utils.OWLClassExpressionLengthMetric]] = None, individuals_cache_size=128, backend_store: bool = False, include_implicit_individuals=False) KnowledgeBase(*, ontology: owlapy.owl_ontology.OWLOntology, reasoner: owlapy.owl_reasoner.OWLReasoner, length_metric: Optional[ontolearn.base.owl.utils.OWLClassExpressionLengthMetric] = None, length_metric_factory: Optional[Callable[[], ontolearn.base.owl.utils.OWLClassExpressionLengthMetric]] = None, individuals_cache_size=128) Bases: :py:obj:`ontolearn.abstracts.AbstractKnowledgeBase` Representation of an OWL knowledge base in Ontolearn. :param path: Path to an ontology file that is to be loaded. :param ontologymanager_factory: Factory that creates an ontology manager to be used to load the file. :param ontology: OWL ontology object. :param reasoner_factory: Factory that creates a reasoner to reason about the ontology. :param reasoner: reasoner Over the ontology. :param length_metric_factory: See :attr:`length_metric`. :param length_metric: Length metric that is used in calculation of class expression lengths. :param individuals_cache_size: How many individuals of class expressions to cache. :param backend_store: Whether to sync the world to backend store. reasoner of this object, if you enter a reasoner using :arg:`reasoner_factory` or :arg:`reasoner` argument it will override this setting. :param include_implicit_individuals: Whether to identify and consider instances which are not set as OWL Named Individuals (does not contain this type) as individuals. .. attribute:: generator Instance of concept generator. :type: ConceptGenerator .. attribute:: path Path of the ontology file. :type: str .. attribute:: use_individuals_cache Whether to use individuals cache to store individuals for method efficiency. :type: bool .. py:property:: concepts :type: Iterable[owlapy.class_expression.OWLClass] Get all concepts of this concept generator. :returns: Concepts. .. py:property:: object_properties :type: Iterable[owlapy.owl_property.OWLObjectProperty] Get all object properties of this concept generator. :returns: Object properties. .. py:property:: data_properties :type: Iterable[owlapy.owl_property.OWLDataProperty] Get all data properties of this concept generator. :returns: Data properties for the given range. .. py:attribute:: length_metric :type: ontolearn.base.owl.utils.OWLClassExpressionLengthMetric .. py:attribute:: ind_set :type: FrozenSet[owlapy.owl_individual.OWLNamedIndividual] .. py:attribute:: ind_cache :type: owlapy.util.LRUCache[owlapy.class_expression.OWLClassExpression, FrozenSet[owlapy.owl_individual.OWLNamedIndividual]] .. py:attribute:: path :type: str .. py:attribute:: use_individuals_cache :type: bool .. py:attribute:: generator :type: ontolearn.concept_generator.ConceptGenerator .. py:method:: individuals(concept: Optional[owlapy.class_expression.OWLClassExpression] = None) -> Iterable[owlapy.owl_individual.OWLNamedIndividual] Given an OWL class expression, retrieve all individuals belonging to it. :param concept: Class expression of which to list individuals. :returns: Individuals belonging to the given class. .. py:method:: abox(individual: Union[owlapy.owl_individual.OWLNamedIndividual, Iterable[owlapy.owl_individual.OWLNamedIndividual]] = None, mode='native') Get all the abox axioms for a given individual. If no individual is given, get all abox axioms :param individual: Individual/s to get the abox axioms from. :type individual: OWLNamedIndividual :param mode: The return format. 1) 'native' -> returns triples as tuples of owlapy objects, 2) 'iri' -> returns triples as tuples of IRIs as string, 3) 'axiom' -> triples are represented by owlapy axioms. :type mode: str Returns: Iterable of tuples or owlapy axiom, depending on the mode. .. py:method:: tbox(entities: Union[Iterable[owlapy.class_expression.OWLClass], Iterable[owlapy.owl_property.OWLDataProperty], Iterable[owlapy.owl_property.OWLObjectProperty], owlapy.class_expression.OWLClass, owlapy.owl_property.OWLDataProperty, owlapy.owl_property.OWLObjectProperty, None] = None, mode='native') Get all the tbox axioms for the given concept-s|propert-y/ies. If no concept-s|propert-y/ies are given, get all tbox axioms. :param @TODO: entities or namedindividuals ?! :param entities: Entities to obtain tbox axioms from. This can be a single OWLClass/OWLDataProperty/OWLObjectProperty object, a list of those objects or None. If you enter a list that combines classes and properties (which we don't recommend doing), only axioms for one type will be returned, whichever comes first (classes or props). :param mode: The return format. 1) 'native' -> returns triples as tuples of owlapy objects, 2) 'iri' -> returns triples as tuples of IRIs as string, 3) 'axiom' -> triples are represented by owlapy axioms. :type mode: str Returns: Iterable of tuples or owlapy axiom, depending on the mode. .. py:method:: triples(mode='native') Get all tbox and abox axioms/triples. :param mode: The return format. 1) 'native' -> returns triples as tuples of owlapy objects, 2) 'iri' -> returns triples as tuples of IRIs as string, 3) 'axiom' -> triples are represented by owlapy axioms. :type mode: str Returns: Iterable of tuples or owlapy axiom, depending on the mode. .. py:method:: ignore_and_copy(ignored_classes: Optional[Iterable[owlapy.class_expression.OWLClass]] = None, ignored_object_properties: Optional[Iterable[owlapy.owl_property.OWLObjectProperty]] = None, ignored_data_properties: Optional[Iterable[owlapy.owl_property.OWLDataProperty]] = None) -> KnowledgeBase Makes a copy of the knowledge base while ignoring specified concepts and properties. :param ignored_classes: Classes to ignore. :param ignored_object_properties: Object properties to ignore. :param ignored_data_properties: Data properties to ignore. :returns: A new KnowledgeBase with the hierarchies restricted as requested. .. py:method:: concept_len(ce: owlapy.class_expression.OWLClassExpression) -> int Calculates the length of a concept and is used by some concept learning algorithms to find the best results considering also the length of the concepts. :param ce: The concept to be measured. :returns: Length of the concept. .. py:method:: clean() Clean all stored values (states and caches) if there is any. .. note:: 1. If you have more than one learning problem that you want to fit to the same model (i.e. to learn the concept using the same concept learner model) use this method to make sure that you have cleared every previous stored value. 2. If you store another KnowledgeBase instance using the same variable name as before, it is recommended to use this method before the initialization to avoid data mismatch. .. py:method:: cache_individuals(ce: owlapy.class_expression.OWLClassExpression) -> None .. py:method:: maybe_cache_individuals(ce: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.owl_individual.OWLNamedIndividual] .. py:method:: maybe_cache_individuals_count(ce: owlapy.class_expression.OWLClassExpression) -> int .. py:method:: individuals_count(concept: Optional[owlapy.class_expression.OWLClassExpression] = None) -> int Returns the number of all individuals belonging to the concept in the ontology. :param concept: Class expression of the individuals to count. :returns: Number of the individuals belonging to the given class. .. py:method:: individuals_set(concept: owlapy.class_expression.OWLClassExpression) individuals_set(individual: owlapy.owl_individual.OWLNamedIndividual) individuals_set(individuals: Iterable[owlapy.owl_individual.OWLNamedIndividual]) Retrieve the individuals specified in the arg as a frozenset. If `arg` is an OWLClassExpression then this method behaves as the method "individuals" but will return the final result as a frozenset. :param arg: more than one individual/ single individual/ class expression of which to list individuals. :returns: Frozenset of the individuals depending on the arg type. .. py:method:: all_individuals_set() Retrieve all the individuals of the knowledge base. :returns: Frozenset of the all individuals. .. py:method:: most_general_object_properties(*, domain: owlapy.class_expression.OWLClassExpression, inverse: bool = False) -> Iterable[owlapy.owl_property.OWLObjectProperty] Find the most general object property. :param domain: Domain for which to search properties. :param inverse: Inverse order? .. py:method:: data_properties_for_domain(domain: owlapy.class_expression.OWLClassExpression, data_properties: Iterable[owlapy.owl_property.OWLDataProperty]) -> Iterable[owlapy.owl_property.OWLDataProperty] .. py:method:: encode_learning_problem(lp: ontolearn.learning_problem.PosNegLPStandard) @TODO: A learning problem (DL concept learning problem) should not be a part of a knowledge base Provides the encoded learning problem (lp), i.e. the class containing the set of OWLNamedIndividuals as follows: kb_pos --> the positive examples set, kb_neg --> the negative examples set, kb_all --> all lp individuals / all individuals set, kb_diff --> kb_all - (kb_pos + kb_neg). .. note:: Simple access of the learning problem individuals divided in respective sets. You will need the encoded learning problem to use the method evaluate_concept of this class. :param lp: The learning problem. :type lp: PosNegLPStandard :returns: The encoded learning problem. :rtype: EncodedPosNegLPStandard .. py:method:: evaluate_concept(concept: owlapy.class_expression.OWLClassExpression, quality_func: ontolearn.abstracts.AbstractScorer, encoded_learning_problem: ontolearn.abstracts.EncodedLearningProblem) -> ontolearn.search.EvaluatedConcept Evaluates a concept by using the encoded learning problem examples, in terms of Accuracy or F1-score. @ TODO: A knowledge base is a data structure and the context of "evaluating" a concept seems to be unrelated .. note:: This method is useful to tell the quality (e.q) of a generated concept by the concept learners, to get the set of individuals (e.inds) that are classified by this concept and the amount of them (e.ic). :param concept: The concept to be evaluated. :param quality_func: Quality measurement in terms of Accuracy or F1-score. :param encoded_learning_problem: The encoded learning problem. :returns: The evaluated concept. .. py:method:: get_leaf_concepts(concept: owlapy.class_expression.OWLClass) Get leaf classes. :param concept: Atomic class for which to find leaf classes. :returns: y subClassOf x )}. :rtype: Leaf classes { x \| (x subClassOf concept) AND not exist y .. py:method:: get_least_general_named_concepts() -> Generator[owlapy.class_expression.OWLClass, None, None] Get leaf classes. @TODO: Docstring needed Returns: .. py:method:: least_general_named_concepts() -> Generator[owlapy.class_expression.OWLClass, None, None] Get leaf classes. @TODO: Docstring needed Returns: .. py:method:: get_most_general_classes() -> Generator[owlapy.class_expression.OWLClass, None, None] Get most general named concepts classes. @TODO: Docstring needed Returns: .. py:method:: get_direct_sub_concepts(concept: owlapy.class_expression.OWLClass) -> Iterable[owlapy.class_expression.OWLClass] Direct sub-classes of atomic class. :param concept: Atomic concept. :returns: Direct sub classes of concept { x \| ( x subClassOf concept )}. .. py:method:: get_object_property_domains(prop: owlapy.owl_property.OWLObjectProperty) -> owlapy.class_expression.OWLClassExpression Get the domains of an object property. :param prop: Object property. :returns: Domains of the property. .. py:method:: get_object_property_ranges(prop: owlapy.owl_property.OWLObjectProperty) -> owlapy.class_expression.OWLClassExpression Get the ranges of an object property. :param prop: Object property. :returns: Ranges of the property. .. py:method:: get_data_property_domains(prop: owlapy.owl_property.OWLDataProperty) -> owlapy.class_expression.OWLClassExpression Get the domains of a data property. :param prop: Data property. :returns: Domains of the property. .. py:method:: get_data_property_ranges(prop: owlapy.owl_property.OWLDataProperty) -> FrozenSet[owlapy.owl_data_ranges.OWLDataRange] Get the ranges of a data property. :param prop: Data property. :returns: Ranges of the property. .. py:method:: most_general_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.owl_property.OWLDataProperty] Find most general data properties that are applicable to a domain. :param domain: Domain for which to search properties. :returns: Most general data properties for the given domain. .. py:method:: most_general_boolean_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.owl_property.OWLDataProperty] Find most general boolean data properties that are applicable to a domain. :param domain: Domain for which to search properties. :returns: Most general boolean data properties for the given domain. .. py:method:: most_general_numeric_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.owl_property.OWLDataProperty] Find most general numeric data properties that are applicable to a domain. :param domain: Domain for which to search properties. :returns: Most general numeric data properties for the given domain. .. py:method:: most_general_time_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.owl_property.OWLDataProperty] Find most general time data properties that are applicable to a domain. :param domain: Domain for which to search properties. :returns: Most general time data properties for the given domain. .. py:method:: most_general_existential_restrictions(*, domain: owlapy.class_expression.OWLClassExpression, filler: Optional[owlapy.class_expression.OWLClassExpression] = None) -> Iterable[owlapy.class_expression.OWLObjectSomeValuesFrom] Find most general existential restrictions that are applicable to a domain. :param domain: Domain for which to search properties. :param filler: Optional filler to put in the restriction (not normally used). :returns: Most general existential restrictions for the given domain. .. py:method:: most_general_universal_restrictions(*, domain: owlapy.class_expression.OWLClassExpression, filler: Optional[owlapy.class_expression.OWLClassExpression] = None) -> Iterable[owlapy.class_expression.OWLObjectAllValuesFrom] Find most general universal restrictions that are applicable to a domain. :param domain: Domain for which to search properties. :param filler: Optional filler to put in the restriction (not normally used). :returns: Most general universal restrictions for the given domain. .. py:method:: most_general_existential_restrictions_inverse(*, domain: owlapy.class_expression.OWLClassExpression, filler: Optional[owlapy.class_expression.OWLClassExpression] = None) -> Iterable[owlapy.class_expression.OWLObjectSomeValuesFrom] Find most general inverse existential restrictions that are applicable to a domain. :param domain: Domain for which to search properties. :param filler: Optional filler to put in the restriction (not normally used). :returns: Most general existential restrictions over inverse property. .. py:method:: most_general_universal_restrictions_inverse(*, domain: owlapy.class_expression.OWLClassExpression, filler: Optional[owlapy.class_expression.OWLClassExpression] = None) -> Iterable[owlapy.class_expression.OWLObjectAllValuesFrom] Find most general inverse universal restrictions that are applicable to a domain. :param domain: Domain for which to search properties. :param filler: Optional filler to put in the restriction (not normally used). :returns: Most general universal restrictions over inverse property. .. py:method:: get_direct_parents(concept: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.class_expression.OWLClass] Direct parent concepts. :param concept: Concept to find super concepts of. :returns: Direct parent concepts. .. py:method:: get_all_direct_sub_concepts(concept: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.class_expression.OWLClassExpression] All direct sub concepts of a concept. :param concept: Parent concept for which to get sub concepts. :returns: Direct sub concepts. .. py:method:: get_all_sub_concepts(concept: owlapy.class_expression.OWLClassExpression) -> Iterable[owlapy.class_expression.OWLClassExpression] All sub concepts of a concept. :param concept: Parent concept for which to get sub concepts. :returns: Sub concepts. .. py:method:: get_concepts() -> Iterable[owlapy.class_expression.OWLClass] Get all concepts of this concept generator. :returns: Concepts. .. py:method:: get_classes_in_signature() .. py:method:: get_object_properties() -> Iterable[owlapy.owl_property.OWLObjectProperty] Get all object properties of this concept generator. :returns: Object properties. .. py:method:: get_data_properties(ranges: Set[owlapy.owl_datatype.OWLDatatype] = None) -> Iterable[owlapy.owl_property.OWLDataProperty] Get all data properties of this concept generator for the given ranges. :param ranges: Ranges for which to extract the data properties. :returns: Data properties for the given range. .. py:method:: get_boolean_data_properties() -> Iterable[owlapy.owl_property.OWLDataProperty] Get all boolean data properties of this concept generator. :returns: Boolean data properties. .. py:method:: get_numeric_data_properties() -> Iterable[owlapy.owl_property.OWLDataProperty] Get all numeric data properties of this concept generator. :returns: Numeric data properties. .. py:method:: get_double_data_properties() -> Iterable[owlapy.owl_property.OWLDataProperty] Get all numeric data properties of this concept generator. :returns: Numeric data properties. .. py:method:: get_time_data_properties() -> Iterable[owlapy.owl_property.OWLDataProperty] Get all time data properties of this concept generator. :returns: Time data properties. .. py:method:: get_types(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = False) -> Iterable[owlapy.class_expression.OWLClass] Get the named classes which are (direct) types of the specified individual. :param ind: Individual. :param direct: Whether to consider direct types. :returns: Types of the given individual. .. py:method:: get_object_properties_for_ind(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = True) -> Iterable[owlapy.owl_property.OWLObjectProperty] Get the object properties for the given individual. :param ind: Individual :param direct: Whether only direct properties should be considered (True), or if also indirect properties should be considered (False). Indirect properties would be super properties super_p of properties p with ObjectPropertyAssertion(p ind obj). :returns: Object properties. .. py:method:: get_data_properties_for_ind(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = True) -> Iterable[owlapy.owl_property.OWLDataProperty] Get the data properties for the given individual :param ind: Individual :param direct: Whether only direct properties should be considered (True), or if also indirect properties should be considered (False). Indirect properties would be super properties super_p of properties p with ObjectPropertyAssertion(p ind obj). :returns: Data properties. .. py:method:: get_object_property_values(ind: owlapy.owl_individual.OWLNamedIndividual, property_: owlapy.owl_property.OWLObjectPropertyExpression, direct: bool = True) -> Iterable[owlapy.owl_individual.OWLNamedIndividual] Get the object property values for the given individual and property. :param ind: Individual. :param property_: Object property. :param direct: Whether only the property property_ should be considered (True), or if also the values of sub properties of property_ should be considered (False). :returns: Individuals. .. py:method:: get_data_property_values(ind: owlapy.owl_individual.OWLNamedIndividual, property_: owlapy.owl_property.OWLDataPropertyExpression, direct: bool = True) -> Iterable[owlapy.owl_literal.OWLLiteral] Get the data property values for the given individual and property. :param ind: Individual. :param property_: Data property. :param direct: Whether only the property property_ should be considered (True), or if also the values of sub properties of property_ should be considered (False). :returns: Literals. .. py:method:: contains_class(concept: owlapy.class_expression.OWLClassExpression) -> bool Check if an atomic class is contained within this concept generator. :param concept: Atomic class. :returns: Whether the class is contained in the concept generator. .. py:method:: __repr__() Return repr(self).