1# Copyright 2020, 2021 PaGMO development team
2#
3# This file is part of the pygmo library.
4#
5# This Source Code Form is subject to the terms of the Mozilla
6# Public License v. 2.0. If a copy of the MPL was not distributed
7# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9from .core import problem
10
11
12def _problem_extract(self, t):
13    """Extract the user-defined problem.
14
15    This method allows to extract a reference to the user-defined problem (UDP) stored within this
16    :class:`~pygmo.problem` instance. The behaviour of this function depends on the value
17    of *t* (which must be a :class:`type`) and on the type of the internal UDP:
18
19    * if the type of the UDP is *t*, then a reference to the UDP will be returned
20      (this mirrors the behaviour of the corresponding C++ method
21      :cpp:func:`pagmo::problem::extract()`),
22    * if *t* is :class:`object` and the UDP is a Python object (as opposed to an
23      :ref:`exposed C++ problem <problems_cpp>`), then a reference to the
24      UDP will be returned (this allows to extract a Python UDP without knowing its type),
25    * otherwise, :data:`None` will be returned.
26
27    Args:
28        t (:class:`type`): the type of the user-defined problem to extract
29
30    Returns:
31        a reference to the internal user-defined problem, or :data:`None` if the extraction fails
32
33    Raises:
34        TypeError: if *t* is not a :class:`type`
35
36    Examples:
37        >>> import pygmo as pg
38        >>> p1 = pg.problem(pg.rosenbrock())
39        >>> p1.extract(pg.rosenbrock) # doctest: +SKIP
40        <pygmo.core.rosenbrock at 0x7f56b870fd50>
41        >>> p1.extract(pg.ackley) is None
42        True
43        >>> class prob:
44        ...     def fitness(self, x):
45        ...         return [x[0]]
46        ...     def get_bounds(self):
47        ...         return ([0],[1])
48        >>> p2 = pg.problem(prob())
49        >>> p2.extract(object) # doctest: +SKIP
50        <__main__.prob at 0x7f56a66b6588>
51        >>> p2.extract(prob) # doctest: +SKIP
52        <__main__.prob at 0x7f56a66b6588>
53        >>> p2.extract(pg.rosenbrock) is None
54        True
55
56    """
57    if not isinstance(t, type):
58        raise TypeError("the 't' parameter must be a type")
59    if hasattr(t, "_pygmo_cpp_problem"):
60        return self._cpp_extract(t())
61    return self._py_extract(t)
62
63
64def _problem_is(self, t):
65    """Check the type of the user-defined problem.
66
67    This method returns :data:`False` if :func:`extract(t) <pygmo.problem.extract>` returns
68    :data:`None`, and :data:`True` otherwise.
69
70    Args:
71        t (:class:`type`): the type that will be compared to the type of the UDP
72
73    Returns:
74        bool: whether the UDP is of type *t* or not
75
76    Raises:
77        unspecified: any exception thrown by :func:`~pygmo.problem.extract()`
78
79    """
80    return not self.extract(t) is None
81
82
83# Do the actual patching.
84setattr(problem, "extract", _problem_extract)
85setattr(problem, "is_", _problem_is)
86