aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/matcher/matcher.py
blob: a89626f8e04c2f89c281e809a16286931d08b67f (plain)
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

# standard imports
import abc
import typing

# bsie imports
from bsie.utils import bsfs

# inner-module imports
from . import nodes

# exports
__all__: typing.Sequence[str] = (
    'Matcher',
    )


## code ##

class Matcher():
    """Determine node uri's from node hints."""
    def __call__(
            self,
            iterable: typing.Iterable[typing.Tuple[nodes.Node, bsfs.URI, typing.Any]],
            ):
        """Apply the matcher on a triple iterator."""
        return MatcherIterator(self, iterable)

    @abc.abstractmethod
    def match_node(self, node: nodes.Node) -> nodes.Node:
        """Apply the matcher on a node."""


class MatcherIterator():
    """Iterates over triples, determines uris according to a *matcher* as it goes."""

    # source triple iterator.
    _iterable: typing.Iterable[typing.Tuple[nodes.Node, bsfs.URI, typing.Any]]

    # node matcher
    _matcher: Matcher

    def __init__(
            self,
            matcher: Matcher,
            iterable: typing.Iterable[typing.Tuple[nodes.Node, bsfs.URI, typing.Any]],
            ):
        self._iterable = iterable
        self._matcher = matcher

    def __iter__(self):
        for node, pred, value in self._iterable:
            # handle subject
            self._matcher.match_node(node)
            # handle value
            if isinstance(value, nodes.Node):
                self._matcher.match_node(value)
            # yield triple
            yield node, pred, value

## EOF ##