<s> """<STR_LIT>""" <EOL> import sys <EOL> from rdflib . sparql . bison . Query import Prolog <EOL> from rdflib import plugin , util <EOL> from rdflib . namespace import Namespace , RDF , RDFS <EOL> from rdflib . term import URIRef , BNode , Variable , Literal <EOL> from rdflib . store import Store <EOL> from rdflib . sparql . parser import parse <EOL> from rdflib . graph import ConjunctiveGraph , Graph <EOL> from rdflib . sparql . sql . RelationalAlgebra import RdfSqlBuilder , ParseQuery , DEFAULT_OPT_FLAGS <EOL> from rdflib . sparql . sql . RdfSqlBuilder import * <EOL> OWL_NS = Namespace ( '<STR_LIT>' ) <EOL> OWL_PROPERTIES_QUERY = """<STR_LIT>""" <EOL> RDFS_NS = Namespace ( '<STR_LIT>' ) <EOL> RDFS_PROPERTIES_QUERY = """<STR_LIT>""" <EOL> def print_set ( intro , aSet , stream = sys . stderr ) : <EOL> print >> stream , intro + '<STR_LIT>' % len ( aSet ) <EOL> for el in aSet : <EOL> print >> stream , '<STR_LIT:U+0020>' , el <EOL> def prepQuery ( queryString , ontGraph ) : <EOL> query = parse ( queryString ) <EOL> if ontGraph : <EOL> if not query . prolog : <EOL> query . prolog = Prolog ( None , [ ] ) <EOL> query . prolog . prefixBindings . update ( dict ( ontGraph . namespace_manager . namespaces ( ) ) ) <EOL> else : <EOL> for prefix , nsInst in ontGraph . namespace_manager . namespaces ( ) : <EOL> if prefix not in query . prolog . prefixBindings : <EOL> query . prolog . prefixBindings [ prefix ] = nsInst <EOL> print "<STR_LIT>" , query . prolog . prefixBindings <EOL> return query <EOL> def main ( ) : <EOL> from optparse import OptionParser <EOL> usage = '''<STR_LIT>''' <EOL> op = OptionParser ( usage = usage ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> op . set_defaults ( debug = False , storeKind = '<STR_LIT>' ) <EOL> ( options , args ) = op . parse_args ( ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> op . error ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> from rdflib . sparql import Algebra <EOL> Algebra . DAWG_DATASET_COMPLIANCE = False <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> connection , identifier , query = args <EOL> else : <EOL> connection , identifier = args <EOL> store = plugin . get ( options . storeKind , Store ) ( identifier ) <EOL> ontGraph = None <EOL> if options . owl : <EOL> ontGraph = Graph ( ) . parse ( options . owl ) <EOL> elif options . rdfs : <EOL> ontGraph = Graph ( ) . parse ( options . rdfs ) <EOL> if options . storeKind == '<STR_LIT>' and options . owl : <EOL> for litProp , resProp in ontGraph . query ( OWL_PROPERTIES_QUERY , <EOL> initNs = { u'<STR_LIT>' : OWL_NS } ) : <EOL> if litProp : <EOL> store . literal_properties . add ( litProp ) <EOL> if resProp : <EOL> store . resource_properties . add ( resProp ) <EOL> if options . debug : <EOL> print "<STR_LIT>" , litProp <EOL> print "<STR_LIT>" , resProp <EOL> if options . storeKind == '<STR_LIT>' and options . rdfs : <EOL> for litProp , resProp in ontGraph . query ( RDFS_PROPERTIES_QUERY , <EOL> initNs = { u'<STR_LIT>' : OWL_NS } ) : <EOL> if litProp : <EOL> store . literal_properties . add ( litProp ) <EOL> if resProp : <EOL> store . resource_properties . add ( resProp ) <EOL> if options . debug : <EOL> print "<STR_LIT>" , litProp <EOL> print "<STR_LIT>" , resProp <EOL> rt = store . open ( connection , create = False ) <EOL> dataset = ConjunctiveGraph ( store ) <EOL> if options . inMemorySQL : <EOL> dataset . store . originalInMemorySQL = True <EOL> if options . literal_properties : <EOL> store . literal_properties . update ( <EOL> [ URIRef ( el ) for el in options . literal_properties ] ) <EOL> if options . resource_properties : <EOL> store . resource_properties . update ( <EOL> [ URIRef ( el ) for el in options . resource_properties ] ) <EOL> if options . debug : <EOL> print_set ( '<STR_LIT>' , store . literal_properties ) <EOL> print_set ( '<STR_LIT>' , store . resource_properties ) <EOL> store . debug = True <EOL> if options . profile : <EOL> import hotshot , hotshot . stats <EOL> prof = hotshot . Profile ( "<STR_LIT>" ) <EOL> res = prof . runcall ( dataset . query , query , DEBUG = options . sparqlDebug ) <EOL> prof . close ( ) <EOL> stats = hotshot . stats . load ( "<STR_LIT>" ) <EOL> stats . strip_dirs ( ) <EOL> stats . sort_stats ( '<STR_LIT:time>' , '<STR_LIT>' ) <EOL> print "<STR_LIT>" * <NUM_LIT:20> <EOL> stats . print_stats ( <NUM_LIT:20> ) <EOL> print "<STR_LIT>" * <NUM_LIT:20> <EOL> if options . originalSPARQL : <EOL> dataset . store . originalInMemorySQL = True <EOL> if options . render : <EOL> flags = DEFAULT_OPT_FLAGS . copy ( ) <EOL> if options . flatten : <EOL> flags [ OPT_FLATTEN ] = False <EOL> sb = RdfSqlBuilder ( Graph ( store ) , optimizations = flags ) <EOL> if options . file : <EOL> query = prepQuery ( open ( options . file ) . read ( ) , ontGraph ) <EOL> res = dataset . query ( query , DEBUG = True ) <EOL> print res <EOL> else : <EOL> query = prepQuery ( query , ontGraph ) <EOL> root = ParseQuery ( query , sb ) <EOL> print repr ( root ) <EOL> root . GenSql ( sb ) <EOL> sql = sb . Sql ( ) <EOL> print sql <EOL> return <EOL> else : <EOL> flags = DEFAULT_OPT_FLAGS . copy ( ) <EOL> if options . flatten : <EOL> flags [ OPT_FLATTEN ] = False <EOL> dataset . store . optimizations = flags <EOL> if options . file : <EOL> query = prepQuery ( open ( options . file ) . read ( ) , ontGraph ) <EOL> res = dataset . query ( query , DEBUG = options . sparqlDebug ) <EOL> else : <EOL> query = prepQuery ( query , ontGraph ) <EOL> res = dataset . query ( query , DEBUG = options . sparqlDebug , initNs = ontGraph and dict ( ontGraph . namespace_manager . namespaces ( ) ) or { } ) <EOL> print res . serialize ( format = '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
