Welcome to ontor’s documentation!

ontor (ONTology editOR) is a Python library built on Owlready2 for creating, editing, extending, debugging, and visualizing OWL2 ontologies

ONTology editOR (ontor) module

exception ontor.ontor.InfoException(**kwargs: str)

exception for invalid ontor inputs

class ontor.ontor.OntoEditor(iri: str, path: str, import_paths: list | None = None)

create, load, and edit ontologies

add_annotation(name: str, comment: str, lang: str | None = None) None

add annotation in language specified as localized string, defaults to regular string if no language is specified

Parameters:
  • name – entity name

  • comment – annotation to append

  • lang – annotation’s language (optional)

add_axioms(axioms: list) None

add entire axioms to onto NOTE: only one axiom may be specified at once NOTE: no error handling implemented for input tuples

Parameters:

axioms – list of tuples of the form [class, superclass, property, inverted(bool), cardinality type, cardinality, op-object, dp-range, dp-min-ex, dp-min-in, dp-exact, dp-max-in, dp-max-ex, negated(bool), equivalence(bool)] may also include dicts containing aggregate axioms of the form {“or”: [ax1, “and”: [ax2, ax3]]}

add_distinctions(distinct_sets: list) None

make classes disjoint and instances distinct NOTE: distinctions may lead to inconsistencies reasoners cannot handle

Parameters:

distinct_sets – list of lists with disjoint/ different elements

add_dps(dp_tuples: list) None

add datatype properties including their axioms to onto

Parameters:

dp_tuples – list of input tuples of the form [dp, super-dp, functional, domain, range, minex, minin, exact, maxin, maxex]

add_gcas(gcas: list) None

workaround for representing General Class Axioms adds two helper classes, each defined via an axiom, that are defined to be equivalent helper classes are denoted with an underscore

Parameters:

gcas – list of two-tuples with axioms as defined by add_axioms()

add_import(other_path: str) None

load an additional onto

Parameters:

other_path – path to file of onto to be imported

add_instances(instance_tuples: list) None

add instances and their relations to onto

Parameters:

instance_tuples – list of tuples of the form [instance, class, property, range, range-type]

add_label(name: str, label: str, lang: str | None = None) None

add label in language specified as localized string, defaults to regular string if no language is specified

Parameters:
  • name – entity name

  • label – label to be appended

  • lang – label’s language (optional)

add_ops(op_tuples: list) None

add object properties including their axioms to onto NOTE: only one inverse_prop can be processed per tuple

Parameters:

op_tuples – list of tuples of the form [op, super-op, domain, range, functional, inverse functional, transitive, symmetric, asymmetric, reflexive, irreflexive, inverse_prop]

add_taxo(class_tuples: list) None

add taxonomy to onto

Parameters:

class_tuples – list of 2-tuples of the form [class, superclass]

static class_dict_to_tuple_list(cls_dict: dict) list

helper function to convert dict with class definitions to list of tuples as required by add_taxo function

Parameters:

cls_dict – dictionary for taxonomy definition of the form {superclass: [subclasses]}

Returns:

list of class definition 2-tuples of the form [[subclass, superclass], …]

debug_onto(reasoner: str = 'hermit', assume_correct_taxo: bool = True) None

interactively (CLI) fix inconsistencies

Parameters:
  • assume_correct_taxo – if True, the user interactions will be limited to restrictions, i.e., options to delete taxonomical relations are not included, e.g., A rdfs:subClassOf B

  • reasoner – reasoner to be used for inferences

export_ntriples() None

saves with same filename, but as ntriples

get_axioms() list

identify all axioms included in the onto

Returns:

list of class, op, and dp axioms

get_class_restrictions(class_name: str, res_type: str = 'is_a', res_only: bool = True) list

retrieve restrictions on specific class by restriction type

Parameters:
  • class_name – name of the class for which restrictions shall be returned

  • res_only – only returns Restrictions if set to True, if set to False parent class(es) are also included

  • res_type – restriction type, either is_a or equivalent_to

Returns:

list of restrictions on class

get_elems() list

get classes, object properties, datatype properties, and instances

Returns:

nodes and edges from onto

query_onto(query: str) list

query onto using SPARQL NOTE: use of query_owlready messes up ranges of dps

Parameters:

query – SPARQL query

Returns:

query results as list

reasoning(reasoner: str = 'hermit', save: bool = False, debug: bool = False) list

run reasoner to check consistency and infer new facts

Parameters:
  • reasoner – reasoner can be eiter hermit or pellet

  • save – bool - save inferences into original file

  • debug – bool - log pellet explanations for inconsistencies; only works with Pellet

Returns:

returns list of inconsistent classes if there are any

remove_elements(elem_list: list) None

remove elements, all their descendents and (in case of classes) instances, and all references from axioms

Parameters:

elem_list – list of elements to be removed from onto

remove_from_taxo(elem_list: list, reassign: bool = True) None

remove a class from the taxonomy, but keep all subclasses and instances by relating them to parent NOTE: elem is not replaced in axioms bc this may be semantically incorrect

Parameters:
  • elem_list – list of elements to be removed from onto

  • reassign – add all restrictions to subclasses via is_a

remove_restrictions_including_prop(prop_name: str) None

remove class restrictions that include a certain property

Parameters:

prop_name – name of the property for which all class restrictions shall be removed

remove_restrictions_on_class(class_name: str) None

remove all restrictions on a given class

Parameters:

class_name – name of the class for which restrictions shall be removed

save_as(new_path: str) None

safe ontology as new file helpful, e.g., if multiple ontos were loaded

Parameters:

new_path – path including filename for saving the onto

visualize(classes: list | None = None, properties: list | None = None, focusnode: str | None = None, radius: int | None = None, bylabel: bool = False, lang: str | None = None, open_html: bool = False, tbox_only: bool = False, bgcolor: str = '#222222', classcolor: str = '#0065bd', instancecolor: str = '#98c6ea', font_color: str = '#FFFFFF') None

visualize onto as a graph; generates html

Parameters:
  • classes – list of classes to be included in plot

  • properties – list of properties to be included in plot

  • focusnode – node around which a partial graph shall be displayed

  • radius – maximum number of relations between a node and a node of one of the classes specified

  • bylabel – render visualization by labels (if available)

  • lang – language of the labels to be displayed

  • open_html – open html file generated

  • tbox_only – only visualizes TBox if set to True

  • bgcolor – background color for the plot as a hex code

  • classcolor – color of class nodes as a hex code

  • instancecolor – color of instance nodes as a hex code

  • font_color – font color for nodes as a hex code

Returns:

None

ontor.ontor.cleanup(complete: bool, *extensions: str) None

delete all files in the current directory with the extensions specified

Parameters:
  • extensions – extensions of files to be deleted

  • complete – do not delete current log file if set to False

ontor.ontor.load_csv(csv_file: str, load_first_line: bool = False) list

load data from CSV file

Parameters:
  • csv_file – input CSV file

  • load_first_line – indicates whether content from first row is also returned

Returns:

CSV contents as list of lists

ontor.ontor.load_json(json_file: str) dict | list

load data from JSON file

Parameters:

json_file – input JSON file

Returns:

JSON contents as dictionary

Indices and tables