Example #1
0
    def _generatePattern(self, tupl):
        """
        Append a tuple to the local patterns. Possible type literals
        are converted to real literals on the fly.  Each tuple should
        be contain either 3 elements (for an RDF Triplet pattern) or
        four, where the fourth element is a per-pattern constraint
        (filter). (The general constraint of SPARQL can be optimized
        by assigning a constraint to a specific pattern; because it
        stops the graph expansion, its usage might be much more
        optimal than the the 'global' constraint).

        :param tupl: either a three- or four-element tuple

        """

        if type(tupl) != tuple:
            raise SPARQLError(
                "illegal argument, pattern must be a tuple, got %s" %
                type(tupl))

        if len(tupl) != 3 and len(tupl) != 4:
            raise SPARQLError(
                "illegal argument, pattern must be a tuple of 3 or 4 element, got %s"
                % len(tupl))

        if len(tupl) == 3:
            (s, p, o) = tupl
            f = None
        else:
            (s, p, o, f) = tupl

        final = []
        for c in (s, p, o):

            if _isResQuest(c):
                if not c in self.unbounds:
                    self.unbounds.append(c)
                final.append(c)

            elif isinstance(c, BNode):
                # Do nothing - BNode name management is handled by SPARQL parser
                # if not c in self.bnodes:
                #     self.bnodes[c] = BNode()
                final.append(c)

            else:
                final.append(_createResource(c))

        final.append(f)

        return tuple(final)
Example #2
0
 def __init__(self,name) :
     """
     @param name: the name of the variable (without the '?' character)
     @type name: unicode or string
     """
     if isinstance(name,basestring) :
         self.name     = _questChar + name
         self.origName = name
     else :
         raise SPARQLError("illegal argument, variable name must be a string or unicode")
Example #3
0
    def addConstraint(self, func):
        """
        Add a global filter constraint to the graph pattern. 'func'
        must be a method with a single input parameter (a dictionary)
        returning a boolean. This method is I{added} to previously
        added methods, ie, I{all} methods must return True to accept a
        binding.

        @param func: filter function
        """
        if type(func) == FunctionType:
            self.constraints.append(func)
        else:
            raise SPARQLError(
                "illegal argument, constraint must be a function type, got %s"
                % type(func))
Example #4
0
 def __init__(self, patterns=[]):
     """
     @param patterns: an initial list of graph pattern tuples
     """
     self.patterns = []
     self.constraints = []
     self.unbounds = []
     self.bnodes = {}
     if type(patterns) == list:
         self.addPatterns(patterns)
     elif type(patterns) == tuple:
         self.addPattern(patterns)
     else:
         raise SPARQLError(
             "illegal argument, pattern must be a tuple or a list of tuples"
         )