Exemple #1
0
    def create_association(self, subject=None, predicate=None, obj=None, assoc_type=AT.H2H):
        """
        Create an association between two IonObjects with a given predicate
        """
        if not subject or not predicate or not obj:
            raise BadRequest("Association must have all elements set")
        if type(subject) is str:
            subject_id = subject
            subject = self.read(subject_id)
        else:
            if "_id" not in subject or "_rev" not in subject:
                raise BadRequest("Subject id or rev not available")
            subject_id = subject._id
        st = type(subject).__name__

        if type(obj) is str:
            object_id = obj
            obj = self.read(object_id)
        else:
            if "_id" not in obj or "_rev" not in obj:
                raise BadRequest("Object id or rev not available")
            object_id = obj._id
        ot = type(obj).__name__

        assoc_type = assoc_type or AT.H2H
        if not assoc_type in AT:
            raise BadRequest("Unsupported assoc_type: %s" % assoc_type)

        # Check that subject and object type are permitted by association definition
        # Note: Need import here, so that import orders are not screwed up
        from pyon.core.registry import getextends
        from pyon.ion.resource import Predicates
        from pyon.core.bootstrap import IonObject

        try:
            pt = Predicates.get(predicate)
        except AttributeError:
            raise BadRequest("Predicate unknown %s" % predicate)
        if not st in pt['domain']:
            found_st = False
            for domt in pt['domain']:
                if st in getextends(domt):
                    found_st = True
                    break
            if not found_st:
                raise BadRequest("Illegal subject type %s for predicate %s" % (st, predicate))
        if not ot in pt['range']:
            found_ot = False
            for rant in pt['range']:
                if ot in getextends(rant):
                    found_ot = True
                    break
            if not found_ot:
                raise BadRequest("Illegal object type %s for predicate %s" % (ot, predicate))

        # Finally, ensure this isn't a duplicate
        assoc_list = self.find_associations(subject, predicate, obj, assoc_type, False)
        if len(assoc_list) != 0:
            assoc = assoc_list[0]
            if assoc_type == AT.H2H:
                raise BadRequest("Association between %s and %s with predicate %s and type %s already exists" % (subject, obj, predicate, assoc_type))
            else:
                if subject._rev == assoc.srv and object._rev == assoc.orv:
                    raise BadRequest("Association between %s and %s with predicate %s and type %s already exists" % (subject, obj, predicate, assoc_type))

        assoc = IonObject("Association",
                          at=assoc_type,
                          s=subject_id, st=st, srv=subject._rev,
                          p=predicate,
                          o=object_id, ot=ot, orv=obj._rev,
                          ts=get_ion_ts())
        return self.create(assoc)
Exemple #2
0
    def create_association(self, subject=None, predicate=None, object=None, assoc_type=None):
        """
        Create an association between two IonObjects with a given predicate
        @param assoc_type  DEPRECATED
        """
        if not (subject and predicate and object):
            raise BadRequest("Association must have all elements set")

        if type(subject) is str:
            subject_id = subject
            subject = self.read(subject_id)
            subject_type = subject.type_
        else:
            if "_id" not in subject:
                raise BadRequest("Subject id not available")
            subject_id = subject._id
            subject_type = subject.type_

        if type(object) is str:
            object_id = object
            object = self.read(object_id)
            object_type = object.type_
        else:
            if "_id" not in object:
                raise BadRequest("Object id not available")
            object_id = object._id
            object_type = object.type_

        # Check that subject and object type are permitted by association definition
        try:
            pt = Predicates.get(predicate)
        except AttributeError:
            raise BadRequest("Predicate unknown %s" % predicate)
        if not subject_type in pt['domain']:
            found_st = False
            for domt in pt['domain']:
                if subject_type in getextends(domt):
                    found_st = True
                    break
            if not found_st:
                raise BadRequest("Illegal subject type %s for predicate %s" % (subject_type, predicate))
        if not object_type in pt['range']:
            found_ot = False
            for rant in pt['range']:
                if object_type in getextends(rant):
                    found_ot = True
                    break
            if not found_ot:
                raise BadRequest("Illegal object type %s for predicate %s" % (object_type, predicate))

        # Finally, ensure this isn't a duplicate
        assoc_list = self.find_associations(subject_id, predicate, object_id, id_only=False)
        if len(assoc_list) != 0:
            assoc = assoc_list[0]
            #print "**** Found associations:"
            #import pprint
            #pprint.pprint(assoc_list)
            raise BadRequest("Association between %s and %s with predicate %s already exists" % (subject_id, object_id, predicate))

        assoc = IonObject("Association",
                          s=subject_id, st=subject_type,
                          p=predicate,
                          o=object_id, ot=object_type,
                          ts=get_ion_ts())
        return self.rr_store.create(assoc, create_unique_association_id())
