<s> from types import FunctionType <EOL> from rdflib . graph import ConjunctiveGraph <EOL> from rdflib . graph import Graph <EOL> from rdflib . term import BNode <EOL> from rdflib . term import Literal <EOL> from rdflib . term import URIRef <EOL> from rdflib . term import Variable <EOL> from rdflib . namespace import NamespaceManager <EOL> from rdfextras . sparql import _questChar <EOL> from rdfextras . sparql import SPARQLError <EOL> from rdflib . util import check_object <EOL> from rdflib . util import check_subject <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class SPARQLGraph ( object ) : <EOL> """<STR_LIT>""" <EOL> SPARQL_DATASET = <NUM_LIT:0> <EOL> NAMED_GRAPH = <NUM_LIT:1> <EOL> __slots__ = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , graph , graphVariable = None , dSCompliance = False ) : <EOL> assert not graphVariable or graphVariable [ <NUM_LIT:0> ] != '<STR_LIT:?>' , repr ( graphVariable ) <EOL> self . graphVariable = graphVariable <EOL> self . DAWG_DATASET_COMPLIANCE = dSCompliance <EOL> self . graphKind = None <EOL> if graph is not None : <EOL> self . graph = graph <EOL> if isinstance ( graph , ConjunctiveGraph ) : <EOL> self . graphKind = self . SPARQL_DATASET <EOL> self . identifier = graph . default_context . identifier <EOL> else : <EOL> self . graphKind = self . NAMED_GRAPH <EOL> self . identifier = graph . identifier <EOL> def setupGraph ( self , store , graphKind = None ) : <EOL> gKind = graphKind and graphKind or self . graphKind <EOL> self . graph = gKind ( store , self . identifier ) <EOL> def __reduce__ ( self ) : <EOL> return ( SPARQLGraph , <EOL> ( None , <EOL> self . graphVariable , <EOL> self . DAWG_DATASET_COMPLIANCE ) , <EOL> self . __getstate__ ( ) ) <EOL> def __getstate__ ( self ) : <EOL> return ( self . graphVariable , <EOL> self . DAWG_DATASET_COMPLIANCE , <EOL> self . identifier ) <EOL> def __setstate__ ( self , arg ) : <EOL> gVar , flag , identifier = arg <EOL> self . graphVariable = gVar <EOL> self . DAWG_DATASET_COMPLIANCE = flag <EOL> self . identifier = identifier <EOL> def _clusterForward ( self , seed , Cluster ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for ( p , o ) in self . graph . predicate_objects ( seed ) : <EOL> if not ( seed , p , o ) in Cluster . graph : <EOL> Cluster . add ( ( seed , p , o ) ) <EOL> self . _clusterForward ( p , Cluster ) <EOL> self . _clusterForward ( o , Cluster ) <EOL> except : <EOL> pass <EOL> def clusterForward ( self , seed , Cluster = None ) : <EOL> """<STR_LIT>""" <EOL> if Cluster == None : <EOL> Cluster = SPARQLGraph ( ) <EOL> check_subject ( seed ) <EOL> self . _clusterForward ( seed , Cluster ) <EOL> return Cluster <EOL> def _clusterBackward ( self , seed , Cluster ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for ( s , p ) in self . graph . subject_predicates ( seed ) : <EOL> if not ( s , p , seed ) in Cluster . graph : <EOL> Cluster . add ( ( s , p , seed ) ) <EOL> self . _clusterBackward ( s , Cluster ) <EOL> self . _clusterBackward ( p , Cluster ) <EOL> except : <EOL> pass <EOL> def clusterBackward ( self , seed , Cluster = None ) : <EOL> """<STR_LIT>""" <EOL> if Cluster == None : <EOL> Cluster = SPARQLGraph ( ) <EOL> check_object ( seed ) <EOL> self . _clusterBackward ( seed , Cluster ) <EOL> return Cluster <EOL> def cluster ( self , seed ) : <EOL> """<STR_LIT>""" <EOL> raise "<STR_LIT>" <EOL> return self . clusterBackward ( seed ) + self . clusterForward ( seed ) <EOL> """<STR_LIT>""" <EOL> def _createResource ( v ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( v , Literal ) or isinstance ( v , BNode ) or isinstance ( v , URIRef ) : <EOL> return v <EOL> else : <EOL> return Literal ( v ) <EOL> def _isResQuest ( r ) : <EOL> """<STR_LIT>""" <EOL> if r and isinstance ( r , basestring ) and r [ <NUM_LIT:0> ] == _questChar : <EOL> return True <EOL> return False <EOL> class GraphPattern : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , patterns = [ ] ) : <EOL> """<STR_LIT>""" <EOL> self . patterns = [ ] <EOL> self . constraints = [ ] <EOL> self . unbounds = [ ] <EOL> self . bnodes = { } <EOL> if type ( patterns ) == list : <EOL> self . addPatterns ( patterns ) <EOL> elif type ( patterns ) == tuple : <EOL> self . addPattern ( patterns ) <EOL> else : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" ) <EOL> def _generatePattern ( self , tupl ) : <EOL> """<STR_LIT>""" <EOL> if type ( tupl ) != tuple : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" % type ( tupl ) ) <EOL> if len ( tupl ) != <NUM_LIT:3> and len ( tupl ) != <NUM_LIT:4> : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" % len ( tupl ) ) <EOL> if len ( tupl ) == <NUM_LIT:3> : <EOL> ( s , p , o ) = tupl <EOL> f = None <EOL> else : <EOL> ( s , p , o , f ) = tupl <EOL> final = [ ] <EOL> for c in ( s , p , o ) : <EOL> if _isResQuest ( c ) : <EOL> if not c in self . unbounds : <EOL> self . unbounds . append ( c ) <EOL> final . append ( c ) <EOL> elif isinstance ( c , BNode ) : <EOL> final . append ( c ) <EOL> else : <EOL> final . append ( _createResource ( c ) ) <EOL> final . append ( f ) <EOL> return tuple ( final ) <EOL> def addPattern ( self , tupl ) : <EOL> """<STR_LIT>""" <EOL> self . patterns . append ( self . _generatePattern ( tupl ) ) <EOL> def insertPattern ( self , tupl ) : <EOL> """<STR_LIT>""" <EOL> self . patterns . insert ( <NUM_LIT:0> , self . _generatePattern ( tupl ) ) <EOL> def addPatterns ( self , lst ) : <EOL> """<STR_LIT>""" <EOL> for l in lst : <EOL> self . addPattern ( l ) <EOL> def insertPatterns ( self , lst ) : <EOL> """<STR_LIT>""" <EOL> for i in xrange ( len ( lst ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> self . insertPattern ( lst [ i ] ) <EOL> def addConstraint ( self , func ) : <EOL> """<STR_LIT>""" <EOL> if type ( func ) == FunctionType : <EOL> self . constraints . append ( func ) <EOL> else : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" % type ( func ) ) <EOL> def addConstraints ( self , lst ) : <EOL> """<STR_LIT>""" <EOL> for l in lst : <EOL> self . addConstraint ( l ) <EOL> def construct ( self , tripleStore , bindings ) : <EOL> """<STR_LIT>""" <EOL> localBnodes = { } <EOL> for c in self . bnodes : <EOL> localBnodes [ c ] = BNode ( ) <EOL> def bind ( st ) : <EOL> if _isResQuest ( st ) : <EOL> if st in bindings : <EOL> return bindings [ st ] <EOL> else : <EOL> if isinstance ( self , GraphPattern ) : <EOL> return st <EOL> else : <EOL> return None <EOL> elif isinstance ( st , BNode ) : <EOL> for c in self . bnodes : <EOL> if self . bnodes [ c ] == st : <EOL> return localBnodes [ c ] <EOL> return st <EOL> else : <EOL> return st <EOL> for pattern in self . patterns : <EOL> ( s , p , o , f ) = pattern <EOL> triplet = [ ] <EOL> valid = True <EOL> for res in ( s , p , o ) : <EOL> val = bind ( res ) <EOL> if val != None : <EOL> triplet . append ( val ) <EOL> else : <EOL> valid = False <EOL> break <EOL> if valid : <EOL> tripleStore . add ( tuple ( triplet ) ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> retval = GraphPattern ( ) <EOL> retval += self <EOL> retval += other <EOL> return retval <EOL> def __iadd__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> self . patterns += other . patterns <EOL> self . constraints += other . constraints <EOL> for c in other . unbounds : <EOL> if not c in self . unbounds : <EOL> self . unbounds . append ( c ) <EOL> for c in other . bnodes : <EOL> if not c in self . bnodes : <EOL> self . bnodes [ c ] = other . bnodes [ c ] <EOL> return self <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def isEmpty ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . patterns ) == <NUM_LIT:0> <EOL> class BasicGraphPattern ( GraphPattern ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , patterns = [ ] , prolog = None ) : <EOL> """<STR_LIT>""" <EOL> GraphPattern . __init__ ( self , patterns ) <EOL> self . prolog = prolog <EOL> def canonicalTerm ( self , term ) : <EOL> if isinstance ( term , URIRef ) : <EOL> if self . prolog is not None : <EOL> namespace_manager = NamespaceManager ( Graph ( ) ) <EOL> for prefix , uri in self . prolog . prefixBindings . items ( ) : <EOL> namespace_manager . bind ( prefix , uri , override = False ) <EOL> try : <EOL> prefix , uri , localName = namespace_manager . compute_qname ( term ) <EOL> except : <EOL> return term <EOL> if prefix not in self . prolog . prefixBindings : <EOL> return term <EOL> else : <EOL> return u'<STR_LIT::>' . join ( [ prefix , localName ] ) <EOL> else : <EOL> return term <EOL> elif isinstance ( term , Literal ) : <EOL> return term . n3 ( ) <EOL> elif isinstance ( term , BNode ) : <EOL> return term . n3 ( ) <EOL> else : <EOL> assert isinstance ( term , Variable ) <EOL> return term . n3 ( ) <EOL> def __repr__ ( self ) : <EOL> if self . constraints : <EOL> return "<STR_LIT>" % ( <EOL> '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:U+002C>' . join ( [ <EOL> self . canonicalTerm ( pat [ <NUM_LIT:0> ] ) , <EOL> self . canonicalTerm ( pat [ <NUM_LIT:1> ] ) , <EOL> self . canonicalTerm ( pat [ <NUM_LIT:2> ] ) ] <EOL> ) <EOL> for pat in self . patterns ] ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( <EOL> '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:(>' + '<STR_LIT:U+002C>' . join ( [ <EOL> self . canonicalTerm ( s ) , <EOL> self . canonicalTerm ( p ) , <EOL> self . canonicalTerm ( o ) ] <EOL> ) + '<STR_LIT:)>' <EOL> for s , p , o , f in self . patterns ] ) ) <EOL> retval = "<STR_LIT>" % self . patterns <EOL> retval += "<STR_LIT>" % self . constraints <EOL> retval += "<STR_LIT>" % self . unbounds <EOL> return retval <EOL> def _generatePattern ( self , tupl ) : <EOL> """<STR_LIT>""" <EOL> if type ( tupl ) != tuple : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" % type ( tupl ) ) <EOL> if len ( tupl ) != <NUM_LIT:3> and len ( tupl ) != <NUM_LIT:4> : <EOL> raise SPARQLError ( <EOL> "<STR_LIT>" % len ( tupl ) ) <EOL> if len ( tupl ) == <NUM_LIT:3> : <EOL> ( s , p , o ) = tupl <EOL> f = None <EOL> else : <EOL> ( s , p , o , f ) = tupl <EOL> final = [ ] <EOL> for c in ( s , p , o ) : <EOL> if isinstance ( c , Variable ) : <EOL> if not c in self . unbounds : <EOL> self . unbounds . append ( c ) <EOL> final . append ( c ) <EOL> elif isinstance ( c , BNode ) : <EOL> final . append ( c ) <EOL> else : <EOL> final . append ( _createResource ( c ) ) <EOL> final . append ( f ) <EOL> return tuple ( final ) <EOL> def fetchTerminalExpression ( self ) : <EOL> yield self <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from rdfextras . sparql . evaluate import Unbound <EOL> v1 = Variable ( "<STR_LIT:a>" ) <EOL> u1 = Unbound ( "<STR_LIT:a>" ) <EOL> g = BasicGraphPattern ( <EOL> [ ( "<STR_LIT:a>" , "<STR_LIT>" , <NUM_LIT> ) , ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> ) , ( v1 , "<STR_LIT>" , <NUM_LIT> ) , ( u1 , "<STR_LIT>" , <NUM_LIT> ) ] ) <EOL> print g </s>
