예제 #1
0
def knowledge_qa(request):
    goal = request.GET.get('goal', 'None')
    status = 0
    error = ' '
    if goal == 'qa':
        sentence = request.GET.get('sentence', 'None')
        if sentence == 'None':
            status = 1
            error = '问句不能为空'
            data = ' '
            response = toJson(data, error, status)
            return HttpResponse(response)
        else:
            # 先对问句进行分词
            first, second, number = common.sentence_similarity(sentence)
            Qtype = KnowledgeModels.question_set[number]
            graph = Graph("localhost:7474", username="******", password="******")
            sql = graph.evaluate(
                "match(e:Qands) where e.Qtype={s} return e.sql", s=Qtype)
            sql = sql.replace('$', first)
            sql = sql.replace('#', second)
            sql = sql.replace('%', second)
            df = graph.evaluate(sql)
            data = ' '.join([str(number), first, second, sql])
            response = toJson(data, error, status)
        return HttpResponse(df)
예제 #2
0
def medical_qa(request):
    global p_id
    question = request.GET.get('question', 'None')
    if question == 'None':
        status = 1
        error = '问句不能为空'
        data = ' '
        response = toJson(data, error, status)
        return HttpResponse(response)
    else:
        # 先对问句进行分词
        first, number = common.sentence_similarity_medical(question)
        Qtype = KnowledgeModels.question_set[number]
        graph = Graph("localhost:7474", username="******", password="******")
        sql = graph.evaluate("match(e:Qands) where e.Qtype={s} return e.sql",
                             s=Qtype)
        sql = sql.replace('*', (str)(p_id))
        sql = sql.replace('$', first)
        df = graph.evaluate(sql)
        flag = request.POST.get('input')
        if flag == None:
            return render(request, 'medicalQA/medicalQA.html', {'goal': df})
        else:
            df, p_id = f(p_id, flag, first)
            return render(request, 'medicalQA/medicalQA.html', {'goal': df})
예제 #3
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    query_file = argv[1]

    pw = get_graph_password()
    if pw is None:
        print(
            f"Environment variable {GRAPHDBPASS} not set. Cannot access graph DB",
            file=sys.stderr)
        sys.exit(1)
    else:
        g = Graph(password=pw)

    with open(query_file, 'r') as f:
        merged_contributors_property_query = f.read()

    try:
        g.evaluate(merged_contributors_property_query)
    except GraphError as ge:
        print_tb(ge, file=sys.stderr)
        sys.exit(1)
    else:
        print(
            f"Query\n{merged_contributors_property_query}\nexecuted successfully",
            file=sys.stderr)
        sys.exit(0)
예제 #4
0
def f(p_id, flag, str):
    graph = Graph("localhost:7474", username="******", password="******")
    cql = "match(e)-[r:关系]->(c) where e.id={} and r.type='".format(p_id)+"{}'".format(flag)+" and e.included='{}'".format(str)+" return c.name"
    p_id = graph.evaluate("match(e)-[r:关系]->(c) where e.id={} and r.type='".format(p_id)+"{}'".format(flag)+" and e.included='{}'".format(str)+" return c.id")
    str = graph.evaluate(cql)
    if str == None:
        str = '谢谢使用'
        p_id = 1
        return str, p_id
    else:
        return str, p_id
예제 #5
0
class GraphMaker(object):
    '''

        neo4j: (https://10-0-1-111-33931.neo4jsandbox.com/browser/)
            Entire triple: 
                CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})-[:ACTED_IN {roles:['Neo']}]->(TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
                MATCH(N) RETURN N

            Create node: 
                CREATE (n:Page {title:'Finance', url:'https://en.wikipedia.org/wiki/Finance'})

            Get node (as "n")
                match(n:Page {title: "Finance"})

            node = self.graph.evaluate("match (n:Section) where n.title='See also' return n")


    '''
    def __init__(self):
        authenticate("localhost:7474", "neo4j", "ece406")
        self.graph = Graph("http://localhost:7474/db/data/")
        self.graph.delete_all()

    def appendNode(self, node):
        self.graph.create(node)

    def appendNodes(self, *nodes):
        for node in nodes:
            self.graph.create(node)

    def makeRelationship(self, subjectnode, propertystring, objectnode):
        self.graph.create(Relationship(subjectnode, propertystring,
                                       objectnode))

    def drawGraph(self):
        options = {"Page": "title", "Section": "title"}
        draw(self.graph, options)

    def getData(self, querystring=None):
        if querystring is None:
            querystring = "match (n) return n"
        return self.graph.data(querystring)

    def printData(self, querystring=None):
        data = self.getData(querystring)
        for d in data:
            print(d)

    def getNodeByTitle(self, nodeTitle):
        node = self.graph.evaluate("match (n:Section) where n.title='" +
                                   nodeTitle + "' return n")
        if node:
            return node
        else:
            print("No node by that title")
            return
예제 #6
0
def create_rating(request, pk):
    if not request.user.is_authenticated:
        return redirect('%s?next=%s' % ('/login', request.path))
    formulario = CreateRating()
    film_name = Film.objects.get(id=pk)
    if request.method == 'POST':
        formulario = CreateRating(request.POST)
        if formulario.is_valid():
            film = get_object_or_404(Film, id=pk)
            user = request.user
            rating = formulario.cleaned_data['rating']

            graph = Graph(scheme=settings.NEO4J_SCHEME, host=settings.NEO4J_HOST, port=settings.NEO4J_PORT, user=settings.NEO4J_USER, password=settings.NEO4J_PASSWORD)
            matcher = NodeMatcher(graph)

            ratings = Rating.objects.filter(user=user.id, film=film.id).count()

            if ratings != 0:
                Rating.objects.filter(user=user.id, film=film.id).delete()
                graph.evaluate('MATCH (u:User {id:' + str(user.id) + '})-[r:RATES]->(f:Film {id:' + str(film.id) + '}) DELETE r')

            new_rating = Rating.objects.create(user=user.id, film=film.id, rating=rating)

            node_user = matcher.match('User', id=int(user.id)).first()

            if node_user is None:
                node_user = Node('User', id=int(user.id))
                graph.create(node_user)

            node_film = matcher.match('Film', id=int(film.id)).first()

            if node_film is None:
                node_film = Node('Film', id=int(film.id))
                graph.create(node_film)

            relation_rating = Relationship(node_user, 'RATES', node_film, rating=float(rating))
            graph.create(relation_rating)

            return redirect('/films/' + str(film.id))
    return render(request, 'create_rating.html', {'formulario': formulario, 'film_name': film_name})
class IntegrationTestCase(TestCase):

    @staticmethod
    def unique_string_generator():
        while True:
            yield "_" + uuid4().hex

    def __init__(self, *args, **kwargs):
        super(IntegrationTestCase, self).__init__(*args, **kwargs)
        self.graph = Graph()
        self.node_matcher = NodeMatcher(self.graph)
        self.db = self.graph.database
        self.schema = self.graph.schema
        self.unique_string = self.unique_string_generator()

    def reset(self):
        graph = self.graph
        schema = self.schema
        for label in schema.node_labels:
            for property_keys in schema.get_uniqueness_constraints(label):
                schema.drop_uniqueness_constraint(label, *property_keys)
            for property_keys in schema.get_indexes(label):
                schema.drop_index(label, *property_keys)
        graph.delete_all()

    def assert_error(self, error, classes, fullname):
        for cls in classes:
            assert isinstance(error, cls)
        name = fullname.rpartition(".")[-1]
        self.assertEqual(error.__class__.__name__, error.exception, name)
        self.assertIn(error.fullname, [None, fullname])
        self.assertTrue(error.stacktrace)

    def assert_new_error(self, error, classes, code):
        for cls in classes:
            assert isinstance(error, cls)
        name = code.rpartition(".")[-1]
        self.assertEqual(error.__class__.__name__, name)
        self.assertEqual(error.code, code)
        self.assertTrue(error.message)

    def get_non_existent_node_id(self):
        node = Node()
        self.graph.create(node)
        node_id = node.identity
        self.graph.delete(node)
        return node_id

    def get_attached_node_id(self):
        return self.graph.evaluate("CREATE (a)-[:TO]->(b) RETURN id(a)")
예제 #8
0
def test_multiple_processes(uri):
    n_workers = 20
    n_nodes_per_worker = 100
    workers = []
    for i in range(n_workers):
        worker = Worker()
        worker.setup(uri, n_nodes_per_worker)
        workers.append(worker)
    for worker in workers:
        worker.start()
    for worker in workers:
        worker.join()
    g = Graph(uri)
    count = g.evaluate("MATCH (n:Thing) RETURN count(n)")
    assert count == n_workers * n_nodes_per_worker
예제 #9
0
파일: db.py 프로젝트: knservis/neo4j-demo
class Neo4jClient(object):
    """ Client to neo4j database """

    graph = None

    def __init__(self, uri):
        self.graph = Graph(uri)

    def get_shortest_path_relationship(self, label_a, node_id_a, label_b,
                                       node_id_b):
        """Returns the first shortest path between two nodes identified by label and node_id 

        If there are any results (there could be many shortest paths), it returns the first one (as per spec).
        The path is returned as a list, from the start node, to the end node including the relationships in between.

        Keyword arguments:
        label_a -- The label for node A (e.g. 'Address')
        node_id_a -- The node_id for entity A (e.g. 5124536)
        label_b -- The label for node B (e.g. 'Entity')
        node_id_b -- The node_id for entity B (e.g. 5124537)
        """

        errors = [
            '{label} label not in the graph!'.format(label=l)
            for l in [label_a, label_b] if not l in self.graph.node_labels
        ]
        if errors:
            raise ValueError("\n".join(errors))

        query = """MATCH (a:{label_a}{{node_id: \"{node_id_a}\"}}),
            (b:{label_b}{{node_id: \"{node_id_b}\"}}),
            p = shortestPath((a)-[*]-(b)) RETURN p""".format(
            label_a=cypher_escape(label_a),
            label_b=cypher_escape(label_b),
            node_id_a=node_id_a,
            node_id_b=node_id_b)
        result = self.graph.evaluate(query)
        return {
            'path': [write_relationship(i) for i in result.relationships()],
            'path_abbrev': ["{i}".format(i=i) for i in result.relationships()],
            'nodes': [{
                'id': i.__name__,
                'node': i
            } for i in result.nodes()]
        } if result else {}
