def compileQuadToParams(self,quadSlots): subjSlot,predSlot,objSlot,conSlot = quadSlots return (subjSlot.md5Int, term2Letter(subjSlot.term), objSlot.md5Int, term2Letter(objSlot.term), conSlot.md5Int, term2Letter(conSlot.term))
def __init__(self, position, term, useSignedInts=False): assert position in POSITION_LIST, "Unknown quad position: %s" % \ position self.position = position self.term = term self.termType = term2Letter(term) self.useSignedInts = useSignedInts self.md5Int = normalizeValue(term, term2Letter(term), useSignedInts)
def compileQuadToParams(self, quadSlots): subjSlot, predSlot, objSlot, conSlot = quadSlots dTypeParam = objSlot.term.datatype and normalizeValue( objSlot.term.datatype, 'U', self.useSignedInts) or None langParam = objSlot.term.language and objSlot.term.language or None rtList = [ subjSlot.md5Int, term2Letter(subjSlot.term), predSlot.md5Int, term2Letter(predSlot.term), objSlot.md5Int, conSlot.md5Int, term2Letter(conSlot.term) ] for item in [dTypeParam, langParam]: if item: rtList.append(item) return tuple(rtList)
def extractIdentifiers(self,quadSlots): """ Test literal data type extraction >>> from rdflib.namespace import RDF >>> from rdfextras.store.FOPLRelationalModel.QuadSlot import genQuadSlots >>> class DummyClass: ... def __init__(self,test=False): ... self.test = test ... def updateIdentifierQueue(self,stuff): ... if self.test: ... term,termType = stuff[-1] ... assert termType == 'U',"Datatype's are URIs!" >>> class Tester(NamedLiteralProperties): ... def __init__(self): ... self.idHash = DummyClass(True) ... self.valueHash = DummyClass() >>> c = Tester() >>> slots = genQuadSlots([BNode(),RDF.first,Literal(1),BNode()]) >>> c.extractIdentifiers(slots) """ subjSlot,predSlot,objSlot,conSlot = quadSlots idTerms = [ (subjSlot.term,subjSlot.termType), (predSlot.term,predSlot.termType), (conSlot.term,conSlot.termType)] if objSlot.term.datatype: idTerms.append( (objSlot.term.datatype,term2Letter(objSlot.term.datatype))) self.idHash.updateIdentifierQueue(idTerms) self.valueHash.updateIdentifierQueue( [(objSlot.term,objSlot.termType)])
def extractIdentifiers(self, quadSlots): """ Test literal data type extraction >>> from rdflib.namespace import RDF >>> from rdfextras.store.FOPLRelationalModel.QuadSlot import genQuadSlots >>> class DummyClass: ... def __init__(self,test=False): ... self.test = test ... def updateIdentifierQueue(self,stuff): ... if self.test: ... term,termType = stuff[-1] ... assert termType == 'U',"Datatype's are URIs!" >>> class Tester(NamedLiteralProperties): ... def __init__(self): ... self.idHash = DummyClass(True) ... self.valueHash = DummyClass() >>> c = Tester() >>> slots = genQuadSlots([BNode(),RDF.first,Literal(1),BNode()]) >>> c.extractIdentifiers(slots) """ subjSlot, predSlot, objSlot, conSlot = quadSlots idTerms = [(subjSlot.term, subjSlot.termType), (predSlot.term, predSlot.termType), (conSlot.term, conSlot.termType)] if objSlot.term.datatype: idTerms.append( (objSlot.term.datatype, term2Letter(objSlot.term.datatype))) self.idHash.updateIdentifierQueue(idTerms) self.valueHash.updateIdentifierQueue([(objSlot.term, objSlot.termType) ])
def compileQuadToParams(self,quadSlots): subjSlot,predSlot,objSlot,conSlot = quadSlots dTypeParam = objSlot.term.datatype and normalizeValue( objSlot.term.datatype, 'U', self.useSignedInts) or None langParam = objSlot.term.language and objSlot.term.language or None rtList = [ subjSlot.md5Int, term2Letter(subjSlot.term), predSlot.md5Int, term2Letter(predSlot.term), objSlot.md5Int, conSlot.md5Int, term2Letter(conSlot.term)] for item in [dTypeParam,langParam]: if item: rtList.append(item) return tuple(rtList)
def generateWhereClause(self,queryPattern): """ Takes a query pattern (a list of quad terms - subject,predicate,object,context) and generates a SQL WHERE clauses which works in conjunction to the intersections to filter the result set by partial matching (by REGEX), full matching (by integer half-hash), and term types. For maximally efficient SELECT queries """ whereClauses = [] whereParameters = [] asserted = dereferenceQuad(CONTEXT,queryPattern) is None for idx in SlotPrefixes.keys(): queryTerm = dereferenceQuad(idx,queryPattern) lookupAlias = 'rt_'+SlotPrefixes[idx] if idx == CONTEXT and asserted: whereClauses.append("%s.%s_term != 'F'" % \ (self,self.columnNames[idx])) if idx < len(POSITION_LIST) and isinstance(queryTerm,REGEXTerm): whereClauses.append("%s.lexical REGEXP "%lookupAlias+"%s") whereParameters.append(queryTerm) elif idx == CONTEXT \ and isinstance(queryTerm,Graph) \ and isinstance(queryTerm.identifier,REGEXTerm): whereClauses.append("%s.lexical REGEXP "%lookupAlias+"%s") whereParameters.append(queryTerm.identifier) elif idx < len(POSITION_LIST) and queryTerm is not Any: if self.columnNames[idx]: if isinstance(queryTerm,list): whereClauses.append("%s.%s" % \ (self,self.columnNames[idx])+" in (%s)" % \ ','.join([ '%s' for item in range(len(queryTerm)) ])) whereParameters.extend( [normalizeValue(item, term2Letter(item), self.useSignedInts) for item in queryTerm]) else: whereClauses.append("%s.%s" % \ (self,self.columnNames[idx])+" = %s") whereParameters.append(normalizeValue( queryTerm, term2Letter(queryTerm), self.useSignedInts)) if not idx in self.hardCodedResultTermsTypes \ and self.termEnumerations[idx] \ and not isinstance(queryTerm,list): whereClauses.append("%s.%s_term" % \ (self,self.columnNames[idx])+" = %s") whereParameters.append(term2Letter(queryTerm)) elif idx >= len(POSITION_LIST) \ and len(self.columnNames) > len(POSITION_LIST) \ and queryTerm is not None: compVal = idx == DATATYPE_INDEX and normalizeValue( queryTerm, term2Letter(queryTerm), self.useSignedInts) or queryTerm whereClauses.append("%s.%s" % \ (self,self.columnNames[idx][0])+" = %s") whereParameters.append(compVal) return ' AND '.join(whereClauses),whereParameters
def normalizeNode(node, useSignedInts=False): return normalizeValue(node, term2Letter(node), useSignedInts)
def compileQuadToParams(self, quadSlots): subjSlot, predSlot, objSlot, conSlot = quadSlots return (subjSlot.md5Int, term2Letter(subjSlot.term), predSlot.md5Int, term2Letter(predSlot.term), objSlot.md5Int, term2Letter(objSlot.term), conSlot.md5Int, term2Letter(conSlot.term))
def generateWhereClause(self, queryPattern): """ Takes a query pattern (a list of quad terms - subject,predicate,object,context) and generates a SQL WHERE clauses which works in conjunction to the intersections to filter the result set by partial matching (by REGEX), full matching (by integer half-hash), and term types. For maximally efficient SELECT queries """ whereClauses = [] whereParameters = [] asserted = dereferenceQuad(CONTEXT, queryPattern) is None for idx in SlotPrefixes.keys(): queryTerm = dereferenceQuad(idx, queryPattern) lookupAlias = 'rt_' + SlotPrefixes[idx] if idx == CONTEXT and asserted: whereClauses.append("%s.%s_term != 'F'" % \ (self,self.columnNames[idx])) if idx < len(POSITION_LIST) and isinstance(queryTerm, REGEXTerm): whereClauses.append("%s.lexical REGEXP " % lookupAlias + "%s") whereParameters.append(queryTerm) elif idx == CONTEXT \ and isinstance(queryTerm,Graph) \ and isinstance(queryTerm.identifier,REGEXTerm): whereClauses.append("%s.lexical REGEXP " % lookupAlias + "%s") whereParameters.append(queryTerm.identifier) elif idx < len(POSITION_LIST) and queryTerm is not Any: if self.columnNames[idx]: if isinstance(queryTerm, list): whereClauses.append("%s.%s" % \ (self,self.columnNames[idx])+" in (%s)" % \ ','.join([ '%s' for item in range(len(queryTerm)) ])) whereParameters.extend([ normalizeValue(item, term2Letter(item), self.useSignedInts) for item in queryTerm ]) else: whereClauses.append("%s.%s" % \ (self,self.columnNames[idx])+" = %s") whereParameters.append( normalizeValue(queryTerm, term2Letter(queryTerm), self.useSignedInts)) if not idx in self.hardCodedResultTermsTypes \ and self.termEnumerations[idx] \ and not isinstance(queryTerm,list): whereClauses.append("%s.%s_term" % \ (self,self.columnNames[idx])+" = %s") whereParameters.append(term2Letter(queryTerm)) elif idx >= len(POSITION_LIST) \ and len(self.columnNames) > len(POSITION_LIST) \ and queryTerm is not None: compVal = idx == DATATYPE_INDEX and normalizeValue( queryTerm, term2Letter(queryTerm), self.useSignedInts) or queryTerm whereClauses.append("%s.%s" % \ (self,self.columnNames[idx][0])+" = %s") whereParameters.append(compVal) return ' AND '.join(whereClauses), whereParameters