class MetaInfoCreator( object ): def __init__( self, databasePath, datasetDict): #increment to existing db self.db = GraphDatabase( databasePath ) self.datasetDict = datasetDict def start(self): self._createInfoNodes() self._finish() def _createInfoNodes( self ): print "Creating info nodes" # do all insertions within one transaction: complete failure or success! with self.db.transaction: metaVertex = self.db.node() #self.db.reference_node print "Meta node created, id %i" %( metaVertex.id ) index = self.db.node.indexes.create('meta') index['meta']['meta'] = metaVertex for num, (label, patientFile) in enumerate( self.datasetDict.items() ): patientFile = open( patientFile, 'r') patientFile.readline() #header datasetVertex = self.db.node() datasetVertex['datalabel'] = label datasetVertex['barcode'] = patientFile.readline().strip("\n") metaVertex.relationships.create('DATASET', datasetVertex, label=label) patientFile.close() def _finish(self): self.db.shutdown() print "Infonodes created"
def showAllDB(): db = GraphDatabase(workingdb) query = """START n=node(*) MATCH (n) - [r] -> (m) RETURN n.name, r, m.name""" print db.query(query) db.shutdown()
def get_rel_info(dbname,relid): from neo4j import GraphDatabase from neo4j import OUTGOING, INCOMING, ANY db = GraphDatabase(dbname) relid = int(relid) #make sure it is an int rel = db.relationship[relid] print rel for key,value in rel.items(): print "\t"+key,value db.shutdown()
def make_custs_embedded(amount=10000, offset=0, every=10000): db = GraphDatabase(db_path) customers = db.node[1] with db.transaction: for i in range(offset, offset + amount): name = 'Customer {:0=5}'.format(i) customer = db.node(name=name) customer.INSTANCE_OF(customers) if i % every == 0: print datetime.datetime.now(), i db.shutdown()
def get_custs_embed(amount=-1, every=10000): #print len(customers.INSTANCE_OF) #l = customers.INSTANCE_OF db = GraphDatabase(db_path) l = db.nodes for j in range(2): for i, node in enumerate(l): if amount >= 0 and i > amount: break if i % every == 0: print datetime.datetime.now(), node db.shutdown()
def get_node_info(dbname,nodeid): from neo4j import GraphDatabase from neo4j import OUTGOING, INCOMING, ANY db = GraphDatabase(dbname) nodeid = int(nodeid) #make sure it is an int nd = db.node[nodeid] print nd for key,value in nd.items(): print "\t"+key,value if "uniqname" in str(key): break db.shutdown()
def showAllRelations(qname): db = GraphDatabase(workingdb) query = """START n=node(*) MATCH (n) - [r] -> (m) WHERE HAS(n.name) AND n.name = {name} RETURN n.name, r, m.name""" print db.query(query, name=qname) db.shutdown()
def test_create_configured_db(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: creatingConfiguredDatabase from neo4j import GraphDatabase # Example configuration parameters db = GraphDatabase(folder_to_put_db_in, string_block_size=200, array_block_size=240) db.shutdown() # END SNIPPET: creatingConfiguredDatabase finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in)
def test_create_db(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: creatingDatabase from neo4j import GraphDatabase # Create db db = GraphDatabase(folder_to_put_db_in) # Always shut down your database db.shutdown() # END SNIPPET: creatingDatabase finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in)
def showAllNodes(): # open the db db = GraphDatabase(workingdb) number_of_nodes = len(db.nodes) query = "START n=node(*) RETURN n" print "This db has " + str(number_of_nodes) +"nodes" if(number_of_nodes>0): print db.query(query) else: print "no nodes" db.shutdown()
def get_nodeinfo_for_name(dbname,name): from neo4j import GraphDatabase from neo4j import OUTGOING, INCOMING, ANY db = GraphDatabase(dbname) with db.transaction: node_idx = db.node.indexes.get('graphNamedNodes') hits = node_idx['name'][name] for i in hits: print i for key,value in i.items(): print "\t"+key,value #printing after, prints mrcas which are usually too long #comment to let it go, but don't commit if "uniqname" in str(key): break hits.close() db.shutdown()
class NeoCreator: def __init__(self, path): self.db = GraphDatabase(path) def shutdown(self): self.db.shutdown() def createNewNode(self, _id, nick): with self.db.transaction: newNode = self.db.node(uid=_id, Label=nick) if newNode is None: raise return newNode def createRelationship(self, origin, destiny): with self.db.transaction: origin.tweets(destiny, Label="tweets")
class NeoCreator: def __init__(self,path): self.db = GraphDatabase(path) def shutdown(self): self.db.shutdown() def createNewNode(self,_id,nick): with self.db.transaction: newNode = self.db.node(uid=_id,Label=nick) if newNode is None: raise return newNode def createRelationship(self,origin,destiny): with self.db.transaction: origin.tweets(destiny,Label="tweets")
def addUser(usuario): # Create a database db = GraphDatabase('/tmp/') user_idx = db.node.indexes.get('users') cafe_idx = db.node.indexes.get('cafes') # All write operations happen in a transaction with db.transaction: firstNode = db.node(name=usuario, type_record='user') user_idx['name'][usuario] = firstNode secondNode = cafe_idx['name']['lungo'].single # Create a relationship with type 'knows' relationship = firstNode.toma(secondNode, cantidad=3) # Always shut down your database when your application exits db.shutdown()
def main(): global db try: shutil.rmtree('benchmark_db') except: pass db = GraphDatabase('benchmark_db') repetitions = 10 count = 1000 transaction_benchmark(repetitions) node_benchmark(repetitions,count) relation_benchmark(repetitions,count) traversal_benchmark(repetitions,count) indexing_benchmark(repetitions,count) lookup_benchmark(repetitions,count) db.shutdown()
def addtodb(lnode, rnode, relationship): # Create a database db = GraphDatabase(workingdb) # rel['subjsym'], rel['objsym'], rel['filler'] with db.transaction: lnodef = False #lnodef and rnodef store whether the node has been found in the db rnodef = False for node in db.nodes: for key, value in node.items(): if (key == "name"): if(value == lnode): leftnode = node lnodef = True if(value == rnode): rightnode = node rnodef = True if (not lnodef): leftnode = db.node(name=(lnode)) print "Lnode " + lnode + "created" if (not rnodef): rightnode = db.node(name=(rnode)) print "Rnode " + rnode + "created" relf = False for rel in leftnode.relationships.outgoing: for key, value in rel.items(): if (str(rel.type) == relationship and key == 'hits'and rel.end == rightnode): rel[key] = value + 1 relf = True print "rel found. Increasing number of hits " if (not relf): rel = leftnode.relationships.create(relationship, rightnode) print "created relationship " + relationship rel['hits'] = 1 db.shutdown()
def test_hello_world(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: helloworld from neo4j import GraphDatabase # Create a database db = GraphDatabase(folder_to_put_db_in) # All write operations happen in a transaction with db.transaction: firstNode = db.node(name='Hello') secondNode = db.node(name='world!') # Create a relationship with type 'knows' relationship = firstNode.knows(secondNode, name='graphy') # Read operations can happen anywhere message = ' '.join( [firstNode['name'], relationship['name'], secondNode['name']]) print message # Delete the data with db.transaction: firstNode.knows.single.delete() firstNode.delete() secondNode.delete() # Always shut down your database when your application exits db.shutdown() # END SNIPPET: helloworld finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in) self.assertEqual(message, 'Hello graphy world!')
def test_hello_world(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: helloworld from neo4j import GraphDatabase # Create a database db = GraphDatabase(folder_to_put_db_in) # All write operations happen in a transaction with db.transaction: firstNode = db.node(name="Hello") secondNode = db.node(name="world!") # Create a relationship with type 'knows' relationship = firstNode.knows(secondNode, name="graphy") # Read operations can happen anywhere message = " ".join([firstNode["name"], relationship["name"], secondNode["name"]]) print message # Delete the data with db.transaction: firstNode.knows.single.delete() firstNode.delete() secondNode.delete() # Always shut down your database when your application exits db.shutdown() # END SNIPPET: helloworld finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in) self.assertEqual(message, "Hello graphy world!")
class MetaInfoCreator(object): def __init__(self, databasePath, datasetDict): #increment to existing db self.db = GraphDatabase(databasePath) self.datasetDict = datasetDict def start(self): self._createInfoNodes() self._finish() def _createInfoNodes(self): print "Creating info nodes" # do all insertions within one transaction: complete failure or success! with self.db.transaction: metaVertex = self.db.node() #self.db.reference_node print "Meta node created, id %i" % (metaVertex.id) index = self.db.node.indexes.create('meta') index['meta']['meta'] = metaVertex for num, (label, patientFile) in enumerate(self.datasetDict.items()): patientFile = open(patientFile, 'r') patientFile.readline() #header datasetVertex = self.db.node() datasetVertex['datalabel'] = label datasetVertex['barcode'] = patientFile.readline().strip("\n") metaVertex.relationships.create('DATASET', datasetVertex, label=label) patientFile.close() def _finish(self): self.db.shutdown() print "Infonodes created"
class VcwebGraphDatabase(object): _db = None @property def db(self): return self._db def get_db(self, data_dir=None): from neo4j import GraphDatabase if self._db is None: if data_dir is None: data_dir = settings.GRAPH_DATABASE_PATH self._db = GraphDatabase(data_dir) db = self._db return self._db def shutdown(self): if self._db is not None: self._db.shutdown() self._db = None def link(self, group, number=5): db = self.get_db()
def test_invoice_app(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: invoiceapp-setup from neo4j import GraphDatabase, INCOMING, Evaluation # Create a database db = GraphDatabase(folder_to_put_db_in) # All write operations happen in a transaction with db.transaction: # A node to connect customers to customers = db.node() # A node to connect invoices to invoices = db.node() # Connected to the reference node, so # that we can always find them. db.reference_node.CUSTOMERS(customers) db.reference_node.INVOICES(invoices) # An index, helps us rapidly look up customers customer_idx = db.node.indexes.create('customers') # END SNIPPET: invoiceapp-setup # START SNIPPET: invoiceapp-domainlogic-create def create_customer(name): with db.transaction: customer = db.node(name=name) customer.INSTANCE_OF(customers) # Index the customer by name customer_idx['name'][name] = customer return customer def create_invoice(customer, amount): with db.transaction: invoice = db.node(amount=amount) invoice.INSTANCE_OF(invoices) invoice.RECIPIENT(customer) return customer # END SNIPPET: invoiceapp-domainlogic-create # START SNIPPET: invoiceapp-domainlogic-get-by-idx def get_customer(name): return customer_idx['name'][name].single # END SNIPPET: invoiceapp-domainlogic-get-by-idx # START SNIPPET: invoiceapp-domainlogic-get-by-traversal def get_invoices_with_amount_over(customer, min_sum): def evaluator(path): node = path.end if node.has_key('amount') and node['amount'] > min_sum: return Evaluation.INCLUDE_AND_CONTINUE return Evaluation.EXCLUDE_AND_CONTINUE return db.traversal()\ .relationships('RECIPIENT', INCOMING)\ .evaluator(evaluator)\ .traverse(customer)\ .nodes # END SNIPPET: invoiceapp-domainlogic-get-by-traversal # START SNIPPET: invoiceapp-create-and-search for name in ['Acme Inc.', 'Example Ltd.']: create_customer(name) # Loop through customers for relationship in customers.INSTANCE_OF: customer = relationship.start for i in range(1,12): create_invoice(customer, 100 * i) # Finding large invoices large_invoices = get_invoices_with_amount_over(get_customer('Acme Inc.'), 500) # Getting all invoices per customer: for relationship in get_customer('Acme Inc.').RECIPIENT.incoming: invoice = relationship.start # END SNIPPET: invoiceapp-create-and-search self.assertEqual(len(large_invoices), 6) db.shutdown() finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in)
for index,t in enumerate(tags): t = tag(t) t.setLA(get_last_access_id(t)) tags[index] = t for t in tags: #get some search results try: results = client.GetSearch(term=t.tag,since_id=t.la,per_page=per_page) except Exception: print 'Prob getting search results ' continue i = 0 for s in results: create_twt(s) if i == 0: t.setLA(s.id) i+=1 if not set_last_access_id(t): print 'WARNING could not set last accessed ID in json file' write_out() db.shutdown()
class SocialGraph: def __init__(self, neo4j_url="graph.db"): self.db = GraphDatabase(neo4j_url) self.nodes = {} self.rels = [] def file_to_db(self, data_path): data = json_io.read_json(data_path) with self.db.transaction: for source_name, targets in data.iteritems(): if source_name in self.nodes: source = self.nodes[source_name] else: source = self.db.node(name=source_name) self.nodes[source_name] = source for target_name in targets: if target_name in self.nodes: target = self.nodes[target_name] else: target = self.db.node(name=target_name) self.nodes[target_name] = target #for attr, val in targets[target_name].iteritems(): self.rels.append(source.knows(target)) return self.nodes def load_pattern(self, dir_file, clip_file): self.dir_patterns = json_io.read_json(dir_file) self.clip_patterns = json_io.read_json(clip_file) def has_relationship(self, source_name, target_name): source = self.nodes[source_name] target = self.nodes[target_name] query = '''start source=node({s_id}) \ match (source)-[r]->(target) \ where target.name = {t_name} return r''' number_rel = self.db.query(query, s_id=source.id, t_name=target_name)['r'] if len(number_rel) > 1: return True else: return False def pattern_matching(self, source_name, target_name, keyword): source = self.nodes[source_name] target = self.nodes[target_name] result = self.dir_query(source, target_name) if result == keyword: return True elif result: return result elif result == False: return False result = self.dir_query(target, source_name) if result == keyword: return True elif result: return result elif result == False: return False result = self.clip_query(source, target_name) if result == keyword: return True elif result: return result elif result == False: return False return True def dir_query(self, source, target_name): dir_query = '''START source=node({s_id}) \ MATCH (source)-[r1]->(middleman)-[r2]->(target) \ WHERE target.name = {t_name} RETURN r1, r2''' results = self.db.query(dir_query, s_id=source.id, t_name=target_name) for result in results: if 'rel' in result['r1'].keys() and \ 'rel' in result['r2'].keys(): relationship1 = result['r1']['rel'] relationship2 = result['r2']['rel'] if relationship2 in self.dir_patterns[relationship1]: predict_rel = self.dir_patterns[relationship1][relationship2] else: return False return predict_rel return None def clip_query(self, source, target_name): dir_query = '''START source=node({s_id}) \ MATCH (source)-[r1]->(middleman)<-[r2]-(target) \ WHERE target.name = {t_name} RETURN r1, r2''' results = self.db.query(dir_query, s_id=source.id, t_name=target_name) for result in results: if 'rel' in result['r1'].keys() and 'rel' in result['r2'].keys(): relationship1 = result['r1']['rel'] relationship2 = result['r2']['rel'] if relationship2 in self.clip_patterns[relationship1]: predict_rel = self.clip_patterns[relationship1][relationship2] else: return False return predict_rel return None def relationship_tagging(self, source_name, target_name, keyword, confidence): source = self.nodes[source_name] target = self.nodes[target_name] with self.db.transaction: relationship = source.knows(target, rel=keyword) if confidence <= 2: print source_name + ' <-- ' + keyword + ' <-- ' + target_name relationship = target.knows(source, rel=keyword) def clear(self): with self.db.transaction: for rel in self.db.relationships: rel.delete() for node in self.db.nodes: node.delete() def shutdown(self): self.db.shutdown()
class Memory(object): def __init__(self,mind,dbpath="data"): self.mind = mind self.dbpath = dbpath self.mind.log("Starting Graph Database at "+dbpath) self.db = GraphDatabase(dbpath) self.root = None memorySize = len(self.db.nodes) self.log("starting with "+str(memorySize)+" nodes in memory") self.vars = {} self.initialGraph(memorySize<=1) def __del__(self): self.shutdown() def shutdown(self): self.log("shutting down Graph Database...") self.db.shutdown() def log(self,something): self.mind.log(something) def error(self,errorType,command): self.mind.error(errorType,command) def initialTime(self,create=False): if create: self.log("create time graph...") with self.db.transaction: self.timeIndex = self.db.node.indexes.create('abstractTime') self.eventIndex = self.db.node.indexes.create('events') self.vars["timeroot"] = self.createNode("TIME_ROOT") self.vars["root"].TIMERELATION(self.vars["timeroot"]) self.vars["timeroot"].TIMERELATION(self.createNode("JALALI_CALENDAR"),cal_type="J") self.vars["timeroot"].TIMERELATION(self.createNode("HIJRI_CALENDAR"),cal_type="H") self.vars["timeroot"].TIMERELATION(self.createNode("GREGORY_CALENDAR"),cal_type="G") else: self.log("initial time...") with self.db.transaction: self.vars["timeroot"] = self.vars["root"].TIMERELATION.single.end self.timeIndex = self.db.node.indexes.get('abstractTime') self.eventIndex = self.db.nodes.indexes.get('events') def initialPlace(self,create=False): if create: self.log("create place graph ...") with self.db.transaction: self.placeIndex = self.db.node.indexes.create('abstractPlace') self.vars["placeroot"] = self.createNode(name="PLACE_ROOT") self.vars["root"].PLACERELATION(self.vars["placeroot"]) else: self.log("initial place ...") with self.db.transaction: self.vars["placeroot"] = self.vars["root"].PLACERELATION.single.end self.placeIndex = self.db.node.indexes.get('abstractPlace') def initialObjects(self,create=False): if create: with self.db.transaction: self.vars["objectroot"] = self.createNode("OBJECT_ROOT") self.vars["root"].OBJECTRELATION(self.vars["objectroot"]) self.vars["actionroot"] = self.createNode("ACTION_ROOT") self.vars["objectroot"].ACTIONRELATION(self.vars["actionroot"]) self.vars["eventroot"] = self.createNode("EVENT_ROOT") self.vars["objectroot"].EVENTRELATION(self.vars["eventroot"]) self.vars["me"] = self.createNode('me') self.vars["me"].ISA(self.vars["objectroot"],type="me") self.vars["master"] = self.createNode('master') self.vars["master"].ISA(self.vars["objectroot"],type="master") else: self.vars["objectroot"] = self.vars["root"].OBJECTRELATION.single.end self.vars["actionroot"] = self.vars["objectroot"].ACTIONRELATION.single.end self.vars["eventroot"] = self.vars["objectroot"].EVENTRELATION.single.end self.vars["me"] = [rel for rel in self.vars["objectroot"].ISA.incoming if rel["type"]=="me" ][0].start self.vars["master"] = [rel for rel in self.vars["objectroot"].ISA.incoming if rel["type"]=="master" ][0].start def initialGraph(self,create=False): if create: self.log("create Graph ...") with self.db.transaction: self.vars["root"] = self.db.node[0] self.vars["root"]['name'] = 'ROOT' self.nameIndex = self.db.node.indexes.create('name') self.indexName(self.vars["root"]) self.messageIndex = self.db.node.indexes.create('message',type='fulltext') else: self.log("initial graph...") with self.db.transaction: self.vars["root"] = self.db.node[0] self.nameIndex = self.db.node.indexes.get('name') self.messageIndex = self.db.node.indexes.get('message') self.initialTime(create) self.initialPlace(create) self.initialObjects(create) def getNodes(self,str): if str.startswith("`") and str.endswith("`"): result = [] for id in str[1:-1].split(","): result.append(self.db.node[int(id)]) if len(result)==1: return result[0] if len(result)>1: return result return str def cypher(self,query): return self.db.query(query) def findNodeById(self,id): return self.db.nodes[id] def indexName(self,obj): try: del self.nameIndex['name'][obj['name']][obj] except: pass self.nameIndex['name'][obj['name']] = obj def createNode(self,name=None): with self.db.transaction: if name == None: node = self.db.node() else: node = self.db.node(name=name) self.indexName(node) return node def createNodeOFType(self, typename=None, prop={}): with self.mind.memory.db.transaction: name = None if "name" in prop.keys(): name = prop["name"] node = self.createNode(name) self.setNodeProperties(node,prop) if typename is not None: typevar = self.objectType(typename) node.IS(typevar) return node def setNodeProperties(self,node,prop): with self.db.transaction: for k,v in prop.items(): node[k] = v if k=="name": self.indexName(node) def setRelationProperties(self,rel,prop): with self.db.transaction: for k,v in prop.items(): rel[k] = v def createRelation(self,type,prop,src,target): with self.db.transaction: rel = src.relationships.create(type,target) self.setRelationProperties(rel, prop) def isTypeOf(self,node,type): return node is not None and node.hasProperty("type") and node["type"]==type def getProperty(self,node,name,default=None): if node is None or not node.hasProperty(name): return default else: return node[name] def typeOfObject(self,node): if node is None: return [] result = [] for v in node.IS.outgoing: if v.end.id != self.vars["objectroot"].id: result.append(v.end) return result def objectType(self,typename): with self.db.transaction: if typename==None: return self.vars["objectroot"] else: resultVarname = self.mind.workingMemory.getTmpVarname() query = """CYPHER 1.9 start root=node(0),t=node:name(name='%s') MATCH (root)-[:OBJECTRELATION]->()<-[:IS]-(t) RETURN t"""%(typename) self.mind.memoryWindow.cypher(resultVarname,query) resultVar = self.mind.workingMemory.getVar(resultVarname) if len(resultVar)==0 or resultVar[0] is None: typevar = self.createNode(typename) typevar.IS(self.mind.workingMemory.getVar("objectroot")[0]) else: typevar = resultVar[0] return typevar def actionType(self,subject,actionname): with self.db.transaction: resultVarname = self.mind.workingMemory.getTmpVarname() if len(subject)==0: query = """CYPHER 1.9 start root=node(0),t=node:name(name='%s') MATCH (root)-[:OBJECTRELATION]->()-[:ACTIONRELATION]->()<-[r:IS]-(t) WHERE has(r.type) and r.type="objective" RETURN t"""%(actionname) self.mind.memoryWindow.cypher(resultVarname,query) resultvar = self.mind.workingMemory.getVar(resultVarname) if resultvar is None or len(resultvar)==0 or resultvar[0] is None: actionvar = self.createNode(actionname) actionvar.IS(self.vars["actionroot"],type="objective") else: actionvar = resultvar[0] return [actionvar] else: for sbj in subject: sbjtype = self.typeOfObject(sbj) print sbjtype if len(sbjtype) == 0: sbjtype = [sbj] actionvars = [] for st in sbjtype: query = """CYPHER 1.9 start root=node(0),t=node:name(name='%s') MATCH (root)-[:OBJECTRELATION]->()-[:ACTIONRELATION]->()<-[r:IS]-(w)-[:WHO]->(t) WHERE has(r.type) and r.type="subjective" RETURN w"""%(self.getProperty(st, "name", "---")) self.mind.memoryWindow.cypher(resultVarname,query) resultvar = self.mind.workingMemory.getVar(resultVarname) if resultvar is None or len(resultvar)==0 or resultvar[0] is None: actionvar = self.createNode(self.getProperty(st, "name", "---")) actionvar.IS(self.vars["actionroot"],type="subjective") actionvar.WHO(st) else: actionvar = resultvar[0] created = False for ins in actionvar.IS.incoming: if self.getProperty(ins, "name") == actionname: created = True actionvars.append(ins) if not created: ins = self.createNode(actionname) ins.IS(actionvar) actionvars.append(ins) return actionvars
#count all outgoing links from all nodes from neo4j import GraphDatabase db = GraphDatabase("/mnt/neo4j_full") def writeOutgoings(arr): arr.sort(key=lambda v:v[2], reverse=True) with open('/home/ubuntu/outgoings', 'w') as f: for e in arr: f.write(e[0] + ", " + str(e[1])+", "+str(e[2])+"\n") i = 0 for anode in db.nodes: i += 1 arr.append((anode.get('url',"unknown"), anode.get('title',"uknown"), len(anode.LINKS_TO.outgoing))) if i % 500 == 0: print i if i % 1000 == 0: writeOutgoings(arr) writeOutgoings(arr) db.shutdown()
from neo4j import GraphDatabase import atexit folder_to_put_db_in="database" # Create a database db = GraphDatabase(folder_to_put_db_in) # install shutdown hook to shutdown database atexit.register(lambda : db.shutdown() )
def test_invoice_app(self): folder_to_put_db_in = tempfile.mkdtemp() try: # START SNIPPET: invoiceapp-setup from neo4j import GraphDatabase, INCOMING, Evaluation # Create a database db = GraphDatabase(folder_to_put_db_in) # All write operations happen in a transaction with db.transaction: # A node to connect customers to customers = db.node() # A node to connect invoices to invoices = db.node() # Connected to the reference node, so # that we can always find them. db.reference_node.CUSTOMERS(customers) db.reference_node.INVOICES(invoices) # An index, helps us rapidly look up customers customer_idx = db.node.indexes.create('customers') # END SNIPPET: invoiceapp-setup # START SNIPPET: invoiceapp-domainlogic-create def create_customer(name): with db.transaction: customer = db.node(name=name) customer.INSTANCE_OF(customers) # Index the customer by name customer_idx['name'][name] = customer return customer def create_invoice(customer, amount): with db.transaction: invoice = db.node(amount=amount) invoice.INSTANCE_OF(invoices) invoice.SENT_TO(customer) return customer # END SNIPPET: invoiceapp-domainlogic-create # START SNIPPET: invoiceapp-domainlogic-get-by-idx def get_customer(name): return customer_idx['name'][name].single # END SNIPPET: invoiceapp-domainlogic-get-by-idx # START SNIPPET: invoiceapp-domainlogic-get-by-cypher def get_invoices_with_amount_over(customer, min_sum): # Find all invoices over a given sum for a given customer. # Note that we return an iterator over the "invoice" column # in the result (['invoice']). return db.query('''START customer=node({customer_id}) MATCH invoice-[:SENT_TO]->customer WHERE has(invoice.amount) and invoice.amount >= {min_sum} RETURN invoice''', customer_id = customer.id, min_sum = min_sum)['invoice'] # END SNIPPET: invoiceapp-domainlogic-get-by-cypher # START SNIPPET: invoiceapp-create-and-search for name in ['Acme Inc.', 'Example Ltd.']: create_customer(name) # Loop through customers for relationship in customers.INSTANCE_OF: customer = relationship.start for i in range(1,12): create_invoice(customer, 100 * i) # Finding large invoices large_invoices = get_invoices_with_amount_over(get_customer('Acme Inc.'), 500) # Getting all invoices per customer: for relationship in get_customer('Acme Inc.').SENT_TO.incoming: invoice = relationship.start # END SNIPPET: invoiceapp-create-and-search self.assertEqual(len(list(large_invoices)), 7) db.shutdown() finally: if os.path.exists(folder_to_put_db_in): import shutil shutil.rmtree(folder_to_put_db_in)