Exemplo n.º 1
0
 def test_jit_2(self):
     G = nx.Graph()
     G.add_node(1, node_data=3)
     G.add_node(3, node_data=0)
     G.add_edge(1, 2, weight=9, something=0)
     G.add_edge(2, 3, weight=4, something=3)
     G.add_edge(1, 2)
     d = jit_data(G)
     K = jit_graph(json.loads(d))
     assert_true(nx.is_isomorphic(G, K))
Exemplo n.º 2
0
 def test_jit_directed(self):
     G = nx.DiGraph()
     G.add_node(1, node_data=3)
     G.add_node(3, node_data=0)
     G.add_edge(1, 2, weight=9, something=0)
     G.add_edge(2, 3, weight=4, something=3)
     G.add_edge(1, 2)
     d = jit_data(G)
     K = jit_graph(json.loads(d), create_using=nx.DiGraph())
     assert nx.is_isomorphic(G, K)
Exemplo n.º 3
0
 def test_jit_2(self):
     G = nx.Graph()
     G.add_node(1, node_data=3)
     G.add_node(3, node_data=0)
     G.add_edge(1, 2, weight=9, something=0)
     G.add_edge(2, 3, weight=4, something=3)
     G.add_edge(1, 2)
     d = jit_data(G)
     K = jit_graph(json.loads(d))
     assert_true(nx.is_isomorphic(G, K))
Exemplo n.º 4
0
 def test_jit(self):
     G = nx.Graph()
     G.add_node('Node1', node_data='foobar')
     G.add_node('Node3', node_data='bar')
     G.add_node('Node4')
     G.add_edge('Node1', 'Node2', weight=9, something='isSomething')
     G.add_edge('Node2', 'Node3', weight=4, something='isNotSomething')
     G.add_edge('Node1', 'Node2')
     d = jit_data(G)
     K = jit_graph(json.loads(d))
     assert nx.is_isomorphic(G, K)
Exemplo n.º 5
0
 def test_jit(self):
     G = nx.Graph()
     G.add_node('Node1', node_data='foobar')
     G.add_node('Node3', node_data='bar')
     G.add_node('Node4')
     G.add_edge('Node1', 'Node2', weight=9, something='isSomething')
     G.add_edge('Node2', 'Node3', weight=4, something='isNotSomething')
     G.add_edge('Node1', 'Node2')
     d = jit_data(G)
     K = jit_graph(json.loads(d))
     assert_true(nx.is_isomorphic(G, K))
Exemplo n.º 6
0
 def test_jit(self):
     G = nx.Graph()
     G.add_node("Node1", node_data="foobar")
     G.add_node("Node3", node_data="bar")
     G.add_node("Node4")
     G.add_edge("Node1", "Node2", weight=9, something="isSomething")
     G.add_edge("Node2", "Node3", weight=4, something="isNotSomething")
     G.add_edge("Node1", "Node2")
     d = jit_data(G)
     K = jit_graph(json.loads(d))
     assert nx.is_isomorphic(G, K)
Exemplo n.º 7
0
    def test_jit_multi_directed(self):
        G = nx.MultiDiGraph()
        G.add_node(1, node_data=3)
        G.add_node(3, node_data=0)
        G.add_edge(1, 2, weight=9, something=0)
        G.add_edge(2, 3, weight=4, something=3)
        G.add_edge(1, 2)
        pytest.raises(nx.NetworkXNotImplemented, jit_data, G)

        H = nx.DiGraph(G)
        d = jit_data(H)
        K = jit_graph(json.loads(d), create_using=nx.MultiDiGraph())
        assert nx.is_isomorphic(H, K)
        K.add_edge(1, 2)
        assert not nx.is_isomorphic(H, K)
        assert nx.is_isomorphic(G, K)
