""" module rdflist Author: G.Naudts Input is a triple. Output is twofold: * the object is changed * output is true of false Output format: (0, bool, []) where bool = 0 or 1 (1, whatever (0 or 1), [triple]) In the second case the triple is returned with the changed subject. This module can also create resources containing a list; resource attribute: RDFList liste: builtins are handling resource lists; tlist: builtins are handling triple lists Formats: :name tlist:addQuery {query}. # add the results of a query to a triple list # with name :name. :name1 tlist:execute :name2. # execute a list :name2 as a query and # and put the results in :name1 :name tlist:save "filename". # save a list to a file :name tlist:read "filename". # read a list from a file :T$$$x tlist:print :name. # print a triple list :name tlist:addTS {ts}. # add a tripleset to the list (or create a list) :name1 tlist:subList (:name2, :n1, :n2) # get a sublist of a list indexed by n1 and n2 # the current substitution must be applied # to the resources. :name1 tlist:del (:name2, :n1) # delete element n1. # name1 and name2 may be equal :name liste:save "filename". # save a list to a file :name liste:read "filename". # read a list from a file :T$$$x liste:print :name. # print a list :name liste:add (rsl1 rsln). # add two resource lists # the current subtitution must be applied to # the resources :name1 liste:subList (:name2, :n1, :n2) # get a sublist of a list indexed by n1 and n2 # the current substitution must be applied # to the resources. :name1 liste:del (:name2, :n1) # delete element n1. # name1 and name2 may be equal """ import Resource, copy, ITripleX import xmlx as x1 import xml as x list = "http://www.w3.org/2000/10/swap/list#" def select(triple, inf): nr = triple[2][1][0] s = inf.revres[abs(nr) - 1000].label().content i = s.index(":") s1 = s[:i] s = s[i+1:] print "ssssss", s1, s if s1 == "liste" and s == "add": return add(triple, inf) elif s1 == "liste" and s == "sublist": return subList(triple, inf) elif s1 == "liste" and s == "print": return printRL(triple, inf) ## elif s == "greaterThan": ## return greaterThan(triple) ## elif s == "lessThan": ## return lessThan(triple) else: return (0, 0, []) def add(triple, inf): """ append a resource list to a resource list :name liste:add (rsl1 rsln) where rsl1 and rsln are resource lists """ print "triiiiiple",ITripleX.trToString(triple, inf) list = triple[3][1] lout = [] for l in list: print "l111&", l[1] lout = lout + l[1] # needs to create a new resource with this list name = "T$$$" + str(inf.anonCounter) inf.anonCounter += 1 newres = createResL(name, lout, inf) # need to create a substitution sub = triple[1] inf.subst.extend([(sub, newres)]) return (1, 1, triple) def subList(triple, inf): """ Format: :name1 liste:subList (:name2, :n1, :n2). # get a sublist of a resource list indexed by n1 and n2 # the current substitution must be applied # to the resources. """ print "triple sublist", ITripleX.trToString(triple, inf) list = triple[3][1] print "liiist", list nr1 = list[1][1][0] nr2 = list[2][1][0] n1 = int(inf.revres[abs(nr1) - 1000].label().content) n2 = int(inf.revres[abs(nr2) - 1000].label().content) print "nrsss", n1 ,n2 # print "laaaaabel", triple.subject.label, triple.subject.number list2 = list[0][1][n1:n2] # needs to create a new resource with this list name = "T$$$" + str(inf.anonCounter) inf.anonCounter += 1 newres = createResL(name, list2, inf) # need to create a substitution sub = triple[1] inf.subst.extend([(sub, newres)]) return (1, 1, triple) def printRL(triple, inf): """ print a resource list. Format: [liste:print :name]. """ list = triple[3][1] s = "Out: (" for res in list: s = s + ITripleX.resToString(res, inf) + ", " s = s[:-2] + ")" print s return (0, 1, triple) def createResL(const, list, inf): """ create a new resource """ re = x1.XmlTreeX("resource") re.addTreeC("simple","T") nr = inf.currRes nrTag = x.XmlTree("number") nrTag.addContent(str(nr)) res = (2, list) re.addTree(nrTag) inf.resdic[const] = (nr, re) inf.revres.append(re) inf.currRes = inf.currRes + 1 re.addRes(const,"",const) re.addTreeC("const", "T") return res