示例#1
0
    def ClassInstances(cls):
        """return a generator for instances of this rdf:type
        you can look in MyClass.rdf_type to see the predicate being used"""
        # Start with all things of "my" type in the db
        beenthere = set([])
        for i in cls.db.subjects(RDF.type, cls.rdf_type):
            if not i in beenthere:
                yield cls(i)
                beenthere.add(i)

        # for all subclasses of me in python do the same (recursivly)
        pySubClasses = allsub(cls)
        for sub in pySubClasses:
            for i in sub.ClassInstances():
                if not i in beenthere:
                    yield i
                    beenthere.add(i)

        # not done yet, for all db subclasses that I have not processed
        # already...get them too
        dbSubClasses = rdfsClass(cls.rdf_type).transitive_subClasses
        moreSubClasses = [
            dbsub.resUri for dbsub in dbSubClasses
            if dbsub.resUri not in [
                pysub.rdf_type for pysub in pySubClasses]]
        for sub in moreSubClasses:
            for i in cls.db.subjects(RDF.type, sub):
                if '' and not i in beenthere:
                    yield i
                    beenthere.add(i)
示例#2
0
    def ClassInstances(cls):
        """return a generator for instances of this rdf:type
        you can look in MyClass.rdf_type to see the predicate being used"""
        # Start with all things of "my" type in the db
        beenthere = set([])
        for i in cls.db.subjects(RDF.type, cls.rdf_type):
            if not i in beenthere:
                yield cls(i)
                beenthere.add(i)

        # for all subclasses of me in python do the same (recursivly)
        pySubClasses = allsub(cls)
        for sub in pySubClasses:
            for i in sub.ClassInstances():
                if not i in beenthere:
                    yield i
                    beenthere.add(i)

        # not done yet, for all db subclasses that I have not processed
        # already...get them too
        dbSubClasses = rdfsClass(cls.rdf_type).transitive_subClasses
        moreSubClasses = [
            dbsub.resUri for dbsub in dbSubClasses
            if dbsub.resUri not in [
                pysub.rdf_type for pysub in pySubClasses]]
        for sub in moreSubClasses:
            for i in cls.db.subjects(RDF.type, sub):
                if '' and not i in beenthere:
                    yield i
                    beenthere.add(i)
示例#3
0
    def __new__(cls, resUri=None, schemaGraph=None, *args, **kwargs):
        #  create a bnode
        if not resUri or isinstance(resUri, BNode) or issubclass(cls, BNode):
            obj = BNode.__new__(cls, resUri)
            obj._nodetype = BNode
        # user the identifier passed in
        elif isinstance(resUri, URIRef) or issubclass(cls, URIRef):
            obj = URIRef.__new__(cls, resUri)
            obj._nodetype = URIRef
        # use the resUri of the subject passed in
        elif isinstance(resUri, rdfSubject):
            obj = type(resUri.resUri).__new__(cls, resUri.resUri)
            obj._nodetype = type(resUri.resUri)
        # create one from a <uri> or _:bnode string
        elif isinstance(resUri, (str, unicode)):
            if resUri[0] == "<" and resUri[-1] == ">":
                obj = URIRef.__new__(cls, resUri[1:-1])
                obj._nodetype = URIRef
            elif resUri.startswith("_:"):
                obj = BNode.__new__(cls, resUri[2:])
                obj._nodetype = BNode
        else:
            raise AttributeError(
                "cannot construct rdfSubject from %s" % (str(resUri)))

        # At this point we have an obj to return...but we might want to look
        # deeper if there is an RDF:type entry on the Graph, find the mapped
        # subclass and return an object of that new type
        if resUri:
            rdf_type = obj[RDF.type]
            if rdf_type:
                class_dict = dict(
                    [(str(cl.rdf_type), cl)
                        for cl in allsub(cls) if cl.rdf_type])
                subclass = class_dict.get(str(rdf_type.resUri), cls)
            else:
                subclass = cls
        else:
            subclass = cls

        # improve this do do some kind of hash with classname??
        # this uses _weakrefs to allow us to return an existing object
        # rather than copies
        md5id = obj.md5_term_hash()
        newobj = rdfsSubject._weakrefs.get(md5id, None)
        log.debug("looking for weakref %s found %s" % (md5id, newobj))
        if newobj:
            return newobj
        newobj = super(rdfSubject, obj).__new__(subclass, obj.resUri)
        log.debug("add a weakref %s", newobj)
        newobj._nodetype = obj._nodetype
        rdfsSubject._weakrefs[newobj.md5_term_hash()] = newobj
        return newobj
示例#4
0
    def __new__(cls, resUri=None, schemaGraph=None, *args, **kwargs):
        #  create a bnode
        if not resUri or isinstance(resUri, BNode) or issubclass(cls, BNode):
            obj = BNode.__new__(cls, resUri)
            obj._nodetype = BNode
        # user the identifier passed in
        elif isinstance(resUri, URIRef) or issubclass(cls, URIRef):
            obj = URIRef.__new__(cls, resUri)
            obj._nodetype = URIRef
        # use the resUri of the subject passed in
        elif isinstance(resUri, rdfSubject):
            obj = type(resUri.resUri).__new__(cls, resUri.resUri)
            obj._nodetype = type(resUri.resUri)
        # create one from a <uri> or _:bnode string
        elif isinstance(resUri, (str, unicode)):
            if resUri[0] == "<" and resUri[-1] == ">":
                obj = URIRef.__new__(cls, resUri[1:-1])
                obj._nodetype = URIRef
            elif resUri.startswith("_:"):
                obj = BNode.__new__(cls, resUri[2:])
                obj._nodetype = BNode
        else:
            raise AttributeError(
                "cannot construct rdfSubject from %s" % (str(resUri)))

        # At this point we have an obj to return...but we might want to look
        # deeper if there is an RDF:type entry on the Graph, find the mapped
        # subclass and return an object of that new type
        if resUri:
            rdf_type = obj[RDF.type]
            if rdf_type:
                class_dict = dict(
                    [(str(cl.rdf_type), cl)
                        for cl in allsub(cls) if cl.rdf_type])
                subclass = class_dict.get(str(rdf_type.resUri), cls)
            else:
                subclass = cls
        else:
            subclass = cls

        # improve this do do some kind of hash with classname??
        # this uses _weakrefs to allow us to return an existing object
        # rather than copies
        md5id = obj.n3()
        newobj = rdfsSubject._weakrefs.get(md5id, None)
        log.debug("looking for weakref %s found %s" % (md5id, newobj))
        if newobj:
            return newobj
        newobj = super(rdfSubject, obj).__new__(subclass, obj.resUri)
        log.debug("add a weakref %s", newobj)
        newobj._nodetype = obj._nodetype
        rdfsSubject._weakrefs[newobj.n3()] = newobj
        return newobj
示例#5
0
def initRDFDatabase(graph=None):
    """
    Initialize the database of every mapped RDF class.  Returns the prior
    value of rdfSubject.db
    """
    mapper()
    old = getattr(rdfSubject, 'db', None)

    # recursively purge class-bound 'db' attributes, which might be
    # polluted by other rdfalchemy code.  Instance-bound attributes should not
    # be a problem since we'll be fetching new instances.
    for sub in allsub(rdfSubject):
        if 'db' in sub.__dict__: # hasattr returns true for superclasses, must
                                 # check the subclass's own __dict__
            del sub.db

    rdfSubject.db = graph
    return old