Exemplo n.º 8
0
    def test_jit_multi_directed(self):
        G = nx.MultiDiGraph()
        G.add_node(1, node_data=3)
        G.add_node(3, node_data=0)
        G.add_edge(1, 2, weight=9, something=0)
        G.add_edge(2, 3, weight=4, something=3)
        G.add_edge(1, 2)
        assert_raises(nx.NetworkXNotImplemented, jit_data, G)

        H = nx.DiGraph(G)
        d = jit_data(H)
        K = jit_graph(json.loads(d), create_using=nx.MultiDiGraph())
        assert_true(nx.is_isomorphic(H, K))
        K.add_edge(1, 2)
        assert_false(nx.is_isomorphic(H, K))
        assert_true(nx.is_isomorphic(G, K))
Exemplo n.º 9
0
__author__ = """Ollie Glass ([email protected])"""

import json

import matplotlib.pyplot as plt
import networkx as nx
from networkx.readwrite.json_graph import jit_data, jit_graph

# add some nodes to a graph
G = nx.Graph()

G.add_node("one", type="normal")
G.add_node("two", type="special")
G.add_node("solo")

# add edges
G.add_edge("one", "two")
G.add_edge("two", 3, type="extra special")

# convert to JIT JSON
jit_json = jit_data(G, indent=4)
print(jit_json)

X = jit_graph(json.loads(jit_json))
print("Nodes: %s" % list(X.nodes(data=True)))
print("Edges: %s" % list(X.edges(data=True)))

