Example #1
0
 public void SaveGraphToFile(Stream stream, bool raise = true)
 {
     using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
     {
         string oriented = IsOrgraph ? "orgraph" : "graph";
         string weighted = IsWeighted ? "weighted" : "nonweighted";
         sw.WriteLine($"graphmodel {oriented} {weighted}");
         _models.Values.ToList().ForEach(m =>
         {
             var e = m as AEdgeModel;
             if (e == null)
             {
                 var v = m as AVertexModel;
                 sw.WriteLine($"[v] {v.Key} {v.Pos.x} {v.Pos.y}");
             }
             else
             {
                 sw.WriteLine($"[e] {e.Source.VertexStr} {e.Stock.VertexStr} {e.Weight}");
             }
         });
         sw.WriteLine("graphmodel end");
         Graph.SaveGraphToFile(sw);
     }
     if (raise)
     {
         FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.SaveGrapToFile));
     }
 }
Example #2
0
        public void UnionGraphs(string[] graphsNames, string newGrapName, bool raise = true)
        {
            if (graphsNames.Length < 2)
            {
                throw new Exception("количество графов для соединении должно быть не меньше двух");
            }
            if (!_storedGraphs.ContainsKey(graphsNames[0]) || !_storedGraphs.ContainsKey(graphsNames[1]))
            {
                throw new Exception("Некорректное имя/имена грфа");
            }
            var res = _storedGraphs[graphsNames[0]].Union(_storedGraphs[graphsNames[1]]);

            for (int i = 2; i < graphsNames.Length; i++)
            {
                if (!_storedGraphs.ContainsKey(graphsNames[i]))
                {
                    throw new Exception("Некорректное имя/имена грфа");
                }
                res = res.Union(_storedGraphs[graphsNames[i]]);
            }
            _storedGraphs.Add(newGrapName, res);
            if (raise)
            {
                FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.UpdateStoredGraphs));
            }
        }
Example #3
0
        public List <GraphModel> RemoveVertexModel(AVertexModel vertexModel, bool raise = true)
        {
            var vertex = new TVertex();

            vertex.SetFromString(vertexModel.VertexStr);
            var retVal = Graph.RemoveVertex(vertex);
            List <GraphModel> removeModels = new List <GraphModel>();

            if (retVal == ReturnValue.Succsess)
            {
                removeModels.Add(vertexModel);
                _models.Remove(vertexModel.Key);
                _models.Values.ToList().ForEach(m =>
                {
                    var edge = m as AEdgeModel;
                    if (edge != null &&
                        (edge.Source.Key == vertexModel.Key ||
                         edge.Stock.Key == vertexModel.Key))
                    {
                        removeModels.Add(edge);
                        _models.Remove(edge.Key);
                    }
                });
                if (raise)
                {
                    FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.RemoveModel));
                }
                return(removeModels);
            }
            else
            {
                throw new Exception(retVal.ToString());
            }
        }
Example #4
0
        public List <GraphModel> RemoveGraphModels(List <GraphModel> models, bool raise = true)
        {
            List <AVertexModel> vertices = new List <AVertexModel>();
            List <AEdgeModel>   edges    = new List <AEdgeModel>();

            models.ForEach(m =>
            {
                var v = m as AVertexModel;
                if (v != null)
                {
                    vertices.Add(v);
                }
                else
                {
                    edges.Add((AEdgeModel)m);
                }
            });
            List <GraphModel> res = new List <GraphModel>();

            res.AddRange(edges.Select(e => RemoveEdgeModel(e, false)));
            res.AddRange(vertices.SelectMany(v => RemoveVertexModel(v, false)));
            if (raise)
            {
                FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.RemoveModels));
            }
            return(res);
        }
Example #5
0
        public GraphModel RemoveEdgeModel(AEdgeModel edgeModel, bool raise = true)
        {
            int weight = 0;

            if (edgeModel.Weight != null)
            {
                weight = int.Parse(edgeModel.Weight);
            }
            var source = new TVertex();
            var stock  = new TVertex();

            source.SetFromString(edgeModel.Source.VertexStr);
            stock.SetFromString(edgeModel.Stock.VertexStr);
            var    res = Graph.RemoveEdge(source, stock);
            string key = edgeModel.Key;

            if (res == ReturnValue.Succsess)
            {
                _models.Remove(key);
                if (raise)
                {
                    FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.RemoveModel));
                }
                return(edgeModel);
            }
            else
            {
                throw new Exception(res.ToString());
            }
        }
Example #6
0
        public void AddEdgeModel(AEdgeModel edgeModel, bool raise = true)
        {
            var weight = 0;

            if (edgeModel.Weight != null && edgeModel.Weight != "")
            {
                weight = int.Parse(edgeModel.Weight);
            }
            var source = new TVertex();
            var stock  = new TVertex();

            source.SetFromString(edgeModel.Source.VertexStr);
            stock.SetFromString(edgeModel.Stock.VertexStr);
            var res = Graph.AddEdge(source, stock, weight);

            if (res != ReturnValue.Succsess)
            {
                throw new Exception(res.ToString());
            }
            else
            {
                _models.Add(edgeModel.Key, edgeModel);
                if (raise)
                {
                    FieldUpdate?.Invoke(this, null);
                }
            }
        }
Example #7
0
 public void UnmarkGraphModels(bool raise = true)
 {
     _models.Values.ToList().ForEach(m => m.Marked = false);
     if (raise)
     {
         FieldUpdate?.Invoke(null, null);
     }
 }
