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(); }
/// <summary> /// Call this method for a single run without simulation. /// </summary> /// <param name="task"></param> /// <param name="simulationId"></param> /// <returns></returns> public async Task InitializeMrp(MrpTask task, int simulationId) { await Task.Run(async() => { _messageHub.SendToAllClients("Prepare InMemory Tables...", MessageType.info); rebuildNets = new RebuildNets(_context); capacityScheduling = new CapacityScheduling(_context); _context = InMemoryContext.CreateInMemoryContext(); InMemoryContext.LoadData(_evaluationContext, _context); await PrepareSimulationContext(); var simConfig = _context.SimulationConfigurations.Single(x => x.Id == simulationId); await OrderGenerator.GenerateOrders(_context, simConfig, simulationId); var simulationNumber = _context.GetSimulationNumber(simulationId, SimulationType.Central); //call initial central MRP-run await _processMrp.CreateAndProcessOrderDemand(task, _context, simulationId, _evaluationContext); if (task == MrpTask.Backward || task == MrpTask.Forward || task == MrpTask.GifflerThompson) { var list = _context.ProductionOrderWorkSchedules.Select(schedule => new PowsSimulationItem(_context) { End = (task == MrpTask.Backward) ? schedule.EndBackward : (task == MrpTask.Forward) ? schedule.EndForward : schedule.End, Start = (task == MrpTask.Backward) ? schedule.StartBackward : (task == MrpTask.Forward) ? schedule.StartForward : schedule.Start, ProductionOrderId = schedule.ProductionOrderId, ProductionOrderWorkScheduleId = schedule.Id, SimulationId = simulationId }) .ToList(); _evaluationContext.SaveChanges(); FillSimulationWorkSchedules(list, simulationId, simulationNumber, task); } SimulationType simType = SimulationType.Central; switch (task) { case MrpTask.Forward: simType = SimulationType.ForwardPlanning; break; case MrpTask.Backward: simType = SimulationType.BackwardPlanning; break; } CopyResults.ExtractSimulationOrders(_context, _evaluationContext, simulationId, simulationNumber, simType); _messageHub.EndScheduler(); }); }
private void CopyAndChangeConstraints(Template baseTemplate, Template destinationTemplate, ExcelSheet constraintsSheet, IEnumerable <ExcelRow> changes, CopyResults results, TemplateConstraint baseParentConstraint = null, TemplateConstraint destinationParentConstraint = null) { var baseConstraints = baseParentConstraint != null? baseParentConstraint.ChildConstraints.ToList() : baseTemplate.ChildConstraints.Where(y => y.ParentConstraintId == null).ToList(); foreach (var baseConstraint in baseConstraints) { TemplateConstraint newConstraint = new TemplateConstraint() { Cardinality = baseConstraint.Cardinality, CodeSystemId = baseConstraint.CodeSystemId, Conformance = baseConstraint.Conformance, Context = baseConstraint.Context, DataType = baseConstraint.DataType, Description = baseConstraint.Description, DisplayName = baseConstraint.DisplayName, HeadingDescription = baseConstraint.HeadingDescription, IsBranch = baseConstraint.IsBranch, IsBranchIdentifier = baseConstraint.IsBranchIdentifier, IsHeading = baseConstraint.IsHeading, IsInheritable = baseConstraint.IsInheritable, IsSchRooted = baseConstraint.IsSchRooted, IsPrimitive = baseConstraint.IsPrimitive, IsStatic = baseConstraint.IsStatic, Label = baseConstraint.Label, Notes = baseConstraint.Notes, Order = baseConstraint.Order, PrimitiveText = baseConstraint.PrimitiveText, Schematron = baseConstraint.Schematron, Value = baseConstraint.Value, ValueConformance = baseConstraint.ValueConformance, ValueSetId = baseConstraint.ValueSetId, ValueSetDate = baseConstraint.ValueSetDate }; // Find changes for this constraint var constraintChanges = changes.Where(y => GetRowField(constraintsSheet, y, Fields.ConstraintNumber) == baseConstraint.Number.ToString()); foreach (var constraintChange in constraintChanges) { string dataType = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintDataType); string conformance = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintConformance); string cardinality = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintCardinality); string valueConformance = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintValueConformance); string code = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintCode); string display = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintDisplay); string valueSet = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintValueSet); string valueSetDate = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintValueSetDate); string codeSystem = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintCodeSystem); string containedTemplateField = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintContainedTemplate); string[] containedTemplatesFields = containedTemplateField != null?containedTemplateField.Split(',') : null; string description = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintDescription); string label = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintLabel); string binding = GetRowField(constraintsSheet, constraintChange, Fields.ConstraintBinding); if (!string.IsNullOrEmpty(conformance) && conformance != "SHALL" && conformance != "SHOULD" && conformance != "MAY" && conformance != "SHALL NOT" && conformance != "SHOULD NOT" && conformance != "MAY NOT") { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " has an invalid conformance value. Valid options are: SHALL, SHOULD, MAY, SHALL NOT, SHOULD NOT, MAY NOT"); } if (!string.IsNullOrEmpty(valueConformance) && valueConformance != "SHALL" && valueConformance != "SHOULD" && valueConformance != "MAY" && valueConformance != "SHALL NOT" && valueConformance != "SHOULD NOT" && valueConformance != "MAY NOT") { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " has an invalid value-conformance value. Valid options are: SHALL, SHOULD, MAY, SHALL NOT, SHOULD NOT, MAY NOT"); } if (!string.IsNullOrEmpty(cardinality) && !System.Text.RegularExpressions.Regex.IsMatch(cardinality, @"^(\d*|\*)\.\.(\d*|\*)$")) { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " has an invalid format. Format should be \"X..Y\""); } if (!string.IsNullOrEmpty(binding) && binding.ToLower() != "<remove>" && binding.ToLower() != "static" && binding.ToLower() != "dynamic") { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " has an invalid binding type. Valid values are \"STATIC\" and \"DYNAMIC\""); } SetConstraintStringValue(newConstraint, "DataType", dataType); SetConstraintStringValue(newConstraint, "Conformance", conformance); SetConstraintStringValue(newConstraint, "Cardinality", cardinality); SetConstraintStringValue(newConstraint, "DataType", dataType); SetConstraintStringValue(newConstraint, "Value", code); SetConstraintStringValue(newConstraint, "DisplayName", display); SetConstraintStringValue(newConstraint, "Description", description); SetConstraintStringValue(newConstraint, "Label", label); // ValueSet if (!string.IsNullOrEmpty(valueSet)) { if (valueSet.ToLower() == "<remove>") { newConstraint.ValueSetId = null; } else { ValueSet foundValueSet = (from vs in this.tdb.ValueSets join vsi in this.tdb.ValueSetIdentifiers on vs.Id equals vsi.ValueSetId where vsi.Identifier.ToLower().Trim() == valueSet.ToLower().Trim() select vs) .Distinct() .FirstOrDefault(); if (foundValueSet == null) { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " defines a value set \"" + valueSet + "\" which could not be found."); } else { newConstraint.ValueSetId = foundValueSet.Id; } } } // Value Set Date if (!string.IsNullOrEmpty(valueSetDate)) { if (valueSetDate.ToLower() == "<remove>") { newConstraint.ValueSetDate = null; } else { DateTime parsedValueSetDate = DateTime.MinValue; if (!DateTime.TryParse(valueSetDate, out parsedValueSetDate)) { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " has an invalid date/time format for the value set date"); } else { newConstraint.ValueSetDate = parsedValueSetDate; } } } // CodeSystem if (!string.IsNullOrEmpty(codeSystem)) { if (valueSet.ToLower() == "<remove>") { newConstraint.CodeSystemId = null; } else { CodeSystem foundCodeSystem = this.tdb.CodeSystems.SingleOrDefault(y => y.Oid == codeSystem); if (foundCodeSystem == null) { results.Errors.Add("Constraint change #" + constraintChange.RowNumber.ToString() + " defines a code system \"" + codeSystem + "\" which could not be found."); } else { newConstraint.CodeSystemId = foundCodeSystem.Id; } } } // Contained Template if (containedTemplatesFields != null && containedTemplateField != "<remove>") { foreach (var containedTemplateIdentifier in containedTemplatesFields) { newConstraint.References.Add(new TemplateConstraintReference() { ReferenceIdentifier = containedTemplateIdentifier, ReferenceType = ConstraintReferenceTypes.Template }); } } // Binding if (!string.IsNullOrEmpty(binding)) { if (binding.ToLower() == "<remove>") { newConstraint.IsStatic = null; } else if (binding.ToLower() == "static") { newConstraint.IsStatic = true; } else if (binding.ToLower() == "dynamic") { newConstraint.IsStatic = false; } } } // Add the constraint to the template and to the correct parent (if one is specified) if (destinationParentConstraint != null) { destinationParentConstraint.ChildConstraints.Add(newConstraint); } destinationTemplate.ChildConstraints.Add(newConstraint); // Recursively copy additional constraints CopyAndChangeConstraints(baseTemplate, destinationTemplate, constraintsSheet, changes, results, baseConstraint, newConstraint); } }
public CopyResults Copy(CopyModel model) { CopyResults results = new CopyResults(); Template baseTemplate = this.tdb.Templates.Single(y => y.Id == model.BaseTemplateId); List <Template> templates = new List <Template>(); if (!CheckPoint.Instance.GrantEditImplementationGuide(baseTemplate.OwningImplementationGuideId)) { throw new AuthorizationException("You do not have permission to modify this implementation guide"); } foreach (var templateModel in model.TemplateSheet.Rows) { string templateOid = GetRowField(model.TemplateSheet, templateModel, Fields.TemplateOid); var foundTemplate = this.tdb.Templates.SingleOrDefault(y => y.Oid == templateOid); // If a template with the same OID already exists, try to delete it if (foundTemplate != null) { if (foundTemplate.OwningImplementationGuide.IsPublished()) { results.Errors.Add("Template " + templateOid + " already exists and cannot be re-created because it is part of an implementation guide that has already been published."); continue; } if (!CheckPoint.Instance.GrantEditTemplate(foundTemplate.Id)) { results.Errors.Add("Template " + templateOid + " already exists and cannot be re-created because you do not have permission to edit the template."); continue; } this.DeleteTemplate(foundTemplate); } var user = CheckPoint.Instance.GetUser(this.tdb); // Create the new template var newTemplate = new Template() { Name = GetRowField(model.TemplateSheet, templateModel, Fields.TemplateName), Oid = templateOid, Bookmark = GetRowField(model.TemplateSheet, templateModel, Fields.TemplateBookmark), Description = GetRowField(model.TemplateSheet, templateModel, Fields.TemplateDescription), Author = user, PrimaryContext = baseTemplate.PrimaryContext, PrimaryContextType = baseTemplate.PrimaryContextType, ImplementationGuideType = baseTemplate.ImplementationGuideType, ImpliedTemplate = baseTemplate.ImpliedTemplate, IsOpen = baseTemplate.IsOpen, Notes = baseTemplate.Notes, OwningImplementationGuide = baseTemplate.OwningImplementationGuide, Status = baseTemplate.Status, TemplateType = baseTemplate.TemplateType }; this.tdb.Templates.Add(newTemplate); results.NewTemplates.Add(new CopyResults.TemplateEntry() { Name = newTemplate.Name, Oid = newTemplate.Oid, Url = "/TemplateManagement/View/" + templateOid }); var changes = model.ConstraintSheet.Rows.Where(y => GetRowField(model.ConstraintSheet, y, Fields.ConstraintTemplate) == newTemplate.Oid); this.CopyAndChangeConstraints(baseTemplate, newTemplate, model.ConstraintSheet, changes, results); } if (results.Errors.Count == 0) { try { this.tdb.SaveChanges(); } catch (Exception ex) { results.Errors.Add("System error: " + ex.Message); } } else { results.NewTemplates.Clear(); } return(results); }