ontolearn.knowledge_base

Knowledge Base.

Module Contents

Classes

KnowledgeBase

Representation of an OWL knowledge base in Ontolearn.

Functions

depth_Default_ReasonerFactory(...)

Attributes

logger

ontolearn.knowledge_base.logger
ontolearn.knowledge_base.depth_Default_ReasonerFactory(onto: owlapy.owl_ontology.OWLOntology) owlapy.owl_reasoner.OWLReasoner[source]
class ontolearn.knowledge_base.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: OWLClassExpressionLengthMetric | None = None, length_metric_factory: Callable[[], OWLClassExpressionLengthMetric] | None = None, individuals_cache_size=128, backend_store: bool = False, include_implicit_individuals=False)[source]
class ontolearn.knowledge_base.KnowledgeBase(*, ontology: owlapy.owl_ontology.OWLOntology, reasoner: owlapy.owl_reasoner.OWLReasoner, length_metric: OWLClassExpressionLengthMetric | None = None, length_metric_factory: Callable[[], OWLClassExpressionLengthMetric] | None = None, individuals_cache_size=128)

Bases: ontolearn.abstracts.AbstractKnowledgeBase

Representation of an OWL knowledge base in Ontolearn.

Parameters:
  • path – Path to an ontology file that is to be loaded.

  • ontologymanager_factory – Factory that creates an ontology manager to be used to load the file.

  • ontology – OWL ontology object.

  • reasoner_factory – Factory that creates a reasoner to reason about the ontology.

  • reasoner – reasoner Over the ontology.

  • length_metric_factory – See length_metric.

  • length_metric – Length metric that is used in calculation of class expression lengths.

  • individuals_cache_size – How many individuals of class expressions to cache.

  • 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.

  • include_implicit_individuals – Whether to identify and consider instances which are not set as OWL Named Individuals (does not contain this type) as individuals.

generator

Instance of concept generator.

Type:

ConceptGenerator

path

Path of the ontology file.

Type:

str

use_individuals_cache

Whether to use individuals cache to store individuals for method efficiency.

Type:

bool

property concepts: Iterable[owlapy.class_expression.OWLClass]

Get all concepts of this concept generator.

Returns:

Concepts.

property object_properties: Iterable[owlapy.owl_property.OWLObjectProperty]

Get all object properties of this concept generator.

Returns:

Object properties.

property data_properties: Iterable[owlapy.owl_property.OWLDataProperty]

Get all data properties of this concept generator.

Returns:

Data properties for the given range.

length_metric: OWLClassExpressionLengthMetric
ind_set: FrozenSet[owlapy.owl_individual.OWLNamedIndividual]
ind_cache: owlapy.util.LRUCache[owlapy.class_expression.OWLClassExpression, FrozenSet[owlapy.owl_individual.OWLNamedIndividual]]
path: str
use_individuals_cache: bool
generator: ConceptGenerator
individuals(concept: owlapy.class_expression.OWLClassExpression | None = None) Iterable[owlapy.owl_individual.OWLNamedIndividual][source]

Given an OWL class expression, retrieve all individuals belonging to it.

Parameters:

concept – Class expression of which to list individuals.

Returns:

Individuals belonging to the given class.

abox(individual: owlapy.owl_individual.OWLNamedIndividual | Iterable[owlapy.owl_individual.OWLNamedIndividual] = None, mode='native')[source]

Get all the abox axioms for a given individual. If no individual is given, get all abox axioms

Parameters:
  • individual (OWLNamedIndividual) – Individual/s to get the abox axioms from.

  • mode (str) – 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.

Returns: Iterable of tuples or owlapy axiom, depending on the mode.

tbox(entities: 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')[source]

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.

Parameters:
  • @TODO – entities or namedindividuals ?!

  • 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).

  • mode (str) – 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.

Returns: Iterable of tuples or owlapy axiom, depending on the mode.

triples(mode='native')[source]

Get all tbox and abox axioms/triples.

Parameters:

mode (str) – 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.

Returns: Iterable of tuples or owlapy axiom, depending on the mode.

ignore_and_copy(ignored_classes: Iterable[owlapy.class_expression.OWLClass] | None = None, ignored_object_properties: Iterable[owlapy.owl_property.OWLObjectProperty] | None = None, ignored_data_properties: Iterable[owlapy.owl_property.OWLDataProperty] | None = None) KnowledgeBase[source]

Makes a copy of the knowledge base while ignoring specified concepts and properties.

Parameters:
  • ignored_classes – Classes to ignore.

  • ignored_object_properties – Object properties to ignore.

  • ignored_data_properties – Data properties to ignore.

Returns:

A new KnowledgeBase with the hierarchies restricted as requested.

concept_len(ce: owlapy.class_expression.OWLClassExpression) int[source]

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.

Parameters:

ce – The concept to be measured.

Returns:

Length of the concept.

clean()[source]

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.

