public ulong WriteGenericNode(StreamWriter sw, IGraphML_Node node, Dictionary <IGraphML_Node, ulong> dic) { if (!dic.ContainsKey(node)) { try { if (node.ID <= 0) { node.ID = NextID; } dic.Add(node, node.ID); Type type = node.GetType(); sw.WriteLine(" <Node Type=\"" + type.FullName + "\" ID=\"" + node.ID + "\">"); Dictionary <IGraphML_Node, string> relatedVar = new Dictionary <IGraphML_Node, string>(); Dictionary <IGraphML_List, string> lists = new Dictionary <IGraphML_List, string>(); //Properties PropertyInfo[] properties = type.GetProperties(propertyFlags); foreach (PropertyInfo property in properties) { if (!"ID".Equals(property.Name) && property.CanWrite && property.GetSetMethod(true).IsPublic) { if (property.PropertyType.GetInterface(strGraphList) != null) { lists.Add((IGraphML_List)property.GetValue(node, null), property.Name); } else if (property.PropertyType.GetInterface(strGraphNode) != null) { relatedVar.Add((IGraphML_Node)property.GetValue(node, null), property.Name); } else { try { object obj = property.GetValue(node, null); if (obj != null) { string str = obj.ToString(); if (!string.IsNullOrEmpty(str)) { sw.WriteLine(" '" + property.Name + "' = \"" + ReplaceBadChars(str) + "\""); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } } } //Fields FieldInfo[] fields = type.GetFields(fieldFlags); foreach (FieldInfo field in fields) { if (field.FieldType.GetInterface(strGraphList) != null) { lists.Add((IGraphML_List)field.GetValue(node), field.Name); } else if (field.FieldType.GetInterface(strGraphNode) != null) { relatedVar.Add((IGraphML_Node)field.GetValue(node), field.Name); } else { object obj = field.GetValue(node); if (obj != null) { string str = obj.ToString(); if (!string.IsNullOrEmpty(str)) { sw.WriteLine(" '" + field.Name + "' = \"" + ReplaceBadChars(str) + "\""); } } } } sw.WriteLine(" </Node>"); foreach (IGraphML_Node linkedNode in relatedVar.Keys) { ulong id = WriteGenericNode(sw, linkedNode, dic); sw.WriteLine(" <Relation Type=\"" + relatedVar[linkedNode] + "\" TO=\"" + id + "\" FROM=\"" + node.ID + "\">"); sw.WriteLine(" </Relation>"); } foreach (IGraphML_List list in lists.Keys) { foreach (GraphML_Node item in ((System.Collections.IEnumerable)list)) { ulong id = WriteGenericNode(sw, item, dic); sw.WriteLine(" <Relation Type=\"" + lists[list] + "\" TO=\"" + id + "\" FROM=\"" + node.ID + "\">"); sw.WriteLine(" </Relation>"); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } return(node.ID); }
public long AddNode(IGraphML_Node node) { long id = 0; try { //var nodeRef = client.Create(new Neo4jNode()); //client.Update(nodeRef, //var nodeRef = client.Create(node.GetType()); //id = nodeRef.Id; Type type = node.GetType(); /* * Dictionary<IGraphML_Node, string> relatedVar = new Dictionary<IGraphML_Node,string>(); * Dictionary<IGraphML_List, string> lists = new Dictionary<IGraphML_List, string>(); * * StringBuilder strB = new StringBuilder(); * strB.Append("Type : '" + type.FullName + "'"); * //Dictionary<string, string> properties = new Dictionary<string, string>(); * //properties.Add("Type", type.FullName); * * //Properties * PropertyInfo[] propertyInfos = type.GetProperties(propertyFlags); * foreach (PropertyInfo property in propertyInfos) * { * if (!"ID".Equals(property.Name) && property.CanWrite && property.GetSetMethod(true).IsPublic) * { * if (property.PropertyType.GetInterface(strGraphList) != null) * lists.Add((IGraphML_List)property.GetValue(node, null), property.Name); * else * if (property.PropertyType.GetInterface(strGraphNode) != null) * relatedVar.Add((IGraphML_Node)property.GetValue(node, null), property.Name); * else * { * try * { * object obj = property.GetValue(node, null); * if (obj != null) * { * string str = obj.ToString(); * if (!string.IsNullOrEmpty(str)) * properties.Add(property.Name, ReplaceBadChars(str)); * } * } * catch (Exception ex) * { * dbOptions.ConSole.WriteLine(ex.Message); * dbOptions.ConSole.WriteLine(ex.StackTrace); * } * } * } * } * * //Fields * FieldInfo[] fields = type.GetFields(fieldFlags); * foreach (FieldInfo field in fields) * { * if (field.FieldType.GetInterface(strGraphList) != null) * lists.Add((IGraphML_List)field.GetValue(node), field.Name); * else * if (field.FieldType.GetInterface(strGraphNode) != null) * relatedVar.Add((IGraphML_Node)field.GetValue(node), field.Name); * else * { * object obj = field.GetValue(node); * if (obj != null) * { * string str = obj.ToString(); * if (!string.IsNullOrEmpty(str)) * properties.Add(field.Name, ReplaceBadChars(str)); * } * } * } * * var request = new RestRequest("application/json", Method.POST); * //request.AddParameter("text/json",,ParameterType.RequestBody); * //request.AddBody(properties, * * foreach (IGraphML_Node linkedNode in relatedVar.Keys) * { * long id = AddNode(linkedNode, dic); * AddRelation(node.ID, id, relatedVar[linkedNode]); * } * * foreach (IGraphML_List list in lists.Keys) * { * foreach (GraphML_Node item in ((System.Collections.IEnumerable)list)) * { * long id = AddNode(item, dic); * AddRelation(node.ID, id, lists[list]); * } * }//*/ } catch (Exception ex) { results.dbOptions.ConSole.WriteLine(ex.Message); results.dbOptions.ConSole.WriteLine(ex.StackTrace); } return(id);// node.ID; }
public Result ImportGeneric(string fileName) { ulong headID = 0; Dictionary <ulong, IGraphML_Node> dic = new Dictionary <ulong, IGraphML_Node>(); string line = ""; //Result result = null; try { using (StreamReader sr = new StreamReader(fileName)) { line = sr.ReadLine(); IGraphML_Node currentObj = null; Type currentType = null; while (line != null && !line.StartsWith("</Graph>")) { line = line.Trim(); if (line.StartsWith("<Graph HeadID=")) { headID = ulong.Parse(line.Split('"')[1]); } if (line.StartsWith("<Node")) { string[] header = line.Split('"');//beginning type empty id bracket string type = header[1]; ulong id = ulong.Parse(header[3]); currentType = Type.GetType(type); currentObj = (IGraphML_Node)System.Activator.CreateInstance(currentType); currentObj.ID = id; dic.Add(id, currentObj); } if (line.StartsWith("<Relation")) { string[] splits = line.Split('"');//beginning type empty to empty from bracket string varName = splits[1]; object destination = dic[ulong.Parse(splits[3])]; object source = dic[ulong.Parse(splits[5])]; Type sourceType = source.GetType(); Type destinationType = destination.GetType(); System.Reflection.FieldInfo fi = sourceType.GetField(varName); if (fi == null) { System.Reflection.PropertyInfo pi = sourceType.GetProperty(varName); if (pi != null) { if (pi.PropertyType.GetInterface(strGraphList) != null) { try { var add = pi.PropertyType.GetMethod("Add"); add.Invoke(pi.GetValue(source, null), new[] { destination }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { pi.SetValue(source, destination, null); } } } else { if (fi.FieldType.GetInterface(strGraphList) != null) { try { var add = fi.FieldType.GetMethod("Add"); add.Invoke(fi.GetValue(source), new[] { destination }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { fi.SetValue(source, destination); } } } if (!line.StartsWith("<") && currentObj != null) { string[] splits = line.Split('\''); string varName = splits[1]; splits = line.Split('"'); string value = PutBackBadChars(splits[1]); System.Reflection.FieldInfo fi = currentType.GetField(varName); if (fi == null) { System.Reflection.PropertyInfo pi = currentType.GetProperty(varName); if (pi != null) { pi.SetValue(currentObj, ChangeType(value, pi.PropertyType), null); } } else { fi.SetValue(currentObj, ChangeType(value, fi.FieldType));//TODO check if it works for enum } } line = sr.ReadLine(); } sr.Close();//*/ }// } catch (System.Exception ex) { Console.WriteLine("Unexpected error in " + ex.TargetSite + " : " + ex.Message); Console.WriteLine("Stack Trace = " + ex.StackTrace); } Result result = dic[headID] as Result; return(result); }