Example #1
0
    def __init__(self, project, attributes=None, uuid=None):
        """
        Default contructor, creates a derivative activity

        :param uuid: optional uuid...used mostly for reading in existing NIDM document
        :param attributes: optional dictionary of attributes to add qname:value

        """
        if uuid is None:
            self._uuid = getUUID()

            #execute default parent class constructor
            super(Derivative,self).__init__(project.graph, pm.QualifiedName(pm.Namespace("niiri",Constants.NIIRI),self.get_uuid()),attributes)
        else:
            self._uuid = uuid
            super(Derivative,self).__init__(project.graph, pm.Identifier(uuid),attributes)

        project.graph._add_record(self)

        #list to store acquisition objects associated with this activity
        self._derivative_objects=[]
        #if constructor is called with a session object then add this acquisition to the session

        #carry graph object around
        self.graph = project.graph

        project.add_derivatives(self)
Example #2
0
 def get_anon_id(self, obj, local_prefix="id"):
     if obj not in self._cache:
         self._count += 1
         self._cache[obj] = pm.Identifier(
             '_:%s%d' % (local_prefix, self._count)
         ).uri
     return self._cache[obj]
Example #3
0
    def __init__(self, project, attributes=None, uuid=None, add_default_type=True):
        """
        Default contructor, creates an acquisition object and links to acquisition activity object

        :param project: NIDM project to add data element entity to.\
        :param attributes: optional attributes to add to entity
        :param uuid: optional uuid...used mostly for reading in existing NIDM document
        :return: none

        """

        if uuid is None:
            #execute default parent class constructor
            super(DataElement,self).__init__(project.graph, pm.QualifiedName(pm.Namespace("niiri",Constants.NIIRI),getUUID()),attributes)
        else:
            super(DataElement,self).__init__(project.graph,pm.Identifier(uuid),attributes)

        project.graph._add_record(self)

        if add_default_type:
            self.add_attributes({pm.PROV_TYPE: Constants.NIDM_DATAELEMENT})
        project.add_dataelements(self)
        self.graph = project.graph

        #list to store acquisition objects associated with this activity
        self._derivative_objects=[]
Example #4
0
    def __init__(self, derivative,attributes=None, uuid=None):
        """
        Default contructor, creates an derivative object and links to derivative activity object

        :param derivative: a Derivative activity object
        :param attributes: optional attributes to add to entity
        :param uuid: optional uuid...used mostly for reading in existing NIDM document
        :return: none

        """

        if uuid is None:
            #execute default parent class constructor
            super(DerivativeObject,self).__init__(derivative.graph, pm.QualifiedName(pm.Namespace("niiri",Constants.NIIRI),getUUID()),attributes)
        else:
            super(DerivativeObject,self).__init__(derivative.graph, pm.Identifier(uuid),attributes)

        derivative.graph._add_record(self)

        #carry graph object around
        self.graph = derivative.graph
        #create link to acquisition activity
        derivative.add_derivative_object(self)