nx.draw(G, with_labels=True)
plt.show()
Exemplo n.º 10
0
def main():
    start = time.time()
    print "\nstart " + sys.argv[2]

    callGraphDir = sys.argv[1] + "/graphs/callGraph"
    interGraphDir = sys.argv[1] + "/interGraphs"
    targetGraphDir = sys.argv[1] + "/graphs/" + sys.argv[2]
    pathToFileA = sys.argv[3]
    pathToFileB = sys.argv[4]
    fileId = sys.argv[2]
    fileId = "_" + hashlib.md5(fileId.encode()).hexdigest()

    ### read files ###
    # replace id of node
    # BB_entry11[...];
    # BB139 [...]
    # BB_entry11 -> BB139 [...]
    with open(targetGraphDir + "/locMap_A.json") as f:
        locMapA = json.load(f)
    with open(targetGraphDir + "/locMap_B.json") as f:
        locMapB = json.load(f)
    for obj in locMapA + locMapB:
        obj['blockId'] = obj['blockId'] + fileId

    with open(targetGraphDir + "/flowgraphs_A", 'r') as f:
        dotA = f.readlines()
    newLines = []
    for l in dotA:
        s = re.split('(BB\d+|BB_entry\d+)', l)
        l = ""
        for i in range(0, len(s)):
            l += s[i]
            if i % 2 == 1:
                l += fileId
        newLines.append(l)
    dotA = newLines
    with open(targetGraphDir + "/flowgraphs_replaced_A", 'w') as f:
        f.writelines(dotA)
    CFGA = nx.drawing.nx_agraph.read_dot(targetGraphDir +
                                         "/flowgraphs_replaced_A")

    with open(targetGraphDir + "/flowgraphs_B", 'r') as f:
        dotB = f.readlines()
    newLines = []
    for l in dotB:
        s = re.split('(BB\d+|BB_entry\d+)', l)
        l = ""
        for i in range(0, len(s)):
            l += s[i]
            if i % 2 == 1:
                l += fileId
        newLines.append(l)
    dotB = newLines
    with open(targetGraphDir + "/flowgraphs_replaced_B", 'w') as f:
        f.writelines(dotB)
    CFGB = nx.drawing.nx_agraph.read_dot(targetGraphDir +
                                         "/flowgraphs_replaced_B")

    print "create graphs " + str(time.time() - start)

    with open(callGraphDir + "/callgraph_A.json") as f:
        CGEdgesA = json.load(f)
    with open(callGraphDir + "/callgraph_B.json") as f:
        CGEdgesB = json.load(f)

    # mark file of node
    for data in CFGA.nodes.values():
        data['file'] = pathToFileA
    for data in CFGB.nodes.values():
        data['file'] = pathToFileB

    ### compute difference ###
    diffMapA = {'added': [], 'changed': []}
    diffMapB = {'added': [], 'changed': []}
    summerizeDiff(dotA, dotB, diffMapA, diffMapB, fileId)
    # print diffMapA

    # mark diffent node
    markDiffNode(CFGA, CFGB, diffMapA, diffMapB)

    ### calcurate affected node ###
    # map location to node
    for id, data in CFGA.nodes.items():
        data['locList'] = []
        for obj in locMapA:
            if obj['blockId'] == id:
                data['locList'].append(obj['loc'])
    for id, data in CFGB.nodes.items():
        data['locList'] = []
        for obj in locMapB:
            if obj['blockId'] == id:
                data['locList'].append(obj['loc'])

    # mark read and write variable and call block
    for data in CFGA.nodes.values() + CFGB.nodes.values():
        data['read'] = readVars(data)  #{name: x, loc: [0,0,0,0]}
        data['write'] = writeVar(data)  #{name: x, loc: [0,0,0,0]}
        data['cond'] = condLoc(data)  #[0,0,0,0] or null
        if 'label' in data.keys():
            if re.match('{\d+: call', data['label']):
                data['call'] = True

    # delete gray edge
    for id, data in CFGA.edges.items():
        if 'color' in data and data['color'] == 'gray':
            CFGA.remove_edge(id[0], id[1])
    for id, data in CFGB.edges.items():
        if 'color' in data and data['color'] == 'gray':
            CFGB.remove_edge(id[0], id[1])

    ### get subGraph of each function
    funcListA = {}
    entriesA = filter(lambda n: 'BB_entry' in n, CFGA.nodes)
    for en in entriesA:
        clusterId = en[8:]
        name = getFuncName(clusterId, fileId, dotA)
        CFGA.nodes[en]['funcName'] = name
        subGraph = nx.Graph.subgraph(
            CFGA, filter(lambda n: nx.has_path(CFGA, en, n), CFGA.nodes))
        for data in subGraph.nodes.values():
            data["entry"] = en
        markAffected(subGraph, diffMapA, en, fileId)
        cdg = createCDG(subGraph, clusterId, name, pathToFileA)
        funcListA[clusterId] = {
            "funcName": name,
            "subGraph": subGraph,
            "CDG": cdg
        }
    funcListB = {}
    entriesB = filter(lambda n: 'BB_entry' in n, CFGB.nodes)
    for en in entriesB:
        clusterId = en[8:]
        name = getFuncName(clusterId, fileId, dotB)
        CFGB.nodes[en]['funcName'] = name
        subGraph = nx.Graph.subgraph(
            CFGB, filter(lambda n: nx.has_path(CFGB, en, n), CFGB.nodes))
        for data in subGraph.nodes.values():
            data["entry"] = en
        markAffected(subGraph, diffMapB, en, fileId)
        cdg = createCDG(subGraph, clusterId, name, pathToFileB)
        funcListB[clusterId] = {
            "funcName": name,
            "subGraph": subGraph,
            "CDG": cdg
        }

    print "mark node information " + str(time.time() - start)

    ### compute distance from each node to each node in ICDG ###
    # create ICFG and ICDG with callgraph
    # not correspond to function passed as arguments
    ICFGA = nx.DiGraph()
    if os.path.exists(interGraphDir + '/ICFG_A.json'):
        with open(interGraphDir + '/ICFG_A.json') as f:
            ICFGA = json_graph.jit_graph(json.load(f),
                                         create_using=nx.DiGraph())
    ICFGB = nx.DiGraph()
    if os.path.exists(interGraphDir + '/ICFG_B.json'):
        with open(interGraphDir + '/ICFG_B.json') as f:
            ICFGB = json_graph.jit_graph(json.load(f),
                                         create_using=nx.DiGraph())
    ICDGA = nx.DiGraph()
    if os.path.exists(interGraphDir + '/ICDG_A.json'):
        with open(interGraphDir + '/ICDG_A.json') as f:
            ICDGA = json_graph.jit_graph(json.load(f),
                                         create_using=nx.DiGraph())
    ICDGB = nx.DiGraph()
    if os.path.exists(interGraphDir + '/ICDG_B.json'):
        with open(interGraphDir + '/ICDG_B.json') as f:
            ICDGB = json_graph.jit_graph(json.load(f),
                                         create_using=nx.DiGraph())
    updateICFGandICDG(ICFGA, ICDGA, funcListA, CGEdgesA, pathToFileA)
    updateICFGandICDG(ICFGB, ICDGB, funcListB, CGEdgesB, pathToFileB)

    # map next node in icfg to node
    for id, data in ICDGA.nodes.items():
        data['next'] = list(ICFGA.succ[id])
    for id, data in ICDGB.nodes.items():
        data['next'] = list(ICFGB.succ[id])

    with open(interGraphDir + '/ICFG_A.json', 'w') as f:
        f.write(json_graph.jit_data(ICFGA, 2))
    with open(interGraphDir + '/ICFG_B.json', 'w') as f:
        f.write(json_graph.jit_data(ICFGB, 2))
    with open(interGraphDir + '/ICDG_A.json', 'w') as f:
        f.write(json_graph.jit_data(ICDGA, 2))
    with open(interGraphDir + '/ICDG_B.json', 'w') as f:
        f.write(json_graph.jit_data(ICDGB, 2))
    with open(interGraphDir + '/nodeMap_A.json', 'w') as f:
        json.dump(dict(ICDGA.nodes(data=True)), f, indent=2)
    with open(interGraphDir + '/nodeMap_B.json', 'w') as f:
        json.dump(dict(ICDGB.nodes(data=True)), f, indent=2)
    edgeListA = map(lambda x: {
        "from": x[0],
        "to": x[1],
        "data": x[2]
    }, ICDGA.edges(data=True))
    edgeListB = map(lambda x: {
        "from": x[0],
        "to": x[1],
        "data": x[2]
    }, ICDGB.edges(data=True))
    with open(interGraphDir + '/edgeList_A.json', 'w') as f:
        json.dump(edgeListA, f, indent=2)
    with open(interGraphDir + '/edgeList_B.json', 'w') as f:
        json.dump(edgeListB, f, indent=2)

    print "create icfg and icdg " + str(time.time() - start)
Exemplo n.º 11
0
def save_test_ontology_to_json(G):
    with open('Climate_Mind_Digraph_Test_Ont.json', 'w') as outfile:
        outfile.write(json_graph.jit_data(G, indent=4))
Exemplo n.º 12
0
def save_graph_to_json(G):
    with open('Climate_Mind_DiGraph.json', 'w') as outfile:
        outfile.write(json_graph.jit_data(G, indent=4))
Exemplo n.º 13
0
An example showing how to use the JavaScript InfoVis Toolkit (JIT)
JSON export

See the JIT documentation and examples at http://thejit.org

"""
__author__ = """Ollie Glass ([email protected])"""

import json
import networkx as nx
from networkx.readwrite.json_graph import jit_data, jit_graph

# add some nodes to a graph
G = nx.Graph()

G.add_node("one", type="normal")
G.add_node("two", type="special")
G.add_node("solo")

# add edges
G.add_edge("one", "two")
G.add_edge("two", 3, type="extra special")

# convert to JIT JSON
jit_json = jit_data(G, indent=4)
print(jit_json)

X = jit_graph(json.loads(jit_json))
print("Nodes: %s" % list(X.nodes(data=True)))
print("Edges: %s" % list(X.edges(data=True)))