Example #8
0
 public void CreateGraph(bool oriented, bool weighted, bool raise = true)
 {
     Graph   = new Graph <TVertex>(oriented, weighted);
     Status  = true;
     _models = new SortedDictionary <string, GraphModel>();
     if (raise)
     {
         FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.InitGraph));
     }
 }
Example #9
0
 public void Clear(bool raise = true)
 {
     Status  = false;
     _models = null;
     Graph   = null;
     if (raise)
     {
         FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.RemoveGraph));
     }
 }
Example #10
0
 public void RemoveStoredGraph(string name, bool raise = true)
 {
     if (_storedGraphs.ContainsKey(name))
     {
         _storedGraphs.Remove(name);
         if (raise)
         {
             FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.UpdateStoredGraphs));
         }
     }
 }
Example #11
0
 public void AddCurrentGraphInStoredGraphs(string name, bool raise = true)
 {
     if (Graph == null)
     {
         throw new Exception("Текущий граф еще не создан");
     }
     _storedGraphs.Add(name, new Graph <TVertex>(Graph));
     if (raise)
     {
         FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.UpdateStoredGraphs));
     }
 }
Example #12
0
        public bool SetColor(string key, RGBcolor color, bool raise = true)
        {
            GraphModel m;

            if (_models.ContainsKey(key) && !(m = _models[key]).Marked)
            {
                m.Color = color;
                if (raise)
                {
                    FieldUpdate?.Invoke(null, null);
                }
                return(true);
            }
            return(false);
        }
Example #13
0
        public bool SetWeightMark(string key, string mark, bool raise = true)
        {
            AEdgeModel edge;

            if (_models.ContainsKey(key) && (edge = _models[key] as AEdgeModel) != null)
            {
                edge.AddMark = mark;
                if (raise)
                {
                    FieldUpdate?.Invoke(null, null);
                }
                return(true);
            }
            return(false);
        }
Example #14
0
        public bool MarkGraphModel(string key, bool raise = true)
        {
            GraphModel m;

            if (_models.ContainsKey(key) && !(m = _models[key]).Marked)
            {
                m.Marked = true;
                if (raise)
                {
                    FieldUpdate?.Invoke(null, null);
                }
                return(true);
            }
            return(false);
        }
Example #15
0
        public bool SetWidth(string key, int width, bool raise = true)
        {
            GraphModel m;

            if (_models.ContainsKey(key) && !(m = _models[key]).Marked)
            {
                m.Width = width;
                if (raise)
                {
                    FieldUpdate?.Invoke(null, null);
                }
                return(true);
            }
            return(false);
        }
Example #16
0
        public void AddVertexModel(AVertexModel vertexModel, bool raise = true)
        {
            var vertex = new TVertex();

            vertex.SetFromString(vertexModel.VertexStr);
            var res = Graph.AddVertex(vertex);

            if (res == ReturnValue.Succsess)
            {
                _models.Add(vertexModel.Key, vertexModel);
                if (raise)
                {
                    FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.AddModel));
                }
            }
        }
Example #17
0
        public bool SetModelDefaultOptions(string key, bool raise = true)
        {
            GraphModel m;

            if (_models.ContainsKey(key) && !(m = _models[key]).Marked)
            {
                m.Color = GraphModel.DefaultColor;
                m.Width = GraphModel.DefaultWidth;
                if (raise)
                {
                    FieldUpdate?.Invoke(null, null);
                }
                return(true);
            }
            return(false);
        }
Example #18
0
 public void RefreshDefault(bool removeAddMark = true, bool raise = true)
 {
     UnmarkGraphModels(false);
     _models.Values.ToList().ForEach(m =>
     {
         m.Color = GraphModel.DefaultColor;
         m.Width = GraphModel.DefaultWidth;
         var e   = m as AEdgeModel;
         if (e != null && removeAddMark)
         {
             e.AddMark = null;
         }
     });
     if (raise)
     {
         FieldUpdate?.Invoke(null, null);
     }
 }
Example #19
0
        public void OpenGraphInFile(List <GraphModel> models, string graphDataText, bool raise = true)
        {
            bool res = true;

            Status = false;
            if (Graph != null)
            {
                res = UserInterface.AnswerTheQuestion("Граф уже создан!!! Вы действительно хотите открыть граф из файла? Текущий граф будет безвозвратно утерен.");
            }
            if (res)
            {
                _models = new SortedDictionary <string, GraphModel>();
                models.ForEach(m => _models.Add(m.Key, m));
                Graph  = new Graph <TVertex>(graphDataText);
                Status = true;
            }
            if (raise)
            {
                FieldUpdate?.Invoke(this, new ModelFieldUpdateArgs(FieldEvents.InitGraph));
            }
        }
Example #20
0
        public void MoveVertexModel(string key, vec2 newPos, bool raise = true)
        {
            GraphModel model;

            if (_models.ContainsKey(key) && (model = _models[key]) is AVertexModel)
            {
                ((AVertexModel)model).UpdatePos(newPos);
                _models.Values.ToList().ForEach(m =>
                {
                    AEdgeModel edge = m as AEdgeModel;
                    if (edge != null && (edge.Source.Key == key || edge.Stock.Key == key))
                    {
                        edge.RefreshPos();
                    }
                });
                if (raise)
                {
                    FieldUpdate?.Invoke(this, null);
                }
            }
        }
Example #21
0
 public void Refresh()
 {
     FieldUpdate?.Invoke(null, null);
 }