pycrap.parser

Contents

pycrap.parser#

Attributes#

Classes#

AbstractParser

An abstract class for parsing

OntologyParser

A class that parses everything from an owlready2 compatible ontology into python files that

OntologiesParser

Class that parses multiple ontologies at once.

Functions#

set_explicit_repr_for_logical_operator()

to_snake_case(→ str)

Convert a string to snake case.

to_camel_case(→ str)

Convert a string to camel case.

replace_types(→ str)

Replace the types in a string with python types

update_class_names(onto)

Update the class names to match python conventions.

update_property_names(onto)

Update the property names to match python conventions of functions and members.

Module Contents#

pycrap.parser.digit_map#
pycrap.parser.set_explicit_repr_for_logical_operator()#
pycrap.parser.to_snake_case(string: str) str#

Convert a string to snake case.

Parameters:

string – The string to convert.

Returns:

The string in snake case.

pycrap.parser.to_camel_case(string: str) str#

Convert a string to camel case.

Parameters:

string – The string to convert.

Returns:

The string in camel case.

pycrap.parser.replace_types(string: str) str#

Replace the types in a string with python types

Example: >>> replace_types(“array_double__<class ‘int’>”) “float__int”

# TODO array_double will be converted to float for now :param string: The string to convert :return: The string with the types replaced

pycrap.parser.update_class_names(onto: owlready2.Ontology)#

Update the class names to match python conventions.

pycrap.parser.update_property_names(onto: owlready2.Ontology)#

Update the property names to match python conventions of functions and members.

class pycrap.parser.AbstractParser(path, indentation: int = 4)#

An abstract class for parsing

current_file: typing_extensions.Any#

The current file where contents are written into.

indentation: int#

The indentation to use for the python file.

file_extension: str = '.py'#

The file extension for the python file.

path: str#

The path to write the files of the parsed ontology into.

apply_indent_to(string)#

Indent a statement at the beginning of every new line.

Parameters:

string – The statement.

Returns:

The indented string.

path_for_file(file_name)#

Generate the path for a file.

Example: >>> parser = AbstractParser(“/tmp”) >>> parser.path_for_file(“base”)

“/tmp/base.py”

Parameters:

file_name – The file name.

Returns:

The path to the file.

class pycrap.parser.OntologyParser(ontology: owlready2.Ontology, dependencies: typing_extensions.List[owlready2.Ontology], path: str, indentation: int = 4)#

Bases: AbstractParser

A class that parses everything from an owlready2 compatible ontology into python files that represent the same ontology.

It will create several files in a directory specified by the constructor: - dependencies: A file that the imports from other packages/modules that are needed to define this ontology. - classes: A file that contains all classes from the ontology without any restrictions. - object_properties: A file the contains all object properties from the ontology without any restrictions. - data_properties: A file the contains all data properties from the ontology without any restrictions. - restrictions: The restrictions for all classes and properties. - individuals: All individuals with the respective properties from the ontology. - __init__.py: A package initialization that loads the content of all files and hence sets the restrictions

and so on.

TODO: Labels metadata and SWRL is not parsed

ontology: owlready2.Ontology#

The ontology to parse.

classes_file_name: str = 'classes'#

The file name where all elements from ontology.classes() are written to.

object_properties_file_name: str = 'object_properties'#

The file name where all elements from ontology.properties() are written to.

data_properties_file_name: str = 'data_properties'#

The file name where all elements from ontology.data_properties() are written to.

restrictions_file_name: str = 'restrictions'#

The file name where all restrictions are written to.

individuals_file_name: str = 'individuals'#

The file name where all individuals are written to.

dependencies_file_name: str = 'dependencies'#

The file name where all dependencies are written to.

dependencies: typing_extensions.List[owlready2.Ontology]#

The other ontologies that have to be imported.

parse()#

Parses the ontology into a python file.

create_dependencies()#
create_init()#

Create the __init__.py

import_restrictions()#

Write the import statement that imports restrictions.

import_dependencies()#

Import from the dependencies.

create_classes()#

Create the classes.py

create_object_properties()#

Create the object_properties.py

create_data_properties()#

Create the data_properties.py

create_restrictions()#

Create the restrictions.py

parse_restrictions_for(element)#

Create all restriction for any element of the ontology.

parse_restrictions_for_class(cls: owlready2.ThingClass)#

Create the restrictions for a class.

Parameters:

cls – The class

import_individuals()#

Write the import statement that imports individuals.

parse_restrictions_for_property(prop)#

Write all restrictions for a property. :param prop: The property

create_individuals()#

Create all individuals of the ontology.

parse_individual(individual: owlready2.Thing)#

Parse the construction of an individual. :param individual: The individual.

parse_individual_properties(individual: owlready2.Thing)#

Parse the properties of an individual. :param individual: The individual.

import_classes()#

Create the import statement to get everything from the classes.py

import_properties()#

Create the import statement to get everything from the properties

apply_indent_to(string)#

Indent a statement at the beginning of every new line.

Parameters:

string – The statement.

Returns:

The indented string.

get_docstring(cls) str#

Get the docstring for a class.

Parameters:

cls – The class

Returns:

The docstring for the class or “…” if no docstring is found.

parse_element(element) str#

Parse an element for representation the source code.

Parameters:

element – The element to parse.

Returns:

A string representation that can be used in python files.

parse_elements(elements: typing_extensions.List) str#

Parse a list of elements from for the representation in the source code. An input can be, for instance, the is_a` field of a class.

Parameters:

elements – A list of elements to parse.

Returns:

A string representation of the elements.

write_docstring(cls)#

Write the docstring of a class to the current file. :param cls: The class.

write_equivalent_to(cls)#

Write the equivalent_to field of a class

Parameters:

cls – The class.

write_is_a(cls)#

Write the is_a field of a class :param cls: The class.

parse_class(cls)#

Parse a class without restrictions. :param cls: The class.

parse_property(prop)#

Parse a property without restrictions. :param prop: The property.

class pycrap.parser.OntologiesParser(ontologies: typing_extensions.List[owlready2.Ontology], path: str, indentation: int = 4)#

Bases: AbstractParser

Class that parses multiple ontologies at once.

The resulting python package has the following form

path/__init__.py path/base.py path/ontology1/__init__.py path/ontology1/classes.py path/ontology1/object_properties.py path/ontology1/data_properties.py path/ontology1/restrictions.py path/ontology1/individuals.py path/ontology2/__init__.py path/ontology2/classes.py …

ontologies: typing_extensions.List[owlready2.Ontology]#

The ontologies to parse.

include_imported_ontologies = True#

If True, the imported ontologies are also parsed.

dependency_graph: networkx.DiGraph#

The dependency graph of the ontologies.

base_file_name: str = 'base'#

The file name where the base classes are written to.

clear_existing: bool = True#

If True, the existing directories are cleared.

create_base()#

Create the base file

create_base_class()#

Create the base class for concepts.

create_base_property()#

Create the base class for properties.

destroy_all_ontologies()#

Destroy all ontologies.

create_dependency_graph()#

Create the dependency graph of the ontologies.

parse()#
create_init()#

Create the __init__.py

create_ontologies()#