public void TestEncoderPassesUpToTopLayer() { Parameters p = NetworkTestHarness.GetParameters(); p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams()); p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42)); p.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE); Net.Network.Network n = Net.Network.Network.Create("test network", p) .Add(Net.Network.Network.CreateRegion("r1") .Add(Net.Network.Network.CreateLayer <IInference>("1", p) .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)) .Add(Net.Network.Network.CreateLayer <IInference>("2", p) .Add(Anomaly.Create(p))) .Add(Net.Network.Network.CreateLayer <IInference>("3", p) .Add(new TemporalMemory())) .Add(Net.Network.Network.CreateLayer <IInference>("4", p) .Add(new SpatialPooler()) .Add((MultiEncoder)MultiEncoder.GetBuilder().Name("").Build()))); Region r1 = n.Lookup("r1"); r1.Connect("1", "2").Connect("2", "3").Connect("3", "4"); Assert.IsNotNull(r1.Lookup("1").GetEncoder()); }
public void TestComputeRawAnomalyPartialNoMatch() { double score = Anomaly.ComputeRawAnomalyScore(new[] { 2, 3, 6 }, new[] { 3, 5, 7 }); Console.WriteLine((2.0 / 3.0)); Assert.AreEqual(score, 2.0 / 3.0, 0.001); }
public static void AddAnomalies(IEnumerable <AnomalyDto> anomalies) { using (var context = new MassDefectEntities()) { foreach (var anomalyDto in anomalies) { if (anomalyDto.OriginPlanet == null || anomalyDto.TeleportPlanet == null) { Console.WriteLine("Error: Invalid data"); } else { var originPlanet = PlanetStore.GetPlanetByName(anomalyDto.OriginPlanet); var teleportPlanet = PlanetStore.GetPlanetByName(anomalyDto.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { Console.WriteLine("Error: Invalid data"); } else { var anomaly = new Anomaly { OriginPlanetId = originPlanet.Id, TeleportPlanetId = teleportPlanet.Id }; context.Anomalies.Add(anomaly); Console.WriteLine($"Successfuly imported Anomaly between {originPlanet.Name} and {teleportPlanet.Name}"); } } } context.SaveChanges(); } }
/** * Creates a {@link Network} containing 2 {@link Region}s with multiple * {@link PALayer}s in each. * * @return a multi-region Network */ internal Network.Network CreateMultiRegionNetwork() { Parameters p = NetworkDemoHarness.GetParameters(); p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams()); return(Network.Network.Create("Network API Demo", p) .Add(Network.Network.CreateRegion("Region 1") .Add(Network.Network.CreateLayer("Layer 2/3", p) .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true) .Add(Anomaly.Create()) .Add(new TemporalMemory())) .Add(Network.Network.CreateLayer("Layer 4", p) .Add(new PASpatialPooler())) .Connect("Layer 2/3", "Layer 4")) .Add(Network.Network.CreateRegion("Region 2") .Add(Network.Network.CreateLayer("Layer 2/3", p) .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true) .Add(Anomaly.Create()) .Add(new TemporalMemory()) .Add(new PASpatialPooler())) .Add(Network.Network.CreateLayer("Layer 4", p) .Add(Sensor <FileInfo> .Create(FileSensor.Create, SensorParams.Create( SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-15m.csv"))))) .Connect("Layer 2/3", "Layer 4")) .Connect("Region 1", "Region 2")); }
private static void ImportAnomalies() { MassDefectContext context = new MassDefectContext(); var json = File.ReadAllText(AnomaliesPath); var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json); foreach (var anomaly in anomalies) { if ((context.Planets.Where(n => n.Name == anomaly.OriginPlanet).Count() == 0) || (context.Planets.Where(n => n.Name == anomaly.TeleportPlanet).Count() == 0)) { Console.WriteLine("Error: Invalid data."); continue; } var anomalyEntity = new Anomaly() { OriginPlanet = GetPlanetByName(anomaly.OriginPlanet, context), TeleportPlanet = GetPlanetByName(anomaly.TeleportPlanet, context) }; context.Anomalies.Add(anomalyEntity); context.SaveChanges(); Console.WriteLine($"Successfully imported anomaly."); } }
public static void AnomalyVictims() { using (MassDefectContext context = new MassDefectContext()) { string json = File.ReadAllText(AnomalyVictimsPath); IEnumerable <AnomalyVictimsDTO> anomalyVictimsDtos = JsonConvert.DeserializeObject <IEnumerable <AnomalyVictimsDTO> >(json); foreach (var anomalyVictimDto in anomalyVictimsDtos) { if (anomalyVictimDto.Id == null || anomalyVictimDto.Person == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } Anomaly anomalyEntity = GetAnomalyById(anomalyVictimDto.Id, context); Person personEntity = GetPersonByName(anomalyVictimDto.Person, context); if (anomalyEntity == null || personEntity == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } anomalyEntity.Victims.Add(personEntity); } context.SaveChanges(); } }
public void AddAnomaly(IAnomalyDto anomaly) { int originPlanetId; int teleportPlanetId; if (anomaly.OriginPlanetId == null) { originPlanetId = this.unitOfWork.PlanetsDbRepository.First(p => p.Name == anomaly.OriginPlanet).Id; } else { originPlanetId = int.Parse(anomaly.OriginPlanetId.ToString()); } if (anomaly.TeleportPlanetId == null) { teleportPlanetId = this.unitOfWork.PlanetsDbRepository.First(p => p.Name == anomaly.TeleportPlanet).Id; } else { teleportPlanetId = int.Parse(anomaly.TeleportPlanetId.ToString()); } Anomaly anomalyEntity = new Anomaly() { OriginPlanetId = originPlanetId, TeleportPlanetId = teleportPlanetId }; this.unitOfWork.AnomaliesDbRepository.Add(anomalyEntity); }
private static void ImportAnomalies(MassDefectContext context) { string jsonString = GetJsonString(AnomaliesPath); List <AnomalyDTO> anomaliesDtos = JsonConvert.DeserializeObject <List <AnomalyDTO> >(jsonString); foreach (var anomaliesDto in anomaliesDtos) { if (string.IsNullOrEmpty(anomaliesDto.OriginPlanet) || string.IsNullOrEmpty(anomaliesDto.TeleportPlanet)) { Console.WriteLine(ErrorMessage); } else { Anomaly anomaly = new Anomaly() { OriginPlanet = GetPlanetByName(anomaliesDto.OriginPlanet, context), TeleportPlanet = GetPlanetByName(anomaliesDto.TeleportPlanet, context) }; if (anomaly.OriginPlanet == null || anomaly.TeleportPlanet == null) { Console.WriteLine(ErrorMessage); } else { context.Anomalies.Add(anomaly); Console.WriteLine("Successfully imported anomaly."); } } } context.SaveChanges(); }
private static void ImportAnomalyAndVictims(XElement anomalyNode, MassDefectContext context) { XAttribute originPlanetName = anomalyNode.Attribute("origin-planet"); XAttribute teleportPlanetName = anomalyNode.Attribute("teleport-planet"); if (originPlanetName == null || teleportPlanetName == null) { Console.WriteLine(ErrorMessage); return; } Planet origin = context.Planets.SingleOrDefault(p => p.Name == originPlanetName.Value); Planet teleport = context.Planets.SingleOrDefault(p => p.Name == teleportPlanetName.Value); if (origin == null || teleport == null) { Console.WriteLine(ErrorMessage); return; } Anomaly anomaly = new Anomaly(origin, teleport); context.Anomalies.Add(anomaly); Console.WriteLine("Successfully imported anomaly."); ImportVictims(context, anomalyNode, anomaly); context.SaveChanges(); }
private static void ImportAnomalies(MassDefectContext context) { string json = File.ReadAllText(AnomaliesPath); IEnumerable <AnomalyDTO> anomalyDtos = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json); foreach (AnomalyDTO anomalyDto in anomalyDtos) { if (anomalyDto.OriginPlanet == null || anomalyDto.TeleportPlanet == null) { Console.WriteLine(ErrorMessage); continue; } Anomaly anomalyEntity = Mapper.Map <Anomaly>(anomalyDto); if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine(ErrorMessage); continue; } Console.WriteLine(SuccessfullyImportMessageAnomaly); context.Anomalies.Add(anomalyEntity); } context.SaveChanges(); }
private static void ImportAnomalyVictims() { var data = File.ReadAllText("../../../datasets/anomaly-victims.json"); var anomalyVictims = JsonConvert.DeserializeObject <IEnumerable <AnomalyVictimsDTO> >(data); using (var context = new MassDefectContext()) { foreach (var av in anomalyVictims) { if (av.Id == null || av.Person == null) { Console.WriteLine("Error: Invalid data."); continue; } Anomaly anomalyEntity = GetAnomalyById((int)av.Id, context); Person personEntity = GetPersonByName(av.Person, context); if (anomalyEntity == null || personEntity == null) { Console.WriteLine("Error: Invalid data."); continue; } anomalyEntity.Victims.Add(personEntity); } context.SaveChanges(); } }
private static void ImportAnomalyVictims(MassDefectContext context) { string json = File.ReadAllText(AnomalyVictimsPath); IEnumerable <AnomalyVicitimDTO> anomalyVicitimDtos = JsonConvert.DeserializeObject <IEnumerable <AnomalyVicitimDTO> >(json); foreach (AnomalyVicitimDTO anomalyVictimDto in anomalyVicitimDtos) { if (anomalyVictimDto.Id <= 0 || anomalyVictimDto.Person == null) { Console.WriteLine(ErrorMessage); continue; } Anomaly anomaly = context.Anomalies.FirstOrDefault(an => an.Id == anomalyVictimDto.Id); Person victim = context.Persons.FirstOrDefault(person => person.Name == anomalyVictimDto.Person); if (anomaly == null || victim == null) { Console.WriteLine(ErrorMessage); continue; } anomaly.Victims.Add(victim); } context.SaveChanges(); }
private static void ImportAnomalyToDatabase(AnomalyDto anomalyDto) { if (anomalyDto.OriginPlanet == null) { throw new ArgumentNullException("Origin planet cannot be null!"); } else if (anomalyDto.TeleportPlanet == null) { throw new ArgumentNullException("Teleport planet cannot be null!"); } using (var db = new MassDefectDatabaseContext()) { var originPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.OriginPlanet); var teleportPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { throw new ArgumentNullException("Origin and teleport planet doesn't exist in dbo.Planets"); } var newAnomaly = new Anomaly() { OriginPlanet = originPlanet, TeleportPlanet = teleportPlanet }; db.Anomalies.Add(newAnomaly); db.SaveChanges(); } }
private static void ImportAnomalies() { var context = new MassDefectContext(); var json = File.ReadAllText(AnomaliesPath); var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json); foreach (var anomaly in anomalies) { if (anomaly.OriginPlanet == null || anomaly.TeleportPlanet == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } var anomalyEntity = new Anomaly { OriginPlanet = GetPlanetByName(anomaly.OriginPlanet, context), TeleportPlanet = GetPlanetByName(anomaly.TeleportPlanet, context) }; if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } context.Anomalies.Add(anomalyEntity); Console.WriteLine(Constants.ImportUnnamedEntitySuccessMessage); } context.SaveChanges(); }
private static void ImportAnomalies() { var context = new MassDefectContext(); var json = File.ReadAllText(AnomaliesPath); var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomalyDto> >(json); foreach (var anomaly in anomalies) { if (anomaly.OriginPlanet == null || anomaly.TeleportPlanet == null) { //throw new ArgumentNullException("Error: Invalid data.”… "); Console.WriteLine("Error: Invalid data."); continue; } var anomalyEntity = new Anomaly { OriginPlanet = GetPlanetByNameOfPlanet(anomaly.OriginPlanet, context), TeleportPlanet = GetPlanetByNameOfPlanet(anomaly.TeleportPlanet, context) }; if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine("Error: Invalid data."); continue; } context.Anomalies.Add(anomalyEntity); Console.WriteLine($"Successfully imported Anomaly {anomaly.OriginPlanet}."); } context.SaveChanges(); }
private static void ImportAnomalies(UnitOfWork unit) { string json = File.ReadAllText(AnomaliesPath); IEnumerable <AnomaliesDto> anomaliesDtos = JsonConvert.DeserializeObject <IEnumerable <AnomaliesDto> >(json); foreach (var anomaliesDto in anomaliesDtos) { if (anomaliesDto.OriginPlanet == null || anomaliesDto.TeleportPlanet == null) { Console.WriteLine(Error); continue; } Anomaly anomaly = Mapper.Map <Anomaly>(anomaliesDto); if (anomaly.OriginPlanet == null || anomaly.TeleportPlanet == null) { Console.WriteLine(Error); continue; } unit.Anomalies.Add(anomaly); unit.Commit(); Console.WriteLine("Successfully imported anomaly."); } }
public static void Anomalies() { using (MassDefectContext context = new MassDefectContext()) { string json = File.ReadAllText(AnomaliesPath); IEnumerable <AnomalyDTO> anomaliesDtos = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json); foreach (AnomalyDTO anomalyDto in anomaliesDtos) { if (anomalyDto.OriginPlanet == null || anomalyDto.TeleportPlanet == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } Anomaly anomalyEntity = new Anomaly() { OriginPlanet = GetPlanetByName(anomalyDto.OriginPlanet, context), TeleportPlanet = GetPlanetByName(anomalyDto.TeleportPlanet, context) }; if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine(Constants.ImportErrorMessage); continue; } context.Anomalies.Add(anomalyEntity); Console.WriteLine(Constants.ImportUnnamedEntitySuccessMessage); } context.SaveChanges(); } }
private static void ImportAnomalyVictims(UnitOfWork unit) { string json = File.ReadAllText(AnomalyVictimsPath); IEnumerable <AnomalyVictimsDto> anomalyVictimsDtos = JsonConvert.DeserializeObject <IEnumerable <AnomalyVictimsDto> >(json); foreach (var anomalyVictimsDto in anomalyVictimsDtos) { if (anomalyVictimsDto.Person == null || anomalyVictimsDto.Id <= 0) { Console.WriteLine(Error); continue; } Anomaly anomaly = unit.Anomalies.First(anom => anom.Id == anomalyVictimsDto.Id); Person victim = unit.Persons.First(per => per.Name == anomalyVictimsDto.Person); if (anomaly == null || victim == null) { Console.WriteLine(Error); continue; } anomaly.Victims.Add(victim); unit.Commit(); } }
/// <summary> /// CreateTicketNC /// </summary> /// <param name="name">TicketNC</param> /// <returns>bool</returns> public bool CreateTicketNC(Domain.TicketNC model) { if (model == null) { return(false); } DataAccess.TicketNC ticketNC = Mapper.Map <Domain.TicketNC, DataAccess.TicketNC>(model); DataAccess.Anomaly anomaly = new Anomaly() { CreationDate = DateTime.Now, ref_ProductionAreaId = model.ref_ProductionAreaId }; using (var unitOfWork = new UnitOfWork(new QualityContext())) { unitOfWork.AnomalyRepository.Add(anomaly); if (unitOfWork.Complete() > 0 == false) { return(false); } ticketNC.Anomaly = anomaly; unitOfWork.TicketNCRepository.Add(ticketNC); return(unitOfWork.Complete() > 0); } }
private static void ImportAnomalies() { MassDefectContext context = new MassDefectContext(); var json = File.ReadAllText(Constants.AnomaliesPath); var anomalies = JsonConvert.DeserializeObject <IEnumerable <AnomalyDTO> >(json); foreach (AnomalyDTO anomaly in anomalies) { if (anomaly.OriginPlanet == null || anomaly.TeleportPlanet == null) { Console.WriteLine(Constants.ErrorMessage); continue; } Planet originPlanet = context.Planets.SingleOrDefault(p => p.Name == anomaly.OriginPlanet); Planet teleportPlanet = context.Planets.SingleOrDefault(p => p.Name == anomaly.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { Console.WriteLine(Constants.ErrorMessage); continue; } Anomaly anomalyEntity = new Anomaly(originPlanet, teleportPlanet); context.Anomalies.Add(anomalyEntity); context.SaveChanges(); Console.WriteLine("Successfully imported anomaly."); } }
private static void ImportAnomalyAndVictims(XElement anomalieNode, MassDefectContext context) { var teleportPlanet = anomalieNode.Attribute("teleport-planet"); var originPlanet = anomalieNode.Attribute("origin-planet"); if (teleportPlanet == null || originPlanet == null) { Console.WriteLine(ErrorMessage); return; } Anomaly anomalyEntity = new Anomaly() { OriginPlanet = GetPlanetByName(originPlanet.Value, context), TeleportPlanet = GetPlanetByName(teleportPlanet.Value, context) }; if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine(ErrorMessage); return; } context.Anomalies.Add(anomalyEntity); Console.WriteLine(SuccessfullMessage); IEnumerable <XElement> victims = anomalieNode.XPathSelectElements("victims/victims"); foreach (XElement victim in victims) { ImportVictim(victim, context, anomalyEntity); } context.SaveChanges(); }
private static void ImportAnomalyVictims() { MassDefectContext context = new MassDefectContext(); string json = File.ReadAllText(Constants.AnomalyVictimsPath); IEnumerable <AnomalyVictimDTO> anomalyVictims = JsonConvert.DeserializeObject <IEnumerable <AnomalyVictimDTO> >(json); foreach (AnomalyVictimDTO anomalyVictim in anomalyVictims) { if (anomalyVictim.Id == null || anomalyVictim.Person == null) { Console.WriteLine(Constants.ErrorMessage); continue; } Anomaly anomaly = context.Anomalies.SingleOrDefault(a => a.Id == anomalyVictim.Id); Person person = context.People.SingleOrDefault(p => p.Name == anomalyVictim.Person); if (anomaly == null || person == null) { Console.WriteLine(Constants.ErrorMessage); continue; } anomaly.Victims.Add(person); context.SaveChanges(); } }
/** * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link PALayer}. However * this basic network contains all algorithmic components. * * @return a basic Network */ internal Network.Network CreateBasicNetwork() { Parameters p = NetworkDemoHarness.GetParameters(); p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams()); p.SetParameterByKey(Parameters.KEY.MIN_THRESHOLD, 22); // 22 18 p.SetParameterByKey(Parameters.KEY.ACTIVATION_THRESHOLD, 16); // 18 p.SetParameterByKey(Parameters.KEY.STIMULUS_THRESHOLD, 0.0); // 0.0 p.SetParameterByKey(Parameters.KEY.CELLS_PER_COLUMN, 1); // 1 Region r = Network.Network.CreateRegion("Region 1"); PALayer <IInference> l = new PALayer <IInference>("Layer 2/3", null, p); l.SetPADepolarize(0.0); // 0.25 l.SetVerbosity(0); PASpatialPooler sp = new PASpatialPooler(); string infile = "rec-center-15m.csv"; // This is how easy it is to create a full running Network! return(Network.Network.Create("Network API Demo", p) .Add(r.Add(l.AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true) .Add(Anomaly.Create()) .Add(new TemporalMemory()) .Add(sp) .Add(Sensor <FileInfo> .Create(FileSensor.Create, SensorParams.Create(SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), infile))))))); }
private static Anomaly GetAnomalyById(string anomalyVictimId, MassDefectContext context) { int Id = int.Parse(anomalyVictimId); Anomaly anomalyEntity = context.Anomalies.FirstOrDefault(anomaly => anomaly.Id == Id); return(anomalyEntity); }
public void TestAnomalyCumulative() { Parameters @params = Parameters.Empty(); @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE); @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_WINDOW_SIZE, 3); @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_USE_MOVING_AVG, true); Anomaly anomalyComputer = Anomaly.Create(@params); object[] predicted = { new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 } }; object[] actual = { new[] { 1, 2, 6 }, new[] { 1, 2, 6 }, new[] { 1, 4, 6 }, new[] { 10, 11, 6 }, new[] { 10, 11, 12 }, new[] { 10, 11, 12 }, new[] { 10, 11, 12 }, new[] { 1, 2, 6 }, new[] { 1, 2, 6 } }; double[] anomalyExpected = { 0.0, 0.0, 1.0 / 9.0, 3.0 / 9.0, 2.0 / 3.0, 8.0 / 9.0, 1.0, 2.0 / 3.0, 1.0 / 3.0 }; for (int i = 0; i < 9; i++) { double score = anomalyComputer.Compute((int[])actual[i], (int[])predicted[i], 0, 0); Assert.AreEqual(anomalyExpected[i], score, 0.01); } }
private static void ImportAnomalyAndVictims(XElement anomalyNode, MassDefectContext context) { var originPlanetName = anomalyNode.Attribute("origin-planet"); var teleportPlanetName = anomalyNode.Attribute("teleport-planet"); if ((context.Planets.Where(n => n.Name == originPlanetName.Value).Count() > 0) && (context.Planets.Where(n => n.Name == teleportPlanetName.Value).Count() > 0) && (originPlanetName.Value != null) && (teleportPlanetName.Value != null)) { var anomalyEntity = new Anomaly() { OriginPlanet = GetPlanetByName(originPlanetName.Value, context), TeleportPlanet = GetPlanetByName(teleportPlanetName.Value, context) }; context.Anomalies.Add(anomalyEntity); var victims = anomalyNode.XPathSelectElements("victims/victim"); foreach (var victim in victims) { ImportVictim(victim, context, anomalyEntity); } context.SaveChanges(); Console.WriteLine("Successfully imported anomaly and victims."); } else { Console.WriteLine("Error: Invalid data."); } }
public void Setup() { Parameters @params = Parameters.Empty(); @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD); an = (AnomalyLikelihood)Anomaly.Create(@params); }
private static void ImportAnomalyAndVictims(XElement anomalyNode, MassDefectContext context) { var originPlanetName = anomalyNode.Attribute("origin-planet"); var teleportPlanetName = anomalyNode.Attribute("teleport-planet"); if (originPlanetName == null || teleportPlanetName == null) { Console.WriteLine("Error: Invalid data."); return; } var anomalyEntity = new Anomaly { OriginPlanet = GetPlanetByName(originPlanetName.Value, context), TeleportPlanet = GetPlanetByName(teleportPlanetName.Value, context) }; if (anomalyEntity.OriginPlanet == null || anomalyEntity.TeleportPlanet == null) { Console.WriteLine("Error: Invalid data."); return; } context.Anomalies.Add(anomalyEntity); Console.WriteLine("Successfully imported anomaly."); var victims = anomalyNode.XPathSelectElements("victims/victim"); foreach (var victim in victims) { ImportVictim(victim, context, anomalyEntity); } context.SaveChanges(); }
public static void AddAnomalieWithVictims(List <AnomalyWithVictimsDto> anomalies) { using (var context = new MassDefectEntities()) { foreach (var anomalyWithVictimsDto in anomalies) { var originPlanet = PlanetStore.GetPlanetByName(anomalyWithVictimsDto.OriginPlanet); var teleportPlanet = PlanetStore.GetPlanetByName(anomalyWithVictimsDto.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { Console.WriteLine("Error: Invalid data"); } else { var anomaly = new Anomaly { OriginPlanetId = originPlanet.Id, TeleportPlanetId = teleportPlanet.Id }; context.Anomalies.Add(anomaly); foreach (var victimName in anomalyWithVictimsDto.Victims) { var victim = context.Persons.FirstOrDefault(p => p.Name == victimName); if (victim != null) { anomaly.Victims.Add(victim); } } } } } }
private static void ImportAnomalies(MassDefectContext context) { var text = File.ReadAllText(AnomaliesPath); var anomalies = JsonConvert.DeserializeObject <ICollection <AnomalyDTO> >(text); foreach (var a in anomalies) { var originPlanet = context.Planets.FirstOrDefault(x => x.Name == a.OriginPlanet); var teleportPlanet = context.Planets.FirstOrDefault(x => x.Name == a.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { Console.WriteLine($"Error: Invalid data."); continue; } Anomaly anomaly = new Anomaly() { OriginPlanetId = originPlanet.Id, TeleportPlanetId = teleportPlanet.Id, }; context.Anomalies.Add(anomaly); Console.WriteLine("Successfully imported anomaly."); } context.SaveChanges(); }