Source

htsql-firebird / src / htsql / core / tr / signature.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#
# Copyright (c) 2006-2012, Prometheus Research, LLC
# See `LICENSE` for license information, `AUTHORS` for the list of authors.
#


"""
:mod:`htsql.core.tr.signature`
==============================

This module defines formula nodes and formula signatures.
"""


from ..util import maybe, listof, Comparable, Clonable, Printable


class Slot(object):
    """
    Represents a formula slot.

    A slot is a parameter of a formula.  A slot is to be filled
    with an argument value when a formula node is instantiated.

    `name` (a string)
        The name of the argument.

    `is_mandatory` (Boolean)
        Indicates that the slot requires at least one value.

    `is_singular` (Boolean)
        Indicates that the slot accepts no more than one value.
    """

    def __init__(self, name, is_mandatory=True, is_singular=True):
        # Sanity check on the arguments.
        assert isinstance(name, str) and len(name) > 0
        assert isinstance(is_mandatory, bool)
        assert isinstance(is_singular, bool)

        self.name = name
        self.is_mandatory = is_mandatory
        self.is_singular = is_singular


class Signature(Comparable, Clonable, Printable):
    """
    Represents a formula signature.

    A signature identifies the type of a formula.  In particular,
    a signature describes all slots of the formula.

    Class attributes:

    `slots` (a list of :class:`Slot`)
        The formula slots.

    Constructor arguments:

    `equality_vector` (an immutable tuple)
        Encapsulates all essential attributes of a signature.

        Two signatures are considered equal if they are of the same type
        and their equality vectors coincide.
    """

    # Override in subclasses.
    slots = []

    def __init__(self, equality_vector=()):
        super(Signature, self).__init__(equality_vector=equality_vector)

    def __str__(self):
        return self.__class__.__name__


class Bag(dict):
    """
    Encapsulates formula arguments.

    `arguments` (a dictionary)
        Maps slot names to argument values.

        Depending on the slot type, a value could be one of:
        - a node or ``None`` for singular slots;
        - a list of nodes for plural slots.

        A missing argument is indicated by ``None`` for a singular
        slot or by an empty list for a plural slot.  Missing
        arguments are not allowed for mandatory slots.

    :class:`Bag` provides a mapping interface to `arguments`.
    """

    # FIXME: respect the order of slots in the signature.

    def __init__(self, **arguments):
        # Initialize the underlying dictionary.
        self.update(arguments)

    def admits(self, kind, signature):
        """
        Verifies that the arguments match the given signature.

        Returns ``True`` if the arguments match the given signature,
        ``False`` otherwise.

        `kind` (a type)
            The expected type of value nodes.

        `signature` (:class:`Signature`)
            The expected signature of the arguments.
        """
        # Sanity check on the arguments.
        assert isinstance(kind, type)
        assert (isinstance(signature, Signature) or
                issubclass(signature, Signature))

        # Verify that the arguments match the slot names.
        if set(self.keys()) != set(slot.name for slot in signature.slots):
            return False

        # Check every slot.
        for slot in signature.slots:
            # The argument.
            value = self[slot.name]

            # A value of a singular slot must be a node of the given
            # type or ``None``; a value equal to ``None`` is allowed
            # only for optional slots.
            if slot.is_singular:
                if not isinstance(value, maybe(kind)):
                    return False
                if slot.is_mandatory:
                    if value is None:
                        return False
            # A value of a plural slot must be a list of nodes of the
            # given type and, unless the slot is optional, must contain
            # at least one node.
            else:
                if not isinstance(value, listof(kind)):
                    return False
                if slot.is_mandatory:
                    if not value:
                        return False

        # All checks passed.
        return True

    def cells(self):
        """
        Returns a list of all subnodes.

        This function extracts all (singular) nodes from the arguments.
        """
        # A list of nodes.
        cells = []
        # Iterate over all the arguments.
        for key in sorted(self.keys()):
            # A value: could be ``None``, a node or a list of nodes.
            value = self[key]
            if value is not None:
                if isinstance(value, list):
                    cells.extend(value)
                else:
                    cells.append(value)
        return cells

    def impress(self, owner):
        """
        Adds the arguments as attributes to the given object.

        `owner` (a node object)
            An object to update.
        """
        # Iterate through all the arguments.
        for key in sorted(self.keys()):
            # Make sure we do not override an existing attribute.
            assert not hasattr(owner, key)
            # Impress the argument to the object.
            setattr(owner, key, self[key])

    def map(self, method):
        """
        Applies the given function to all subnodes.

        Returns a new :class:`Bag` instance of the same shape
        composed from the results of the `method` application
        to every value node.

        `method` (a callable)
            A function to apply.
        """
        # The result of the `method` applications.
        arguments = {}
        # Iterate over all the arguments.
        for key in sorted(self.keys()):
            # An argument value: `None`, a node, or a list of nodes.
            value = self[key]
            # Apply `method` to `value`.
            if value is not None:
                if isinstance(value, list):
                    value = [method(item) for item in value]
                else:
                    value = method(value)
            arguments[key] = value
        # Produce a new `Bag` instance with updated arguments.
        return self.__class__(**arguments)

    def freeze(self):
        """
        Returns an immutable container with all the argument values.

        This function is useful for constructing an equality vector
        of a formula node.
        """
        # An ordered list of the (frozen) argument values.
        values = []
        # Iterate over the arguments; freeze mutable objects
        # (i.e., convert a list to a tuple).
        for key in sorted(self.keys()):
            value = self[key]
            if isinstance(value, list):
                value = tuple(value)
            values.append(value)
        # Finally freeze and return the list itself.
        return tuple(values)


