示例#1
0
def update_node_id(type, old_id, new_id):
    """
    update a node id and also update al instances in the edge collections
    :param type: node name
    :param old_id: old id
    :param new_id: new id
    :return: nothing
    """
    mycol = db["node_" + type]

    myquery = {"id": old_id}
    newvalues = {"$set": {"id": new_id}}

    # update node
    mycol.update_one(myquery, newvalues)

    # update edges
    collections = db.list_collection_names()
    for item in collections:
        if item[:4] == 'edge':
            coll = db[item]
            my_source_query = {"source": old_id}
            new_source_values = {"$set": {"source": new_id}}
            my_target_query = {"target": old_id}
            new_target_values = {"$set": {"target": new_id}}
            coll.update_many(my_source_query, new_source_values)
            coll.update_many(my_target_query, new_target_values)
def get_all_edge_list(base, id="all"):
    collections = db.list_collection_names()
    edge_list = []

    if base == 'edge':
        if id == 'all':
            for item in collections:
                if item[:4] == 'edge':
                    coll = db[item].find()
                    for record in coll:
                        if id == "all":
                            edge_list.append(
                                {"source": str(record['source']), "target": str(record['target']), "value": 1})
        else:
            coll = db['edge_' + id].find()
            for record in coll:
                edge_list.append({"source": str(record['source']), "target": str(record['target']), "value": 1})
    elif base == 'node':
        for item in collections:
            if item[:4] == 'edge':
                coll = db[item].find()
                for record in coll:
                    if id == "all":
                        edge_list.append({"source": str(record['source']), "target": str(record['target']), "value": 1})
                    elif record['source'] == id:
                        edge_list.append({"source": str(record['source']), "target": str(record['target']), "value": 1})
                    elif record['target'] == id:
                        edge_list.append({"source": str(record['source']), "target": str(record['target']), "value": 1})

    return edge_list
示例#3
0
def read_all():
    data = {}
    for col in db.list_collection_names():
        print(col)
        content = []
        for record in db[col].find():
            content.append(record)
        data[col] = content

    return render_template('read.html', data=data)
示例#4
0
def remove_node(data, source_target_id):
    node_type = 'node_' + data[source_target_id + "_collection_name"].lower()
    id = get_node_id(data, source_target_id)

    print(node_type, id)
    # 1 remove from edges
    collections = db.list_collection_names()

    edge_list = []
    for item in collections:
        if item[:4] == 'edge':
            db[item].delete_many({'source': id})
            db[item].delete_many({'target': id})

    # 2 remove node
    db[node_type].remove({'id': id})
示例#5
0
def get_collections(type):
    """
    get collectino names depending on type node or type edge
    :param type: collection type, can be node or edge
    :return: list of collections names
    """
    collections = db.list_collection_names()
    # colls = dbf.drop_collection_identifier(collections)

    try:
        if type == 'node':
            colls = [x[5:] for x in collections if x[:5] == 'node_']

        elif type == 'edge':
            colls = [x[5:] for x in collections if x[:5] == 'edge_']

    except:
        colls = []

    return jsonify(colls)
def get_all_nodes_list(base, id="all"):
    """
    get nodes including node type
    Default is all, unless node id is entered
    Used for d3.js graph
    :param node: node id
    :return: list of nodes including node type
    """

    collections = db.list_collection_names()

    node_list = []

    if id == 'all':
        for item in collections:
            if item[:4] == 'node':
                for identifier in dbf.getCollectionId(item):
                    node_list.append({"id": str(identifier), "type": item[5:]})

    else:
        # get all edges that include the specified node
        if base == 'node':
            edge_list = get_all_edge_list(base='node', id=id)
        elif base == 'edge':
            edge_list = get_all_edge_list(base='edge', id=id)
        lst = []
        # create (set) list of nodes
        for record in edge_list:
            lst.append(record['source'])
            lst.append(record['target'])
        lst = list(set(lst))

        # add node characteristics to node list
        for item in collections:
            if item[:4] == 'node':
                type = item[5:]
                for record in db[item].find():
                    if record['id'] in lst:
                        node_list.append({"id": record['id'], "type": type})

    return node_list
示例#7
0
def find_matching_collections(input):
    """
    -check on lenght
    -check on matching characters

    :param user_input:
    :return:
    """
    collections = db.list_collection_names()

    for collection in collections:
        c = 0
        for i in set(input):
            if i in set(collection):
                c += 1
        set_match = c / len(set(input))
        len_match = min(len(input), len(collection)) / max(
            len(input), len(collection))
        result = set_match * len_match
        print(
            'matching result {} for {} is {} (character match: {}, length match {})'
            .format(input, collection, int(result * 100), set_match,
                    len_match))
示例#8
0
def merge_nodes(data):
    """
    merge target node into source node. The source node properties will be leading.

    :param sourcetype:
    :param sourceid:
    :param targettype:
    :param targetid:
    :return: nothing
    """

    source_type = "node_" + data['source_collection_name']
    source_id = data['source_collection_id']
    target_type = "node_" + data['target_collection_name']
    target_id = data['target_collection_id']

    # handle non existing source node
    upsert_node_data(data, 'source')

    # handle null values

    # merge proc
    # if source_type == target_type:
    # remove target in node collection
    db[target_type].remove({'id': target_id})
    # replace target in edge collections
    collections = db.list_collection_names()
    for item in collections:
        if item[:4] == 'edge':
            coll = db[item]
            my_source_query = {"source": target_id}
            new_source_values = {"$set": {"source": source_id}}
            my_target_query = {"target": target_id}
            new_target_values = {"$set": {"target": source_id}}
            coll.update_many(my_source_query, new_source_values)
            coll.update_many(my_target_query, new_target_values)