Example #1
0
def isOnCollection(collection, item, triplets):
    """Generate a method that can be used as a global constaint in sparql to check whether
    the 'item' is an element of the 'collection' (a.k.a. list). Both collection and item can
    be a real resource or a query string. Furthermore, item might be a plain string, that is
    then turned into a literal run-time.
    The method returns an adapted method.
    """
    #check_subject(collection)
    collUnbound = False
    if isinstance(collection, Variable):
        collUnbound = True
        collection = collection
    elif queryString(collection):
        # just keep 'collection', no reason to reassign
        collUnbound = True
    else:
        collUnbound = False
        # if we got here, this is a valid collection resource
    if isinstance(item, Variable):
        queryItem = item
        itUnbund = True
    elif queryString(item):
        queryItem = item
        itUnbound = True
    else:
        # Note that an exception is raised if the 'item' is invalid
        queryItem = _createResource(item)
        itUnbound = False

    def checkCollection(bindings):
        try:
            if collUnbound == True:
                # the binding should come from the binding
                coll = bindings[collection]
            else:
                coll = collection
            if itUnbound == True:
                it = bindings[queryItem]
            else:
                it = queryItem
            return it in triplets.items(coll)
        except:
            # this means that the binding is not available. But that also means that
            # the global constraint was used, for example, with the optional triplets;
            # not available binding means that the method is irrelevant for those
            # ie, it should not become a show-stopper, hence it returns True
            return True

    return checkCollection
Example #2
0
def isOnCollection(collection,item, triplets) :
    """Generate a method that can be used as a global constaint in sparql to check whether
    the 'item' is an element of the 'collection' (a.k.a. list). Both collection and item can
    be a real resource or a query string. Furthermore, item might be a plain string, that is
    then turned into a literal run-time.
    The method returns an adapted method.
    """
    #check_subject(collection)
    collUnbound = False
    if isinstance(collection,Variable) :
        collUnbound = True
        collection  = collection
    elif queryString(collection) :
        # just keep 'collection', no reason to reassign
        collUnbound = True
    else:
        collUnbound = False
        # if we got here, this is a valid collection resource
    if isinstance(item,Variable) :
        queryItem = item
        itUnbund  = True
    elif queryString(item) :
        queryItem = item
        itUnbound = True
    else :
        # Note that an exception is raised if the 'item' is invalid
        queryItem = _createResource(item)
        itUnbound = False
    def checkCollection(bindings) :
        try :
            if collUnbound == True :
                # the binding should come from the binding
                coll = bindings[collection]
            else :
                coll = collection
            if itUnbound == True :
                it = bindings[queryItem]
            else :
                it = queryItem
            return it in triplets.items(coll)
        except :
            # this means that the binding is not available. But that also means that
            # the global constraint was used, for example, with the optional triplets;
            # not available binding means that the method is irrelevant for those
            # ie, it should not become a show-stopper, hence it returns True
            return True
    return checkCollection
Example #3
0
def isOnCollection(collection, item, triplets):
    """
    Generate a method that can be used as a global constaint in sparql to
    check whether the 'item' is an element of the 'collection' (a.k.a. list).
    Both collection and item can be a real resource or a query string.
    Furthermore, item might be a plain string, that is then turned into a
    literal run-time. The method returns an adapted method.

    Is a resource on a collection?

    The operator can be used to check whether the 'item' is an element of the
    'collection' (a.k.a. list). Both collection and item can be a real resource
    or a query string.

    :param collection: is either a query string (that has to be bound by the query) or an RDFLib Resource
      representing the collection

    :param item: is either a query string (that has to be bound by the query), an RDFLib Resource, or
      a data type value that is turned into a corresponding Literal (with possible datatype)
      that must be tested to be part of the collection

    :returns: a function

    """
    # check_subject(collection)
    collUnbound = False

    if isinstance(collection, Variable):
        collUnbound = True
        collection  = collection

    elif queryString(collection):
        # just keep 'collection', no reason to reassign
        collUnbound = True

    else:
        collUnbound = False
        # if we got here, this is a valid collection resource

    if isinstance(item, Variable):
        queryItem = item
        itUnbound  = True

    elif queryString(item):
        queryItem = item
        itUnbound = True

    else:
        # Note that an exception is raised if the 'item' is invalid
        queryItem = _createResource(item)
        itUnbound = False

    def checkCollection(bindings):
        try:
            if collUnbound == True:
                # the binding should come from the binding
                coll = bindings[collection]
            else:
                coll = collection

            if itUnbound == True:
                it = bindings[queryItem]
            else:
                it = queryItem

            return it in triplets.items(coll)

        except:
            # this means that the binding is not available. But that also
            # means that the global constraint was used, for example, with
            # the optional triplets; not available binding means that the
            # method is irrelevant for those ie, it should not become a
            # show-stopper, hence it returns True
            return True

    return checkCollection
Example #4
0
def isOnCollection(collection, item, triplets):
    """
    Generate a method that can be used as a global constaint in sparql to
    check whether the 'item' is an element of the 'collection' (a.k.a. list).
    Both collection and item can be a real resource or a query string.
    Furthermore, item might be a plain string, that is then turned into a
    literal run-time. The method returns an adapted method.

    Is a resource on a collection?

    The operator can be used to check whether the 'item' is an element of the
    'collection' (a.k.a. list). Both collection and item can be a real resource
    or a query string.

    :param collection: is either a query string (that has to be bound by the query) or an RDFLib Resource
      representing the collection

    :param item: is either a query string (that has to be bound by the query), an RDFLib Resource, or
      a data type value that is turned into a corresponding Literal (with possible datatype)
      that must be tested to be part of the collection

    :returns: a function

    """
    # check_subject(collection)
    collUnbound = False

    if isinstance(collection, Variable):
        collUnbound = True
        collection = collection

    elif queryString(collection):
        # just keep 'collection', no reason to reassign
        collUnbound = True

    else:
        collUnbound = False
        # if we got here, this is a valid collection resource

    if isinstance(item, Variable):
        queryItem = item
        itUnbound = True

    elif queryString(item):
        queryItem = item
        itUnbound = True

    else:
        # Note that an exception is raised if the 'item' is invalid
        queryItem = _createResource(item)
        itUnbound = False

    def checkCollection(bindings):
        try:
            if collUnbound == True:
                # the binding should come from the binding
                coll = bindings[collection]
            else:
                coll = collection

            if itUnbound == True:
                it = bindings[queryItem]
            else:
                it = queryItem

            return it in triplets.items(coll)

        except:
            # this means that the binding is not available. But that also
            # means that the global constraint was used, for example, with
            # the optional triplets; not available binding means that the
            # method is irrelevant for those ie, it should not become a
            # show-stopper, hence it returns True
            return True

    return checkCollection