Example #5
0
def add_records(old_entity, new_entity, instance_dict):
	'''
	function adding instantiated records (entities and relations) to a 
	prov document and containing bundles
	
	calls the match() and attr_match() functions for the instantiation
	
	Args:
	old_entity (bundle or ProvDocument): Prov template for structre info
	
	new_entity (bundle or ProvDocument): Instantiated entity with matched 
	records (entities and relations)
	
	instance_dict: Instantiation dictionary   
	Returns:   
	new_entity (bundle or ProvDocument): Instantiated entity
	
	Todo: change return values of functions (this one and the ones called)    
	
	'''
	
	#print("Here add recs")
	
	relations = []
	nodes = []
	
	# for late use:
	# node_label = six.text_type(record.identifier)
	# uri = record.identifier.uri
	# uri = qname.uri
	
	for rec in old_entity.records:
		if rec.is_element():
			nodes.append(rec)
			#print(rec)
		elif rec.is_relation():
			relations.append(rec)
		else:
			print("Warning: Unrecognized element type: ",rec)

	linkedInfo=checkLinked(nodes, instance_dict)
	nodes_sorted=linkedInfo["nodes"]
	numInstances=linkedInfo["numInstances"]
	linkedGroups=linkedInfo["linkedGroups"]

	for rec in nodes_sorted:
		eid = rec.identifier
		attr = rec.attributes
		args = rec.args
		#print(attr)
		
		#print eid._str
		#dirty trick
		neid = match(eid._str,instance_dict, True, numInstances[eid])
		#print repr(neid)
		#print repr(eid)
		
		#IF no match found then this var is unbound. In case of entities, this is always an error according to
		# https://provenance.ecs.soton.ac.uk/prov-template/#errors

		if neid == eid._str:
			if "var:" in eid._str and eid._str[:4]=="var:":
				raise UnboundMandatoryVariableException("Variable " + eid._str + " at mandatory position is unbound.")


		#print(repr(instance_dict))
		props_raw = attr_match(attr,instance_dict)
		props=dict()
		#eliminate tmpl:linked
		for p in props_raw:
			log.debug(type(p))
			log.debug(repr(p))
			if "tmpl:linked"!=p._str:
				props[p]=props_raw[p]
	
		"""	
		print ("-------------------")
		print (repr(rec))
		print (rec)
		print (rec.get_asserted_types())
		print (rec.get_type())
		print ("######################")
		print (repr(eid))
		print (repr(neid))
		print (repr(props))
		print (repr(args))
		print ("-------------------")
		"""
		#here we cann inject vargen things if there is a linked attr 


		if isinstance(neid,list):
			i = 0
			for n in neid: 
				oa=prop_select(props,i)
				#print (repr(oa))
				otherAttr=list()
				for ea1 in oa:
					#print (ea1)
					#ea1_match=match(ea1[1], instance_dict, False)
					if isinstance(oa[ea1], list):
						for a in oa[ea1]:
							otherAttr.append(tuple([ea1, a]))
					else:
						otherAttr.append(tuple([ea1, oa[ea1]]))
			#print (n)
				newRec=prov.ProvRecord(rec.bundle, n,attributes=otherAttr)
				newRec._prov_type=rec.get_type()
				#print (newRec)
				new_node = new_entity.add_record(newRec)
				#new_node = new_entity.entity(prov.Identifier(n),other_attributes=prop_select(props,i))
				i += 1
		else:
			#print (eid)
			#print (repr(neid))
			#print (instance_dict)
			#print (numInstances)
			#print (linkedGroups)
			#print (repr(props))
			newprop=list()
			for p in props:
				if isinstance(props[p], list):
					for a in props[p]:
						newprop.append(tuple([p, a]))
				else:
					newprop.append(tuple([p, props[p]]))
			"""	
			print (repr(newprop))
			print (rec.bundle)
			print (prov.Identifier(neid))
			"""
			#DG NEW: WE MUST CORRECTLY EXPAND NESTED ATTRS HERE

	
			newRec=prov.ProvRecord(rec.bundle, prov.Identifier(neid),attributes=newprop)
			newRec._prov_type=rec.get_type()
			#print (newRec)
			new_node = new_entity.add_record(newRec)
			#new_node = new_entity.entity(prov.Identifier(neid),other_attributes=props)

	for rel in relations:
		#print (rel)

		#translate any tmpl entries
		

		#print (repr(rel))
		#print (repr(rel.attributes))
	
		#expand all possible formal attributes
		linkedMatrix=collections.OrderedDict()
		expAttr=collections.OrderedDict()

		
		
		for fa1 in rel.formal_attributes:
			linkedMatrix[fa1[0]]=collections.OrderedDict()
			for fa2 in rel.formal_attributes:
				linkedMatrix[fa1[0]][fa2[0]]=False
				for group in linkedGroups:
					if fa1[1] in group and fa2[1] in group: 
						linkedMatrix[fa1[0]][fa2[0]]=True	
			if fa1[1] != None:
				expAttr[fa1[0]]=match(fa1[1], instance_dict, False)
				if not isinstance(expAttr[fa1[0]], list):
					expAttr[fa1[0]]=[expAttr[fa1[0]]]
			else:
				#SPECUIAL CASE: prov:timea
				if fa1[0]._str=="prov:time":
					expAttr[fa1[0]]=[None]
					for ea1 in rel.extra_attributes:
						if ea1[0]._str=="tmpl:time":
							expAttr[fa1[0]]=match(ea1[1], instance_dict, False) 
							if not isinstance(expAttr[fa1[0]], list):
								expAttr[fa1[0]]=[expAttr[fa1[0]]]
							
				else:
					expAttr[fa1[0]]=[None]

		#dont forget extra attrs. these are not expanded but taken as is.
		
		
		#we also want grouped relation attribute names
		linkedRelAttrs=[]
		# I think this is the problem, we need to distinguish if linkedGroups is just one or multiple groups
		#print ("------")
		#print (repr(linkedGroups))
		#print (repr(rel.formal_attributes))
		for group in linkedGroups:
			lst=[]
			for fa1 in rel.formal_attributes:
				if fa1[1] in group:
					lst.append(fa1[0])
			if len(lst)>0:
				linkedRelAttrs.append(lst)
		#print (linkedRelAttrs)	

		#print repr(linkedRelAttrs)

		args = rel.args

		linked=False
		#print ("ITERATING THROUGH " + str(len(linkedGroups)) + " LINKEDGROUPS")
		for group in linkedGroups:
			#print ("------------")
			#print (repr(group))
			#print "IS " + str(args[0]) + " linked with  " + str(args[1])
			if args[0] in group and args[1] in group:
				#print repr(group)
				#print str(args[0]) + " linked with  " + str(args[1])
				linked=True
				break

		(nfirst,nsecond) = (match(args[0],instance_dict, False),match(args[1],instance_dict, False))     
		log.debug("NEW REL")
		log.debug((nfirst,nsecond))
		#print(repr(instance_dict))
		log.debug(repr(rel.extra_attributes))
		relprops_raw = attr_match(rel.extra_attributes, instance_dict)
		relprops=dict()
		log.debug(repr(relprops_raw))
		#eliminate tmpl:linked
		for relp in relprops_raw:
			log.debug(type(relp))
			log.debug(repr(relp))
			if "tmpl:linked"!=relp._str:
				log.debug(repr(relprops_raw[relp]))
				props[relp]=relprops_raw[relp]


		# WE MOVE THAT TO THE POINT WHERE WE NOW HOW MANY RELS WE HAVE FOR THE VAR PAIR

		##dont forget extra attrs
		## we need to rerrange this into 
		#otherAttr=list()
		#for ea1 in props:
		#	log.info(repr(ea1))
		#	if ea1._str != "tmpl:time":
		#		#DG NEW: #we can have multiple cases now
		#		#if the other attr tuple is "fix"/"var", then ... it depends on the state of the link. If its projection
		#
		#		#should be relatively simple:
		#		# if provided as simple list, then the attrs are expected to apply to all the connected instances
		#
		#		#if the other attr tuple is "var/var", its more complicated
		#		
		#		# imagine multiple cases
		#
		#		key_match=match(ea1[0], instance_dict, False)
		#		va1_match=match(ea1[1], instance_dict, False)
		#		if isinstance(key_match, list):
		#			for a in ea1_match:
		#				otherAttr.append(tuple([ea1[0], a]))
		#		else:
		#			otherAttr.append(tuple([ea1[0], ea1_match]))

		##dont forget that the key can also be a variable
		
		
		idents=match(rel.identifier, instance_dict, False)
		"""
		print ("rel -------------------------------")
		print(repr(rel))
		print ("idents -------------------------------")
		print(repr(idents))
		"""
		log.debug(repr(idents))
		"""
		print ("expAttr -------------------------------")
		print(repr(expAttr))
		"""
		log.debug(repr(expAttr))
		"""
		print ("linkedRelaTTRlinkedRelaTTRSS -------------------------------")
		print(repr(linkedRelAttrs))
		"""
		log.debug(repr(linkedRelAttrs))
		"""
		print ("relprops_raw -------------------------------")
		print(repr(relprops_raw))
		"""
		#We need to check if instances are linked    
		new_rel = set_rel(new_entity,rel,idents, expAttr,linkedRelAttrs, relprops_raw)        
		#new_rel = set_rel(new_entity,rel,idents, expAttr,linkedRelAttrs, otherAttr)        
		##new_rel = set_rel_o(new_entity,rel,nfirst,nsecond, linked)        
	return new_entity   
