def _create_association(self, subject=None, predicate=None, obj=None, support_bulk=False): """ Create an association between two IonObjects with a given predicate. Supports bulk mode """ if self.bulk and support_bulk: if not subject or not predicate or not obj: raise BadRequest("Association must have all elements set: %s/%s/%s" % (subject, predicate, obj)) if isinstance(subject, basestring): subject = self._get_resource_obj(subject) if "_id" not in subject: raise BadRequest("Subject id not available") subject_id = subject._id st = subject.type_ if isinstance(obj, basestring): obj = self._get_resource_obj(obj) if "_id" not in obj: raise BadRequest("Object id not available") object_id = obj._id ot = obj.type_ assoc_id = create_unique_association_id() assoc_obj = IonObject("Association", s=subject_id, st=st, p=predicate, o=object_id, ot=ot, ts=get_ion_ts()) assoc_obj._id = assoc_id self.bulk_associations[assoc_id] = assoc_obj return assoc_id, '1-norev' else: return self.rr.create_association(subject, predicate, obj)
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 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 assoc = dict(type_="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.datastore.create_doc(assoc, create_unique_association_id())
def _create_association(self, subject_id, predicate, obj_id): subject = self.data_store.read(subject_id) obj = self.data_store.read(obj_id) ass_obj = IonObject("Association", s=subject._id, st=subject.type_, p=predicate, o=obj._id, ot=obj.type_, ts=get_ion_ts()) res = self.data_store.create(ass_obj, create_unique_association_id()) return res
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)
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())
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 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 == '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 obj._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()) self._count(_create_assoc=1) return self.create(assoc, create_unique_association_id())