cache_individuals(ce: owlapy.class_expression.OWLClassExpression) None[source]
maybe_cache_individuals(ce: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.owl_individual.OWLNamedIndividual][source]
maybe_cache_individuals_count(ce: owlapy.class_expression.OWLClassExpression) int[source]
individuals_count(concept: owlapy.class_expression.OWLClassExpression | None = None) int[source]

Returns the number of all individuals belonging to the concept in the ontology.

Parameters:

concept – Class expression of the individuals to count.

Returns:

Number of the individuals belonging to the given class.

individuals_set(concept: owlapy.class_expression.OWLClassExpression)[source]
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.

Parameters:

arg – more than one individual/ single individual/ class expression of which to list individuals.

Returns:

Frozenset of the individuals depending on the arg type.

all_individuals_set()[source]

Retrieve all the individuals of the knowledge base.

Returns:

Frozenset of the all individuals.

most_general_object_properties(*, domain: owlapy.class_expression.OWLClassExpression, inverse: bool = False) Iterable[owlapy.owl_property.OWLObjectProperty][source]

Find the most general object property.

Parameters:
  • domain – Domain for which to search properties.

  • inverse – Inverse order?

data_properties_for_domain(domain: owlapy.class_expression.OWLClassExpression, data_properties: Iterable[owlapy.owl_property.OWLDataProperty]) Iterable[owlapy.owl_property.OWLDataProperty][source]
encode_learning_problem(lp: PosNegLPStandard)[source]

@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.

Parameters:

lp (PosNegLPStandard) – The learning problem.

Returns:

The encoded learning problem.

Return type:

EncodedPosNegLPStandard

evaluate_concept(concept: owlapy.class_expression.OWLClassExpression, quality_func: AbstractScorer, encoded_learning_problem: EncodedLearningProblem) EvaluatedConcept[source]

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).

Parameters:
  • concept – The concept to be evaluated.

  • quality_func – Quality measurement in terms of Accuracy or F1-score.

  • encoded_learning_problem – The encoded learning problem.

Returns:

The evaluated concept.

get_leaf_concepts(concept: owlapy.class_expression.OWLClass)[source]

Get leaf classes.

Parameters:

concept – Atomic class for which to find leaf classes.

Returns:

y subClassOf x )}.

Return type:

Leaf classes { x | (x subClassOf concept) AND not exist y

get_direct_sub_concepts(concept: owlapy.class_expression.OWLClass) Iterable[owlapy.class_expression.OWLClass][source]

Direct sub-classes of atomic class.

Parameters:

concept – Atomic concept.

Returns:

Direct sub classes of concept { x | ( x subClassOf concept )}.

get_object_property_domains(prop: owlapy.owl_property.OWLObjectProperty) owlapy.class_expression.OWLClassExpression[source]

Get the domains of an object property.

Parameters:

prop – Object property.

Returns:

Domains of the property.

get_object_property_ranges(prop: owlapy.owl_property.OWLObjectProperty) owlapy.class_expression.OWLClassExpression[source]

Get the ranges of an object property.

Parameters:

prop – Object property.

Returns:

Ranges of the property.

get_data_property_domains(prop: owlapy.owl_property.OWLDataProperty) owlapy.class_expression.OWLClassExpression[source]

Get the domains of a data property.

Parameters:

prop – Data property.

Returns:

Domains of the property.

get_data_property_ranges(prop: owlapy.owl_property.OWLDataProperty) FrozenSet[owlapy.owl_data_ranges.OWLDataRange][source]

Get the ranges of a data property.

Parameters:

prop – Data property.

Returns:

Ranges of the property.

most_general_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.owl_property.OWLDataProperty][source]

Find most general data properties that are applicable to a domain.

Parameters:

domain – Domain for which to search properties.

Returns:

Most general data properties for the given domain.

most_general_boolean_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.owl_property.OWLDataProperty][source]

Find most general boolean data properties that are applicable to a domain.

Parameters:

domain – Domain for which to search properties.

Returns:

Most general boolean data properties for the given domain.

most_general_numeric_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.owl_property.OWLDataProperty][source]

Find most general numeric data properties that are applicable to a domain.

Parameters:

domain – Domain for which to search properties.

Returns:

Most general numeric data properties for the given domain.

most_general_time_data_properties(*, domain: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.owl_property.OWLDataProperty][source]

Find most general time data properties that are applicable to a domain.

Parameters:

domain – Domain for which to search properties.

Returns:

Most general time data properties for the given domain.

most_general_existential_restrictions(*, domain: owlapy.class_expression.OWLClassExpression, filler: owlapy.class_expression.OWLClassExpression | None = None) Iterable[owlapy.class_expression.OWLObjectSomeValuesFrom][source]

Find most general existential restrictions that are applicable to a domain.

Parameters:
  • domain – Domain for which to search properties.

  • filler – Optional filler to put in the restriction (not normally used).

