import Utils class Resource: """ The definition of a resource """ # the namespace abrreviation e.g. log: abbrev = ":" def __init__(self, fullname = "", label = "",\ simple = "T", const = "F", list = [], number = 0): # the full name (URI) of the resource self.fullname = fullname # the short label of the resource (for display) self.label = label # is this a simple resource or a tripleset? self.simple = simple # is this a constant? self.const = const # 'list' contains the tripleset self.list = list # the resource number # variables (global universal) have a negative number self.number = number # attribute for renumbering variables self.varn = -1 # the varType serves for restoring to string format # "gul" ==> this log:forAll # "gel" ==> this log:forSome # "gu" ==> ;log:forAll ... # "ge" ==> ;log:forSome # "?" ==> ?x # "_" ==> "_:" self.varType = "" # for handling of "... is ... of ..." construct self.isof = 0 # if this resource is a list self.RDFList = [] def compare(self, res): """ compare this with another resource """ if self.simple == "T": return self.fullname == res.fullname if self.RDFList <> []: list1 = res.RDFList if list1 == []: return 0 for res in self.list: if not self.isIn(res,list1): return 0 return 1 else: if res.simple == "F": list1 = res.list if list1 == []: return 0 for res in self.list: if not self.isIn(res,list1): return 0 return 1 else: return 0 def isIn(self, res, listIn): """ test whether this resource is in a list of resources""" for res1 in listIn: if res.compare(res1): return 1 return 0 def compareNum(self, res): """ compare this with another resource by comparing the resource numbers""" if self.simple == "T": return self.number == res.number if self.RDFList <> []: list1 = res.RDFList if list1 == []: return 0 for res in self.list: if not self.isInNum(res,list1): return 0 return 1 else: if res.simple == "F": list1 = res.list if list1 == []: return 0 for res in self.list: if not self.isInNum(res,list1): return 0 return 1 else: return 0 def isInNum(self, res, listIn): """ test whether this resource is present in a list of resources using resource numbers (compareNum)""" for res1 in listIn: if res.compareNum(res1): return 1 return 0 def res(self, fullnameIn, abbrevIn, labelIn): self.simple = "T" self.fullname = fullnameIn self.abbrev = abbrevIn self.label = labelIn def simple(self, labelIn): self.label = labelIn def multres(self, listIn): self.list = listIn self.simple = "F" # make a long listing def toLString(self): if self.simple == "T": return self.fullname + " " + self.abbrev + " " + self.label else: s = "{" for triple in self.list: s = s + triple.toString() return s + "} " # make a listing with abbreviations def toString(self): if self.RDFList <> []: s = "(" for res in self.RDFList: s = s + res.toString() + " " return s + ")" elif self.simple == "T": if self.const == "T": return "\"" + self.label + "\"" else: if self.varn <> -1: return self.label + "/" + str(self.varn) else: return self.label else: s = "{" for triple in self.list: s = s + triple.toString() return s + "} " # make a listing with abbreviations def toStringVar(self): s = "" if self.RDFList <> []: s = "(" for res in self.RDFList: s = s + res.toStringVar() + " " s = s + ")" if self.simple == "F": if s <> "": s = s + "/" s = "{" for triple in self.list: s = s + triple.toStringVar() s = s + "}" if self.const == "T": if s <> "": s = s + "/" s = s + "\"" + self.label + "\"" else: if s <> "" and self.label <> "": s = s + "/" if self.varn <> -1: s = s + self.label + "/" + str(self.varn) else: s = s + self.label if self.number < 0: s = s + "?" return s + " " def rlToString(resList): """ transform a list of resources into a string """ s ="" for res in resList: s = s + res.toString() + "," s = s[:-1] return s def isIn(res, resList): """ check if a resource is in a list""" for r in resList: if res.compare(r): return 1 return 0 if __name__ == "__main__": r1 = Resource(fullname="test", label="test") import copy r2 = copy.copy(r1) r3 = r1 r2 = Resource(fullname = "test", label = "tst") r1.label = "changed" print r1.toString(), r2.toString(), r3.toString()