Example #6
0
                        tmpl_NS, "2dvalue_" + str(cnt1) + "_" + str(cnt2))
                    #attr="tmpl:2dvalue_" + str(cnt1) + "_" + str(cnt2)
                    cnt2 += 1
                    attrs[attr] = b
            else:
                attr = prov.QualifiedName(
                    tmpl_NS, "2dvalue_" + str(cnt1) + "_" + str(cnt2))
                #attr="tmpl:2dvalue_" + str(cnt1) + "_" + str(cnt2)
                attrs[attr] = a
        else:
            attr = prov.QualifiedName(tmpl_NS, "value_" + str(cnt1))
            attrs[attr] = a
        cnt1 += 1
    #print ID
    #print attrs
    bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier(ID), attributes=attrs)

bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier("var:dendroPlan"), {
    "tmpl:value_0": "ex:thePlan1",
    "tmpl:value_1": "ex:thePlan2"
})
bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier("var:organization"),
                   {"tmpl:value_0": "ex:theOrganization"})

outfileBind = open("excelProvTemplate_bin.ttl", "w")
#outfileBind.write(bindDoc.serialize(format="provn"))
#outfileBind.write(bindDoc.serialize(format="provn", rdf_form="xml"))
outfileBind.write(bindDoc.serialize(format="rdf", rdf_form="ttl"))
outfileBind_json = open("excelProvTemplate_bin.json", "w")
#outfileBind.write(bindDoc.serialize(format="provn"))
#outfileBind.write(bindDoc.serialize(format="provn", rdf_form="xml"))
def add_records(old_entity, new_entity, instance_dict):
    '''
	function adding instantiated records (entities and relations) to a 
	prov document and containing bundles
	
	calls the match() and attr_match() functions for the instantiation
	
	Args:
	old_entity (bundle or ProvDocument): Prov template for structre info
	
	new_entity (bundle or ProvDocument): Instantiated entity with matched 
	records (entities and relations)
	
	instance_dict: Instantiation dictionary   
	Returns:   
	new_entity (bundle or ProvDocument): Instantiated entity
	
	Todo: change return values of functions (this one and the ones called)    
	
	'''

    #print("Here add recs")

    relations = []
    nodes = []

    # for late use:
    # node_label = six.text_type(record.identifier)
    # uri = record.identifier.uri
    # uri = qname.uri

    for rec in old_entity.records:
        if rec.is_element():
            nodes.append(rec)
            #print(rec)
        elif rec.is_relation():
            relations.append(rec)
        else:
            print("Warning: Unrecognized element type: ", rec)

    linkedInfo = checkLinked(nodes, instance_dict)
    nodes_sorted = linkedInfo["nodes"]
    numInstances = linkedInfo["numInstances"]
    linkedGroups = linkedInfo["linkedGroups"]

    for rec in nodes_sorted:
        eid = rec.identifier
        attr = rec.attributes
        args = rec.args
        #print(attr)

        #print eid._str
        #dirty trick
        neid = match(eid._str, instance_dict, True, numInstances[eid])
        #print repr(neid)
        #print repr(eid)

        #IF no match found then this var is unbound. In case of entities, this is always an error according to
        # https://provenance.ecs.soton.ac.uk/prov-template/#errors

        if neid == eid._str:
            if "var:" in eid._str and eid._str[:4] == "var:":
                raise UnboundMandatoryVariableException(
                    "Variable " + eid._str +
                    " at mandatory position is unbound.")

        #print(repr(instance_dict))
        props_raw = attr_match(attr, instance_dict)
        props = dict()
        #eliminate tmpl:linked
        for p in props_raw:
            if "tmpl:linked" != p._str:
                props[p] = props_raw[p]
        """	
		print ("-------------------")
		print (repr(rec))
		print (rec)
		print (rec.get_asserted_types())
		print (rec.get_type())
		print ("######################")
		print (repr(eid))
		print (repr(neid))
		print (repr(props))
		print (repr(args))
		print ("-------------------")
		"""
        #here we cann inject vargen things if there is a linked attr

        if isinstance(neid, list):
            i = 0
            for n in neid:
                oa = prop_select(props, i)
                #print (repr(oa))
                otherAttr = list()
                for ea1 in oa:
                    #print (ea1)
                    #ea1_match=match(ea1[1], instance_dict, False)
                    if isinstance(oa[ea1], list):
                        for a in oa[ea1]:
                            otherAttr.append(tuple([ea1, a]))
                    else:
                        otherAttr.append(tuple([ea1, oa[ea1]]))
            #print (n)
                newRec = prov.ProvRecord(rec.bundle, n, attributes=otherAttr)
                newRec._prov_type = rec.get_type()
                #print (newRec)
                new_node = new_entity.add_record(newRec)
                #new_node = new_entity.entity(prov.Identifier(n),other_attributes=prop_select(props,i))
                i += 1
        else:
            #print (eid)
            #print (repr(neid))
            #print (instance_dict)
            #print (numInstances)
            #print (linkedGroups)
            #print (repr(props))
            newprop = list()
            for p in props:
                if isinstance(props[p], list):
                    for a in props[p]:
                        newprop.append(tuple([p, a]))
                else:
                    newprop.append(tuple([p, props[p]]))
            print(repr(newprop))
            print(rec.bundle)
            print(prov.Identifier(neid))

            newRec = prov.ProvRecord(rec.bundle,
                                     prov.Identifier(neid),
                                     attributes=newprop)
            newRec._prov_type = rec.get_type()
            print(newRec)
            new_node = new_entity.add_record(newRec)
            #new_node = new_entity.entity(prov.Identifier(neid),other_attributes=props)

    for rel in relations:
        #print (rel)

        #translate any tmpl entries

        #print (repr(rel))
        #print (repr(rel.attributes))

        #expand all possible formal attributes
        linkedMatrix = collections.OrderedDict()
        expAttr = collections.OrderedDict()

        for fa1 in rel.formal_attributes:
            linkedMatrix[fa1[0]] = collections.OrderedDict()
            for fa2 in rel.formal_attributes:
                linkedMatrix[fa1[0]][fa2[0]] = False
                for group in linkedGroups:
                    if fa1[1] in group and fa2[1] in group:
                        linkedMatrix[fa1[0]][fa2[0]] = True
            if fa1[1] != None:
                expAttr[fa1[0]] = match(fa1[1], instance_dict, False)
                if not isinstance(expAttr[fa1[0]], list):
                    expAttr[fa1[0]] = [expAttr[fa1[0]]]
            else:
                #SPECUIAL CASE: prov:timea
                if fa1[0]._str == "prov:time":
                    expAttr[fa1[0]] = [None]
                    for ea1 in rel.extra_attributes:
                        if ea1[0]._str == "tmpl:time":
                            expAttr[fa1[0]] = match(ea1[1], instance_dict,
                                                    False)
                            if not isinstance(expAttr[fa1[0]], list):
                                expAttr[fa1[0]] = [expAttr[fa1[0]]]

                else:
                    expAttr[fa1[0]] = [None]

        #dont forget extra attrs. these are not expanded but taken as is.

        #we also want grouped relation attribute names
        linkedRelAttrs = []
        for group in linkedGroups:
            lst = []
            for fa1 in rel.formal_attributes:
                if fa1[1] in group:
                    lst.append(fa1[0])
            if len(lst) > 0:
                linkedRelAttrs.append(lst)

        #print repr(linkedRelAttrs)

        args = rel.args

        linked = False
        for group in linkedGroups:
            #print "IS " + str(args[0]) + " linked with  " + str(args[1])
            if args[0] in group and args[1] in group:
                #print repr(group)
                #print str(args[0]) + " linked with  " + str(args[1])
                linked = True
                break

        (nfirst, nsecond) = (match(args[0], instance_dict,
                                   False), match(args[1], instance_dict,
                                                 False))

        #dont forget extra attrs
        otherAttr = list()
        for ea1 in rel.extra_attributes:
            if ea1[0]._str != "tmpl:time":
                ea1_match = match(ea1[1], instance_dict, False)
                if isinstance(ea1_match, list):
                    for a in ea1_match:
                        otherAttr.append(tuple([ea1[0], a]))
                else:
                    otherAttr.append(tuple([ea1[0], ea1_match]))

        idents = match(rel.identifier, instance_dict, False)

        #We need to check if instances are linked
        new_rel = set_rel(new_entity, rel, idents, expAttr, linkedRelAttrs,
                          otherAttr)
        #new_rel = set_rel_o(new_entity,rel,nfirst,nsecond, linked)
    return new_entity
				for b in a:
					#attr=prov.QualifiedName(tmpl_NS, "2dvalue_" + str(cnt1) + "_" + str(cnt2))
					attr="tmpl:2dvalue_" + str(cnt1) + "_" + str(cnt2)
					cnt2+=1
					attrs[attr]=b
			else:
				#attr=prov.QualifiedName(tmpl_NS, "2dvalue_" + str(cnt1) + "_" + str(cnt2))
				attr="tmpl:2dvalue_" + str(cnt1) + "_" + str(cnt2)
                                attrs[attr]=a
		else:
			attr=prov.QualifiedName(tmpl_NS, "value_" + str(cnt1))
                        attrs[attr]=a
                cnt1+=1
	#print ID			
	#print attrs
	bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier(ID), attributes=attrs)

persDict={}
persList=[]
counter=0
for p in persons:
	prop="tmpl:value_"+str(counter)
	persDict[prop]=prov.QualifiedName(prov.Namespace(outNSpref,outNS), urllib.quote(str(p.encode('utf8', 'replace'))))
	persList.append(prov.QualifiedName(prov.Namespace(outNSpref,outNS), urllib.quote(str(p.encode('utf8', 'replace')))))
	counter+=1

bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier("var:readingAgent"), persDict)
bind_dict["var:readingAgent"]=persList   
	
bindDoc.new_record(prov.PROV_ENTITY, prov.Identifier("var:dendroPlan"), { "tmpl:value_0" :  prov.QualifiedName(prov.Namespace(outNSpref,outNS), "thePlan1"),
									  "tmpl:value_1" :  prov.QualifiedName(prov.Namespace(outNSpref,outNS), "thePlan2")})