# -*- coding: utf-8 -*-
"""
Created on Wed Aug 30 08:46:59 2017

@author: dell
"""

import networkx as nx
import json
import matplotlib.pyplot as plt
data={
"cor_rule":{
				"短路":{
				"1-规则1":["1-2","非1-2-1","1-6"],
				"1-规则2":["1-2","1-2-1","非1-2-2","1-6"],
				"1-规则3":["1-2","1-2-1","1-2-2","非1-2-3","1-6"],
				"1-规则4":["1-3","1-6"] ,
				"1-规则5":["1-4","1-6","非1-4-2"],
				"1-规则6": ["2-4"],
				"1-规则7": ["5-2","非5-2-2","5-8"],
				"1-规则8": ["5-3","非5-3-1"],
				"1-规则9": ["5-3","非5-3-2"],
				"1-规则10": ["5-6","1-2","5-8"]
				},
				"接触电阻过大":{
				"2-规则1":["2-1","3-1"],
				"2-规则2":["2-2","3-1"],
				"2-规则3":["2-3","3-1"],
				"2-规则4":["3-3"]
				},
				"环境不良":{
				"3-规则1":["4-5","4-6"],
				"3-规则2":["4-5","4-7"],
				"3-规则3":["4-8"],
				"3-规则4":["4-4"]
				},
				"过载":{
				"4-规则1":["1-3","2-1"],
				"4-规则2":["1-3","2-3"],
				"4-规则3":["1-4","1-6","2-1"],
				"4-规则4":["1-4","1-6","2-3"],
				"4-规则5":["2-1","非3-1"],
				"4-规则6":["2-3","非3-1"],
				"4-规则7":["2-4"]
				},
				"三相不平衡":{
				"5-规则1":["1-1"]
				},
				"零地混接":{
				"6-规则1":["5-1","5-1-1"],
				"6-规则2":["5-3","5-1-1"]
				},
				"零线混用":{
				"7-规则1":["5-5"],
				"7-规则2":["5-7"],
				"7-规则3":["5-3"],
				"7-规则4":["5-2"]
				},
				"漏电":{
				"8-规则1":["5-1","非5-1-1"],
				"8-规则2":["5-2"],
				"8-规则3":["5-3"],
				"8-规则4":["5-4"],
				"8-规则5":["5-6"]
				},
				"谐波":{
					"9-规则1":["4-1","4-2"],
					"9-规则2":["4-1","4-3"],
					"9-规则3":["5-3"],
					"9-规则4":["5-4"],
					"9-规则5":["5-6","非1-2"]
				}
		},
"rule_data":{
			"1-1":"当判断1-1为真时",
			"1-2":"当判断1-2为真时",
			"1-2-1":"当判断1-2-1为真时",
			"1-2-2":"当判断1-2-2为真时",
			"1-2-3":"当判断1-2-3为真时",
			"1-3":"当判断1-3为真时",
			"1-4":"当判断1-4为真时",
			"1-4-2":"当判断1-4-2为真时",
			"1-6":"当判断1-6为真时",
			"2-1":"当判断2-1为真时",
			"2-2":"当判断2-2为真时",
			"2-3":"当判断2-3为真时",
			"2-4":"当判断2-4为真时",
			"3-1":"当判断3-1为真时",
			"4-1":"当判断4-1为真时",
			"4-2":"当判断4-2为真时",
			"4-3":"当判断4-3为真时",
			"4-4":"当判断4-4为真时",
			"4-5":"当判断4-5为真时",
			"4-6":"当判断4-6为真时",
			"4-7":"当判断4-7为真时",
			"4-8":"当判断4-8为真时",
			"5-1":"当判断5-1为真时",
			"5-2":"当判断5-2为真时",
			"5-3":"当判断5-3为真时",
			"5-4":"当判断5-4为真时",
			"5-5":"当判断5-5为真时",
			"5-6":"当判断5-6为真时",
			"5-7":"当判断5-7为真时",
			"5-8":"当判断5-8为真时"
		}
		}
#data=bytes(data).decode('utf-8')
nodes=[]
edges=[]
#for node in data['cor_rule']:
#    nodes.append(unicode(node,"utf-8"))
#
##    print node
#    for node2 in data['cor_rule'][node]:
#        for node3 in data['cor_rule'][node][node2]:
#            nodes.append(unicode(node3.replace("非",""),"utf-8"))
#            edges.append([unicode(node,"utf-8"),unicode(node3.replace("非",""),"utf-8")])
#            

for node in data['cor_rule']:
    nodes.append(node)

#    print node
    for node2 in data['cor_rule'][node]:
        for node3 in data['cor_rule'][node][node2]:
            nodes.append(node3.replace("非",""))
            edges.append([node,node3.replace("非","")])
#            print node3

G=nx.Graph()
G.add_nodes_from(nodes)               
G.add_edges_from(edges)
pos = nx.spring_layout(G,iterations=10000)
data=json.dumps(data, ensure_ascii=False)
plt.figure(1,figsize=(10,10))
#font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
nx.draw(G,pos,alpha=0.5,linewidths=1, edge_color='b',node_color='aqua',with_labels=True,width=0.8,font_size=16,node_size =1200)

plt.savefig("a.png")
