public List <TypeWithProperty> StringParser(String current_string, IGraphDB myDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { current_string = current_string.Replace(" ", ""); List <TypeWithProperty> list = new List <TypeWithProperty>(); bool endFlag = false; int EndPos = 0; do { EndPos = current_string.IndexOf(','); if (EndPos == -1) { EndPos = current_string.Length; endFlag = true; } var typeVertexString = current_string.Substring(0, current_string.IndexOf('.')); IVertexType typeVertex = null; try { typeVertex = myDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(typeVertexString), (statistics, type) => type); } catch { throw new InvalidFunctionParameterException("edgeType", "Object reference not set to an instance of an object.", "null"); } var propertyIDString = current_string.Substring(current_string.IndexOf('.') + 1, EndPos - current_string.IndexOf('.') - 1); var property = typeVertex.GetPropertyDefinition(propertyIDString); if (property == null) { throw new InvalidFunctionParameterException("Property", "Property: " + propertyIDString + " not exist in VertexType:" + typeVertexString, "null"); } TypeWithProperty value = new TypeWithProperty(); value.PropertyDefinition = property; value.Type = typeVertex; if (!list.Contains(value)) { list.Add(value); } if (!endFlag) { current_string = current_string.Substring(EndPos + 1); } }while (endFlag != true); return(list); }
public List<TypeWithProperty> StringParser(String current_string, IGraphDB myDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { current_string = current_string.Replace(" ",""); List<TypeWithProperty> list = new List<TypeWithProperty>(); bool endFlag = false; int EndPos = 0; do { EndPos = current_string.IndexOf(','); if (EndPos == -1) { EndPos = current_string.Length; endFlag = true; } var typeVertexString = current_string.Substring(0, current_string.IndexOf('.')); IVertexType typeVertex = null; try { typeVertex = myDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(typeVertexString), (statistics, type) => type); } catch { throw new InvalidFunctionParameterException("edgeType", "Object reference not set to an instance of an object.", "null"); } var propertyIDString = current_string.Substring(current_string.IndexOf('.') + 1, EndPos - current_string.IndexOf('.') - 1); var property = typeVertex.GetPropertyDefinition(propertyIDString); if (property==null) throw new InvalidFunctionParameterException("Property", "Property: " + propertyIDString + " not exist in VertexType:" + typeVertexString, "null"); TypeWithProperty value = new TypeWithProperty(); value.PropertyDefinition = property; value.Type = typeVertex; if (!list.Contains(value)) list.Add(value); if (!endFlag) current_string = current_string.Substring(EndPos + 1); } while (endFlag != true); return list; }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myDBObject, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialisation List<TypeWithProperty> ObjectList = new List<TypeWithProperty>(); var str = myParams[0].Value.ToString(); var method = Convert.ToBoolean(myParams[1].Value); if (str == "") throw new InvalidFunctionParameterException("Input String", "Input String is leer", "null"); ObjectList = new TypeWithProperty().StringParser(str, myGraphDB, mySecurityToken, myTransactionToken); if (ObjectList.Count<2) throw new InvalidFunctionParameterException("Input String", "Input String has insufficient quantity objects", "null"); List<Tuple<double, List<Tuple<long,long>>, IVertexType, IVertexType>> all = new List<Tuple<double, List<Tuple<long,long>>, IVertexType, IVertexType>>(); Dictionary<Tuple<long, long>, List<List<Tuple<long, long>>>> allPath = new Dictionary<Tuple<long, long>, List<List<Tuple<long, long>>>>(); #endregion #region search Path between two any VertexTypes foreach (TypeWithProperty vertexStart in ObjectList) { foreach (TypeWithProperty vertexEnd in ObjectList) { if (vertexStart != vertexEnd && vertexStart.Type.ID!=vertexEnd.Type.ID) { if (method) { var output = this.ShortPath(vertexStart.Type, vertexEnd.Type); if (output != null) all.Add(output); } else { List<Tuple<long, long>> value = new List<Tuple<long, long>>(); value.Add(Tuple.Create(vertexStart.Type.ID, 0L)); this.DFS(vertexStart.Type, vertexEnd.Type, value); } } } } #endregion if (!method) { #region DFS #region path->allPath->PATH for better representation foreach (List<Tuple<long, long>> value in _path) { if (!allPath.ContainsKey(Tuple.Create(value.First().Item1, value.Last().Item1))) { List<List<Tuple<long, long>>> temp = new List<List<Tuple<long, long>>>(); temp.Add(value); allPath.Add(Tuple.Create(value.First().Item1, value.Last().Item1), temp); } else { allPath[Tuple.Create(value.First().Item1, value.Last().Item1)].Add(value); } } List<List<Tuple<long, long>>> PATH = new List<List<Tuple<long, long>>>(); foreach (KeyValuePair<Tuple<long, long>, List<List<Tuple<long, long>>>> value in allPath) { var paths = value.Value; foreach (List<Tuple<long, long>> abc in paths) { PATH.Add(abc); } } if (PATH.Count < 1) throw new InvalidFunctionParameterException("PATH", "PATH with DFS not found", "null"); #endregion #region all paths merge foreach (KeyValuePair<Tuple<long, long>, List<List<Tuple<long, long>>>> value in allPath) { var paths = value.Value; foreach (List<Tuple<long, long>> abc in paths) { var flag_ends = false; var iCount = 0; do { if (PATH.Count > 0) { var detekt = (PATH[iCount].Where(x => x.Item1 == value.Key.Item1).Count() > 0) && (PATH[iCount].Where(x => x.Item1 == value.Key.Item2).Count() > 0); //PATH[iCount].Contains(value.Key.Item2); if (!detekt) { var test = 0; foreach (long wert in ObjectList.Select(x => x.Type.ID)) { if (PATH[iCount].Where(x => x.Item1 == wert).Count() > 0) test++; } if (test == ObjectList.Count) flag_ends = true; else { if (PATH[iCount].Contains(abc.Where(x => x.Item2 == 0).First())) PATH.Add(PATH[iCount].Union(abc).ToList()); } } } else { flag_ends = true; } if (PATH.Count - 1 > iCount) iCount++; else flag_ends = true; } while (!flag_ends); } } #endregion #region redundance kill zone var jCount = 0; var flag_ends2 = false; do { var test = 0; test = 0; foreach (long value in ObjectList.Select(x => x.Type.ID)) { if (PATH[jCount].Where(x => x.Item1 == value).Count() > 0) test++; } if (test < ObjectList.Count) { PATH.Remove(PATH[jCount]); if (PATH.Count == 0 || PATH.Count == jCount) flag_ends2 = true; } else { if (PATH.Count - 1 > jCount) jCount++; else flag_ends2 = true; } } while (!flag_ends2); flag_ends2 = false; var iCount2 = 0; do { List<List<Tuple<long, long>>> delete = new List<List<Tuple<long, long>>>(); foreach (List<Tuple<long, long>> wert in PATH) { var flag = true; var flagEnd = true; var jCountB = 0; if (PATH[iCount2].Count == wert.Count) { do { if (!wert.Contains(PATH[iCount2][jCountB])) { flag = false; flagEnd = false; } if (PATH[iCount2].Count - 1 > jCountB) jCountB++; else flagEnd = false; } while (flagEnd); if (flag) if (flag && !PATH[iCount2].Equals(wert)) delete.Add(wert); } } if (delete.Count > 0)//(test > 1) { foreach (List<Tuple<long, long>> value in delete) PATH.Remove(value); if (PATH.Count == 0 || PATH.Count == iCount2) flag_ends2 = true; } else { if (PATH.Count - 1 > iCount2) iCount2++; else flag_ends2 = true; } } while (!flag_ends2); if (PATH.Count < 1) throw new InvalidFunctionParameterException("PATH", "PATH with DFS not found", "null"); var minPATH = PATH.Min(x => x.Count); var indexPATH = PATH.First(x => x.Count == minPATH); this.Helper(myGraphDB, mySecurityToken, myTransactionToken, ObjectList, indexPATH); } #endregion #endregion else { #region Dijkstra #region all shortest paths between two any VertexTypes to PATHShort for better representation List<List<Tuple<long, long>>> PATHShort = new List<List<Tuple<long, long>>>(); foreach (Tuple<double, List<Tuple<long, long>>, IVertexType, IVertexType> value in all) { PATHShort.Add(value.Item2); } if (PATHShort.Count < 1) throw new InvalidFunctionParameterException("PATH", "PATH with Dijkstra not found", "null"); #endregion #region shortest paths merge foreach (Tuple<double, List<Tuple<long, long>>, IVertexType, IVertexType> value in all) { var abc = value.Item2; bool flag_ends = false; int iCount = 0; do { if (PATHShort.Count > 0) { var detekt = (PATHShort[iCount].Where(x => x.Item1 == value.Item3.ID).Count() > 0) && (PATHShort[iCount].Where(x => x.Item1 == value.Item4.ID).Count() > 0); //PATH[iCount].Contains(value.Key.Item2); if (!detekt) { var test = 0; foreach (long wert in ObjectList.Select(x => x.Type.ID)) { if (PATHShort[iCount].Where(x => x.Item1 == wert).Count() > 0) test++; } if (test == ObjectList.Count) flag_ends = true; else { if (PATHShort[iCount].Contains(abc.Where(x => x.Item2 == 0).First())) PATHShort.Add(PATHShort[iCount].Union(abc).ToList()); } } } else { flag_ends = true; } if (PATHShort.Count - 1 > iCount) iCount++; else flag_ends = true; } while (!flag_ends); } #endregion #region redudance kill zone for shortest paths var jCount = 0; var flag_ends2 = false; do { var test = 0; test = 0; foreach (long value in ObjectList.Select(x => x.Type.ID)) { if (PATHShort[jCount].Where(x => x.Item1 == value).Count() > 0) test++; } if (test < ObjectList.Count) { PATHShort.Remove(PATHShort[jCount]); if (PATHShort.Count == 0||PATHShort.Count==jCount) flag_ends2 = true; } else { if (PATHShort.Count - 1 > jCount) jCount++; else flag_ends2 = true; } } while (!flag_ends2); flag_ends2 = false; var iCount2 = 0; do { List<List<Tuple<long, long>>> delete = new List<List<Tuple<long, long>>>(); foreach (List<Tuple<long, long>> wert in PATHShort) { var flag = true; var flagEnd = true; var jCountB = 0; if (PATHShort[iCount2].Count == wert.Count) { do { if (!wert.Contains(PATHShort[iCount2][jCountB])) { flag = false; flagEnd = false; } if (PATHShort[iCount2].Count - 1 > jCountB) jCountB++; else flagEnd = false; } while (flagEnd); if (flag && !PATHShort[iCount2].Equals(wert)) delete.Add(wert); } } if (delete.Count > 0)//(test > 1) { foreach (List<Tuple<long, long>> value in delete) PATHShort.Remove(value); if (PATHShort.Count == 0 || PATHShort.Count == iCount2) flag_ends2 = true; } else { if (PATHShort.Count - 1 > iCount2) iCount2++; else flag_ends2 = true; } } while (!flag_ends2); if (PATHShort.Count < 1) throw new InvalidFunctionParameterException("PATH", "PATH with Dijkstra not found", "null"); var minPATHShort = PATHShort.Min(x => x.Count); var indexPATHShort = PATHShort.First(x => x.Count == minPATHShort); this.Helper(myGraphDB, mySecurityToken, myTransactionToken, ObjectList,indexPATHShort); } #endregion #endregion #region Output var result = new ListCollectionWrapper(_stringPath.Select(x => x.Item1+" = "+ x.Item2)); this._path.Clear(); this._stringPath.Clear(); return new FuncParameter(result); #endregion }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myDBObject, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialisation List <TypeWithProperty> ObjectList = new List <TypeWithProperty>(); var str = myParams[0].Value.ToString(); var method = Convert.ToBoolean(myParams[1].Value); if (str == "") { throw new InvalidFunctionParameterException("Input String", "Input String is leer", "null"); } ObjectList = new TypeWithProperty().StringParser(str, myGraphDB, mySecurityToken, myTransactionToken); if (ObjectList.Count < 2) { throw new InvalidFunctionParameterException("Input String", "Input String has insufficient quantity objects", "null"); } List <Tuple <double, List <Tuple <long, long> >, IVertexType, IVertexType> > all = new List <Tuple <double, List <Tuple <long, long> >, IVertexType, IVertexType> >(); Dictionary <Tuple <long, long>, List <List <Tuple <long, long> > > > allPath = new Dictionary <Tuple <long, long>, List <List <Tuple <long, long> > > >(); #endregion #region search Path between two any VertexTypes foreach (TypeWithProperty vertexStart in ObjectList) { foreach (TypeWithProperty vertexEnd in ObjectList) { if (vertexStart != vertexEnd && vertexStart.Type.ID != vertexEnd.Type.ID) { if (method) { var output = this.ShortPath(vertexStart.Type, vertexEnd.Type); if (output != null) { all.Add(output); } } else { List <Tuple <long, long> > value = new List <Tuple <long, long> >(); value.Add(Tuple.Create(vertexStart.Type.ID, 0L)); this.DFS(vertexStart.Type, vertexEnd.Type, value); } } } } #endregion if (!method) { #region DFS #region path->allPath->PATH for better representation foreach (List <Tuple <long, long> > value in _path) { if (!allPath.ContainsKey(Tuple.Create(value.First().Item1, value.Last().Item1))) { List <List <Tuple <long, long> > > temp = new List <List <Tuple <long, long> > >(); temp.Add(value); allPath.Add(Tuple.Create(value.First().Item1, value.Last().Item1), temp); } else { allPath[Tuple.Create(value.First().Item1, value.Last().Item1)].Add(value); } } List <List <Tuple <long, long> > > PATH = new List <List <Tuple <long, long> > >(); foreach (KeyValuePair <Tuple <long, long>, List <List <Tuple <long, long> > > > value in allPath) { var paths = value.Value; foreach (List <Tuple <long, long> > abc in paths) { PATH.Add(abc); } } if (PATH.Count < 1) { throw new InvalidFunctionParameterException("PATH", "PATH with DFS not found", "null"); } #endregion #region all paths merge foreach (KeyValuePair <Tuple <long, long>, List <List <Tuple <long, long> > > > value in allPath) { var paths = value.Value; foreach (List <Tuple <long, long> > abc in paths) { var flag_ends = false; var iCount = 0; do { if (PATH.Count > 0) { var detekt = (PATH[iCount].Where(x => x.Item1 == value.Key.Item1).Count() > 0) && (PATH[iCount].Where(x => x.Item1 == value.Key.Item2).Count() > 0); //PATH[iCount].Contains(value.Key.Item2); if (!detekt) { var test = 0; foreach (long wert in ObjectList.Select(x => x.Type.ID)) { if (PATH[iCount].Where(x => x.Item1 == wert).Count() > 0) { test++; } } if (test == ObjectList.Count) { flag_ends = true; } else { if (PATH[iCount].Contains(abc.Where(x => x.Item2 == 0).First())) { PATH.Add(PATH[iCount].Union(abc).ToList()); } } } } else { flag_ends = true; } if (PATH.Count - 1 > iCount) { iCount++; } else { flag_ends = true; } }while (!flag_ends); } } #endregion #region redundance kill zone var jCount = 0; var flag_ends2 = false; do { var test = 0; test = 0; foreach (long value in ObjectList.Select(x => x.Type.ID)) { if (PATH[jCount].Where(x => x.Item1 == value).Count() > 0) { test++; } } if (test < ObjectList.Count) { PATH.Remove(PATH[jCount]); if (PATH.Count == 0 || PATH.Count == jCount) { flag_ends2 = true; } } else { if (PATH.Count - 1 > jCount) { jCount++; } else { flag_ends2 = true; } } }while (!flag_ends2); flag_ends2 = false; var iCount2 = 0; do { List <List <Tuple <long, long> > > delete = new List <List <Tuple <long, long> > >(); foreach (List <Tuple <long, long> > wert in PATH) { var flag = true; var flagEnd = true; var jCountB = 0; if (PATH[iCount2].Count == wert.Count) { do { if (!wert.Contains(PATH[iCount2][jCountB])) { flag = false; flagEnd = false; } if (PATH[iCount2].Count - 1 > jCountB) { jCountB++; } else { flagEnd = false; } }while (flagEnd); if (flag) { if (flag && !PATH[iCount2].Equals(wert)) { delete.Add(wert); } } } } if (delete.Count > 0) //(test > 1) { foreach (List <Tuple <long, long> > value in delete) { PATH.Remove(value); } if (PATH.Count == 0 || PATH.Count == iCount2) { flag_ends2 = true; } } else { if (PATH.Count - 1 > iCount2) { iCount2++; } else { flag_ends2 = true; } } }while (!flag_ends2); if (PATH.Count < 1) { throw new InvalidFunctionParameterException("PATH", "PATH with DFS not found", "null"); } var minPATH = PATH.Min(x => x.Count); var indexPATH = PATH.First(x => x.Count == minPATH); this.Helper(myGraphDB, mySecurityToken, myTransactionToken, ObjectList, indexPATH); } #endregion #endregion else { #region Dijkstra #region all shortest paths between two any VertexTypes to PATHShort for better representation List <List <Tuple <long, long> > > PATHShort = new List <List <Tuple <long, long> > >(); foreach (Tuple <double, List <Tuple <long, long> >, IVertexType, IVertexType> value in all) { PATHShort.Add(value.Item2); } if (PATHShort.Count < 1) { throw new InvalidFunctionParameterException("PATH", "PATH with Dijkstra not found", "null"); } #endregion #region shortest paths merge foreach (Tuple <double, List <Tuple <long, long> >, IVertexType, IVertexType> value in all) { var abc = value.Item2; bool flag_ends = false; int iCount = 0; do { if (PATHShort.Count > 0) { var detekt = (PATHShort[iCount].Where(x => x.Item1 == value.Item3.ID).Count() > 0) && (PATHShort[iCount].Where(x => x.Item1 == value.Item4.ID).Count() > 0); //PATH[iCount].Contains(value.Key.Item2); if (!detekt) { var test = 0; foreach (long wert in ObjectList.Select(x => x.Type.ID)) { if (PATHShort[iCount].Where(x => x.Item1 == wert).Count() > 0) { test++; } } if (test == ObjectList.Count) { flag_ends = true; } else { if (PATHShort[iCount].Contains(abc.Where(x => x.Item2 == 0).First())) { PATHShort.Add(PATHShort[iCount].Union(abc).ToList()); } } } } else { flag_ends = true; } if (PATHShort.Count - 1 > iCount) { iCount++; } else { flag_ends = true; } }while (!flag_ends); } #endregion #region redudance kill zone for shortest paths var jCount = 0; var flag_ends2 = false; do { var test = 0; test = 0; foreach (long value in ObjectList.Select(x => x.Type.ID)) { if (PATHShort[jCount].Where(x => x.Item1 == value).Count() > 0) { test++; } } if (test < ObjectList.Count) { PATHShort.Remove(PATHShort[jCount]); if (PATHShort.Count == 0 || PATHShort.Count == jCount) { flag_ends2 = true; } } else { if (PATHShort.Count - 1 > jCount) { jCount++; } else { flag_ends2 = true; } } }while (!flag_ends2); flag_ends2 = false; var iCount2 = 0; do { List <List <Tuple <long, long> > > delete = new List <List <Tuple <long, long> > >(); foreach (List <Tuple <long, long> > wert in PATHShort) { var flag = true; var flagEnd = true; var jCountB = 0; if (PATHShort[iCount2].Count == wert.Count) { do { if (!wert.Contains(PATHShort[iCount2][jCountB])) { flag = false; flagEnd = false; } if (PATHShort[iCount2].Count - 1 > jCountB) { jCountB++; } else { flagEnd = false; } }while (flagEnd); if (flag && !PATHShort[iCount2].Equals(wert)) { delete.Add(wert); } } } if (delete.Count > 0) //(test > 1) { foreach (List <Tuple <long, long> > value in delete) { PATHShort.Remove(value); } if (PATHShort.Count == 0 || PATHShort.Count == iCount2) { flag_ends2 = true; } } else { if (PATHShort.Count - 1 > iCount2) { iCount2++; } else { flag_ends2 = true; } } }while (!flag_ends2); if (PATHShort.Count < 1) { throw new InvalidFunctionParameterException("PATH", "PATH with Dijkstra not found", "null"); } var minPATHShort = PATHShort.Min(x => x.Count); var indexPATHShort = PATHShort.First(x => x.Count == minPATHShort); this.Helper(myGraphDB, mySecurityToken, myTransactionToken, ObjectList, indexPATHShort); } #endregion #endregion #region Output var result = new ListCollectionWrapper(_stringPath.Select(x => x.Item1 + " = " + x.Item2)); this._path.Clear(); this._stringPath.Clear(); return(new FuncParameter(result)); #endregion }