public async Task AgentSimulatioAsync(int simulationConfigurationId) { // In-memory database only exists while the connection is open var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionStringBuilder.ToString()); // create OptionsBuilder with InMemmory Context var builder = new DbContextOptionsBuilder <MasterDBContext>(); builder.UseSqlite(connection); var simNumber = _evaluationContext.GetSimulationNumber(simulationConfigurationId, SimulationType.Decentral); using (var c = new InMemoryContext(builder.Options)) { c.Database.OpenConnection(); c.Database.EnsureCreated(); InMemoryContext.LoadData(_evaluationContext, c); var sim = new AgentSimulation(c, _messageHub); await sim.RunSim(simulationConfigurationId, simNumber); CopyResults.Copy(c, _evaluationContext, simulationConfigurationId, simNumber, SimulationType.Decentral); CalculateKpis.MachineSattleTime(_evaluationContext, simulationConfigurationId, SimulationType.Decentral, simNumber); CalculateKpis.CalculateAllKpis(_evaluationContext, simulationConfigurationId, SimulationType.Decentral, simNumber, true); } connection.Close(); _messageHub.EndSimulation("Simulation with Id:" + simulationConfigurationId + " Completed." , simulationConfigurationId.ToString() , simNumber.ToString()); }
private void FinishSimulation(TimeTable <ISimulationItem> timeTable, int simulationId, int simNumber, SimulationConfiguration simConfig) { //copy workschedules to the simulationworkschedules which get written back to the local-db //FillSimulationWorkSchedules(timeTable.Items.OfType<PowsSimulationItem>().ToList(), simulationId, simNumber, 0); _messageHub.SendToAllClients("last Item produced at: " + _context.SimulationWorkschedules.Max(a => a.End)); //calculate kpis CalculateKpis.MachineSattleTime(_context, simulationId, SimulationType.Central, simNumber); CalculateKpis.CalculateAllKpis(_context, simulationId, SimulationType.Central, simNumber, true, simConfig.Time); RemoveSimulationWorkschedules(); //copy the relevant tables from the in-memory db to the local-db CopyResults.Copy(_context, _evaluationContext, simulationId, simNumber, SimulationType.Central); _messageHub.EndScheduler(); _context.Database.CloseConnection(); _evaluationContext.Database.CloseConnection(); }