Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Created August 26, 2012 21:59
Show Gist options
  • Save lawlesst/3483836 to your computer and use it in GitHub Desktop.
Save lawlesst/3483836 to your computer and use it in GitHub Desktop.
Helper to get a random number identifier for a given namespace in a Jena model.
"""
Get the nextURI for the given namespace.
Jython interpretation of getNextURI here:
http://svn.code.sf.net/p/vivo/vitro/code/branches/dev-sdb/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/JenaIngestUtils.java
"""
def get_next_uri(namespace, model):
from com.hp.hpl.jena.rdf.model import SimpleSelector
import random
next_uri = None
while True:
next_uri = namespace + str(random.randint(1, 9999999))
resource = model.createResource(next_uri)
#Check if this uri is the subject of any statements.
sub_selector = SimpleSelector(resource, None, None)
sub_iter = model.listStatements(sub_selector)
if sub_iter.hasNext():
sub_iter.close()
continue
else:
#Check if this uri is the object of any statements.
obj_selector = SimpleSelector(None, None, resource)
obj_iter = model.listStatements(obj_selector)
if obj_iter.hasNext():
obj_iter.close()
continue
else:
break
return next_uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment