import N3Parser, InfData, Triple class ITriple: # this class makes a short format of triples for inferencing # format of a triple = (isR, s, p, o, fr, ruleNr, lev, trNr) # format of a rule = (isR, s, p, o, fr, ruleNr, lev, trNr) # the predicate of a rule is always log:implies and p = 0 # for a triple ruleNr = -1 # resource = tuple(kind, res) where kind is the kind of resource: # 0 = simple, 1 = list of triples, 2 = list of resources # where s = subject, p = predicate, o = object (s, p and o are integers) # isR = isRule, 0 or 1 # ruleNr = rule number, integer # fr = from rule: indicates origin from a rule # lev = inferencing level # trNr = triple number # format of res: # if kind = 0: (number, varn) ; varn indicates the inferencing level; # This is a simple resource # if kind = 1: [triple0, ..., triplen] # if kind = 2: [res1, ..., resn] # if kind = -1: global universal variable ==> this log:forAll # if kind = -2: global existential variable ==> this log:forSome; _: # if kind = -3: local universal variable ==> ;log:forAll ...; ?x # if kind = -4: local existential variable ==> ;log:forSome # (gul, gel,gu, ge, _:, ?): see class Resource def __init__(self): self.tripleNr = 0 self.ruleNr = 1 def getRDFDB(self, parser, fileName, infData): """ getRDFDB return an RDFDB object containing all the necessary data structures for inferencing. infData contains global inferencing data that is needed """ parser.revres = infData.revres parser.resdic = infData.resdic parser.currRes = infData.currRes parser.originList = infData.originList # a counter for assigning anonymous subjects parser.anonCounter = infData.anonCounter # a counter for tranforming local variables into global variables parser.locCounter = infData.locCounter # a counter for instantiating existential variables parser.existCounter = infData.existCounter # a counter for naming lists format: (res1 ...resn) parser.listCounter = infData.listCounter self.ruleNr = infData.ruleNr self.tripleNr = infData.tripleNr parser.parseAFile(fileName) if parser.errorflag == "T": parser.writeOutput1() (infDB, varPred) = self.makePredDic(parser.tripleset.db) data = (infDB, varPred, parser.prefixDictionary) infData.revres = parser.revres infData.resdic = parser.resdic infData.currRes = parser.currRes infData.resStart = 1000 infData.originList = parser.originList # a counter for assigning anonymous subjects infData.anonCounter = parser.anonCounter # a counter for tranforming local variables into global variables infData.locCounter = parser.locCounter # a counter for instantiating existential variables infData.existCounter = parser.existCounter # a counter for naming lists format: (res1 ...resn) infData.listCounter = parser.listCounter infData.ruleNr = self.ruleNr infData.tripleNr = self.tripleNr return (data, infData) def testGetRDFDB(self, fileName, parser): """ test the function getRDFDB """ infData = InfData.InfData() (ts, infData) = self.getRDFDB(parser, fileName, infData) print "DB by predicate:\n" printPredDic(ts[0], infData) print "\n" print "Variable predicates:\n" printVarPred(ts[1], infData) print "\n" def makePredDic(self, ts): """ make a dictionary accessed by predicate number returning a list of triples with the same predicate. Put triples with variable predicate into a list. Rules are added with the predicates of the consequents. A a side effect rules are marked as rules and numbered starting from ruleNr. Triples are also numbered. Returns a dictionary and a list. Input is a list of triples format of a triple = (isR, s, p, o, fr, lev, tripleNr) """ infDB = {} varPred = [] log = "http://www.w3.org/2000/10/swap/log#" for triple in ts: nr = triple.predicate.number sname = triple.predicate.label # handle equality if sname == "=": nr = triple.subject.number nr1 = triple.object.number triple.object.number = nr # keeps track of the old number triple.object.onr = nr1 fname = triple.predicate.fullname # print "fname = " + fname if fname == log + "implies": # this is a rule triple.rule = 1 triple.ruleNr = self.ruleNr (res1, res2, res3) = self.makeTriple(triple, 1) rule = (1, res1, res2, res3, 0, self.ruleNr, 0, self.tripleNr) self.ruleNr += 1 self.tripleNr += 1 ts = triple.object.list for t in ts: nr = t.predicate.number if nr > 0: if infDB.has_key(nr): infDB[nr].append(rule) else: infDB[nr] = [rule] else: varPred.append(rule) else: tr = self.makeTriple(triple, 0) self.tripleNr += 1 if nr > 0: if infDB.has_key(nr): infDB[nr].append(tr) else: infDB[nr] = [tr] else: varPred.append(tr) return (infDB, varPred) def makeTriple(self, tr, bool): """ make a compact triple from a triple object format of a triple = (isR, s, p, o, fr, ruleNr, lev, tripleNr) """ s = tr.subject p = tr.predicate o = tr.object res1 = self.makeRes(s) res2 = self.makeRes(p) res3 = self.makeRes(o) if bool: return (res1,res2,res3) else: return (0, res1, res2, res3, 0, -1, 0, self.tripleNr) def makeRes(self, res): """ make a compact resource from a resource object format of a resource: resource = tuple(kind, res) where kind is the kind of resource: 0 = simple, 1 = list of triples, 2 = list of resources """ if res.simple == "F": # complex resource = tripleset list = [] for t in res.list: list.append(self.makeTriple(t, 0)) resO = (1, list) return resO if res.RDFList <> []: list = [] for r in res.RDFList: list.append(self.makeRes(r)) resO = (2, list) return resO if res.varType == "gul": return (-1, (res.number, res.varn)) if res.varType == "gel": return (-2, (res.number, res.varn)) if res.varType == "gu": return (-3, (res.number, res.varn)) if res.varType == "ge": return (-4, (res.number, res.varn)) if res.varType == "_": return (-2, (res.number, res.varn)) if res.varType == "?": return (-3, (res.number, res.varn)) return (0, (res.number, res.varn)) def printPredDic(predDic, inf): for key in predDic.keys(): trl = predDic[key] for tr in trl: print "triple = ", tr print "\nkey: ", key, " content:\n", trToString(tr, inf) def printVarPred(varPred, inf): for tr in varPred: print "\n", trToString(tr, inf) def printPredicates(predDic, varPred, infDB): print "\nTriples by predicate:\n\n" printPredDic(predDic, infDB) print "\nTriples with variable predicates:\n\n" printVarPred(varPred, infDB) def setFr(tr, frRule): (isR, s, p, o, fr, ruleNr, lev, trNr) = tr return (isR, s, p, o, frRule, ruleNr, lev, trNr) def resetFr(tr): (isR, s, p, o, fr, ruleNr, lev, trNr) = tr return (isR, s, p, o, 0, ruleNr, lev, trNr) def setLevel(tr, level): (isR, s, p, o, fr, ruleNr, lev, trNr) = tr return (isR, s, p, o, fr, ruleNr, level, trNr) def changeObject(tr, object): (isR, s, p, o, fr, ruleNr, lev, trNr) = tr return (isR, s, p, object, fr, ruleNr, level, trNr) def setLevelVar(res, level): (k, (nr, varn)) = res return (k, (nr, level)) ## format of a resource: ## resource = tuple(kind, res) where kind is the kind of resource: ## 0 = simple, 1 = list of triples, 2 = list of resources def compareNum(res1, res2): # print "res1, res2", res1,"\n", res2 if res1[0] <> res2[0]: return 0 if res1[0] <= 0: (nr1, varn1) = res1[1] (nr2, varn2) = res2[1] if nr1 == nr2: return 1 return 0 if res1[0] == 1: return compareNumTripleList(res1[1], res2[1]) if res1[1] == 2: return compareNumResourceList(res1[2], res2[2]) return 0 def compareNumTriple(tr1, tr2): if compareNum(tr1[1], tr2[1]) and compareNum(tr1[2], tr2[2])\ and compareNum(tr1[3], tr2[3]): return 1 return 0 def testCompareNumTriple(): return compareNumTriple(triple8, triple9) triple5 = (1, (1, [(0, (0, (-1012, -1)), (0, (1006, -1)), (0, (-1013, -1)),\ 0, -1, 0, 5), (0, (0, (-1013, -1)), (0, (1008, -1)),\ (0, (1009, -1)), 0, -1, 0, 5), (0, (0, (-1013, -1)),\ (0, (1010, -1)), (0, (-1014, -1)), 0, -1, 0, 5)]), (0,(0,0)), \ (1, [(0, (0, (-1012, -1)), (0, (1016, -1)), (0, (-1014, -1)),\ 0, -1, 0, 5)]), 0, 1, 0, 5) triple6 = (1, (1, [(0, (0, (1010, -1)), (0, (1008, -1)), (-3, (-1013, 11)), \ 0, -1, 0, 6), (0, (-3, (-1013, 11)), (0, (1008, -1)),\ (-3, (-1014, 11)), 0, -1, 0, 6)]), (0, (1015, -1)), \ (1, [(0, (0, (1010, -1)), (0, (1008, -1)), (-3, (-1014, 11)),\ 0, -1, 0, 6)]), 0, 1, 0, 6) triple7 = (1, (1, [(0, (0, (1010, -1)), (0, (1008, -1)), (-3, (-1013, 7)),\ 0, -1, 0, 6), (0, (-3, (-1013, 7)), (0, (1008, -1)), \ (-3, (-1014, 7)), 0, -1, 0, 6)]), (0, (1015, -1)),\ (1, [(0, (0, (1010, -1)), (0, (1008, -1)), (-3, (-1014, 7)),\ 0, -1, 0, 6)]), 0, 1, 0, 6) triple8 = (1, (1, [(0, (-3, (-1012, 5)), (0, (1008, -1)), (-3, (-1013, 5)), 0, -1, 0, 6), (0, (-3, (-1013, 5)), (0, (1008, -1)), (-3, (-1014, 5)), 0, -1, 0, 6)]), (0, (1015, -1)), (1, [(0, (-3, (-1012, 5)), (0, (1008, -1)), (-3, (-1014, 5)), 0, -1, 0, 6)]), 0, 1, 0, 6) triple9 = (1, (1, [(0, (-3, (-1012, 2)), (0, (1008, -1)), (-3, (-1013, 2)), 0, -1, 0, 6), (0, (-3, (-1013, 2)), (0, (1008, -1)), (-3, (-1014, 2)), 0, -1, 0, 6)]), (0, (1015, -1)), (1, [(0, (-3, (-1012, 2)), (0, (1008, -1)), (-3, (-1014, 2)), 0, -1, 0, 6)]), 0, 1, 0, 6) def compareNumTripleList(tl1, tl2): if len(tl1) <> len(tl2): return 0 for tr1 in tl1: if not isInNumTr(tr1, tl2): return 0 return 1 def isInNumTr(tr1, tl): for tr in tl: if compareNumTriple(tr1, tr): return 1 return 0 def compareNumResourceList(tr1, rl2): if len(rl1) <> len(rl2): return 0 for r1 in rl1: if not isInNumRes(r1, rl2): return 0 return 1 def isInNumRes(r1, rl2): for r in rl2: if compareNum(r1, r): return 1 return 0 def tsToString(ts, inf): if ts == []: return "" s = "" for tr in ts: s = s + trToString(tr, inf) return s def trToString(tr, inf): if tr == (): return "" (isR, s, p, o, fr, ruleNr, lev, trNr) = tr return resToString(s, inf) + " " + resToString(p, inf) + " " +\ resToString(o, inf) + ".\n" def resToString(res, inf): if res == (): return "" (k, r) = res if k <= 0: (nr, varn) = r if nr < 0: nr = -nr label = inf.revres[nr - 1000].label if k == 0: return label # + "/"+ str(varn) else: return label +"?" #/" + str(varn) if k == 1: s = "{" for tr in r: s = s + trToString(tr, inf) return s + "}" if k == 2: s == "" for res in r: s = s + resToString(res, inf) return s if __name__ == "__main__": #parser = N3Parser.N3Parser() #i = ITriple() #i.testGetRDFDB("authen.axiom.n3", parser) print testCompareNumTriple()