Skip to content

regexmatching

This wrapper intercepts calls through the store interface which make use of the REGEXTerm class to represent matches by REGEX instead of literal comparison.

Implemented for stores that don’t support this and essentially provides the support by replacing the REGEXTerms by wildcards (None) and matching against the results from the store it’s wrapping.

Classes:

Functions:

Attributes:

NATIVE_REGEX module-attribute

NATIVE_REGEX = 0

PYTHON_REGEX module-attribute

PYTHON_REGEX = 1

REGEXMatching

REGEXMatching(storage)

Bases: Store

Methods:

Attributes:

Source code in rdflib/plugins/stores/regexmatching.py
def __init__(self, storage):
    self.storage = storage
    self.context_aware = storage.context_aware
    # NOTE: this store can't be formula_aware as it doesn't have enough
    # info to reverse the removal of a quoted statement.
    self.formula_aware = storage.formula_aware
    self.transaction_aware = storage.transaction_aware

context_aware instance-attribute

context_aware = context_aware

formula_aware instance-attribute

formula_aware = formula_aware

storage instance-attribute

storage = storage

transaction_aware instance-attribute

transaction_aware = transaction_aware

__len__

__len__(context=None)
Source code in rdflib/plugins/stores/regexmatching.py
def __len__(self, context=None):
    # NOTE: If the context is a REGEX this could be an expensive
    # proposition
    return self.storage.__len__(context)

add

add(triple, context, quoted=False)
Source code in rdflib/plugins/stores/regexmatching.py
def add(self, triple, context, quoted=False):
    (subject, predicate, object_) = triple
    self.storage.add((subject, predicate, object_), context, quoted)

bind

bind(prefix, namespace, override=True)
Source code in rdflib/plugins/stores/regexmatching.py
def bind(self, prefix, namespace, override=True):
    self.storage.bind(prefix, namespace, override=override)

close

close(commit_pending_transaction=False)
Source code in rdflib/plugins/stores/regexmatching.py
def close(self, commit_pending_transaction=False):
    self.storage.close()

commit

commit()
Source code in rdflib/plugins/stores/regexmatching.py
def commit(self):
    self.storage.commit()

contexts

contexts(triple=None)
Source code in rdflib/plugins/stores/regexmatching.py
def contexts(self, triple=None):
    # NOTE: There is no way to control REGEX matching for this method at
    # this level as it only returns the contexts, not the matching
    # triples.
    for ctx in self.storage.contexts(triple):
        yield ctx

destroy

destroy(configuration)
Source code in rdflib/plugins/stores/regexmatching.py
def destroy(self, configuration):
    self.storage.destroy(configuration)

namespace

namespace(prefix)
Source code in rdflib/plugins/stores/regexmatching.py
def namespace(self, prefix):
    return self.storage.namespace(prefix)

namespaces

namespaces()
Source code in rdflib/plugins/stores/regexmatching.py
def namespaces(self):
    return self.storage.namespaces()

open

open(configuration, create=True)
Source code in rdflib/plugins/stores/regexmatching.py
def open(self, configuration, create=True):
    return self.storage.open(configuration, create)

prefix

prefix(namespace)
Source code in rdflib/plugins/stores/regexmatching.py
def prefix(self, namespace):
    return self.storage.prefix(namespace)

remove

remove(triple, context=None)
Source code in rdflib/plugins/stores/regexmatching.py
def remove(self, triple, context=None):
    (subject, predicate, object_) = triple
    if (
        isinstance(subject, REGEXTerm)
        or isinstance(predicate, REGEXTerm)
        or isinstance(object_, REGEXTerm)
        or (context is not None and isinstance(context.identifier, REGEXTerm))
    ):
        # One or more of the terms is a REGEX expression, so we must
        # replace it / them with wildcard(s)and match after we query.
        s = not isinstance(subject, REGEXTerm) and subject or None
        p = not isinstance(predicate, REGEXTerm) and predicate or None
        o = not isinstance(object_, REGEXTerm) and object_ or None
        c = (
            (context is not None and not isinstance(context.identifier, REGEXTerm))
            and context
            or None
        )

        removeQuadList = []  # noqa: N806
        for (s1, p1, o1), cg in self.storage.triples((s, p, o), c):
            for ctx in cg:
                ctx = ctx.identifier
                if regexCompareQuad(
                    (s1, p1, o1, ctx),
                    (
                        subject,
                        predicate,
                        object_,
                        context is not None and context.identifier or context,
                    ),
                ):
                    removeQuadList.append((s1, p1, o1, ctx))
        for s, p, o, c in removeQuadList:
            self.storage.remove((s, p, o), c and Graph(self, c) or c)
    else:
        self.storage.remove((subject, predicate, object_), context)

remove_context

remove_context(identifier)
Source code in rdflib/plugins/stores/regexmatching.py
def remove_context(self, identifier):
    self.storage.remove((None, None, None), identifier)

rollback

rollback()
Source code in rdflib/plugins/stores/regexmatching.py
def rollback(self):
    self.storage.rollback()

triples

triples(triple, context=None)
Source code in rdflib/plugins/stores/regexmatching.py
def triples(self, triple, context=None):
    (subject, predicate, object_) = triple
    if (
        isinstance(subject, REGEXTerm)
        or isinstance(predicate, REGEXTerm)
        or isinstance(object_, REGEXTerm)
        or (context is not None and isinstance(context.identifier, REGEXTerm))
    ):
        # One or more of the terms is a REGEX expression, so we must
        # replace it / them with wildcard(s) and match after we query.
        s = not isinstance(subject, REGEXTerm) and subject or None
        p = not isinstance(predicate, REGEXTerm) and predicate or None
        o = not isinstance(object_, REGEXTerm) and object_ or None
        c = (
            (context is not None and not isinstance(context.identifier, REGEXTerm))
            and context
            or None
        )
        for (s1, p1, o1), cg in self.storage.triples((s, p, o), c):
            matchingCtxs = []  # noqa: N806
            for ctx in cg:
                if c is None:
                    if context is None or context.identifier.compiledExpr.match(
                        ctx.identifier
                    ):
                        matchingCtxs.append(ctx)
                else:
                    matchingCtxs.append(ctx)
            if matchingCtxs and regexCompareQuad(
                (s1, p1, o1, None), (subject, predicate, object_, None)
            ):
                yield (s1, p1, o1), (c for c in matchingCtxs)
    else:
        for (s1, p1, o1), cg in self.storage.triples(
            (subject, predicate, object_), context
        ):
            yield (s1, p1, o1), cg

REGEXTerm

REGEXTerm(expr)

Bases: str

REGEXTerm can be used in any term slot and is interpreted as a request to perform a REGEX match (not a string comparison) using the value (pre-compiled) for checking rdf:type matches

Methods:

Attributes:

Source code in rdflib/plugins/stores/regexmatching.py
def __init__(self, expr):
    self.compiledExpr = re.compile(expr)

compiledExpr instance-attribute

compiledExpr = compile(expr)

__reduce__

__reduce__()
Source code in rdflib/plugins/stores/regexmatching.py
def __reduce__(self):
    return (REGEXTerm, ("",))

regexCompareQuad

regexCompareQuad(quad, regexQuad)
Source code in rdflib/plugins/stores/regexmatching.py
def regexCompareQuad(quad, regexQuad):  # noqa: N802, N803
    for index in range(4):
        if isinstance(regexQuad[index], REGEXTerm) and not regexQuad[
            index
        ].compiledExpr.match(quad[index]):
            return False
    return True