<s> import rdflib <EOL> from rdflib . sparql . bison import Parse <EOL> from rdflib . sparql . bison . Query import * <EOL> from rdflib . sparql . bison . GraphPattern import * <EOL> from rdflib . sparql . bison . Operators import * <EOL> from rdflib . sparql . bison . FunctionLibrary import * <EOL> from rdflib . sparql . bison . Util import ListRedirect <EOL> from rdflib . sparql . bison . Expression import * <EOL> from rdflib . Variable import Variable <EOL> from rdflib . Literal import Literal <EOL> def QueryStats ( queryString , log ) : <EOL> log [ '<STR_LIT>' ] = queryString <EOL> try : <EOL> q = Parse ( queryString ) <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> print '<STR_LIT>' % e <EOL> q = None <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return False <EOL> GetQueryInfo ( q , log ) <EOL> return True <EOL> def GetQueryInfo ( q , log ) : <EOL> if isinstance ( q . query , SelectQuery ) : <EOL> log [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif isinstance ( q . query , AskQuery ) : <EOL> log [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif isinstance ( q . query , ConstructQuery ) : <EOL> log [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif isinstance ( q . query , DescribeQuery ) : <EOL> log [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> if q . query . distinct : <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> vars = dict ( ) <EOL> if q . query . solutionModifier . limitClause : <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if q . query . solutionModifier . offsetClause : <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if q . query . solutionModifier . orderClause : <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> log [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> if q . query . whereClause : <EOL> GetQueryWhereInfo ( q . query . whereClause . parsedGraphPattern , None , vars , log ) <EOL> log [ '<STR_LIT>' ] = len ( q . query . variables ) <EOL> log [ '<STR_LIT>' ] = len ( vars ) <EOL> if len ( q . query . variables ) < <NUM_LIT:1> : <EOL> log [ '<STR_LIT>' ] = len ( vars ) <EOL> def GetQueryWhereInfo ( pat , context , vars , log ) : <EOL> foundType = False <EOL> if isinstance ( pat , ParsedGraphGraphPattern ) : <EOL> foundType = True <EOL> if isinstance ( pat . name , Variable ) : <EOL> context = '<STR_LIT:?>' <EOL> else : <EOL> context = '<STR_LIT:c>' <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> if isinstance ( pat , ParsedAlternativeGraphPattern ) : <EOL> foundType = True <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> for a in pat . alternativePatterns : <EOL> GetQueryWhereInfo ( a , context , vars , log ) <EOL> if isinstance ( pat , ParsedOptionalGraphPattern ) : <EOL> foundType = True <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> if isinstance ( pat , ParsedGroupGraphPattern ) : <EOL> foundType = True <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> for p in pat . graphPatterns : <EOL> GetQueryWhereInfo ( p , context , vars , log ) <EOL> if isinstance ( pat , GraphPattern ) : <EOL> foundType = True <EOL> for s in pat . triples : <EOL> for p in s . propVals : <EOL> for o in p . objects : <EOL> GetTripleInfo ( s . identifier , p . property , o , context , vars , log ) <EOL> if pat . nonTripleGraphPattern : <EOL> GetQueryWhereInfo ( pat . nonTripleGraphPattern , context , vars , log ) <EOL> if pat . filter : <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> GetQueryFilterInfo ( pat . filter . filter , log ) <EOL> if not foundType : <EOL> print '<STR_LIT>' % pat . type <EOL> raise Exception ( '<STR_LIT>' % pat . type ) <EOL> def GetTripleInfo ( s , p , o , context , vars , log ) : <EOL> log [ '<STR_LIT>' ] = log [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> si = '<STR_LIT:s>' <EOL> pi = '<STR_LIT:p>' <EOL> oi = '<STR_LIT:o>' <EOL> if isinstance ( s , Variable ) : <EOL> si = '<STR_LIT:?>' <EOL> GetVarInfo ( s , vars ) <EOL> if isinstance ( p , Variable ) : <EOL> pi = '<STR_LIT:?>' <EOL> GetVarInfo ( p , vars ) <EOL> if isinstance ( o , Variable ) : <EOL> oi = '<STR_LIT:?>' <EOL> GetVarInfo ( o , vars ) <EOL> if context : <EOL> quadPat = '<STR_LIT>' % ( si , pi , oi , context ) <EOL> if not log . has_key ( quadPat ) : <EOL> log [ quadPat ] = <NUM_LIT:1> <EOL> else : <EOL> log [ quadPat ] = log [ quadPat ] + <NUM_LIT:1> <EOL> else : <EOL> triplePat = '<STR_LIT>' % ( si , pi , oi ) <EOL> if not log . has_key ( triplePat ) : <EOL> log [ triplePat ] = <NUM_LIT:1> <EOL> else : <EOL> log [ triplePat ] = log [ triplePat ] + <NUM_LIT:1> <EOL> def GetVarInfo ( v , vars ) : <EOL> if not vars . has_key ( v ) : <EOL> vars [ v ] = <NUM_LIT:1> <EOL> else : <EOL> vars [ v ] = vars [ v ] + <NUM_LIT:1> <EOL> def GetQueryFilterInfo ( filter , log ) : <EOL> op = None <EOL> if filter == None : <EOL> return <EOL> if isinstance ( filter , Variable ) : <EOL> return <EOL> if isinstance ( filter , Literal ) : <EOL> return <EOL> if isinstance ( filter , BinaryOperator ) : <EOL> op = '<STR_LIT>' + filter . NAME <EOL> GetQueryFilterInfo ( filter . left , log ) <EOL> GetQueryFilterInfo ( filter . right , log ) <EOL> if isinstance ( filter , UnaryOperator ) : <EOL> op = '<STR_LIT>' + filter . NAME <EOL> GetQueryFilterInfo ( filter . argument , log ) <EOL> if isinstance ( filter , ParsedREGEXInvocation ) : <EOL> op = '<STR_LIT>' <EOL> GetQueryFilterInfo ( filter . arg1 , log ) <EOL> GetQueryFilterInfo ( filter . arg2 , log ) <EOL> GetQueryFilterInfo ( filter . arg3 , log ) <EOL> if isinstance ( filter , ParsedDatatypedLiteral ) : <EOL> op = '<STR_LIT>' + filter . dataType <EOL> if isinstance ( filter , ListRedirect ) : <EOL> for f in filter : <EOL> GetQueryFilterInfo ( f , log ) <EOL> if isinstance ( filter , ParsedConditionalAndExpressionList ) : <EOL> op = '<STR_LIT>' <EOL> if isinstance ( filter , ParsedRelationalExpressionList ) : <EOL> op = '<STR_LIT>' <EOL> if isinstance ( filter , ParsedPrefixedMultiplicativeExpressionList ) : <EOL> op = '<STR_LIT>' + filter . prefix <EOL> if not op : <EOL> print '<STR_LIT>' % filter <EOL> raise Exception ( '<STR_LIT>' % filter ) <EOL> if not log . has_key ( op ) : <EOL> log [ op ] = <NUM_LIT:1> <EOL> else : <EOL> log [ op ] = log [ op ] + <NUM_LIT:1> <EOL> def GetQueryStats ( queryEntries ) : <EOL> colDict = dict ( ) <EOL> cols = [ ] <EOL> entries = [ ] <EOL> entryCount = <NUM_LIT:0> <EOL> distinctQueries = dict ( ) <EOL> for log in queryEntries : <EOL> entryCount = entryCount + <NUM_LIT:1> <EOL> if QueryStats ( log [ '<STR_LIT>' ] , log ) : <EOL> if not distinctQueries . has_key ( log [ '<STR_LIT>' ] . strip ( ) ) : <EOL> distinctQueries [ log [ '<STR_LIT>' ] . strip ( ) ] = len ( distinctQueries ) + <NUM_LIT:1> <EOL> log [ '<STR_LIT>' ] = distinctQueries [ log [ '<STR_LIT>' ] . strip ( ) ] <EOL> entries . append ( log ) <EOL> for k in log : <EOL> if not colDict . has_key ( k ) : <EOL> colDict [ k ] = k <EOL> cols . append ( k ) <EOL> cols . sort ( ) <EOL> return ( cols , entries ) </s>
