public async Task GenerateLogs(Bos bos) { Console.WriteLine($"wood: {bos.Id} writes log -start"); int langsteLogStringList = 0; string fileName = $@"{bos.Id}_log.txt"; List <string> logsPerBos = new List <string>(); foreach (Aap aap in bos.Apen.Values) { if (aap.LogsPerAap.Count > langsteLogStringList) { langsteLogStringList = aap.LogsPerAap.Count; } } //Nu hebben we hebben we het meeste aantal sprongen voor een aap uit het bos. for (int i = 0; i < langsteLogStringList; i++) { foreach (Aap aap in bos.Apen.Values) { if (i < aap.LogsPerAap.Count) { logsPerBos.Add(aap.LogsPerAap[i]); } } } //Nu hebben we de List met strings in juiste volgorde om de log file mee te maken. System.IO.File.WriteAllLines(fileName, logsPerBos); logsPerBos.Clear(); Console.WriteLine($"wood: {bos.Id} writes log -end"); }
public async Task AddWoodRecords(Bos bos) { Console.WriteLine("start adding Wood to DB"); SqlConnection connection = GetConnection(); string query = "INSERT INTO woodrecords(woodid, treeid, x, y) VALUES(@woodid, @treeid, @x, @y)"; foreach (Boom boom in bos.Bomen) { using SqlCommand command = connection.CreateCommand(); connection.Open(); try { command.Parameters.Add(new SqlParameter("@woodid", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@treeid", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@x", SqlDbType.Float)); command.Parameters.Add(new SqlParameter("@y", SqlDbType.Float)); command.CommandText = query; command.Parameters["@woodid"].Value = bos.Id; command.Parameters["@treeid"].Value = boom.Id; command.Parameters["@x"].Value = boom.X; command.Parameters["@y"].Value = boom.Y; command.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex); } finally { connection.Close(); } } Console.WriteLine("stop adding Wood to DB"); }
public async Task StartCalculatingEscapeRoutes(Bos bos) { foreach (Aap aap in bos.Apen.Values) { await OntsnappingsRoutePerAap(aap); } }
public Aap(string naam, Bos bos) { this.Bos = bos; this.Id = aapId; this.Naam = naam; aapId++; }
public static async Task MaakTekstBestand(Bos bos, string path) { using (StreamWriter sw = new StreamWriter(Path.Combine(path, $"{bos.id}_log.txt"))) { await sw.WriteLineAsync(bos.log.getSprongenLog()); } }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Bos bos1 = new Bos(1000, -1000); bos1.CreateBos(50, 5); Bos bos2 = new Bos(500, -500); bos2.CreateBos(30, 3); Bos bos3 = new Bos(1000, -1000); bos3.CreateBos(40, 4); DatabankTools databankTools = new DatabankTools(); databankTools.ClearDatabase(); //ASYNC TESTCODE: Taken taken = new Taken(); List <Task> tasks = new List <Task>(); tasks.Add(Task.Run(() => taken.AllAsyncStuff(bos1))); tasks.Add(Task.Run(() => taken.AllAsyncStuff(bos2))); //Task.WaitAll(tasks[1]); //Kan gebruikt worden stel dat we het anders async willen laten werken. tasks.Add(Task.Run(() => taken.AllAsyncStuff(bos3))); Task.WaitAll(tasks.ToArray()); stopwatch.Stop(); Console.WriteLine(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); Console.WriteLine("______________________________________________"); Console.ReadKey(); }
public static void MakeXMlFile(string path, Bos bos) { string xml = GetDataTables(bos).GetXml(); using (StreamWriter sw = new StreamWriter(Path.Combine(path, "EscapeFromTheWoods.xml"))) { sw.Write(xml); } }
public static void Start(Bos bos) { KnownFolder downloads = new KnownFolder(KnownFolderType.Downloads); Databank db = new Databank(@"Data Source=DESKTOP-OF28PIK\SQLEXPRESS;Initial Catalog=EscapeFromTheWoods;Integrated Security=True"); BitmapMaker bmm = new BitmapMaker(bos); Task datab = db.InsertAll(bos); Task bitmap = Task.Run(() => bmm.MaakBitmap(downloads.Path)); Task txt = TekstBestand.MaakTekstBestand(bos, downloads.Path); //Databank.MakeXMlFile(downloads.Path, bos); Task.WaitAll(datab, txt, bitmap); }
static void Main(string[] args) { Bos bos = new Bos(new List <int>() { 10, 10 }, 5, 90, 5); bos.Start(); OutputFactory.Start(bos); //Console.Clear(); //bos.log.PrintLog(); //BitmapMaker bm = new BitmapMaker(bos); //bm.maakBitmap(@"C:\Users\Biebem\Downloads"); }
public async Task Escape(Bos bos, Stopwatch stopwatch) { Console.WriteLine($"start escape wood {bos.Id}"); DatabaseManager dbm = new DatabaseManager(@"Data Source=LAPTOP-1U6AQSEQ\SQLEXPRESS;Initial Catalog=EscapeFromTheWoods;Integrated Security=True"); List <Task> tasks = new List <Task>(); tasks.Add(Task.Run(() => dbm.AddWoodRecords(bos))); Random rnd = new Random(); for (int i = 0; i < bos.Apen.Count; i++) { Console.WriteLine($"{bos.Apen[i].Naam} starts escape"); Aap aap = bos.Apen[i]; stopwatch.Start(); Boom beginboom = bos.Bomen[rnd.Next(1, bos.Bomen.Count - 1)]; bos.Graphics.FillCircle(new SolidBrush(aap.Color), beginboom.X, beginboom.Y, 3); aap.TouchedBomen.Add(beginboom); int seqNr = 1; while (beginboom != null) { Log log = new Log(aap, beginboom, stopwatch.Elapsed, seqNr); aap.Logs.Add(log); Boom nieuweBoom = bos.Bomen.OrderBy(boom => BerekenAfstand(boom, beginboom)).Except(aap.TouchedBomen).First(); if (DistanceToBorder(beginboom, bos.Bitmap) > BerekenAfstand(beginboom, nieuweBoom)) { bos.Graphics.DrawLine(aap.Pen, beginboom.X, beginboom.Y, nieuweBoom.X, nieuweBoom.Y); aap.TouchedBomen.Add(nieuweBoom); beginboom = nieuweBoom; } else { beginboom = null; } Console.WriteLine(log.ToString()); tasks.Add(Task.Run(() => dbm.AddLogs(bos.Id, log))); tasks.Add(Task.Run(() => dbm.AddMonkeyRecords(bos.Id, log))); seqNr++; } Console.WriteLine($"{bos.Apen[i].Naam} has escaped"); } Writer writer = new Writer(); tasks.Add(Task.Run(() => writer.WriteLogs(bos))); Task.WaitAll(tasks.ToArray()); bos.Bitmap.Save(Path.Combine(@"C:\Users\davy\Documents\data\EscapeFromTheWoods", $"{bos.Id}_Escapethewoods.Jpeg"), ImageFormat.Jpeg); Console.WriteLine($"Stop escape wood {bos.Id}"); }
public async Task WriteWoodRecordsToDatabase(Bos bos, DataTable WoodRecords) { using (SqlConnection connection = new SqlConnection(ConnString)) { connection.Open();// Transaction not allowed! using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = "WoodRecords"; bulkCopy.ColumnMappings.Clear(); bulkCopy.ColumnMappings.Add("recordId", "recordId"); bulkCopy.ColumnMappings.Add("woodID", "woodID"); bulkCopy.ColumnMappings.Add("treeID", "treeID"); bulkCopy.ColumnMappings.Add("x", "x"); bulkCopy.ColumnMappings.Add("y", "y"); bulkCopy.WriteToServer(WoodRecords); } } Console.WriteLine($"write wood {bos.Id} to database -end"); await StartCalculatingEscapeRoutes(bos); }
public async Task CreateBitmapAndLogsFile(Bos bos) { string pth = @"D:\HoGent Progammeren\Programmeren 4\AsynchroonProgrammeren Oefening\EscapeFromTheWoods\bin\Debug\netcoreapp3.1\Bitmap\"; double drawingFactor = 1; int ellipseStraal = 20; using (Bitmap bm = new Bitmap(Convert.ToInt32(bos.Xmaximum * drawingFactor), Convert.ToInt32(Math.Abs(bos.Yminimum) * drawingFactor))) using (Graphics g = Graphics.FromImage(bm)) using (Pen p = new Pen(Color.Green, 1)) { Console.WriteLine($"write bitmap routes wood: {bos.Id} -start"); Array colorsArray = Enum.GetValues(typeof(KnownColor)); KnownColor[] allColors = new KnownColor[colorsArray.Length]; Array.Copy(colorsArray, allColors, colorsArray.Length); //bovenstaande om een nieuwe kleur bij elke aap in te stellen; //BOS MET BOMEN: foreach (Boom b in bos.Bomen.Values) { g.DrawEllipse(p, b.X - 10, Math.Abs(b.Y) - 10, ellipseStraal, ellipseStraal); } //Bos met bomen aangemaakt. //STARTPOSITIE AAP & ROUTE AAP: int aapCount = 35; foreach (Aap aap in bos.Apen.Values) { Brush brush = new SolidBrush(Color.FromName(allColors[aapCount].ToString())); Pen penPerAap = new Pen(Color.FromName(allColors[aapCount].ToString())); aapCount++; g.FillEllipse(brush, (aap.StartBoom.X - 10), Math.Abs(aap.StartBoom.Y) - 10, ellipseStraal, ellipseStraal); for (int i = 0; i < aap.BezochteBomen.Count - 1; i++) { g.DrawLine(penPerAap, aap.BezochteBomen[i].X, Math.Abs(aap.BezochteBomen[i].Y), aap.BezochteBomen[i + 1].X, Math.Abs(aap.BezochteBomen[i + 1].Y)); } brush.Dispose(); } bm.Save(Path.Combine(pth, $@"{bos.Id.ToString()}_escapeRoutes.jpg"), ImageFormat.Jpeg); Console.WriteLine($"write bitmap routes wood: {bos.Id} -end"); await GenerateLogs(bos); } }
public async Task WriteLogs(Bos bos) { Console.WriteLine("start creating TxtFile"); int maxLogI = bos.Apen.OrderByDescending(aap => aap.Logs.Count).First().Logs.Count(); bos.Apen.Sort(); using (StreamWriter writer = File.CreateText(Path.Combine(@"C:\Users\davy\Documents\data\EscapeFromTheWoods", $"{bos.Id}_log.txt"))) { for (int logI = 0; logI < maxLogI; logI++) { for (int aapI = 0; aapI < bos.Apen.Count; aapI++) { if (bos.Apen[aapI].Logs.Count > logI) { writer.WriteLine(bos.Apen[aapI].Logs[logI].ToString()); } } } } Console.WriteLine("stop creating TxtFile"); }
public async Task InsertAll(Bos bos) { DataSet set = GetDataTables(bos); DataTableCollection collection = set.Tables; using (SqlConnection connection = GetConnection()) { connection.Open(); using (SqlBulkCopy sqlBulk = new SqlBulkCopy(connection)) { for (int i = 0; i < collection.Count; i++) { DataTable table = collection[i]; sqlBulk.BulkCopyTimeout = 0; sqlBulk.DestinationTableName = table.TableName; Console.WriteLine($"write {table.TableName} {i} to database - start"); await Task.Run(async() => { await sqlBulk.WriteToServerAsync(table); }); Console.WriteLine($"write {table.TableName} {i} to database - end"); } } } }
public async Task CreateWoodRecordsDataTable(Bos bos) { DataTable WoodRecords = new DataTable(); WoodRecords.Clear(); WoodRecords.Columns.Add(new DataColumn("recordId", typeof(int))); WoodRecords.Columns.Add(new DataColumn("woodID", typeof(int))); WoodRecords.Columns.Add(new DataColumn("treeID", typeof(int))); WoodRecords.Columns.Add(new DataColumn("x", typeof(int))); WoodRecords.Columns.Add(new DataColumn("y", typeof(int))); Console.WriteLine($"write wood {bos.Id} to database -start"); foreach (Boom boom in bos.Bomen.Values) { DataRow dr = WoodRecords.NewRow(); dr[0] = boom.woodRecordId; dr[1] = bos.Id; dr[2] = boom.Id; dr[3] = boom.X; dr[4] = boom.Y; WoodRecords.Rows.Add(dr); } await WriteWoodRecordsToDatabase(bos, WoodRecords); }
static void Main() { Stopwatch stopwatch = new Stopwatch(); DatabaseManager dbm = new DatabaseManager(@"Data Source=LAPTOP-1U6AQSEQ\SQLEXPRESS;Initial Catalog=EscapeFromTheWoods;Integrated Security=True"); int monkeyId = dbm.GetMonkeyId(); int bosId = dbm.GetWoodId(); List <Bos> bossen = new List <Bos>(); BosGenerator bgn = new BosGenerator(); Bos bos1 = bgn.MaakBos(bosId, 500, 500, 600); List <Aap> apen1 = new List <Aap> { new Aap(monkeyId, "Jeff", Color.Red), new Aap(monkeyId + 1, "Kwak", Color.Yellow), new Aap(monkeyId + 2, "Azomopoirodovvody", Color.Blue) }; bos1.AddApen(apen1); bossen.Add(bos1); Bos bos2 = bgn.MaakBos(bosId + 1, 1000, 1000, 1700); List <Aap> apen2 = new List <Aap> { new Aap(monkeyId + 3, "Eduardo", Color.White), new Aap(monkeyId + 4, "Ivan", Color.Aqua), new Aap(monkeyId + 5, "Hartje", Color.DarkOrange) }; bos2.AddApen(apen2); bossen.Add(bos2); MonkeyMadness mm = new MonkeyMadness(); List <Task> tasks = new List <Task>(); bossen.ForEach(bos => tasks.Add(Task.Run(() => mm.Escape(bos, stopwatch)))); Task.WaitAll(tasks.ToArray()); Console.WriteLine($"finished at: {stopwatch.Elapsed }"); }
public async Task AllAsyncStuff(Bos bos) { await CreateWoodRecordsDataTable(bos); await CreateBitmapAndLogsFile(bos); }
private static DataSet GetDataTables(Bos bos) { Console.WriteLine("getting info"); //WoodRecords DataTable woodRecords = new DataTable("WoodRecords"); woodRecords.Columns.Add("recordId", typeof(int)); woodRecords.Columns.Add("woodID", typeof(int)); woodRecords.Columns.Add("treeID", typeof(int)); woodRecords.Columns.Add("x", typeof(int)); woodRecords.Columns.Add("y", typeof(int)); woodRecords.PrimaryKey = new DataColumn[] { woodRecords.Columns["recordId"] }; //MonkeyRecords DataTable monkeyRecords = new DataTable("MonkeyRecords"); monkeyRecords.Columns.Add("recordID", typeof(int)); monkeyRecords.Columns.Add("monkeyID", typeof(int)); monkeyRecords.Columns.Add("monkeyName", typeof(string)); monkeyRecords.Columns.Add("woodID", typeof(int)); monkeyRecords.Columns.Add("seqnr", typeof(int)); monkeyRecords.Columns.Add("treeID", typeof(int)); monkeyRecords.Columns.Add("x", typeof(int)); monkeyRecords.Columns.Add("y", typeof(int)); monkeyRecords.PrimaryKey = new DataColumn[] { monkeyRecords.Columns["recordID"] }; //Logs DataTable logs = new DataTable("Logs"); logs.Columns.Add("Id", typeof(int)); logs.Columns.Add("woodID", typeof(int)); logs.Columns.Add("monkeyID", typeof(int)); logs.Columns.Add("message", typeof(string)); logs.PrimaryKey = new DataColumn[] { logs.Columns["Id"] }; #region datatables aanvullen //WoodRecords int bosId = bos.id; for (int i = 0; i < bos.bomen.Count; i++) { Boom currentBoom = bos.bomen[i]; woodRecords.Rows.Add(woodRecordID, bosId, currentBoom.id, currentBoom.xCoordinaat, currentBoom.yCoordinaat); woodRecordID++; } //MonkeyRecords for (int i = 0; i < bos.log.ontsnapteApen.Count; i++) { Aap currentAap = bos.log.ontsnapteApen[i]; for (int seqnr = 0; seqnr < currentAap.bezochteBomen.Count; seqnr++) { Boom currentBoom = currentAap.bezochteBomen[seqnr]; monkeyRecords.Rows.Add(monkeyRecordID, currentAap.id, currentAap.naam, bosId, seqnr, currentBoom.id, currentBoom.xCoordinaat, currentBoom.yCoordinaat); monkeyRecordID++; //Logs logs.Rows.Add(logID, bosId, currentAap.id, $"{currentAap.naam} is now in tree {currentBoom.id} at location ({currentBoom.xCoordinaat},{currentBoom.yCoordinaat})"); logID++; } } #endregion //create DataSet => kan gebruikt worden om xml mee te maken DataSet set = new DataSet("EscapeFromTheWoods"); set.Tables.Add(woodRecords); set.Tables.Add(monkeyRecords); set.Tables.Add(logs); return(set); }
public Boom(Bos bos, int id, int woodrecordId) { this.Id = id; this.Bos = bos; this.woodRecordId = woodrecordId; }
public BitmapMaker(Bos bos) { this.bos = bos; }