Exemple #3
0
    def create_association_mult(self, assoc_list=None):
        """
        Create multiple associations between two IonObjects with a given predicate.
        @param assoc_list  A list of 3-tuples of (subject, predicate, object). Subject/object can be str or object
        """
        if not assoc_list:
            return []

        lookup_rid = set()
        for s, p, o in assoc_list:
            if type(s) is str:
                lookup_rid.add(s)
            if type(o) is str:
                lookup_rid.add(o)
        lookup_rid = list(lookup_rid)
        lookup_obj = self.read_mult(lookup_rid) if lookup_rid else []
        res_by_id = dict(zip(lookup_rid, lookup_obj))

        create_ts = get_ion_ts()
        new_assoc_list = []
        for s, p, o in assoc_list:
            new_s = s
            new_o = o
            if type(s) is str:
                new_s = res_by_id[s]
                if not new_s:
                    raise NotFound("Subject %s not found" % s)
            else:
                if "_id" not in s:
                    raise BadRequest("Subject id not available")
            if type(o) is str:
                new_o = res_by_id[o]
                if not new_o:
                    raise NotFound("Object %s not found" % o)
            else:
                if "_id" not in object:
                    raise BadRequest("Object id not available")

            # Check that subject and object type are permitted by association definition
            if p not in Predicates:
                raise BadRequest("Predicate unknown %s" % p)
            pt = Predicates.get(p)
            if not new_s.type_ in pt['domain']:
                found_st = False
                for domt in pt['domain']:
                    if new_s.type_ in getextends(domt):
                        found_st = True
                        break
                if not found_st:
                    raise BadRequest("Illegal subject type %s for predicate %s" % (new_s.type_, p))
            if not new_o.type_ in pt['range']:
                found_ot = False
                for rant in pt['range']:
                    if new_o.type_ in getextends(rant):
                        found_ot = True
                        break
                if not found_ot:
                    raise BadRequest("Illegal object type %s for predicate %s" % (new_o.type_, p))

            # Skip duplicate check

            assoc = IonObject("Association",
                              s=new_s._id, st=new_s.type_,
                              p=p,
                              o=new_o._id, ot=new_o.type_,
                              ts=create_ts)
            new_assoc_list.append(assoc)

        new_assoc_ids = [create_unique_association_id() for i in xrange(len(new_assoc_list))]
        return self.rr_store.create_mult(new_assoc_list, new_assoc_ids)
Exemple #4
0
    def create_association(self, subject=None, predicate=None, obj=None, assoc_type="H2H"):
        """
        Create an association between two IonObjects with a given predicate
        """
        if not subject or not predicate or not obj:
            raise BadRequest("Association must have all elements set")
        if type(subject) is str:
            subject_id = subject
            subject = self.read(subject_id)
        else:
            if "_id" not in subject or "_rev" not in subject:
                raise BadRequest("Subject id or rev not available")
            subject_id = subject._id
        st = type(subject).__name__

        if type(obj) is str:
            object_id = obj
            obj = self.read(object_id)
        else:
            if "_id" not in obj or "_rev" not in obj:
                raise BadRequest("Object id or rev not available")
            object_id = obj._id
        ot = type(obj).__name__

        assoc_type = assoc_type or "H2H"
        if not assoc_type in ("H2H", "R2R", "H2R", "R2H", "R2R"):
            raise BadRequest("Unsupported assoc_type: %s" % assoc_type)

        # Check that subject and object type are permitted by association definition
        # Note: Need import here, so that import orders are not screwed up
        from pyon.core.registry import getextends
        from pyon.ion.resource import Predicates
        from pyon.core.bootstrap import IonObject

        try:
            at = Predicates.get(predicate)
        except AttributeError:
            raise BadRequest("Predicate unknown %s" % predicate)
        if not st in at["domain"]:
            found_st = False
            for domt in at["domain"]:
                if st in getextends(domt):
                    found_st = True
                    break
            if not found_st:
                raise BadRequest("Illegal subject type %s for predicate %s" % (st, predicate))
        if not ot in at["range"]:
            found_ot = False
            for rant in at["range"]:
                if ot in getextends(rant):
                    found_ot = True
                    break
            if not found_ot:
                raise BadRequest("Illegal object type %s for predicate %s" % (ot, predicate))

        assoc = IonObject(
            "Association",
            at=assoc_type,
            s=subject_id,
            st=st,
            srv=subject._rev,
            p=predicate,
            o=object_id,
            ot=ot,
            orv=obj._rev,
            ts=get_ion_ts(),
        )
        return self.create(assoc)