Returns:

Most general existential restrictions for the given domain.

most_general_universal_restrictions(*, domain: owlapy.class_expression.OWLClassExpression, filler: owlapy.class_expression.OWLClassExpression | None = None) Iterable[owlapy.class_expression.OWLObjectAllValuesFrom][source]

Find most general universal restrictions that are applicable to a domain.

Parameters:
  • domain – Domain for which to search properties.

  • filler – Optional filler to put in the restriction (not normally used).

Returns:

Most general universal restrictions for the given domain.

most_general_existential_restrictions_inverse(*, domain: owlapy.class_expression.OWLClassExpression, filler: owlapy.class_expression.OWLClassExpression | None = None) Iterable[owlapy.class_expression.OWLObjectSomeValuesFrom][source]

Find most general inverse existential restrictions that are applicable to a domain.

Parameters:
  • domain – Domain for which to search properties.

  • filler – Optional filler to put in the restriction (not normally used).

Returns:

Most general existential restrictions over inverse property.

most_general_universal_restrictions_inverse(*, domain: owlapy.class_expression.OWLClassExpression, filler: owlapy.class_expression.OWLClassExpression | None = None) Iterable[owlapy.class_expression.OWLObjectAllValuesFrom][source]

Find most general inverse universal restrictions that are applicable to a domain.

Parameters:
  • domain – Domain for which to search properties.

  • filler – Optional filler to put in the restriction (not normally used).

Returns:

Most general universal restrictions over inverse property.

get_direct_parents(concept: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.class_expression.OWLClass][source]

Direct parent concepts.

Parameters:

concept – Concept to find super concepts of.

Returns:

Direct parent concepts.

get_all_direct_sub_concepts(concept: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.class_expression.OWLClassExpression][source]

All direct sub concepts of a concept.

Parameters:

concept – Parent concept for which to get sub concepts.

Returns:

Direct sub concepts.

get_all_sub_concepts(concept: owlapy.class_expression.OWLClassExpression) Iterable[owlapy.class_expression.OWLClassExpression][source]

All sub concepts of a concept.

Parameters:

concept – Parent concept for which to get sub concepts.

Returns:

Sub concepts.

get_concepts() Iterable[owlapy.class_expression.OWLClass][source]

Get all concepts of this concept generator.

Returns:

Concepts.

get_object_properties() Iterable[owlapy.owl_property.OWLObjectProperty][source]

Get all object properties of this concept generator.

Returns:

Object properties.

get_data_properties(ranges: Set[owlapy.owl_datatype.OWLDatatype] = None) Iterable[owlapy.owl_property.OWLDataProperty][source]

Get all data properties of this concept generator for the given ranges.

Parameters:

ranges – Ranges for which to extract the data properties.

Returns:

Data properties for the given range.

get_boolean_data_properties() Iterable[owlapy.owl_property.OWLDataProperty][source]

Get all boolean data properties of this concept generator.

Returns:

Boolean data properties.

get_numeric_data_properties() Iterable[owlapy.owl_property.OWLDataProperty][source]

Get all numeric data properties of this concept generator.

Returns:

Numeric data properties.

get_time_data_properties() Iterable[owlapy.owl_property.OWLDataProperty][source]

Get all time data properties of this concept generator.

Returns:

Time data properties.

get_types(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = False) Iterable[owlapy.class_expression.OWLClass][source]

Get the named classes which are (direct) types of the specified individual.

Parameters:
  • ind – Individual.

  • direct – Whether to consider direct types.

Returns:

Types of the given individual.

get_object_properties_for_ind(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = True) Iterable[owlapy.owl_property.OWLObjectProperty][source]

Get the object properties for the given individual.

Parameters:
  • ind – Individual

  • 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.

get_data_properties_for_ind(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = True) Iterable[owlapy.owl_property.OWLDataProperty][source]

Get the data properties for the given individual

Parameters:
  • ind – Individual

  • 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.

get_object_property_values(ind: owlapy.owl_individual.OWLNamedIndividual, property_: owlapy.owl_property.OWLObjectPropertyExpression, direct: bool = True) Iterable[owlapy.owl_individual.OWLNamedIndividual][source]

Get the object property values for the given individual and property.

Parameters:
  • ind – Individual.

  • property – Object property.

  • 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.

get_data_property_values(ind: owlapy.owl_individual.OWLNamedIndividual, property_: owlapy.owl_property.OWLDataPropertyExpression, direct: bool = True) Iterable[owlapy.owl_literal.OWLLiteral][source]

Get the data property values for the given individual and property.

Parameters:
  • ind – Individual.

  • property – Data property.

  • 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.

contains_class(concept: owlapy.class_expression.OWLClassExpression) bool[source]

Check if an atomic class is contained within this concept generator.

Parameters:

concept – Atomic class.

Returns:

Whether the class is contained in the concept generator.

__repr__()[source]

Return repr(self).