Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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()
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()
Exemplo n.º 5
0
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
import os,jpype,time,random, cPickle
os.environ['NEO4J_PYTHON_JVMARGS'] = '-Xms4G -Xmx8G -XX:MaxPermSize=1G'
os.environ['JAVA_HOME']='/usr/lib/jvm/jdk1.7.0/'

from neo4j import GraphDatabase
db = GraphDatabase('data')

print 'deleting'
with db.transaction:
    db.query('start r=relationship(*) delete r');
    db.query('start n=node(*) delete n;')

print 'reading'
friends = cPickle.load(open('friends.pickle'))

print 'nodes'
nodes = {}
with db.transaction:
    for a,data in friends.items():
        n = db.node(noscenda_name='person%s'%a)
        nodes[a]=n
        if a%1000==0:
            print 'n ',a
i= 0
print 'relations'
with db.transaction:
    for a,targets in friends.items():
        for b in targets:
            i+=1
            getattr(nodes[a],'friend')(nodes[b])
            if i%1000==0:
Exemplo n.º 7
0
Arquivo: neo.py Projeto: xftbee/py
    return user
 
 #根据用户名获得用户节点
def get_user(name):
    return user_idx['name'][name].single
 
#建立节点
for name in ['user1', 'user2','user3','user4']:
   create_user(name)
 
#为节点间添加关注关系(FOLLOWS)
with db.transaction:
    get_user('user2').FOLLOWS(get_user('user1'))
    get_user('user3').FOLLOWS(get_user('user1'))
    get_user('user4').FOLLOWS(get_user('user3'))
 
# 获得用户1的粉丝
for relationship in get_user('user1').FOLLOWS.incoming:
    u = relationship.start
    print u['name']
#输出结果:user2,user3
 
#为用户4推荐好友,即该用户关注的用户所关注的用户
nid = get_user('user4').id
#设置查询语句
query = "START n=node({id}) MATCH n-[:FOLLOWS]->m-[:FOLLOWS]->fof RETURN n,m,fof"
 
for row in db.query(query,id=nid):
    node = row['fof']
    print node['name'] 
#输出结果:user1
Exemplo n.º 8
0
#script for excecuting a cypher query with embedded python

from neo4j import GraphDatabase
import traceback
import time
import redis

r = redis.StrictRedis(host='46.137.39.99', port=6379, db=0)
cypherRedisKey = r.get("cypherQueries")

db = GraphDatabase("/mnt/neo4py_store/data/graph.db")
try:
	cypher_query = ""
	while cypher_query is not None:
		print "query found"
		cypher_query = r.rpop(cypherRedisKey)
		if cypher_query is not None:
			db.query(cypher_query)
	print "sleeping..."
	time.sleep(10)
except Exception, e:
	logfile = open('./pythonError.log', 'a')
	logfile.write("\n"+e.message())
	logfile.close()

db.shutdown()