Make it possible to load ontologies once for multiple instantiations

parent 75230068
Pipeline #74876174 passed with stage
in 1 minute and 29 seconds
......@@ -7,9 +7,15 @@ project adheres to [Semantic Versioning](http://semver.org).
### Added
- Add `FilePathOntologyLocator`
- Add `create_world`
### Changed
- `Query#execute` now returns XML Schema datatypes represented by their IRIs as
strings instead of `None`
- `Instantiation#__init__` now takes a `owlready2.namespace.World` as its first
parameter as an alternative to a list of ontology locators. An
`olwready2.namespace.World` object can be created by hand or with the help
of the new function `create_world`. This makes it possible to load ontologies
just once for multiple instantiations.
## [1.0.0] - 2019-02-19
......@@ -2,13 +2,16 @@
# Licensed under LGPLv3+, see LICENSE for details.
"""
Instantiations tie everything together. An
:class:`~ontocode.instantiation.Instantiation` aggregates a list of
:ref:`ontology locators<ontology-locators>`, a :ref:`template<templates>`, and
:class:`~ontocode.instantiation.Instantiation` aggregates an
:ref:`owlready2.namespace.World` object, a :ref:`template<templates>`, and
list, as well as optionally a single instance, of
:ref:`template inputs<template-inputs>`.
Calling one of their execution methods will cause it to load the ontologies
specified by the :ref:`ontology locators<ontology-locators>`, let the
When a list of :ref:`ontology locators<ontology-locators>` is passed to a
constructor as its first argument, an :ref:`owlready2.namespace.World` object
is created with the ontologies specified by the locators loaded into it.
Calling one of their execution methods will cause it to let the
:ref:`template inputs<template-inputs>` generate arguments for the
:ref:`template<templates>` and pass the results to its
:ref:`template<templates>`.
......@@ -62,6 +65,8 @@ import errno
import os
import owlready2 as owl
import ontocode.ontology as ontology
__all__ = ['Instantiation', 'TemplateInputArgumentError',
'TemplateInputResultError']
......@@ -88,8 +93,12 @@ def _write_to_file(path, content):
class Instantiation():
"""Instantiation of a template based on data queried from ontologies.
:param list ontology_locators: a list of
:ref:`ontology locators<ontology-locators>`
An `owlready2.namespace.World` object can be created from ontology locators
with :func:`~ontocode.ontology.create_world`.
:param object ontology_locators_or_world: a list of
:ref:`ontology locators<ontology-locators>` or an
`owlready2.namespace.World` object
:param ontocode.template.Template template: a template
:param list at_once_inputs: a list of
:class:`~ontocode.template_input.TemplateInput`\\s
......@@ -97,19 +106,22 @@ class Instantiation():
input
"""
def __init__(self, ontology_locators, template, at_once_inputs,
def __init__(self, ontology_locators_or_world, template, at_once_inputs,
per_row_input=None):
self._ontology_locators = ontology_locators
if isinstance(ontology_locators_or_world, owl.namespace.World):
self._world = ontology_locators_or_world
else:
self._world = ontology.create_world(ontology_locators_or_world)
self._at_once_inputs = at_once_inputs
self._per_row_input = per_row_input
self._template = template
def _execute(self, *args, **kwargs):
world = owl.World()
self._load_ontologies(world)
at_once_input = self._generate_at_once_input(world)
at_once_input = self._generate_at_once_input(self._world)
if self._per_row_input:
per_row_input = self._generate_per_row_input(world)
per_row_input = self._generate_per_row_input(self._world)
render_kwargs_list = [{**at_once_input, **row, **kwargs}
for row in per_row_input]
return [(args, render_kwargs,
......@@ -158,10 +170,6 @@ class Instantiation():
path = path_function(*args, **kwargs)
_write_to_file(path, content)
def _load_ontologies(self, world):
for locator in self._ontology_locators:
locator.load(world)
def _generate_at_once_input(self, world):
def generate_at_once_input(input_, world):
result = input_.generate(world)
......
......@@ -28,8 +28,8 @@ import abc
import os
import owlready2 as owl
__all__ = ['FilePathOntologyLocator', 'FileSystemOntologyLocator',
'URLOntologyLocator']
__all__ = ['create_world', 'FilePathOntologyLocator',
'FileSystemOntologyLocator', 'URLOntologyLocator']
def _load_ontology_in_world(world, iri):
......@@ -37,6 +37,25 @@ def _load_ontology_in_world(world, iri):
ontology.load()
def create_world(ontology_locators):
"""Create world object with preloaded ontologies.
With `create_world` you can create a single world object for multiple
instantiations, which is usefull when loading an ontology takes a
significant amount of time.
:param list ontology_locators: a list of
:ref:`ontology locators<ontology-locators>`
:return: An :ref:`owlready2.namespace.World` object into which the
ontologies specified via `ontology_locators` have been loaded.
:rtype: owlready2.namespace.World
"""
world = owl.World()
for locator in ontology_locators:
locator.load(world)
return world
class _OntologyLocator(metaclass=abc.ABCMeta):
"""Abstract base class for ontology locator classes."""
......
......@@ -61,6 +61,18 @@ def test_execute_and_write_to_files():
_test_file_content('/tmp/' + type_, type_)
def test_precreated_world():
world = ontocode.create_world([DEFAULT_LOCATOR])
input_ = ontocode.TemplateInput(DEFAULT_QUERY, NAME_PROCESSOR_CHAIN)
instantiation = ontocode.Instantiation(world, DEFAULT_TEMPLATE, [input_])
result = instantiation.execute()
assert DEFAULT_RESULT == result[0]
def _constantly_none(_result, _world):
pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment