def generateTriangle(figureInfoArray, cordArray, triangleName,node_root):
	print("Inside generateTriangle  function")
	write_Name_In_File(triangleName[0])
	write_Name_In_File(triangleName[1])
	write_Name_In_File(triangleName[2])
	side1 = str(triangleName[0] + triangleName[1])
	side2 = str(triangleName[1] + triangleName[2])
	side3 = str(triangleName[0] + triangleName[2])

	angle2 = triangleName
	angle3 = (side2 + triangleName[0])
	angle1 = str(triangleName[1] + triangleName[0]+ triangleName[2])

	point1 = triangleName[0]
	point2 = triangleName[1]
	point3 = triangleName[2]

	#Create root node
	node_triangle, = graph_db.create({"name": triangleName,"type":"triangle"})
	if node_triangle :
	    print(" node_triangle is generated")
	rel_triangle_root = node_root.create_relationship_to(node_triangle, "KD1")

	# Create three nodes
	node_a, node_b, node_c = graph_db.create(
    {"name": side1, "value":figureInfoArray[0],"type":"line"},
    {"name": side2, "value":figureInfoArray[1],"type":"line"},
    {"name": side3, "value":figureInfoArray[2],"type":"line"}
)

	# Join the nodes with a HAS relationship
	rel_triangle_a = node_triangle.create_relationship_to(node_a, "HAS")
	rel_triangle_b = node_triangle.create_relationship_to(node_b, "HAS")
	rel_triangle_c = node_triangle.create_relationship_to(node_c, "HAS")

	rel_a_b, = graph_db.get_or_create_relationships((node_a, "ANGLE",node_b, {"value": figureInfoArray[3]}))
	rel_b_c, = graph_db.get_or_create_relationships((node_b, "ANGLE",node_c, {"value": figureInfoArray[4]}))
	rel_a_c, = graph_db.get_or_create_relationships((node_a, "ANGLE",node_c, {"value": figureInfoArray[5]}))

	#Create input nodes
	node_point1, node_point2, node_point3= graph_db.create({"name":point1, "cordx":cordArray[0],"cordy":cordArray[1],"type":"point"},{"name":point2, "cordx":cordArray[2],"cordy":cordArray[3],"type":"point"},{"name":point3, "cordx":cordArray[4],"cordy":cordArray[5],"type":"point"})

	# Join the nodes with a  INPUT relationship
	rel_a_point1 = node_a.create_relationship_to(node_point1, "CONTAIN")
	rel_a_point2 = node_a.create_relationship_to(node_point2, "CONTAIN")
	rel_b_point2 = node_b.create_relationship_to(node_point2, "CONTAIN")
	rel_b_point3 = node_b.create_relationship_to(node_point3, "CONTAIN")
	rel_c_point1 = node_c.create_relationship_to(node_point1, "CONTAIN")
	rel_c_point3 = node_c.create_relationship_to(node_point3, "CONTAIN")

	# Join the point nodes with a  CONNECTED relationship
	node_point1.create_relationship_to(node_point2, "CONNECTED")
	node_point2.create_relationship_to(node_point3, "CONNECTED")
	node_point1.create_relationship_to(node_point3, "CONNECTED")
	
	print("Exiting generate triangle function")
def check_join_Existing_three_non_collinear_non_connected_Points():

	print("Pick existing set of three non collinear points which are not connected")
	global count_row
	global dict_three_point
	count_row = 0
	dict_three_point = []
	query = "START b=node(*),a=node(*),c=node(*) MATCH a-[r?:CONNECTED]-b, b-[s?:CONNECTED]-c,c-[t?:CONNECTED]-a WHERE NOT(a = b) AND NOT(b = c) AND NOT(a = c) AND  b.type = {B} AND a.type = {B} AND c.type = {B} AND t IS NULL AND s IS NULL AND r IS NULL  RETURN a,b,c"
	cypher.execute(graph_db, query, {"B":"point"}, row_handler=print_three_points_row)
	#global count_row
	if count_row == 0:
		print(" Not found 3 points, hence exiting from this function")
		return False

	#Join these points and add info to the triangle TODO  (Please fill the values of angle and length in the below code)
	
	randomSet = check_saveObjFigure("Triangle_three_existing_points")
    	print( "first selectedPoint is " +randomSet[0][0])
    	print( "second selectedPoint is " +randomSet[0][1])
    	print( "third selectedPoint is " +randomSet[0][2])
	    
	query = "START a=node(*) WHERE a.type = {A} AND a.name = {B} return a"
	data,metadata = cypher.execute(graph_db, query, {"A" :"point","B":randomSet[0][0]})
	node1 = data[0][0]
    	
	query = "START a=node(*) WHERE a.type = {A} AND a.name = {B} return a"
	data,metadata = cypher.execute(graph_db, query, {"A" :"point","B":randomSet[0][1]})
	node2 = data[0][0]

	query = "START a=node(*) WHERE a.type = {A} AND a.name = {B} return a"
	data,metadata = cypher.execute(graph_db, query, {"A" :"point","B":randomSet[0][2]})
	node3 = data[0][0]

	node1.create_relationship(node2, "CONNECTED")
	node2.create_relationship(node3, "CONNECTED")
	node3.create_relationship(node1, "CONNECTED")

	sideName1 = node1["name"] + node2["name"]
	sideName2 = node2["name"] + node3["name"]
	sideName3 = node1["name"] + node3["name"]

	node_a, node_b, node_c = graph_db.create(
    {"name": sideName1, "value":quesArray[0],"type":"line"},
    {"name": sideName2, "value":quesArray[1],"type":"line"},
    {"name": sideName3, "value":quesArray[2],"type":"line"})
	
	node_a.create_relationship_to(node_b, "ANGLE")
	node_b.create_relationship_to(node_c, "ANGLE")
	node_c.create_relationship_to(node_a, "ANGLE")

	triangleName = node1["name"] + node2["name"] + node3["name"]
	addTriangle(triangleName)
	count_row = 0
	print("exiting from check_join_Existing_three_non_collinear_non_connected_Points function")
	return True
def addPointOnLine(pointName,cordx,cordy,lineName, line1Length, line2Length,node_triangle):
	print("Inside addPointOnLine function and lineName is "+ lineName + " and pointName is "+pointName)
    
	#Getting the node of line for making collinear relationship
	query = "START z=node(*) WHERE z.name={A} AND z.type = {B} RETURN z"
	data, metadata = cypher.execute(graph_db, query, {"A": lineName, "B":"line"})
	temp = data[0][0]
	print(temp["name"])

    #Create two nodes for two new lines made
	line1Name = lineName[0]+pointName
	line2Name = lineName[1]+pointName
	node_line1new, = graph_db.create({"name": line1Name, "value":line1Length,"type":"line"})
	node_line2new, = graph_db.create({"name": line2Name, "value":line2Length,"type":"line"})
    
    #Create a new node for point
	node_new, = graph_db.create({"name": pointName, "cordx":cordx,"cordy":cordy,"type":"point"})
    
    #Get othernodes of given line
	nodeCollection  = temp.get_related_nodes(0,"CONTAIN")
	for node in nodeCollection:
		node.create_relationship_to(node_new, "COLLINEAR")
		node.create_relationship_to(node_new, "MIDPOINT")
		graph_db.get_or_create_relationships((node, "CONNECTED",node_new))
 

    #Create new relationships between these two new lines and points
	node_line1new.create_relationship_to(node_new, "CONTAIN")
	node_line2new.create_relationship_to(node_new, "CONTAIN")
    
	for node in nodeCollection:
		if(node["name"] == lineName[0]):	node_line1new.create_relationship_to(node, "CONTAIN")
		if(node["name"] == lineName[1]):	node_line2new.create_relationship_to(node, "CONTAIN")
    

        #Create new relationships between these two new lines and triangle
	node_triangle.create_relationship_to(node_line1new, "INDIRECTLY_HAS")
	node_triangle.create_relationship_to(node_line2new, "INDIRECTLY_HAS")
	
	print("Exiting addPointOnLine function")
def addTriangle(triangleName):
    print("Inside addTriangleName function")
	
    #This function will save triangle which will help in checking changeTriangle
    #save_triangle.append(triangleName)
	
    
    node_new, = graph_db.create({"name":triangleName,"type":"triangle"})
  
    node_root.create_relationship_to(node_new, "KD1")
  
    side1 = triangleName[0]+ triangleName[1]
    side4 = triangleName[1]+ triangleName[0]
    side5 = triangleName[2]+ triangleName[1]
    side6 = triangleName[0]+ triangleName[2]
    side2 = triangleName[1]+ triangleName[2]
    side3 = triangleName[2]+ triangleName[0]
    
    query = "START z=node(*) WHERE z.name={A} OR z.name={B}  RETURN z"
    data, metadata = cypher.execute(graph_db, query, {"A": side1, "B":side4})
    temp1 = data[0][0]
    print(temp1["name"])

    query = "START z=node(*) WHERE z.name={A} OR z.name={B} RETURN z"
    data, metadata = cypher.execute(graph_db, query, {"A": side2, "B":side5})
    temp2 = data[0][0]
    print(temp2["name"])

    query = "START z=node(*) WHERE z.name={A} OR z.name={B} RETURN z"
    data, metadata = cypher.execute(graph_db, query, {"A": side3, "B":side6})
    temp3 = data[0][0]
    print(temp3["name"])

    #graph_db.get_or_create_relationships(node_new, "HAS",temp1)
    node_new.create_relationship_to(temp1, "HAS")
    node_new.create_relationship_to(temp2, "HAS")
    node_new.create_relationship_to(temp3, "HAS")
    #graph_db.get_or_create_relationships(node_new, "HAS",temp2)
    #graph_db.get_or_create_relationships(node_new, "HAS",temp3)
    print("Exiting addTriangleName function")
from __future__ import print_function

# Import Neo4j modules
from py2neo import neo4j, cypher
from macro import graph_db, node_root

# Attach to the graph db instance
#graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

#graph_db.clear()
#Create root node
#root = "Root"
#node_root, = graph_db.create({"name":root,"type":"node"})

node_kd2, = graph_db.create({"name": "THEOREM1","type":"node"})
node_root.create_relationship_to(node_kd2, "KD2")
node_triangle, = graph_db.create({"name": "ABC","type":"triangle"})
node_kd2.create_relationship_to(node_triangle, "Pythagorean")

# Create three nodes
node_a, node_b, node_c = graph_db.create(
    {"name": "AB","type":"line"},
    {"name": "BC","type":"line"},
    {"name": "CA","type":"line"}
)

# Join the nodes with a HAS relationship
rel_triangle_a = node_triangle.create_relationship_to(node_a, "HAS")
rel_triangle_b = node_triangle.create_relationship_to(node_b, "HAS")
rel_triangle_c = node_triangle.create_relationship_to(node_c, "HAS")
def addLineInTriangle(lineName, length, ang1, ang2,ang3,ang4,node_triangle):
	print("Inside addLineInTriangle function")
	
	node_new, = graph_db.create({"name": lineName, "value":length,"type":"line"})
    
	node_triangle.create_relationship_to(node_new, "INDIRECTLY_HAS")
 
	query = "START z=node(*) WHERE z.name={A} RETURN z"
	data, metadata = cypher.execute(graph_db, query, {"A": lineName[0]})
	temp = data[0][0]
	print(temp["name"])


	query = "START z=node(*) WHERE z.name={A} RETURN z"
	data, metadata = cypher.execute(graph_db, query, {"A": lineName[1]})
	temp1 = data[0][0]
	print(temp1["name"])

	node_new.create_relationship_to(temp, "CONTAIN")
	node_new.create_relationship_to(temp1, "CONTAIN")
	graph_db.get_or_create_relationships((temp, "CONNECTED",temp1))


	p1 = [temp["cordx"],temp["cordy"]]	
	p2 = [temp1["cordx"],temp1["cordy"]]	
	print("Find all possible triangles formed by adding this line TODO")
	query = "START z=node({B}), c=node({C}) MATCH z-[:CONNECTED]-b-[:CONNECTED]-c WHERE z.type={A} AND c.type={A} RETURN b"
        data, metadata = cypher.execute(graph_db, query, {"A": "point","B": temp.id,"C":temp1.id})
    	for row in data:
            print(row[0]["name"])
	    tempPoint = str(row[0]["name"])
	    if tempPoint in lineName: print("found point "+ tempPoint)
	    else: 
	        tempSide1 = lineName + tempPoint
	        print("calling addTriangle for " + tempSide1)
	        addTriangle(tempSide1)
		#Compute angles and addAngleRelationship TODO
		p3 = [row[0]["cordx"],row[0]["cordy"]]	
		print("Before calculating angle for first point")
		print(p1)
		print(p2)
		print(p3)
		ang1 = 10  #TODO angle_lines(p1, p2, p1, p3)
		temp.create_relationship_to(row[0], "ANGLE", {"value": ang1})
		graph_db.get_or_create_relationships((temp, "CONNECTED",row[0]))

	'''
	query = "START z=node({B}) MATCH z-[:CONNECTED]-b WHERE z.type={A} RETURN b"
    	data, metadata = cypher.execute(graph_db, query, {"A": "point","B": temp1.id})
    	for row in data:
        	print(row[0]["name"])
	 	tempPoint = str(row[0]["name"])
	if tempPoint in lineName: print("found point "+ tempPoint)
	else: 
	    tempSide1 = lineName + tempPoint
	    print("calling addTriangle for " + tempSide1)
	    addTriangle(tempSide1)
	#Compute angles and addAngleRelationship TODO
	p3 = [row[0]["cordx"],row[0]["cordy"]]	
	print("Before calculating angle for second point")
	print(p1)
	print(p2)
	print(p3)
	ang1 = 10  #TODO angle_lines(p1, p2, p2, p3)
	temp.create_relationship_to(row[0], "ANGLE", {"value": ang1})
	graph_db.get_or_create_relationships((temp, "CONNECTED",row[0]))

	#print("Add triangles via hardcoding")
	#addTriangle("PSR")
	#addTriangle("PSQ")
	'''
	print("Exiting addLineInTriangle function")