Ejemplo n.º 1
0
        //save rneuron
        public async void saveRneuron(Rneuron rneuron)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    if (networkLoaded)
                    {
                        Task.Delay(5000).Wait();

                        Rneuron _rneuron = db.Rneurons.Add(rneuron);

                        _rneuron.Rnetwork_ID = networkID;

                        foreach (var rn in _rneuron.Input_Values_Reneurons)
                        {
                            db.Input_Values_Reneurons.Add(rn);
                        }

                        await db.SaveChangesAsync();
                    }
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "saveRneuron");
                }
            }
        }
Ejemplo n.º 2
0
        //save case
        private async void saveCase(Case theCase)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                db.Cases.Add(theCase);
start:

                try
                {
                    await db.SaveChangesAsync();

                    if (theCase.Rneuron != null)
                    {
                        theCase.Rneuron.SavedToDb = true;
                    }

                    if (theCase.Solution != null)
                    {
                        theCase.Solution.SavedToDb = true;
                    }

                    theCase.SavedToDb = true;

                    //RlmDbLogger.Info(string.Format("\n[{0:d/M/yyyy HH:mm:ss:ms}]: Saving case...", DateTime.Now), databaseName);
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "saveCase");
                    Task.Delay(TaskDelay).Wait();
                    goto start;
                }
            }
        }
Ejemplo n.º 3
0
        //create new network
        public async void SaveNetwork(Rnetwork rnetwork, Input_Output_Type io_type, List <Input> inputs, List <Output> outputs)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    //ready network for saving
                    Rnetwork _newNetwork = db.Rnetworks.Add(rnetwork);

                    //add inputs to io_type
                    foreach (Input input in inputs)
                    {
                        io_type.Inputs.Add(input);
                    }

                    //add outputs to io_type
                    foreach (Output output in outputs)
                    {
                        io_type.Outputs.Add(output);
                    }

                    //ready io_type for saving
                    Input_Output_Type _ioType = db.Input_Output_Types.Add(io_type);

                    //save to database
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "SaveNetwork");
                }
            }
        }
Ejemplo n.º 4
0
        //save case
        private async Task saveCase(BlockingCollection <Case[]> cases)
        {
            Exception error    = null;
            int       retryCnt = 0;

            foreach (var item in cases.GetConsumingEnumerable())
            {
                do
                {
                    try
                    {
                        error = null;
                        using (RlmDbEntities db = new RlmDbEntities(databaseName))
                        {
                            db.Cases.AddRange(item);

                            await db.SaveChangesAsync();

                            //if (theCase.Rneuron != null)
                            //{
                            //    theCase.Rneuron.SavedToDb = true;
                            //}

                            //if (theCase.Solution != null)
                            //{
                            //    theCase.Solution.SavedToDb = true;
                            //}

                            //theCase.SavedToDb = true;

                            //RlmDbLogger.Info(string.Format("\n[{0:d/M/yyyy HH:mm:ss:ms}]: Saving case...", DateTime.Now), databaseName);
                        }

                        if (retryCnt > 0)
                        {
                            System.Diagnostics.Debug.WriteLine($"Cases saved after {retryCnt}");
                        }
                    }
                    catch (Exception ex)
                    {
                        RlmDbLogger.Error(ex, databaseName, "saveCase");
                        error = ex;
                        retryCnt++;
                        Task.Delay(TaskRetryDelay).Wait();
                    }
                } while (error != null);

                retryCnt = 0;
            }


            // TODO needs more testing and fix for Sessions concurrency bug
            //await batchProcessor.InsertCases(cases);
        }
Ejemplo n.º 5
0
        public void StartCaseWorker(CancellationToken tc)
        {
            Task.Run(async() => {
                while (true)
                {
                    using (RlmDbEntities db = new RlmDbEntities(databaseName))
                    {
                        try
                        {
                            var cases = caseBox.Where(a => a.SavedToDb == false);
                            var count = cases.Count();

                            if (count > 0)
                            {
                                foreach (var theCase in cases)
                                {
                                    db.Cases.Add(theCase);

                                    if (theCase.Rneuron != null)
                                    {
                                        theCase.Rneuron.SavedToDb = true;
                                    }

                                    if (theCase.Solution != null)
                                    {
                                        theCase.Solution.SavedToDb = true;
                                    }

                                    theCase.SavedToDb = true;
                                }

                                caseCount += count;
                                RlmDbLogger.Info(string.Format("[{0:d/M/yyyy HH:mm:ss:ms}]: Saving {1} cases [Total: {2}]", DateTime.Now, count, caseCount), databaseName);

                                await db.SaveChangesAsync();
                            }

                            Task.Delay(50).Wait();
                        }
                        catch (Exception ex)
                        {
                            RlmDbLogger.Error(ex, databaseName, "StartCaseWorker");
                        }
                    }
                }
            });
        }
Ejemplo n.º 6
0
        private async Task updateSession(Session session)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    if (networkLoaded)
                    {
                        Session existing = null;
                        while (existing == null)
                        {
                            existing = db.Sessions.FirstOrDefault(a => a.ID == session.ID);

                            if (existing != null)
                            {
                                existing.Hidden       = session.Hidden;
                                existing.SessionScore = session.SessionScore;
                                existing.DateTimeStop = session.DateTimeStop;

                                int a = await db.SaveChangesAsync();

                                if (a > 0)
                                {
                                    session.UpdatedToDb = true;
                                }
                                break;
                            }

                            //Task.Delay(50).Wait();
                        }
                    }
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "updateSession");
                }
            }
        }
Ejemplo n.º 7
0
        //save session (create and update)
        private async Task createSession(Session session)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    if (networkLoaded)
                    {
                        db.Sessions.Add(session);
                        int a = await db.SaveChangesAsync();

                        if (a > 0)
                        {
                            session.CreatedToDb = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "createSession");
                }
            }
        }
Ejemplo n.º 8
0
        //save solution
        private async void saveSolution(Solution solution)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    if (networkLoaded)
                    {
                        Solution _solution = db.Solutions.Add(solution);

                        foreach (var sol in _solution.Output_Values_Solutions)
                        {
                            db.Output_Values_Solutions.Add(sol);
                        }

                        await db.SaveChangesAsync();
                    }
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "saveSolution");
                }
            }
        }
Ejemplo n.º 9
0
        //save case
        private async Task saveCase(IEnumerable <Case> cases)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                db.Cases.AddRange(cases);
start:

                try
                {
                    await db.SaveChangesAsync();

                    //if (theCase.Rneuron != null)
                    //{
                    //    theCase.Rneuron.SavedToDb = true;
                    //}

                    //if (theCase.Solution != null)
                    //{
                    //    theCase.Solution.SavedToDb = true;
                    //}

                    //theCase.SavedToDb = true;

                    //RlmDbLogger.Info(string.Format("\n[{0:d/M/yyyy HH:mm:ss:ms}]: Saving case...", DateTime.Now), databaseName);
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "saveCase");
                    Task.Delay(TaskDelay).Wait();
                    goto start;
                }
            }

            // TODO needs more testing and fix for Sessions concurrency bug
            //await batchProcessor.InsertCases(cases);
        }