pycrap.parser
=============

.. py:module:: pycrap.parser


Attributes
----------

.. autoapisummary::

   pycrap.parser.digit_map


Classes
-------

.. autoapisummary::

   pycrap.parser.AbstractParser
   pycrap.parser.OntologyParser
   pycrap.parser.OntologiesParser


Functions
---------

.. autoapisummary::

   pycrap.parser.set_explicit_repr_for_logical_operator
   pycrap.parser.to_snake_case
   pycrap.parser.to_camel_case
   pycrap.parser.replace_types
   pycrap.parser.update_class_names
   pycrap.parser.update_property_names


Module Contents
---------------

.. py:data:: digit_map

.. py:function:: set_explicit_repr_for_logical_operator()

.. py:function:: to_snake_case(string: str) -> str

   Convert a string to snake case.

   :param string: The string to convert.
   :return: The string in snake case.


.. py:function:: to_camel_case(string: str) -> str

   Convert a string to camel case.

   :param string: The string to convert.
   :return: The string in camel case.


.. py:function:: 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


.. py:function:: update_class_names(onto: owlready2.Ontology)

   Update the class names to match python conventions.


.. py:function:: update_property_names(onto: owlready2.Ontology)

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


.. py:class:: AbstractParser(path, indentation: int = 4)

   An abstract class for parsing


   .. py:attribute:: current_file
      :type:  typing_extensions.Any

      The current file where contents are written into.



   .. py:attribute:: indentation
      :type:  int

      The indentation to use for the python file.



   .. py:attribute:: file_extension
      :type:  str
      :value: '.py'


      The file extension for the python file.



   .. py:attribute:: path
      :type:  str

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



   .. py:method:: apply_indent_to(string)

      Indent a statement at the beginning of every new line.

      :param string: The statement.
      :return: The indented string.



   .. py:method:: path_for_file(file_name)

      Generate the path for a file.

      Example:
      >>> parser = AbstractParser("/tmp")
      >>> parser.path_for_file("base")
          "/tmp/base.py"

      :param file_name: The file name.
      :return: The path to the file.



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

   Bases: :py:obj:`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


   .. py:attribute:: ontology
      :type:  owlready2.Ontology

      The ontology to parse.



   .. py:attribute:: classes_file_name
      :type:  str
      :value: 'classes'


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



   .. py:attribute:: object_properties_file_name
      :type:  str
      :value: 'object_properties'


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



   .. py:attribute:: data_properties_file_name
      :type:  str
      :value: 'data_properties'


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



   .. py:attribute:: restrictions_file_name
      :type:  str
      :value: 'restrictions'


      The file name where all restrictions are written to.



   .. py:attribute:: individuals_file_name
      :type:  str
      :value: 'individuals'


      The file name where all individuals are written to.



   .. py:attribute:: dependencies_file_name
      :type:  str
      :value: 'dependencies'


      The file name where all dependencies are written to.



   .. py:attribute:: dependencies
      :type:  typing_extensions.List[owlready2.Ontology]

      The other ontologies that have to be imported.



   .. py:method:: parse()

      Parses the ontology into a python file.



   .. py:method:: create_dependencies()


   .. py:method:: create_init()

      Create the __init__.py



   .. py:method:: import_restrictions()

      Write the import statement that imports restrictions.



   .. py:method:: import_dependencies()

      Import from the dependencies.



   .. py:method:: create_classes()

      Create the classes.py



   .. py:method:: create_object_properties()

      Create the object_properties.py



   .. py:method:: create_data_properties()

      Create the data_properties.py



   .. py:method:: create_restrictions()

      Create the restrictions.py



   .. py:method:: parse_restrictions_for(element)

      Create all restriction for any element of the ontology.



   .. py:method:: parse_restrictions_for_class(cls: owlready2.ThingClass)

      Create the restrictions for a class.

      :param cls: The class



   .. py:method:: import_individuals()

      Write the import statement that imports individuals.



   .. py:method:: parse_restrictions_for_property(prop)

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



   .. py:method:: create_individuals()

      Create all individuals of the ontology.



   .. py:method:: parse_individual(individual: owlready2.Thing)

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



   .. py:method:: parse_individual_properties(individual: owlready2.Thing)

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



   .. py:method:: import_classes()

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



   .. py:method:: import_properties()

      Create the import statement to get everything from the properties



   .. py:method:: apply_indent_to(string)

      Indent a statement at the beginning of every new line.

      :param string: The statement.
      :return: The indented string.



   .. py:method:: get_docstring(cls) -> str

      Get the docstring for a class.

      :param cls: The class
      :return: The docstring for the class or "..." if no docstring is found.



   .. py:method:: parse_element(element) -> str

      Parse an element for representation the source code.

      :param element: The element to parse.
      :return: A string representation that can be used in python files.



   .. py:method:: 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.

      :param elements: A list of elements to parse.
      :return: A string representation of the elements.



   .. py:method:: write_docstring(cls)

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



   .. py:method:: write_equivalent_to(cls)

      Write the `equivalent_to` field of a class

      :param cls: The class.



   .. py:method:: write_is_a(cls)

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



   .. py:method:: parse_class(cls)

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



   .. py:method:: parse_property(prop)

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



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

   Bases: :py:obj:`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
   ...


   .. py:attribute:: ontologies
      :type:  typing_extensions.List[owlready2.Ontology]

      The ontologies to parse.



   .. py:attribute:: include_imported_ontologies
      :value: True


      If True, the imported ontologies are also parsed.



   .. py:attribute:: dependency_graph
      :type:  networkx.DiGraph

      The dependency graph of the ontologies.



   .. py:attribute:: base_file_name
      :type:  str
      :value: 'base'


      The file name where the base classes are written to.



   .. py:attribute:: clear_existing
      :type:  bool
      :value: True


      If True, the existing directories are cleared.



   .. py:method:: create_base()

      Create the base file



   .. py:method:: create_base_class()

      Create the base class for concepts.



   .. py:method:: create_base_property()

      Create the base class for properties.



   .. py:method:: destroy_all_ontologies()

      Destroy all ontologies.



   .. py:method:: create_dependency_graph()

      Create the dependency graph of the ontologies.



   .. py:method:: parse()


   .. py:method:: create_init()

      Create the __init__.py



   .. py:method:: create_ontologies()


