Пример #1
0
 /// <summary>
 /// Adds a link to the entity.
 /// </summary>
 /// <param name="link">The link.</param>
 /// <exception cref="T:System.ArgumentNullException">link</exception>
 public void AddLink(Link link)
 {
     if (link == null)
     {
         throw new ArgumentNullException("link");
     }
     if (Links == null)
     {
         Links = new List <Link>();
     }
     if (link is SelfLink)
     {
         this.HRef = link.HRef;
         if (this.GetType().IsGenericType&& typeof(IEnumerable).IsAssignableFrom(this.GetType()) &&
             typeof(Resource).IsAssignableFrom(this.GetType().GetGenericArguments()[0]))
         {
             this.Rel = this.GetType().GetGenericArguments()[0].Name;
         }
         else
         {
             this.Rel = this.GetType().Name;
         }
         if (Links.Count(x => x.Rel == link.Rel) > 0)
         {
             Links.Remove(Links.First(x => x.Rel == link.Rel));
         }
     }
     if (Links.Count(x => x.Rel == link.Rel && x.HRef == link.HRef) == 0)
     {
         Links.Add(link);
     }
 }
Пример #2
0
 public ILink this[string key] => Links.First(l => l.Name == key || l.Url == key);
Пример #3
0
        private void Load(string Path)
        {
            if (File.Exists(Path))
            {
                canNetwork.Items.Clear();
                NetworkParameters = Newtonsoft.Json.JsonConvert.DeserializeObject <SimulationParameters>(File.ReadAllText(Path));

                var networkDir = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Path), System.IO.Path.GetFileNameWithoutExtension(Path));

                try
                {
                    var nodes = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Node> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "nodes.json")));
                    canNetwork.AddItems(nodes);
                    NodeStore.Nodes = nodes;

                    try
                    {
                        var links = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Link> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "links.json")));
                        canNetwork.AddItems(links);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Links are corrupt.");
                    }

                    try
                    {
                        var streams = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Stream> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "streams.json")));
                        canNetwork.AddItems(streams);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Streams are corrupt.");
                    }

                    try
                    {
                        var domains = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Domain> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "domains.json")));
                        canNetwork.AddItems(domains);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Domains are corrupt.");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nodes are corrupt.");
                }

                try
                {
                    DisplayProperties.Reset();
                    var displayProperties = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Tuple <Type, object> > >(File.ReadAllText(System.IO.Path.Combine(networkDir, "display.json")));

                    foreach (var property in displayProperties)
                    {
                        if (property.Value.Item1.IsEnum)
                        {
                            dynamic value = Enum.ToObject(property.Value.Item1, property.Value.Item2);

                            var field = typeof(DisplayProperties).GetField(property.Key);
                            field.SetValue(null, value);
                        }
                        else
                        {
                            var field = typeof(DisplayProperties).GetField(property.Key);
                            field.SetValue(null, property.Value.Item2);
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Display properties are corrupt.");
                }

                // JSON can't store circular reference to parent or reference to actual Node / Link / Stream / Domain object.
                foreach (var trace in NetworkParameters.Traces)
                {
                    foreach (var attribute in trace.Attributes)
                    {
                        attribute.Parent = trace;
                        if (attribute.Element is Node)
                        {
                            attribute.Element = NodeStore.Nodes.First((x) => x.Name == (attribute.Element as Node).Name);
                        }
                        else if (attribute.Element is Link)
                        {
                            attribute.Element = Links.First((x) => x.Name == (attribute.Element as Link).Name);
                        }
                        else if (attribute.Element is Stream)
                        {
                            attribute.Element = Streams.First((x) => x.Name == (attribute.Element as Stream).Name);
                        }
                        else if (attribute.Element is Domain)
                        {
                            attribute.Element = Domains.First((x) => x.Name == (attribute.Element as Domain).Name);
                        }
                    }
                }

                // Fix up the links in plot attributes.
                foreach (var plot in NetworkParameters.Plots)
                {
                    foreach (var attribute in plot.Attributes)
                    {
                        attribute.TraceParameter = NetworkParameters.Traces.First((x) => x.Name == attribute.TraceParameter.Name);
                    }
                }

                canNetwork.Invalidate();
                NodeStore.Nodes = null;
                SetTitle();
            }
            else
            {
                MessageBox.Show($"Cannot find file \"{Path}\"");
            }
        }