class Formula(Printable):
    """
    Represents a formula node.

    This is a mixin class; it is mixed with
    :class:`htsql.core.tr.binding.Binding`, :class:`htsql.core.tr.flow.Code`
    and :class:`htsql.core.tr.frame.Phrase` to produce respective formula node
    types.

    `signature` (:class:`Signature`)
        The formula signature.

    `arguments` (:class:`Bag`)
        The formula arguments; must be compatible with the signature.

    The rest of the arguments are passed to the next base class constructor
    unchanged.
    """

    def __init__(self, signature, arguments, *args, **kwds):
        assert isinstance(signature, Signature)
        # The caller is responsible for checking that the arguments
        # are compatible with the signature.
        assert isinstance(arguments, Bag)
        super(Formula, self).__init__(*args, **kwds)
        self.signature = signature
        self.arguments = arguments
        # Add an attribute for each argument.
        arguments.impress(self)

    def __str__(self):
        # Display:
        #   Signature: ...
        return "%s: %s" % (self.signature, super(Formula, self).__str__())


def isformula(formula, signatures):
    """
    Checks if a node is a formula with the given signature.

    The function returns ``True`` if the given node is a formula
    and its signature is a subclass of the given signature class;
    ``False`` otherwise.

    `formula` (a node, possibly a :class:`Formula` node)
        A node to check.

    `signatures` (a subclass or a tuple of subclasses of :class:`Signature`)
        The expected formula signature(s).
    """
    # Normalize the signatures.
    if not isinstance(signatures, tuple):
        signatures = (signatures,)
    # Check that the given node is, indeed, a formula, and that
    # its signature is among the given signature classes.
    return (isinstance(formula, Formula) and
            any(isinstance(formula.signature, signature)
                for signature in signatures))


class NullarySig(Signature):
    """
    Represents a signature with no slots.
    """

    slots = []


class UnarySig(Signature):
    """
    Represents a signature with one singular slot.
    """

    slots = [
            Slot('op'),
    ]


class BinarySig(Signature):
    """
    Represents a signature with two singular slots.
    """

    slots = [
            Slot('lop'),
            Slot('rop'),
    ]


class NArySig(Signature):
    """
    Represents a signature with one singular slot and one plural slot.
    """

    slots = [
            Slot('lop'),
            Slot('rops', is_singular=False),
    ]


class ConnectiveSig(Signature):
    """
    Represents a signature with one plural slot.
    """

    slots = [
            Slot('ops', is_singular=False),
    ]


class PolarSig(Signature):
    """
    Denotes a formula with two forms: positive and negative.

    `polarity` (``+1`` or ``-1``)
        Indicates the form of the formula: ``+1`` for positive,
        ``-1`` for negative.
    """

    def __init__(self, polarity):
        assert polarity in [+1, -1]
        super(PolarSig, self).__init__(equality_vector=(polarity,))
        self.polarity = polarity

    def reverse(self):
        """
        Returns the signature with the opposite polarity.
        """
        return self.clone(polarity=-self.polarity)

    def __str__(self):
        # Display:
        #   Signature(+/-)
        return "%s(%s)" % (self.__class__.__name__,
                           '+' if self.polarity > 0 else '-')


class IsEqualSig(BinarySig, PolarSig):
    """
    Denotes an equality (``=``) and an inequality (``!=``) operator.
    """


class IsTotallyEqualSig(BinarySig, PolarSig):
    """
    Denotes a total equality (``==`` and ``!==``) operator.
    """


class IsInSig(NArySig, PolarSig):
    """
    Denotes an N-ary equality (``={}`` and ``!={}``) operator.
    """


class IsNullSig(UnarySig, PolarSig):
    """
    Denotes an ``is_null()`` operator.
    """


class IfNullSig(BinarySig):
    """
    Denotes an ``if_null()`` operator.
    """


class NullIfSig(BinarySig):
    """
    Denotes a ``null_if()`` operator.
    """


class CompareSig(BinarySig):
    """
    Denotes a comparison operator.

    `relation` (one of: ``'<'``, ``'<='``, ``'>'``, ``'>='``)
        Indicates the comparison relation.
    """

    def __init__(self, relation):
        assert relation in ['<', '<=', '>', '>=']
        super(CompareSig, self).__init__(equality_vector=(relation,))
        self.relation = relation


class AndSig(ConnectiveSig):
    """
    Denotes a Boolean "AND" (``&``) operator.
    """


class OrSig(ConnectiveSig):
    """
    Denotes a Boolean "OR" (``|``) operator.
    """


class NotSig(UnarySig):
    """
    Denotes a Boolean "NOT" (``!``) operator.
    """


class ToPredicateSig(UnarySig):
    pass


class FromPredicateSig(UnarySig):
    pass