예제 #10
0
然后再执行此程序
'''

from py2neo import Graph
import configparser

cfg = configparser.ConfigParser()
cfg.read('../config/config.ini', encoding='utf-8')
username = cfg.get('neo4j', 'username')
password = cfg.get('neo4j', 'password')

graph = Graph('http://localhost:7474', username=username, password=password)

# 创建Genre类节点:电影体裁类
statement = 'load csv with headers from "file:///genre.csv" as line merge (p:Genre{gid:toInteger(line.gid),name:line.gname})'
graph.evaluate(statement)
print('加载genre.csv成功........')

# 创建Actor类节点:演员类
statement = 'load csv with headers from "file:///person.csv" as line merge(p:Actor{pid:toInteger(line.pid),birth:line.birth,death:line.death,name:line.name,biography:line.biography,birthplace:line.birthplace})'
graph.evaluate(statement)
print('加载person.csv成功........')

# 创建Movie类节点:电影类
statement = 'load csv with headers from "file:///movie.csv" as line merge(p:Movie{movie_id:toInteger(line.movie_id),title:line.title,introduction:line.introduction,rating:toFloat(line.rating),releasedate:line.releasedate})'
graph.evaluate(statement)
print('加载movie.csv成功........')

# 创建关系:is
statement = 'load csv with headers from "file:///movie_to_genre.csv" as line match(from:Movie{movie_id:toInteger(line.movie_id)}),(to:Genre{gid:toInteger(line.gid)}) merge (from)-[r:is{movie_id:toInteger(line.movie_id),gid:toInteger(line.gid)}]->(to)'
graph.evaluate(statement)
예제 #11
0
from py2neo import Graph
import config

uri = "bolt://*****:*****@localhost:11016"

graph = Graph(uri)

print("[INFO] Clearing graph of any existing data")
graph.evaluate("MATCH (n) DETACH DELETE n")

print("[INFO] Asserting schema")
graph.evaluate(
    "CALL apoc.schema.assert({Category:['name']},{Business:['id'],User:['id'],Review:['id']})"
)

print("[INFO] Loading businesses")
graph.evaluate(
    'CALL apoc.periodic.iterate("'
    'CALL apoc.load.json(\'file:///business.json\') YIELD value RETURN value '
    '"," '
    'MERGE (b:Business{id:value.business_id}) '
    'SET b += apoc.map.clean(value, [\'business_id\',\'categories\',\'postal_code\'],[]) '
    'WITH b,value.categories as categories '
    'UNWIND categories as category '
    'MERGE (c:Category{id:category}) '
    'MERGE (b)-[:IN_CATEGORY]->(c)"'
    ',{batchSize: 10000, iterateList: true});')

print("[INFO] Loading users")
graph.evaluate('CALL apoc.periodic.iterate("'
               'CALL apoc.load.json(\'file:///user.json\') '
예제 #12
0
graphdb = Graph("http://localhost:7474/db/data/",
                user="******",
                password="******")

delete_by_name(graphdb.run("CALL db.constraints()").data(), "CONSTRAINT")
delete_by_name(graphdb.run("CALL db.indexes()").data(), "INDEX")

del_cql = """MATCH (n)
optional match (n)-[r]-()
DELETE r, n"""
graphdb.run(del_cql)

# Primary key like constraints
constraint_un_name_usuario = "CREATE CONSTRAINT constraint_un_name_usuario ON (n:Usuario) ASSERT n.name IS UNIQUE"
graphdb.evaluate(constraint_un_name_usuario)

# Constraint unique já cria index btree
# index_usuario_name = "CREATE INDEX ON :Usuario(name)"
# graphdb.evaluate(index_usuario_name)

# enterprise
# constraint_ex_name_usuario = "CREATE CONSTRAINT constraint_ex_name_usuario ON (n:Usuario) ASSERT exists(n.name)"
# graphdb.evaluate(constraint_ex_name_usuario)

alice = get_user_node("Alice")
bob = get_user_node("Bob")
lea = get_user_node("Lea")
ana = get_user_node("Ana")
joel = get_user_node("Joel")
예제 #13
0
class Neo4j(object):
    def __init__(self):
        self.graph = Graph(Config.NEO_URL,
                           username=Config.NEO_USR,
                           password=Config.NEO_PSW)
        self.mm = MongoManager.DBManager()

    def add_relation(self,
                     node_name1,
                     node_name2,
                     movie_name='name',
                     url='url'):
        """
        图中添加新的导演关系
        若关系中的两个节点不在图中则自动创建
        同时为关系添加电影名、发行时间、关系计数这几个参数
        :param node_name1:
        :param node_name2:
        :param movie_name:
        :param release_time:
        :return:
        """
        node1 = Node(DIRECTOR_LABEL, name=node_name1)
        node1['type'] = 'director'
        node2 = Node(ACTOR_LABEL, name=node_name2)
        # node3 = Node(MOVIE_LABEL, name=movie_name)
        # node3['url'] = url
        #
        # actor_movie_relation = Relationship(node2, ACTIN_LABEL, node3)
        # director_movie_relation = Relationship(node1, DIRECT_LABEL, node3)
        # self.graph.merge(actor_movie_relation, DEFAULT_LABEL, 'name')
        # self.graph.merge(director_movie_relation, DEFAULT_LABEL, 'name')

        # print(actor_movie_relation)
        # print(director_movie_relation)

        # if self.find_relation(node_name1, node_name2):
        #     print('relation already existed, add count')
        # else:
        relation = Relationship(node1, COOPERATE_LABEL, node2)
        relation['count'] = 1
        self.graph.merge(relation, DEFAULT_LABEL, 'name')
        # print("成功创建关系", node_name1, ',', COOPERATE_LABEL, ',', node_name2)

    def print(self, name, relation):
        """
        打印所有以名字为name的节点开始、具有relation关系的边的终节点的信息
        :param name:
        :param relation:
        :return:
        """
        print('##########')
        query = 'MATCH (n) WHERE n.name={name} RETURN n'
        params = dict(name=name)
        node = self.graph.evaluate(query, params)
        print(node)
        for rel in self.graph.match((node, ), relation):
            print(rel.end_node['name'], rel.end_node.labels, rel['movie_name'],
                  rel['release_time'])

    def find_director_node(self, name):
        """
        查找具有某名字的节点,若图中有此节点则返回true,反之返回false
        :param name:
        :return:
        """
        query = 'MATCH (n:Director) WHERE n.name={name} RETURN n'
        params = dict(name=name)
        node = self.graph.evaluate(query, params)
        if node is None:
            return False
        if self.graph.exists(node):
            return True
        else:
            return False

    def find_actor_node(self, name):
        """
        查找具有某名字的节点,若图中有此节点则返回true,反之返回false
        :param name:
        :return:
        """
        query = 'MATCH (n:Actor) WHERE n.name={name} RETURN n'
        params = dict(name=name)
        node = self.graph.evaluate(query, params)
        if node is None:
            return False
        if self.graph.exists(node):
            return True
        else:
            return False

    def get_labeled_node(self, count=1):
        """
        获取具有某个标签的节点列表
        打印节点数量
        并返回该list
        :return:
        """
        # 用CQL进行查询,返回的结果是list
        datas = self.graph.data('MATCH(p:Director) return p')
        # 目标节点数量
        # print(len(datas))
        # 数据类型为list
        # print(type(datas))
        _count = 1
        for data in datas:
            # data类型为dict
            # print(type(data))
            # if _count > count:
            #     break
            print(data)
            _count += 1
        print('Total count of Director is', _count)
        return datas

    def find_relation_and_add_count(self, name1, name2):
        """
        查找分别以name1, name2为起始、终止节点的 CooperateWith 关系
        若找到则对应count数加一
        :param name1:
        :param name2:
        :return:
        """
        sn = self.graph.find_one(DIRECTOR_LABEL,
                                 property_key='name',
                                 property_value=name1)
        en = self.graph.find_one(ACTOR_LABEL,
                                 property_key='name',
                                 property_value=name2)
        rel = self.graph.match(start_node=sn,
                               rel_type=COOPERATE_LABEL,
                               end_node=en)
        # print(rel)

        # print('--------')
        query = 'MATCH(n:Director)-[r:CooperateWith]->(m:Actor) WHERE n.name={name1} and m.name={name2} RETURN r'
        params = dict(name1=name1, name2=name2)
        relation = self.graph.evaluate(query, params)
        if relation is None:
            print('relation is none')
            self.add_relation(name1, name2)
            return False
        if self.graph.exists(relation):
            print('relation exists, add count')
            relation['count'] += 1
            self.graph.push(relation)
            print(relation.start_node()['name'], '->', relation['count'], '->',
                  relation.end_node()['name'])
            return True
        else:
            print('relation does not exist')
            return False

    def clear_graph(self):
        """
        清空图数据库
        :return:
        """
        self.graph.delete_all()

    def show_end_node(self, name, relation_label):
        """
        根据输入的起始节点名和关系标签,遍历全部对应关系,并打印终节点的属性群
        :param name:
        :param relation_label:
        :param attrs:
        :return:
        """
        query = 'MATCH (n) WHERE n.name={name} RETURN n'
        params = dict(name=name)
        node = self.graph.evaluate(query, params)
        if node is None:
            print('node is None!')
            return False
        if self.graph.exists(node):
            print(node)
            # 遍历此起始节点的全部关系,打印关系的个数
            for rel in self.graph.match((node, ), relation_label):
                print(name, '->', rel['count'], '->', rel.end_node['name'])
        else:
            print('node not exists!')
            return False

    def get_coop_count(self):
        """
        获取全部导演、演员合作关系及次数并打印
        :return:
        """
        directors = self.get_labeled_node()
        # print(type(directors))
        count = 1
        for director in directors:
            if count > 1:
                break
            # print(director['p']['name'])
            self.show_end_node(director['p']['name'], COOPERATE_LABEL)
            count += 1

    def get_cooperations(self):
        directors = self.get_labeled_node()
        # datas = []
        for director in directors:
            query = 'MATCH (n) WHERE n.name={name} RETURN n'
            params = dict(name=director['p']['name'])
            node = self.graph.evaluate(query, params)
            if node is None:
                print('node is None!')
                return None
            if self.graph.exists(node):
                # 遍历此起始节点的全部关系,一一存入结果集并返回
                for rel in self.graph.match(start_node=node,
                                            rel_type=COOPERATE_LABEL):
                    data = {
                        'director': director['p']['name'],
                        'actor': rel.end_node()['name'],
                        'count': rel['count']
                    }
                    # print("合作信息,", data)
                    self.mm.save_data(Config.COOPERATION_TEMP, data)
                    # datas.append(data)
            else:
                print('node not exists!')
                return None
예제 #14
0
class neoHandler:
    def __init__(self, uri, user, passwd):
        self.DB_URI = config['neo4j']['uri']
        self.DB_USER = config['neo4j']['User']
        self.DB_PASS = config['neo4j']['Password']
        self.WPA2_COLOR = config['legend']['WPA2']
        self.WPA_COLOR = config['legend']['WPA']
        self.WEP_COLOR = config['legend']['WEP']
        self.OPEN_COLOR = config['legend']['Open']
        self.AP_COLOR = config['legend']['AP']
        self.CLIENT_COLOR = config['legend']['Client']
        self.PROBE_COLOR = config['legend']['Probes']
        self.ASSOC_COLOR = config['legend']['AssociatedTo']

        self.graph = Graph(uri, auth=(user, passwd))
        self.user = user
        self.uri = self.graph.database.uri
        self.INITIAL = True

        for con in bg_presets.CONSTRAINT_QUERIES:
            self.graph.run(con)

        self.names = self.getNames()
        self.bssids = self.getBssids()
        self.ouis = self.getOUIs()
        self.types = self.getTypes()
        self.auths = self.getAuths()
        self.ciphers = self.getCiphers()
        self.channels = self.getChannels()
        self.speeds = self.getSpeeds()
        self.lanips = self.getLanIPs()

    def getNames(self):
        return list(
            map(lambda x: x['name'],
                self.graph.run(bg_presets.NAMES_QUERY).data()))

    def getBssids(self):
        return list(
            map(lambda x: x['bssid'],
                self.graph.run(bg_presets.BSSID_QUERY).data()))

    def getOUIs(self):
        return list(
            map(lambda x: x['oui'],
                self.graph.run(bg_presets.OUI_QUERY).data()))

    def getTypes(self):
        return list(
            map(lambda x: x['type'],
                self.graph.run(bg_presets.TYPE_QUERY).data()))

    def getAuths(self):
        return list(
            map(lambda x: x['auth'],
                self.graph.run(bg_presets.AUTH_QUERY).data()))

    def getCiphers(self):
        return list(
            map(lambda x: x['cipher'],
                self.graph.run(bg_presets.CIPHER_QUERY).data()))

    def getChannels(self):
        return list(
            map(lambda x: x['channel'],
                self.graph.run(bg_presets.CHANNEL_QUERY).data()))

    def getSpeeds(self):
        return list(
            map(lambda x: x['speed'],
                self.graph.run(bg_presets.SPEED_QUERY).data()))

    def getLanIPs(self):
        return list(
            map(lambda x: x['lan'],
                self.graph.run(bg_presets.LANIP_QUERY).data()))

    def getDbStats(self):
        c = Counter()
        nodeRes = self.graph.run(
            '''MATCH (n) WITH DISTINCT labels(n) AS temp, COUNT(n) as tempCnt
                                UNWIND temp AS label
                                RETURN label, SUM(tempCnt) AS cnt''').data()
        probeCnt = self.graph.run(
            'MATCH ()-[r:Probes]->() RETURN count(r) AS probes').data()[0].get(
                'probes')
        assocCnt = self.graph.run(
            'MATCH ()-[r:AssociatedTo]->() RETURN count(r) AS assoc').data(
            )[0].get('assoc')

        for res in nodeRes:
            c.update({res['label']: res['cnt']})
        c.update({'Probes': probeCnt, 'Assoc': assocCnt})

        self.names = self.getNames()
        return c

    def dataToJSON(self, relationJSON):
        checkDupes, returnJson = {}, {}
        returnJson['nodes'] = []
        returnJson['edges'] = []

        for idx, node in enumerate(relationJSON['nodes']):
            node['type'] = node['type'][0]

            #Merge nodes that are both APs and Clients (mesh)
            if node['id'] not in checkDupes.values():
                checkDupes[idx] = node['id']
                returnJson['nodes'].append({
                    "data": node,
                    "selected": 'false',
                    "group": "nodes"
                })
            else:
                inDupe = [
                    key for (key, value) in checkDupes.items()
                    if value == node['id']
                ][0]
                if node['type'] == "Client":
                    node = {**node, **returnJson['nodes'][inDupe]['data']}
                else:
                    node = {**returnJson['nodes'][inDupe]['data'], **node}
                returnJson['nodes'][inDupe]['data'] = node

        for idx, edge in enumerate(relationJSON['edges']):
            returnJson['edges'].append({
                "data": edge,
                "selected": 'false',
                "group": "edges"
            })

        return returnJson

    def initialQuery(self):
        relations = self.graph.evaluate(bg_presets.INITIAL)
        self.INITIAL = False
        return self.dataToJSON(relations)

    def searchQuery(self, value, prop):
        if prop == "type":
            labelRelations = self.graph.evaluate(
                bg_presets.searchLabelRelations(value[0]))
            labelNoRelations = self.graph.evaluate(
                bg_presets.searchLabelNoRelations(value[0]))
            tempDict = {
                k: v + labelRelations[k]
                for k, v in labelNoRelations.items()
            }
            merged = {**labelRelations, **tempDict}
        else:
            relations = self.graph.evaluate(
                bg_presets.searchRelations(value, prop))
            noRelations = self.graph.evaluate(
                bg_presets.searchNoRelations(value, prop))
            tempDict = {k: v + relations[k] for k, v in noRelations.items()}
            merged = {**relations, **tempDict}

        return self.dataToJSON(merged)

    def handleIncomingData(self, dType, data):
        if dType == "Airodump":
            self.insertAiroData(data)

    def insertAiroData(self, data):
        print("Inserting node data!")
        bssidNodes, stationNodes = data[0][0], data[0][1]
        for b in bssidNodes:
            try:
                bNode = Node(b['type'],
                             name=b['name'],
                             bssid=b['bssid'],
                             oui=b['oui'],
                             encryption=b["encryption"],
                             speed=b['speed'],
                             channel=b['channel'],
                             auth=b['auth'],
                             cipher=b['cipher'],
                             lan=b['lan'])
                bNode.add_label("Device")
                self.graph.create(bNode)
            except ClientError:
                pass

        for essids, s in stationNodes:
            sNode = self.graph.nodes.match("Device", bssid=s['bssid']).first()
            if sNode is None:
                sNode = Node(s["type"],
                             name=s['name'],
                             bssid=s['bssid'],
                             FirstTimeSeen=s['fts'],
                             LastTimeSeen=s['lts'],
                             Power=s['pwr'],
                             NumPackets=s['pkts'],
                             Association=s['assoc'],
                             oui=s['oui'])
                sNode.add_label("Device")
            else:
                sNode['FirstTimeSeen'] = s['fts']
                sNode['LastTimeSeen'] = s['lts']
                sNode['Power'] = s['pwr']
                sNode['NumPackets'] = s['pkts']
                sNode['Association'] = s['assoc']
                self.graph.push(sNode)
                sNode = self.graph.nodes.match("Device",
                                               bssid=s['bssid']).first()

            for essid in essids:
                nExisting = self.graph.nodes.match("Device",
                                                   name=essid).first()
                if len(essid) > 0:
                    newProbe = Node("AP", name=essid)
                    newProbe.add_label("Device")
                    self.graph.create(
                        Relationship(sNode, "Probes", nExisting or newProbe))

            if s['assoc'] is not None:
                aExisting = self.graph.nodes.match("Device",
                                                   bssid=s['assoc']).first()
                newAssoc = Node("AP", bssid=s['assoc'])
                newAssoc.add_label("Device")
                self.graph.create(
                    Relationship(sNode, "AssociatedTo", aExisting or newAssoc))

        print("Database updated!")

    def deleteDB(self):
        self.graph.run(bg_presets.DELETEDB_QUERY)
예제 #15
0
def stocksinfo(request):
    form = StockForm(request.GET)
    form.is_valid()

    s = form.cleaned_data['input_string']
    ticker=None
    ticker,tickerOrgName=findTicker(s)
    if(ticker!=None):
        # tickerOrgName=findCompanyNameFromBloomberg(ticker)
        authenticate("localhost:7474", "neo4j", "root")
        graph = Graph()
        graph.delete_all()
        tx=graph.begin()
        tx.run("CREATE (n:%s {name:{b}})"%"Company",b=tickerOrgName)
        tx.commit()

        executives,boardMembers=getCompanyKeyPersonnel(ticker)
        procs = []
         
        companyBoardInfo = Manager().list()
        proc = Process(target=getCompanyBoardInfo, args=(boardMembers,tickerOrgName,companyBoardInfo))
        procs.append(proc)
        proc.start()

        executivesInfo = Manager().list()
        proc = Process(target=getCompanyExecutivesInfo, args=(executives,tickerOrgName,executivesInfo))
        procs.append(proc)
        proc.start()

        competitorName = Manager().list()
        marketCap = Manager().list()
        proc = Process(target=getCompetitorInfo, args=(ticker,tickerOrgName,competitorName,marketCap))
        procs.append(proc)
        proc.start()

        stakeHoldersName = Manager().list()
        OverallPR = Value('d', 0)
        proc = Process(target=getStakeHolderInfo, args=(ticker,tickerOrgName,stakeHoldersName,OverallPR))
        procs.append(proc)
        proc.start()

        for proc in procs:
            proc.join()

        C,div3=produceGraph(tickerOrgName,companyBoardInfo,"BoardMember")

        D,div4=produceGraph(tickerOrgName,executivesInfo,"Executive")
        
        A,div1=produceGraph(tickerOrgName,competitorName,"Competitor")

        B,div2=produceGraph(tickerOrgName,stakeHoldersName,"StakeHolder")

        E=nx.compose_all([A,B,C,D])
        div5=printGraph(E,tickerOrgName,"Overall")

        boardcareerhistory=0
        boardboardmembership=0
        for board in companyBoardInfo:
            boardcareerhistory+=len(board[2])
            boardboardmembership+=len(board[3])
        avgboardcareerhistory=1.0/len(companyBoardInfo)*boardcareerhistory 
        avgboardboardmembership=1.0/len(companyBoardInfo)*boardboardmembership 
        executivecareerhistory=0
        executiveboardmembership=0
        for executive in executivesInfo:
            executivecareerhistory+=len(board[2])
            executiveboardmembership+=len(board[3])
        avgexecutivecareerhistory=1.0/len(executivesInfo)*executivecareerhistory 
        avgexecutiveboardmembership=1.0/len(executivesInfo)*executiveboardmembership

        summarized_text=""
        summarized_text+="There are %s nodes in total.\n"%str(graph.evaluate("MATCH (n) RETURN count(*)"))
        summarized_text+="The computed value (based on pagerank analysis of stakeholders) of the company is %0.2f millions.\n"%OverallPR.value
        summarized_text+="There are %d Competitors in the same industry.\n"%len(competitorName)
        summarized_text+="There are %d Stakeholders(with more than 1 percent shares).\n"%len(stakeHoldersName)
        summarized_text+="There are %d Boardmembers in the company:\n"%len(companyBoardInfo)
        summarized_text+="---Each Boardmembers has an average number of %d past career in other companies.\n"%avgboardcareerhistory
        summarized_text+="---Each Boardmembers has an average number of %d board membership experiences in other companies.\n"%avgboardboardmembership
        summarized_text+="There are %d Executives in the company:\n"%len(executivesInfo)
        summarized_text+="---Each Executives has an average number of %d past career in other companies.\n"%avgexecutivecareerhistory
        summarized_text+="---Each Executives has an average number of %d board membership experiences in other companies.\n"%avgexecutiveboardmembership

        labels = [x for x in competitorName]
        labels.insert(0,tickerOrgName)
        values = [x for x in marketCap]

        trace = graph_objs.Pie(labels=labels, values=values,textinfo='none')
        fig = Figure(data=Data([trace]))
        piechart=plot(fig,auto_open=False,output_type='div')

        context={'input_string':s,'piechart':piechart,'graph':div1,'graph2':div2,'graph3':div3,'graph4':div4,'graph5':div5,'summarized_text':summarized_text}
        # context={'input_string':s,'graph':div1}
    else:
        context={'input_string':s}
    return render(request, 'stocks/stocksinfo.html', context)
예제 #16
0
class Neo4j:
    def __init__(self):
        # initialize the self.graph
        self.graph = Graph("bolt://localhost:7687",
                           auth=("neo4j", "password"),
                           database="twitter")
        self.matcher = NodeMatcher(self.graph)

    def delete_all(self):
        self.graph.delete_all()

    def load_data(self, tweet):
        """
        Loads one tweet at a time
        :param tweet: a json doc with following schema
        {
            "type": "record",
            "name": "tweet",
            "keys" : [
                {"name": "company", "type": "string"},
                {"name": "sentiment", "type": "integer"},
                {"name": "id", "type": "string"},
                {"name": "date", "type": "string"},
                {"name": "time", "type": "string"},
                {"name": "retweet_count", "type": "integer"}
                {"name":"hashtags", "type":array}
                ]
        }
        :return: None
        """
        # begin transaction
        tx = self.graph.begin()

        # retrieve company node from the remote self.graph
        company = self.graph.evaluate(
            "MATCH(n) WHERE n.name = {company} return n",
            company=tweet["company"])
        # if remote node is null, create company node
        if company is None:
            company = Node("Company", name=tweet["company"])
            tx.create(company)
            # print("Node created:", company)

        # repeat above for all nodes
        tweet_node = self.graph.evaluate("MATCH(n) WHERE n.id = {id} return n",
                                         id=tweet["id"])
        if tweet_node is None:
            tweet_node = Node("Tweet",
                              id=tweet["id"],
                              sentiment=tweet["sentiment"],
                              retweet_count=tweet["retweet_count"])
            tx.create(tweet_node)
            # print("Node created:", tweet_node)

        datetime = self.graph.evaluate(
            "MATCH(n) WHERE n.time = {time} AND n.date = {date} return n",
            time=tweet["time"].split(":")[0] + ':' +
            tweet["time"].split(':')[1],
            date=tweet["date"])
        if datetime is None:
            datetime = Node("DateTime",
                            time=tweet["time"].split(":")[0] + ':' +
                            tweet["time"].split(":")[1],
                            date=tweet["date"])
            # tx.create(datetime)
            # print("Node created:", datetime)

        # create relationships
        # check if describes already exists
        # describes = Relationship(tweet_node, "DESCRIBES", company)
        # created_on = Relationship(tweet_node, "CREATED_ON", datetime)
        # tx.create(describes)
        # tx.create(created_on)
        # print("Relationships created")

        # create hashtag nodes and connect them with tweet nodes
        for hashtag in tweet["hashtags"]:
            hashtag_node = self.matcher.match("Hashtag", name=hashtag).first()
            # hashtag_node = self.graph.evaluate("MATCH(n) WHERE n.name = {hashtag} return n", hashtag=hashtag)
            if hashtag_node is None:
                hashtag_node = Node("Hashtag", name=hashtag)
                tx.create(hashtag_node)
                about = Relationship(hashtag_node, "ABOUT", company)
                tx.create(about)

            contains_hashtag = Relationship(tweet_node, "CONTAINS",
                                            hashtag_node)
            tx.create(contains_hashtag)

        # commit transaction
        tx.commit()

    def bulk_load(self, tweets):
        """
        Bulk loads list of tweets
        :param self:
        :param tweets:
        :return:
        """
        for t in tweets:
            self.load_data(t)
            print("Tweet loaded into neo4j")

    def prune_graph(self):
        self.graph.evaluate(
            'MATCH (t:Tweet)-[:CONTAINS]->(n) WITH n as n, count(t) as tweet_count WHERE tweet_count '
            '< 2 DETACH DELETE n')
        print('Graph pruned!')
예제 #17
0
        

        '''
        Adding the Knowledge Graph layer, this inclues
            - identifying entities among stories
            - identifying sub stories among stories
            - and their relationships
        '''

        flag = False
        
        # Match full keyword
        for entity in entities:
            if entity in data[i]['Summary'].lower():
                node = graph.evaluate('MATCH(e:Entity {name: "' + entity + '"}) RETURN e')
                if not node:
                    node = graph.evaluate('CREATE(e:Entity {name: "' + entity + '"}) RETURN e')
                
                # Create relationship of story with an entity
                r = Relationship(node, "HAS_STORY", summary)
                graph.create(r)
                flag = True

        # No entity related to story, link to another story
        if not flag:

            # Delete the previous Story node
            graph.delete(summary)

            # Create a "Substory" node of the summary instead
예제 #18
0
 def get_nlg(graph_query):
     graph = Graph(auth=("neo4j", "pass"))
     graph_response = graph.evaluate(graph_query)
     return graph_response
예제 #19
0
def init_graph():
    your_password = passw.ord()
    uri = "bolt://*****:*****@localhost:8000".format(your_password)
    graph = Graph(uri)

    #Clean graph
    graph.evaluate("MATCH (n) DETACH DELETE n")

    #Set up key\val with APOC schema.assert
    graph.evaluate(
        "CALL apoc.schema.assert({Category:['name']},{Business:['id'],User:['id'],Review:['id'],Photo:['id']})"
    )

    #Load business.json, user.json and review.json respectivelu
    graph.evaluate(
        'CALL apoc.periodic.iterate("'
        'CALL apoc.load.json(\'file:///business.json\') YIELD value RETURN value '
        '"," '
        'MERGE (b:Business{id:value.business_id}) '
        'SET b += apoc.map.clean(value, [\'business_id\',\'categories\'],[]) '
        'WITH b,value.categories as categories '
        'UNWIND categories as category '
        'MERGE (c:Category{id:category}) '
        'MERGE (b)-[:IN_CATEGORY]->(c)"'
        ',{batchSize: 10000, iterateList: true});')

    graph.evaluate(
        'CALL apoc.periodic.iterate("'
        'CALL apoc.load.json(\'file:///user.json\') '
        'YIELD value RETURN value '
        '"," '
        'MERGE (u:User{id:value.user_id}) '
        'SET u += apoc.map.clean(value, [\'friends\',\'user_id\'],[0]) '
        'WITH u,value.friends as friends '
        'UNWIND friends as friend '
        'MERGE (u1:User{id:friend}) '
        'MERGE (u)-[:FRIEND]-(u1) '
        '",{batchSize: 100, iterateList: true});')

    graph.evaluate(
        'CALL apoc.periodic.iterate("'
        'CALL apoc.load.json(\'file:///review.json\') '
        'YIELD value RETURN value '
        '"," '
        'MERGE (b:Business{id:value.business_id}) '
        'MERGE (u:User{id:value.user_id}) '
        'MERGE (u)-[r:REVIEWS]->(b) '
        'SET r += apoc.map.clean(value, [\'business_id\',\'user_id\',\'review_id\'],[0])'
        '",{batchSize: 10000, iterateList: true});')

    graph.evaluate(
        'CALL apoc.periodic.iterate("'
        'CALL apoc.load.json(\'file:///photo.json\') '
        'YIELD value RETURN value '
        '"," '
        'MERGE (p:Photo{id:value.photo_id}) '
        'SET p += apoc.map.clean(value, [\'business_id\',\'photo_id\'],[0]) '
        'WITH p,value.business_id as businesses '
        'UNWIND businesses as business '
        'MERGE (b:Business{id:business}) '
        'MERGE (p)-[:PHOTO_OF]->(b) '
        '",{batchSize: 100, iterateList: true});')
예제 #20
0
class PubGraph:
    """
    PubGraph

    This class creates the Publication Graph for determining experts. It uses the neo4j
    graph database and the corresponding py2neo Python library. It also provides a way
    to easily access the graph's data and a node's adjacencies.
    """
    def __init__(self,
                 fnames,
                 pswd,
                 bolt=None,
                 secure=False,
                 host="localhost",
                 portNum=7474,
                 portType="http",
                 user="******"):
        """
        Creates the Publication Graph using the paper JSONs in fnames.
        
        Parameters:
        * fnames: a list of absolute paths to the JSON files to be used in making the graph
        * pswd: the password for the neo4j database to be used
        * bolt (Default = None): specifies whether to use the Bolt protocol for connection (None means autodetect). This is not needed for py2neo v4.0 or higher, as it uses bolt by default.
        * secure (Default = False): specifies whether to use a secure connection
        * host (Default = \"localhost\"): specifies the database server host name, which is \"localhost\" by default in neo4j.
        * portNum (Default = 7474): specifies the database server port, which is 7474 by default for an http connection in neo4j.
          - Note: In py2neo v4.0, the default connection type is bolt, not http. As a result, portnum should probably be set to 7687.
        * portType (Default = \"http\"): specifies the type of port that you want to use. Can be \"bolt\", \"https\", or \"http\". If set to a different value, it will assume \"http\".
          - Note: In py2neo v4.0, the default connection type is bolt. As a result, it is best to set portType to \"bolt\", although you can still use the others.
        * user (Default = \"neo4j\"): the user used to authenticate the connection to the neo4j database.
        """
        # Initializes the neo4j graph
        if portType == "bolt":
            self.graph = Graph(bolt=bolt,
                               secure=secure,
                               host=host,
                               bolt_port=portNum,
                               user=user,
                               password=pswd)
        elif portType == "https":
            self.graph = Graph(bolt=bolt,
                               secure=secure,
                               host=host,
                               https_port=portNum,
                               user=user,
                               password=pswd)
        else:
            self.graph = Graph(bolt=bolt,
                               secure=secure,
                               host=host,
                               http_port=portNum,
                               user=user,
                               password=pswd)
        # Creates constraints so that the id numbers must be unique
        self.graph.run("CREATE CONSTRAINT ON (a:Author) ASSERT a.id IS UNIQUE")
        self.graph.run("CREATE CONSTRAINT ON (v:Venue) ASSERT v.id IS UNIQUE")
        self.graph.run("CREATE CONSTRAINT ON (p:Paper) ASSERT p.id IS UNIQUE")
        # nodeList stores the author nodes of the graph so that their ids can be added later
        nodeList = []
        # jsonidDict is used to store citations between a paper in the graph and a paper not yet in the graph.
        # Each element has the cited paper as the key and a list of papers that cite the cited paper as the data.
        jsonidDict = {}
        for idx, fn in enumerate(fnames):
            # Opens the current JSON and adds the paper it represents to the graph if it is not already present
            f = json.loads(fn)
            paper = Node("Paper", title=f["title"], jid=f["id"], id=idx)
            self.graph.merge(paper)
            # If other papers have cited this paper, add the citations as directed edges from the cited paper to the citing papers.
            if f["id"] in jsonidDict:
                for jid in jsonidDict[f["id"]]:
                    citer = self.graph.evaluate(
                        "MATCH (p:Paper {{jid:\"{0:s}\"}}) RETURN p".format(
                            jid))
                    if citer is not None:
                        self.graph.merge(Relationship(paper, "Cites", citer))
            # Obtain the "venue name"
            if "venue" in f:
                vname = f["venue"]
            elif "isbn" in f:
                vname = "{0:s} {1:s}".format(f["publisher"], f["isbn"])
            else:
                vname = "{0:s} {1:s}".format(f["publisher"], f["doc_type"])
            # Check if the venue already has a node in the graph
            vexists = self.graph.evaluate(
                "MATCH (v:Venue {{name:\"{0:s}\"}}) RETURN v".format(vname))
            # If the venue is not in the graph, add it to the graph, and add an undirected edge between it and the paper published in it.
            if vexists is None:
                venueid = self.graph.evaluate(
                    "MATCH (v:Venue) RETURN count(*)")
                venueid += len(fnames)
                venue = Node("Venue", name=vname, id=venueid)
                self.graph.merge(venue)
                self.graph.merge(
                    Relationship(paper, "Published_In", venue)
                    | Relationship(venue, "Published_In", paper))
            # If the venue is in the graph, add an undirected edge between its node and the paper published in it.
            else:
                venue = vexists
                self.graph.merge(
                    Relationship(paper, "Published_In", vexists)
                    | Relationship(vexists, "Published_In", paper))
            for i in range(len(f["authors"]) - 1):
                # Creates a node in the graph for the current author if a node doesn't already exist
                auth = Node("Author",
                            name=f["authors"][i]["name"],
                            org=f["authors"][i]["org"])
                self.graph.merge(auth)
                # Adds the node to nodeList if it isn't already present
                if auth not in nodeList:
                    nodeList.append(auth)
                # Adds undirected edges between the author and the current paper and venue
                self.graph.merge(
                    Relationship(auth, "Authored", paper)
                    | Relationship(paper, "Authored", auth))
                self.graph.merge(
                    Relationship(auth, "Pubed", venue)
                    | Relationship(venue, "Pubed", auth))
                # This for loop associates the current author with all other authors in the paper
                for j in range(i + 1, len(f["authors"])):
                    # Creates a node in the graph for the current author if a node doesn't already exist
                    auth2 = Node("Author",
                                 name=f["authors"][j]["name"],
                                 org=f["authors"][j]["org"])
                    self.graph.merge(auth2)
                    # Adds the node to nodeList if it isn't already present
                    if auth2 not in nodeList:
                        nodeList.append(auth2)
                    # Adds undirected edges between the author and the current paper and venue
                    self.graph.merge(
                        Relationship(auth2, "Authored", paper)
                        | Relationship(paper, "Authored", auth2))
                    self.graph.merge(
                        Relationship(auth2, "Pubed", venue)
                        | Relationship(venue, "Pubed", auth2))
                    # Adds a undirected edge between the two authors because they are co-authors
                    rel1 = Relationship(auth, "CoAuth", auth2)
                    rel2 = Relationship(auth2, "CoAuth", auth)
                    self.graph.merge(rel1 | rel2)
            # Adds the references
            for ref in f["references"]:
                pexists = self.graph.evaluate(
                    "MATCH (p:Paper {{jid:\"{0:s}\"}}) RETURN p".format(ref))
                # If the cited paper is not already in the graph, adds the current and cited papers to jsonidDict.
                if pexists is None:
                    if ref not in jsonidDict:
                        jsonidDict[ref] = [f["id"]]
                    else:
                        jsonidDict[ref].append(f["id"])
                # Otherwise, creates a directed edge from the cited paper to the current paper
                else:
                    self.graph.merge(Relationship(pexists, "Cites", paper))
        # Sets member variables for number of papers and venues
        self.numPapers = self.graph.evaluate("MATCH (p:Paper) RETURN count(*)")
        self.numVenues = self.graph.evaluate("MATCH (v:Venue) RETURN count(*)")
        # Adds the ids for all the author nodes
        for idx, node in enumerate(nodeList):
            node["id"] = idx + self.numPapers + self.numVenues
            self.graph.push(node)
        # Sets member variable for number of authors
        self.numAuthors = self.graph.evaluate(
            "MATCH (a:Author) RETURN count(*)")

    def numNodes(self):
        """
        Returns the tuple (Number of Papers, Number of Venues, Number of Authors)
        """
        return self.numPapers, self.numVenues, self.numAuthors

    def __getitem__(self, index):
        """
        Returns the py2neo Node object with the provided index. If the index is not in the graph, raises an IndexError.

        Parameters:
        * index: an integer corresponding to an id in the graph
        """
        if index < self.numPapers:
            return self.graph.evaluate(
                "MATCH (p:Paper {{id:{0:d} }}) RETURN p".format(index))
        elif index < self.numPapers + self.numVenues:
            return self.graph.evaluate(
                "MATCH (v:Venue {{id:{0:d} }}) RETURN v".format(index))
        elif index < self.numPapers + self.numVenues + self.numAuthors:
            return self.graph.evaluate(
                "MATCH (a:Author {{id:{0:d} }}) RETURN a".format(index))
        else:
            raise IndexError(
                "Index must be less than {0:d}".format(self.numPapers +
                                                       self.numVenues +
                                                       self.numAuthors))

    def __len__(self):
        """
        Returns the number of nodes in the graph
        """
        return self.numPapers + self.numVenues + self.numAuthors

    def getAdj(self, index):
        """
        Generates a list of node ids for the nodes that are adjacent to the node represented by index.
        If the index is not in the graph, raises an IndexError.

        Parameters:
        * index: an integer corresponding to an id in the graph

        Returns: a list of node id values corresponding to the adjacencies of the desired node.
        """
        adj = []
        if index < self.numPapers:
            label = "Paper"
        elif index < self.numPapers + self.numVenues:
            label = "Venue"
        elif index < self.numPapers + self.numVenues + self.numAuthors:
            label = "Author"
        else:
            raise IndexError(
                "Index must be less than {0:d}".format(self.numPapers +
                                                       self.numVenues +
                                                       self.numAuthors))
        cursor = self.graph.run(
            "MATCH (a:{0:s} {{id:{1:d} }})-[]->(b) RETURN b.id".format(
                label, index))
        nodes = cursor.data()
        for n in nodes:
            adj.append(n["b.id"])
        # This guarantees that there are no duplicates in adj.
        adj = list(set(adj))
        adj.sort(reverse=True)
        return adj
예제 #21
0
logging.basicConfig(
    level=logging.INFO,
    filename="log/loader.log",
    filemode="w",
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

NEO4J_AUTH = (os.getenv(key="NEO4J_USERNAME"), os.getenv(key="NEO4J_PASSWORD"))

g = Graph(uri="bolt://neo4j:7687", auth=NEO4J_AUTH)

if __name__ == """__main__""":
    argp = argparse.ArgumentParser()
    argp.add_argument("-f",
                      "--file",
                      type=str,
                      help="Path for where Cypher query is.")
    args = argp.parse_args()

    logging.info(f"Reading {args.file}\n")
    with open(file=args.file, mode="r") as f:
        queries = f.read()

    logging.info(f"Formatting {args.file} for importing into neo4j\n")
    queries = queries.split(sep=";")
    queries = [txt for txt in queries if txt != "\n"]

    logging.info(f"Executing {args.file} in neo4j\n")
    for query in queries:
        g.evaluate(cypher=query)
예제 #22
0
파일: test.py 프로젝트: lx86110/QAKG
from py2neo import Graph
graph = Graph("http://localhost:7474/db/data/",
              username="******",
              password="******")
my_node = graph.evaluate('match (x:Drug) return x')
print(my_node)
예제 #23
0
# Ref: add relationship between exsited nodes https://segmentfault.com/a/1190000014488430

from py2neo import Graph
import csv

graph = Graph("bolt://localhost:7687", auth=("neo4j", "algorithm"))
# tx = graph.begin()
print(graph.run("MATCH (a:Airports) WHERE a.STATE = 'TX' RETURN a").to_table())
print(
    graph.evaluate("MATCH (a:Airports) WHERE a.STATE = 'TX' RETURN count(a)"))

with open('data/flights_100.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0

    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
        line_count += 1
        print(row['ORIGIN_AIRPORT'], row['DESTINATION_AIRPORT'],
              row['DISTANCE'])
        print(
            graph.run(
                "match (a:Airports), (b:Airports) where a.IATA_CODE = '" +
                row['ORIGIN_AIRPORT'] + "' and b.IATA_CODE = '" +
                row['DESTINATION_AIRPORT'] +
                "' create (a)-[r:airline{distance: " + row['DISTANCE'] +
                "}]->(b)"))
    print(line_count)
예제 #24
0
파일: main.py 프로젝트: Uranium2/Git-Docker
    # Save clean JSON
    with open("outputs.json", "w") as out:
        json.dump(cleanDico, out)

    allPersons = []
    for movie in cleanDico["allMovies"]:

        # Insert movie information in mongoDB
        insertMovieInformation(database, movie)

        # Insert person information in mongoDB
        insertPersonInformation(database, movie)

        # insert Movie
        if graph.evaluate("MATCH (a:MOVIE) WHERE a.id = %s RETURN a" %
                          (movie["id"])) is None:
            graph.run("CREATE (a:MOVIE {id: $id})", id=movie["id"])

        # Insert genre
        if "genres" in movie:
            for genre in movie["genres"]:
                genre = genre.upper()
                if graph.evaluate("MATCH (a: %s) RETURN a" % genre) is None:
                    graph.run("CREATE (a: %s)" % genre.upper())

                    # Create link
                    if graph.evaluate(
                            "MATCH (m:MOVIE)-[j]-(g:%s) WHERE m.id = %s return j"
                            % (genre, movie["id"])) is None:
                        graph.run(
                            "MATCH (m:MOVIE),(g:%s) WHERE m.id = %s CREATE (m)-[:GENRE]->(g)"
예제 #25
0
def query_from_file(neo4j: Graph, cypher_file):
    with open(cypher_file) as f:
        query = f.read().rstrip("\n")
        print(query)
        return neo4j.evaluate(query)
예제 #26
0
파일: api.py 프로젝트: maxmilian/lawsHack
def count_relation():
    graph = Graph("http://localhost:7474/db/data/", password=NEO4J_PASSWORD)
    result = graph.evaluate("MATCH ()-->() RETURN count(*)")
    return {"count": result}
예제 #27
0
파일: 7.py 프로젝트: zuleimin/neo4jDemos
#neo4j 查询
from py2neo import Node, Relationship, Graph

from py2neo import Graph
graph = Graph(password='******')
print(graph.run("MATCH (a:Person) RETURN a.name LIMIT 3").to_table())
print(graph.evaluate("MATCH (a:Person) RETURN count(a)"))

print(graph.run('MATCH (p:Person) return p')) #返回的是py2neo.database.Cursor
data = graph.run('MATCH (p:Person) return p').to_table()
print(data)


nodeMatch = graph.nodes.match("Person", age='33')
print(len(nodeMatch))
print(nodeMatch.first()['name'])
for node in nodeMatch:
    print(node['name'])



예제 #28
0
#authenticating with neo4j pleaase note my username is neo4j and password is neo
authenticate("localhost:7474", "neo4j", "neo")
#creating graphs were we can add node note: topic is the twitter topic we are searching for
graph = Graph()
file = open("newfile.txt", "r")

for line in file:
    var = line

topic = var
topic_string = '"' + var + '" '
topic_tweet = Node("Topic", name=topic)
#graph.create(topic_tweet)
graph.merge(topic_tweet)
topic_tweet = graph.evaluate('match(x:Topic{name:' + topic_string +
                             '}) return x')


#this function takes in json data from tweeter and store them in neo4j
def getVariables(data):
    #created_at = data.split('"created_at":"')[1].split('","id')[0]
    # twitterId = data.split(',"id":')[1].split(',"id_str')[0]
    #tweet = data.split(',"text":"')[1].split('","source')[0]
    #name = data.split(',"user":')[1].split()
    #print(data)

    python_obj = json.loads(data)
    #here we are taking in data from json file and storing them in python variables
    created_at = python_obj["created_at"]
    twitterId = python_obj["id_str"]
    tweet = python_obj["text"]
예제 #29
0
		else:
			Matrix[i][j]=r1
			
#for i in Author_id:
	#print (Author_id[i],' ',Author_name[i],' ', i)
#print (Author_id)
#print (Matrix)
j=1
i=0

while (j < 40):
 	
	#a=Relationship(Author_id[i], "Advisor_of",Author_id[j])
	#graph.merge(a)
	q1="MATCH (n:Author{ID:"+str(Author_id[i])+"}),(m:Author{ID:"+str(Author_id[j])+"})  CREATE UNIQUE (n)-[:Advisor_of{Wt:1}]->(m)"
	graph.evaluate(q1)
	j+=1
	#b=Relationship(Author_id[i], "Advisor_of",Author_id[j])
	#graph.merge(b)
	q1="MATCH (n:Author{ID:"+str(Author_id[i])+"}),(m:Author{ID:"+str(Author_id[j])+"})  CREATE UNIQUE (n)-[:Advisor_of{Wt:1}]->(m)"
	graph.evaluate(q1)
	i+=1
	j+=1


for i in range(40,44):
	r1 = random.randint(5,35)
	#a=Relationship(Author_id[i], "Advisor_of",Author_id[r1])
	#graph.merge(a)

예제 #30
0
def run():

    # args
    parser = argparse.ArgumentParser(
        description='k-means clustering of samples belonging to Pair')
    parser.add_argument(
        '--recalculate',
        '-r',
        action='store_true',
        help=
        'recalculates and rewrites (Sample)-[IN_CLUSTER]->(Cluster) and (Pair)-[HAS_CLUSTER]->(Cluster)'
    )
    parser.add_argument(
        '--fly',
        '-f',
        action='store',
        nargs='?',
        default='None',
        const='None',
        help=
        'pass Pair <id> as an argument to get a single result quickly on the fly'
    )

    run_mode = parser.parse_args()
    recalculate_arg = run_mode.recalculate
    fly_arg = run_mode.fly

    # initialise database graph
    graph = Graph('http://localhost:7474/db/data',
                  user='******',
                  password='******')

    # writing output
    start_timestamp = get_timestamp()
    logname = str('log/' + start_timestamp + '_mancluster.log')
    with open(logname, 'w') as outF:
        outF.write('LOG FILE for mancluster.py\n\n' + 'Start time: ' +
                   start_timestamp + '\n')

        if fly_arg != 'None':  # if the program is passed a pair directly via the fly arg process it instantly and return result
            pairID = int(fly_arg)
            n = graph.evaluate('MATCH (u:Pair) WHERE ID(u) = $p RETURN u',
                               parameters={"p": pairID})
            # print(n['name'])
            if n['k'] is None:
                outF.write(
                    'CRITICAL: Engaged in --fly mode but given pairID does not have a manually entered k'
                )
                print(
                    'CRITICAL: Engaged in --fly mode but given pairID does not have a manually entered k'
                )
            else:
                clus2Neo(n)

        else:
            already_computed_count = 0
            newly_computed_count = 0
            missing_count = 0
            kpairs_total = graph.data(
                'MATCH (p:Pair) WHERE NOT p.k = "None" RETURN count(*) AS total'
            )  # just for tqdm
            kpairs_total_asNum = kpairs_total[0]['total']

            for n in tqdm(graph.run(
                    'MATCH (p:Pair) WHERE NOT p.k = "None" RETURN p ORDER BY p.confidence'
            ),
                          total=kpairs_total_asNum,
                          unit='pairs'):

                already_calculated = n[0]['mancluster_update_timestamp']

                if missing_count == 0:
                    pairs_total = graph.data(
                        'MATCH (p:Pair) WHERE p.k = "None" RETURN count(*) AS total'
                    )
                    pairs_total_asNum = pairs_total[0]['total']
                    missing_count = pairs_total_asNum

                if recalculate_arg:  # argument passed at command line to recalculate all nodes
                    newly_computed_count += 1
                    pairID = remote(n[0])._id
                    node_name = n[0]['name']
                    k = n[0]['k']
                    clus2Neo(n)
                    print()
                    print()
                    print('NEWLY MANUALLY CLUSTERED')
                    print('--------------------------------------------')
                    print('Pair Name: ' + str(node_name))
                    print('Pair ID:' + str(pairID))
                    print('k: ' + str(k))
                    print('--------------------------------------------')
                    print()
                    print('No. of missing pairs so far: ', missing_count)
                    print('Pairs previously computed so far: ',
                          already_computed_count)
                    print('Pairs newly computed so far: ',
                          newly_computed_count)
                elif already_calculated is None:
                    newly_computed_count += 1
                    pairID = remote(n[0])._id
                    node_name = n[0]['name']
                    k = n[0]['k']
                    clus2Neo(n)
                    print()
                    print()
                    print('NEWLY MANUALLY CLUSTERED')
                    print('--------------------------------------------')
                    print('Pair Name: ' + str(node_name))
                    print('Pair ID:' + str(pairID))
                    print('k: ' + str(k))
                    print('--------------------------------------------')
                    print()
                    print('No. of missing pairs so far: ', missing_count)
                    print('Pairs previously computed so far: ',
                          already_computed_count)
                    print('Pairs newly computed so far: ',
                          newly_computed_count)
                else:
                    pairID = remote(n[0])._id
                    node_name = n[0]['name']
                    k = n[0]['k']
                    already_computed_count += 1

                    print()
                    print()
                    print('PREVIOUSLY MANUALLY CLUSTERED')
                    print('--------------------------------------------')
                    print('Pair Name: ' + str(node_name))
                    print('Pair ID:' + str(pairID))
                    print('k: ' + str(k))
                    print('--------------------------------------------')
                    print()
                    print('No. of missing pairs so far: ', missing_count)
                    print('Pairs previously computed so far: ',
                          already_computed_count)
                    print('Pairs newly computed so far: ',
                          newly_computed_count)
예제 #31
0
class GraphFSNeo4j(Operations):
    def __init__(self):

        self.graph = Graph(password="******")

        self.fileTime = time()

    # Helpers
    # =======

    def __parsePathInGroups(self, path):

        # Split the path in single elements.
        # Each element is a group, apart the last one, which could be a file.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = (os.path.normpath(os.path.splitdrive(path)[1]))
        if groupIDs == "/":
            return None
        else:
            if not "/" in groupIDs:
                return [groupIDs]
            else:
                groupIDs = groupIDs.split('/')

                if groupIDs[0] == "" and len(groupIDs) > 1:
                    return groupIDs[1:]
                else:
                    raise ValueError(
                        "There was an error parsing path [{}].".format(path))

    def __isGroup(self, groupId):
        if not Group.select(self.graph, groupId).first() is None:
            return True

        return False

    def __isFile(self, fileId):
        if not File.select(self.graph, fileId).first() is None:
            return True

        return False

    def __verifyPath(self, path, lastElementMustExist=False):
        """
        A path is valid if all the elements apart the last one are existing groups.
        If lastElementMustExist is True, then the last element must be either a Group or a File
        """
        elementsIDs = self.__parsePathInGroups(path)

        if elementsIDs is None:
            return True
        elif lastElementMustExist:
            if len(elementsIDs) > 1:
                if all(self.__isGroup(element) for element in elementsIDs[:-1]) \
                    and (self.__isGroup(elementsIDs[-1]) \
                        or self.__isFile(elementsIDs[-1])):
                    return True
                else:
                    return False
            else:
                if self.__isGroup(elementsIDs[-1]) \
                    or self.__isFile(elementsIDs[-1]):
                    return True
                else:
                    return False
        else:
            if len(elementsIDs) > 1:
                if all(
                        self.__isGroup(element)
                        for element in elementsIDs[:-1]):
                    return True
                else:
                    return False
            else:
                return True

    # Filesystem methods
    # ==================

    def access(self, path, mode):
        """
        This is the same as the access(2) system call.
        It returns:
        * -ENOENT if the path doesn't exist
        * -EACCESS if the requested permission isn't available
        * 0 for success
        
        Note that it can be called on files, directories, or any other object that appears in the filesystem.
        This call is not required but is highly recommended. 
        """

        print("-------")
        print("access: {}".format(path))
        print("mode: {}".format(mode))

        if not self.__verifyPath(path, lastElementMustExist=True):
            raise FuseOSError(-errno.ENOENT)

        #if ---: VERIFY PERMISSION
        #     raise FuseOSError(errno.EACCES)

        return 0

    def chmod(self, path, mode):
        # full_path = self._full_path(path)
        # return os.chmod(full_path, mode)
        pass

    def chown(self, path, uid, gid):
        # full_path = self._full_path(path)
        # return os.chown(full_path, uid, gid)
        pass

    def getattr(self, path, fh=None):
        # full_path = self._full_path(path)
        # st = os.lstat(full_path)
        # return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
        #              'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))

        print("-------")
        print("getattr: {}".format(path))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print('\tDoesn\'t exist')
            raise FuseOSError(errno.ENOENT)

        # Check if the last element refers to a group or a file
        if path == "/":
            return dict(st_mode=(stat.S_IFDIR | 0o755),
                        st_nlink=2,
                        st_size=1024,
                        st_ctime=self.fileTime,
                        st_mtime=self.fileTime,
                        st_atime=self.fileTime,
                        st_uid=os.getuid(),
                        st_gid=os.getgid())

        # Split the path in single elements.
        # Each element is a group, apart the last one, which could be a file.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        if self.__isGroup(groupIDs[-1]):
            return dict(st_mode=(stat.S_IFDIR | 0o755),
                        st_nlink=2,
                        st_size=1024,
                        st_ctime=self.fileTime,
                        st_mtime=self.fileTime,
                        st_atime=self.fileTime,
                        st_uid=os.getuid(),
                        st_gid=os.getgid())

        if self.__isFile(groupIDs[-1]):

            query = "MATCH (f:File{{name:'{fileId}'}}) RETURN f.value as value".format(
                fileId=groupIDs[-1])
            queryResult = self.graph.evaluate(query)

            if queryResult is None:
                fileSize = 0
            else:
                fileSize = len(queryResult.encode("utf8"))

            print('fileSize: {}'.format(fileSize))

            return dict(
                st_mode=(stat.S_IFREG | 0o755),
                st_nlink=1,
                st_size=fileSize  # Full size of the file
                ,
                st_ctime=self.fileTime,
                st_mtime=self.fileTime,
                st_atime=self.fileTime,
                st_uid=os.getuid(),
                st_gid=os.getgid())

        # The path does not refer to a group nor a file
        raise FuseOSError(-errno.ENOENT)

    def readdir(self, path, fh=None):

        # How to manage the fact that a file can have the same name as a group?

        print("-------")
        print("readdir: {}".format(path))

        dirents = ['.', '..']

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which could be a file.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        # Retrieve all the groups
        if groupIDs is None:
            query = "MATCH (g:Group) RETURN g.name as name"
        else:
            # Retrieve files that are connected to all the groups
            # We achieve that by checking which files belong to the groups specified in the path
            # and see if they belong to additional groups as well
            query = """WITH {groupIDs} as groups
                MATCH (g:Group)<-[:isInGroup]-(f:File)-[:isInGroup]->(gNew:Group)
                WHERE g.name in groups
                and not gNew.name in groups
                WITH gNew, size(groups) as inputCnt, count(DISTINCT g) as cnt
                WHERE cnt = inputCnt
                RETURN gNew.name as name""".format(groupIDs=list(groupIDs))

        print("query: {}".format(query))

        queryResults = self.graph.run(query)

        dirents.extend([queryResult["name"] for queryResult in queryResults])

        # Retrieve all the files
        if groupIDs is None:
            query = "MATCH (f:File) RETURN f.name as name"
        else:
            # Retrieve files that are connected to all the groups
            # We achieve that by checking which files belong to the groups specified in the path
            query = """WITH {groupIDs} as groups
                MATCH (g:Group)<-[:isInGroup]-(f:File)
                WHERE g.name in groups
                WITH f, size(groups) as inputCnt, count(DISTINCT g) as cnt
                WHERE cnt = inputCnt
                RETURN f.name as name""".format(groupIDs=list(groupIDs))

        print("query: {}".format(query))

        queryResults = self.graph.run(query)

        dirents.extend([queryResult["name"] for queryResult in queryResults])

        print("dirents: {}".format(dirents))

        for r in dirents:
            yield r

    def readlink(self, path):
        # pathname = os.readlink(self._full_path(path))
        # if pathname.startswith("/"):
        #     # Path name is absolute, sanitize it.
        #     return os.path.relpath(pathname, self.rootDB)
        # else:
        #     return pathname
        pass

    def mknod(self, path, mode, dev):
        # return os.mknod(self._full_path(path), mode, dev)
        pass

    def rmdir(self, path):
        """
        Remove the given directory.
        This should succeed only if the directory is empty (except for "." and "..").
        See rmdir(2) for details. 
        """

        print("-------")
        print("rmdir {}".format(path))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which could be a file.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        if groupIDs is None:
            print("Cannot remove root.")
            raise FuseOSError(errno.EPERM)

        # Check if the last element exists already as a group
        if not self.__isGroup(groupIDs[-1]):
            # It already exists
            print("The group {} does not exists".format(groupIDs[-1]))
            raise FuseOSError(errno.ENOENT)

        # Check if the group contains files
        if len(Group.select(self.graph, groupIDs[-1]).first().hasFiles) > 0:
            # It already exists
            print("The group {} contains files".format(groupIDs[-1]))
            raise FuseOSError(errno.ENOTEMPTY)

        print("Delete group {}".format(groupIDs[-1]))

        query = "MATCH (g:Group{{name:'{groupId}'}}) DELETE g".format(
            groupId=groupIDs[-1])
        queryResults = self.graph.run(query)

    def mkdir(self, path, mode):

        print("-------")
        print("mkdir {}".format(path))

        if not self.__verifyPath(path):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which could be a file.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        if groupIDs is None:
            print("Cannot create root.")
            raise FuseOSError(errno.EPERM)

        # Check if the last element exists already as a group
        if self.__isGroup(groupIDs[-1]):
            print("The group {} already exists".format(groupIDs[-1]))
            raise FuseOSError(errno.EEXIST)

        # Check if the last element exists already as a file
        if self.__isFile(groupIDs[-1]):
            # It already exists
            print("The file {} already exists".format(groupIDs[-1]))
            raise FuseOSError(errno.EEXIST)

        print("Create group {}".format(groupIDs[-1]))

        query = "CREATE (g:Group{{name:'{groupId}'}})".format(
            groupId=groupIDs[-1])
        queryResults = self.graph.run(query)

    def statfs(self, path):
        # full_path = self._full_path(path)
        # stv = os.statvfs(full_path)
        # return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
        #     'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
        #     'f_frsize', 'f_namemax'))
        return dict(f_bsize=512, f_blocks=4096, f_bavail=2048)

    def unlink(self, path):
        """
        Remove (delete) the given file, symbolic link, hard link, or special node. 
        Note that if you support hard links, unlink only deletes the data when 
        the last hard link is removed. See unlink(2) for details. 
        """

        print("-------")
        print("unlink {}".format(path))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        groupIDs = self.__parsePathInGroups(path)

        if groupIDs is None:
            print("Cannot unlink root.")
            raise FuseOSError(errno.EPERM)

        # Check if the last element exists already as a group
        if self.__isGroup(groupIDs[-1]):
            print("Cannot unlink group {}".format(groupIDs[-1]))
            raise FuseOSError(errno.EPERM)

        # Delete the file with all its relationships
        query = "MATCH (f:File {{ name: '{fileId}' }}) DETACH DELETE f".format(
            fileId=groupIDs[-1])
        queryResults = self.graph.run(query)

    def symlink(self, name, target):
        # return os.symlink(target, self._full_path(name))
        pass

    def rename(self, old, new):
        """
        Rename the file, directory, or other object "from" to the target "to".
        Note that the source and target don't have to be in the same directory,
        so it may be necessary to move the source to an entirely new directory.

        From https://stackoverflow.com/questions/20258807/implementing-rename-in-fuse-file-system:
        > The rename function replaces the target file atomically with removal of the old name.
        > This is the whole point of it, and if it doesn't do that correctly, various things would break badly.
        > For applications that want to prevent renaming over top of another file, they have to use the link function
        > (which will fail if the target exists) first, then unlink the old name if link succeeded.
        """

        print("-------")
        print("rename")
        print("old: {}".format(old))
        print("new: {}".format(new))

        if not isinstance(old, str):
            print("The element to rename must be a string.")
            raise FuseOSError(errno.EINVAL)

        if not isinstance(new, str):
            print("The element to rename must be a string.")
            raise FuseOSError(errno.EINVAL)

        if not self.__verifyPath(old, lastElementMustExist=True):
            print("The path [{}] is invalid".format(old))
            raise FuseOSError(errno.ENOENT)

        if not self.__verifyPath(new):
            print("The path [{}] is invalid".format(new))
            raise FuseOSError(errno.ENOENT)

        oldGroupIDs = self.__parsePathInGroups(old)
        newGroupIDs = self.__parsePathInGroups(new)

        if oldGroupIDs is None:
            print("Cannot rename/move root.")
            raise FuseOSError(errno.EPERM)

        if newGroupIDs is None:
            # All groups and files are already in the root group,
            # so we do not have to do anything.
            return 0

        # Check if the last element newGroupIDs
        # is the same of oldGroupIDs. In yes, it means we
        # are trying to move the old element
        if oldGroupIDs[-1] == newGroupIDs[-1]:

            # Remove the last element, the remaining ones are for sure valid groups
            newGroupIDs = newGroupIDs[:-1]

            # We have to move old into new

            # Check if last element of old is a group
            if self.__isGroup(oldGroupIDs[-1]):
                print("Cannot move folder into a folder.")
                raise FuseOSError(errno.EPERM)

            elif self.__isFile(oldGroupIDs[-1]):
                # We have to:
                # - remove file oldGroupIDs[-1] from all the groups in oldGroupIDs[:-1]
                # - add file oldGroupIDs[-1] to all the groups newGroupIDs

                for groupId in oldGroupIDs[:-1]:
                    query = "MATCH (f:File)-[r:isInGroup]->(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' DELETE r".format(
                        fileId=oldGroupIDs[-1], groupId=groupId)

                    queryResults = self.graph.run(query)

                if len(newGroupIDs) > 0:
                    for groupId in newGroupIDs:
                        query = "MATCH (f:File),(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' CREATE (f)-[r:isInGroup]->(g) RETURN r".format(
                            fileId=oldGroupIDs[-1], groupId=groupId)

                        queryResults = self.graph.run(query)

            else:
                # We shouldn't be here
                print('Something went wrong.')
                raise FuseOSError(errno.EBADR)

        else:

            # Check if last element of old is a group
            if self.__isGroup(oldGroupIDs[-1]):

                if self.__isFile(newGroupIDs[-1]):
                    print("Cannot rename file as an existing folder.")
                    raise FuseOSError(errno.EPERM)

                    # Is the following the right behaviour?

                    # We have to:
                    # - delete file newGroupIDs[-1],
                    # - rename group oldGroupIDs[-1] into newGroupIDs[-1],
                    # - move group oldGroupIDs[-1] into all the groups newGroupIDs[:-1]
                    # TBD: LAST STEP MISSING

                    self.unlink(newGroupIDs[-1])

                    query = """MATCH (g:Group {{ name: '{oldGroupId}' }})
                        SET g.name = '{newGroupId}'
                        RETURN g""".format(oldGroupId=oldGroupIDs[-1],
                                           newGroupId=newGroupIDs[-1])

                    queryResults = self.graph.run(query)

                elif self.__isGroup(newGroupIDs[-1]):
                    # We have to move group oldGroupIDs[-1] into all the groups newGroupIDs
                    print("Cannot move folder into a folder.")
                    raise FuseOSError(errno.EPERM)

                else:
                    # We have to:
                    # - rename group oldGroupIDs[-1] into newGroupIDs[-1],
                    # - move group oldGroupIDs[-1] into all the groups newGroupIDs[:-1]
                    # TBD: LAST STEP MISSING

                    query = """MATCH (g:Group {{ name: '{oldGroupId}' }})
                        SET g.name = '{newGroupId}'
                        RETURN g""".format(oldGroupId=oldGroupIDs[-1],
                                           newGroupId=newGroupIDs[-1])

                    queryResults = self.graph.run(query)

            elif self.__isFile(oldGroupIDs[-1]):

                if self.__isFile(newGroupIDs[-1]):
                    # We have to:
                    # - copy the content of file oldGroupIDs[-1] in file newGroupIDs[-1],
                    # - delete file oldGroupIDs[-1]
                    # - add the file newGroupIDs[-1] to all the groups of file oldGroupIDs[-1]
                    query = """MATCH (fOld:File {{ name: '{oldFileId}' }}), (fNew:File {{ name: '{newFileId}' }})
                        SET fNew.value = fOld.value
                        """.format(oldFileId=oldGroupIDs[-1],
                                   newFileId=newGroupIDs[-1])

                    queryResults = self.graph.run(query)

                    self.unlink(oldGroupIDs[-1])

                    if len(oldGroupIDs) > 1:
                        for groupId in oldGroupIDs[:-1]:
                            query = "MATCH (f:File),(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' CREATE (f)-[r:isInGroup]->(g) RETURN r".format(
                                fileId=newGroupIDs[-1], groupId=groupId)

                            queryResults = self.graph.run(query)

                elif self.__isGroup(newGroupIDs[-1]):
                    # We have to:
                    # - remove file oldGroupIDs[-1] from all the groups in oldGroupIDs[:-1]
                    # - add file oldGroupIDs[-1] to all the groups newGroupIDs

                    for groupId in oldGroupIDs[:-1]:
                        query = "MATCH (f:File)-[r:isInGroup]->(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' DELETE r".format(
                            fileId=oldGroupIDs[-1], groupId=groupId)

                        queryResults = self.graph.run(query)

                    for groupId in newGroupIDs:
                        query = "MATCH (f:File),(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' CREATE (f)-[r:isInGroup]->(g) RETURN r".format(
                            fileId=oldGroupIDs[-1], groupId=groupId)

                        queryResults = self.graph.run(query)

                else:
                    # We have to:
                    # - rename file oldGroupIDs[-1] into newGroupIDs[-1],
                    # - remove file oldGroupIDs[-1] from all the groups in oldGroupIDs[:-1]
                    # - add file oldGroupIDs[-1] to all the groups newGroupIDs
                    query = """MATCH (f:File {{ name: '{oldFileId}' }})
                        SET f.name = '{newFileId}'
                        RETURN f""".format(oldFileId=oldGroupIDs[-1],
                                           newFileId=newGroupIDs[-1])

                    queryResults = self.graph.run(query)

                    # We remove file oldGroupIDs[-1] from groups oldGroupIDs[:-1]
                    for groupId in oldGroupIDs[:-1]:
                        query = "MATCH (f:File)-[r:isInGroup]->(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' DELETE r".format(
                            fileId=oldGroupIDs[-1], groupId=groupId)

                        queryResults = self.graph.run(query)

                    # We have to move file old into folders new
                    for groupId in newGroupIDs:
                        query = "MATCH (f:File),(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' CREATE (f)-[r:isInGroup]->(g) RETURN r".format(
                            fileId=oldGroupIDs[-1], groupId=groupId)

                        queryResults = self.graph.run(query)
            else:
                # We shouldn't be here
                print('Something went wrong.')
                raise FuseOSError(errno.EBADR)

            return 0

    def link(self, target, name):
        # return os.link(self._full_path(name), self._full_path(target))
        pass

    def utimens(self, path, times=None):
        # return os.utime(self._full_path(path), times)
        pass

    # File methods
    # ============

    def open(self, path, flags):
        """
        Open a file.
        If you aren't using file handles, this function should just check for existence and permissions and return either success or an error code.
        If you use file handles, you should also allocate any necessary structures and set fi->fh.
        In addition, fi has some other fields that an advanced filesystem might find useful;
        see the structure definition in fuse_common.h for very brief commentary. 
        """
        print("-------")
        print("open {}".format(path))

        if not self.__verifyPath(path):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which is the file name.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        # Check if path is root or the last element is a group
        if groupIDs is None \
            or self.__isGroup(groupIDs[-1]):
            print(
                "Must specify a proper file name. The path [{}] refers to a group"
                .format(path))
            raise FuseOSError(errno.EISDIR)

        return 0

    def create(self, path, mode, fi=None):
        """
        Create and open a file.
        If the file does not exist, first create it with the specified mode, and then open it.
        """

        print("-------")
        print("create {}".format(path))

        if not self.__verifyPath(path):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which is the file name.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        # Check if path is root or the last element exists already as a group
        if groupIDs is None \
            or self.__isGroup(groupIDs[-1]):
            print("The path [{}] refers to a group.".format(path))
            raise FuseOSError(errno.EISDIR)

        # Check if the last element exists already as a file
        if self.__isFile(groupIDs[-1]):
            # It already exists
            return 0

        print("Create file {}".format(groupIDs[-1]))

        query = "CREATE (f:File{{name:'{fileId}'}})".format(
            fileId=groupIDs[-1])
        queryResults = self.graph.run(query)

        # Link the file to all the groups appearing in groupIDs
        if len(groupIDs) > 1:
            for groupId in groupIDs[:-1]:
                query = "MATCH (f:File),(g:Group) WHERE f.name = '{fileId}' AND g.name = '{groupId}' CREATE (f)-[r:isInGroup]->(g) RETURN r".format(
                    fileId=groupIDs[-1], groupId=groupId)

                queryResults = self.graph.run(query)

        return 0

    def read(self, path, length, offset, fh):

        print("-------")
        print("read {}".format(path))
        print("length, offset, fh:\n{}\n{}\n{}".format(length, offset, fh))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which is the file name.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        # Check if path is root or the last element exists already as a group
        if groupIDs is None \
            or self.__isGroup(groupIDs[-1]):
            print("The path [{}] refers to a group.".format(path))
            raise FuseOSError(errno.EISDIR)

        # Check if the last element exists as a file
        if not self.__isFile(groupIDs[-1]):
            # It already exists
            print("The element {} is not a file".format(groupIDs[-1]))
            raise FuseOSError(errno.ENOENT)

        query = "MATCH (f:File{{name:'{fileId}'}}) RETURN f.value as value".format(
            fileId=groupIDs[-1])
        queryResult = self.graph.evaluate(query)

        print('value:{}'.format(queryResult))

        if queryResult is None:
            return None
        else:
            return queryResult.encode()

    def write(self, path, buf, offset, fh):

        print("-------")
        print("write {}".format(path))
        print("buf:\n{}".format(buf.decode('utf-8')))
        print("\tfh {}".format(fh))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which is the file name.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        if groupIDs is None:
            print("Must specify a proper file name. The path [{}] is invalid".
                  format(path))
            raise FuseOSError(errno.ENOENT)

        # Check if the last element exists already as a file
        if not self.__isFile(groupIDs[-1]):
            print("The file {} does not exists".format(groupIDs[-1]))
            raise FuseOSError(errno.ENOENT)

        query = "MATCH (f:File{{name:'{fileId}'}}) SET f.value = '{value}' RETURN f".format(
            fileId=groupIDs[-1], value=buf.decode('utf-8'))
        queryResults = self.graph.run(query)

        return len(buf)

    def truncate(self, path, length, fh=None):

        print("-------")
        print("truncate {}".format(path))
        print("\tfh {}".format(fh))

        if not self.__verifyPath(path, lastElementMustExist=True):
            print("The path [{}] is invalid".format(path))
            raise FuseOSError(errno.ENOENT)

        # Split the path in single elements.
        # Each element is a group, apart the last one, which is the file name.
        # First we normalize the path (so we have all '/' as delimiters),
        # then we remove the eventual drive letter, as we don't need it.
        groupIDs = self.__parsePathInGroups(path)

        if groupIDs is None:
            print("Must specify a proper file name. The path [{}] is invalid".
                  format(path))
            raise FuseOSError(errno.ENOENT)

        # Check if the last element exists already as a file
        if not self.__isFile(groupIDs[-1]):
            print("The file {} does not exists".format(groupIDs[-1]))
            raise FuseOSError(errno.ENOENT)

        query = "MATCH (f:File{{name:'{fileId}'}}) SET f.value = NULL RETURN f".format(
            fileId=groupIDs[-1])
        queryResults = self.graph.run(query)

        return 0

    def flush(self, path, fh):
        # return os.fsync(fh)
        print("-------")
        print("flush {}".format(path))
        print("\tfh {}".format(fh))
        return 0

    def release(self, path, fh):
        # return os.close(fh)
        print("-------")
        print("release {}".format(path))
        print("\tfh {}".format(fh))
        return 0

    def fsync(self, path, fdatasync, fh):
        # return self.flush(path, fh)
        print("-------")
        print("fsync {}".format(path))
        print("\tfdatasync {}".format(fdatasync))
        print("\tfh {}".format(fh))
        return 0