/// <summary> /// Reads a collection of <see cref="Double"/> values from XML /// </summary> /// <param name="collection">Collection to be filled with values read from XML</param> /// <param name="collectionElementName">Name of the collection parent XML element</param> /// <param name="valueElementName">Name of the XML element that represents collection item</param> /// <param name="parentElement"><see cref="XElement"/> instance that is parent to the collection element</param> private static void ReadValueCollection(IDictionary <TId, double> collection, XName collectionElementName, XName valueElementName, XElement parentElement) { XElement valueCollectionElement = ReadCollectionElement(collectionElementName, parentElement); IEnumerable <XElement> valueElements = valueCollectionElement.Descendants(valueElementName); foreach (XElement valueElement in valueElements) { TId id = TId.Parse(valueElement.Attribute(Attributes.Id).Value); double value = ConvertEx.ToDoubleInvariant(valueElement.Attribute(Attributes.Value).Value); collection.Add(id, value); } }
/// <summary> /// Reads a collection of <see cref="AdequacyCriterion"/> from XML /// </summary> /// <param name="criteriaCollection">A collection to be read from XML</param> /// <param name="criteriaCollectionElement"><see cref="XElement"/> to read a collection from</param> private static void ReadCriteria(NamedModelEntityCollection <AdequacyCriterion> criteriaCollection, XElement criteriaCollectionElement) { IEnumerable <XElement> criterionElements = criteriaCollectionElement.Descendants(Elements.Criterion); foreach (XElement criterionElement in criterionElements) { TId id = TId.Parse(criterionElement.Attribute(Attributes.Id).Value); string name = criterionElement.Attribute(Attributes.Name).Value; string variableIdentifier = criterionElement.Attribute(Attributes.VariableIdentifier).Value; AdequacyCriterionType criterionType = EnumEx.Parse <AdequacyCriterionType>(criterionElement.Attribute(Attributes.Type).Value); AdequacyCriterion criterion = new AdequacyCriterion(id, name, variableIdentifier, criterionType); ReadPropertyCollection(criterion.Properties, ReadCollectionElement(Elements.Properties, criterionElement, false)); criteriaCollection.Add(criterion); } }
/// <summary> /// Reads a collection of <see cref="Parameter"/> from XML /// </summary> /// <param name="parametersCollection">A collection to be read from XML</param> /// <param name="parametersCollectionElement"><see cref="XElement"/> to read a collection from</param> /// <param name="parameterElementName">Name of the XML element that represents <see cref="Parameter"/></param> private static void ReadParameters(NamedModelEntityCollection <Parameter> parametersCollection, XElement parametersCollectionElement, XName parameterElementName) { IEnumerable <XElement> parameterElements = parametersCollectionElement.Descendants(parameterElementName); foreach (XElement parameterElement in parameterElements) { TId id = TId.Parse(parameterElement.Attribute(Attributes.Id).Value); string name = parameterElement.Attribute(Attributes.Name).Value; string variableIdentifier = parameterElement.Attribute(Attributes.VariableIdentifier).Value; double minValue = ConvertEx.ToDoubleInvariant(parameterElement.Attribute(Attributes.MinValue).Value); double maxValue = ConvertEx.ToDoubleInvariant(parameterElement.Attribute(Attributes.MaxValue).Value); Parameter parameter = new Parameter(id, name, variableIdentifier, minValue, maxValue); ReadPropertyCollection(parameter.Properties, ReadCollectionElement(Elements.Properties, parameterElement, false)); parametersCollection.Add(parameter); } }
/// <summary> /// Reads a collection of real <see cref="Experiment"/> from XML /// </summary> /// <param name="experimentCollection">A collection to be read from XML</param> /// <param name="experimentsCollectionElement"><see cref="XElement"/> to read a collection from</param> private static void ReadRealExperiments(ExperimentCollection experimentCollection, XElement experimentsCollectionElement) { IEnumerable <XElement> experimentElements = experimentsCollectionElement.Descendants(Elements.RealExperiment); foreach (XElement experimentElement in experimentElements) { TId id = TId.Parse(experimentElement.Attribute(Attributes.Id).Value); int number = Convert.ToInt32(experimentElement.Attribute(Attributes.Number).Value); Experiment experiment = new Experiment(id, number); ReadValueCollection(experiment.ParameterValues, Elements.OptimizationParameterValues, Elements.OptimizationParameterValue, experimentElement); ReadValueCollection(experiment.CriterionValues, Elements.RealCriterionValues, Elements.RealCriterionValue, experimentElement); ReadValueCollection(experiment.ConstraintValues, Elements.FunctionalConstraintValues, Elements.FunctionalConstraintValue, experimentElement); ReadPropertyCollection(experiment.Properties, ReadCollectionElement(Elements.Properties, experimentElement, false)); experimentCollection.Add(experiment); } }
/// <summary> /// Reads a collection of <see cref="Constraint"/> from XML /// </summary> /// <param name="constraintsCollection">A collection to be read from XML</param> /// <param name="constraintsCollectionElement"><see cref="XElement"/> to read a collection from</param> private static void ReadConstraints(NamedModelEntityCollection <Constraint> constraintsCollection, XElement constraintsCollectionElement) { IEnumerable <XElement> constraintElements = constraintsCollectionElement.Descendants(Elements.FunctionalConstraint); foreach (XElement constraintElement in constraintElements) { TId id = TId.Parse(constraintElement.Attribute(Attributes.Id).Value); string name = constraintElement.Attribute(Attributes.Name).Value; string variableIdentifier = constraintElement.Attribute(Attributes.VariableIdentifier).Value; Relation constraintRelation = EnumEx.Parse <Relation>(constraintElement.Attribute(Attributes.Relation).Value); double value = ConvertEx.ToDoubleInvariant(constraintElement.Attribute(Attributes.Value).Value); string expression = constraintElement.Attribute(Attributes.Expression).Value; Constraint constraint = new Constraint(id, name, variableIdentifier, constraintRelation, value, expression); ReadPropertyCollection(constraint.Properties, ReadCollectionElement(Elements.Properties, constraintElement, false)); constraintsCollection.Add(constraint); } }
/// <summary> /// Gets the ID of entity which is currently selected in the DataGridView /// </summary> /// <returns>Index of selected entity or -1 if several or none rows are selected</returns> protected TId GetSelectedEntityId() { if (this.dgvModelEntities.SelectedRows.Count == 0 || this.dgvModelEntities.SelectedRows.Count > 1) { MessageBoxHelper.ShowExclamation("Выберите в списке только один элемент"); return(-1); } string result = this.dgvModelEntities.SelectedRows[0].Cells[0].Value as string; if (result == null) { return((TId)this.dgvModelEntities.SelectedRows[0].Cells[0].Value); } else { return(TId.Parse((string)this.dgvModelEntities.SelectedRows[0].Cells[0].Value)); } }
/// <summary> /// Deletes selected row /// </summary> /// <typeparam name="T">inheritors of NamedModelEntity</typeparam> /// <param name="entities">collection of model which will be shown in the DataGrid</param> protected void DeleteEntities <T>(NamedModelEntityCollection <T> entities) where T : NamedModelEntity { if (this.dgvModelEntities.SelectedRows.Count == 0) { MessageBoxHelper.ShowExclamation("Выберите в списке хотя бы один элемент для удаления"); return; } DialogResult result = MessageBox.Show("Удалить выбранные элементы?", Program.ApplicationSettings.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { foreach (DataGridViewRow selRow in this.dgvModelEntities.SelectedRows) { TId entityId = TId.Parse((string)selRow.Cells[0].Value); entities.Remove(entityId); } this.UpdateParametersDataGrid(entities); } }
/// <summary> /// Reads a collection of <see cref="Criterion"/> from XML. /// </summary> /// <param name="model"><see cref="Model"/> instance to be read from XML.</param> /// <param name="criteriaCollectionElement"><see cref="XElement"/> to read a collection from.</param> private static void ReadCriteria(Model model, XElement criteriaCollectionElement) { IEnumerable <XElement> criterionElements = criteriaCollectionElement.Descendants(Elements.Criterion); foreach (XElement criterionElement in criterionElements) { TId id = TId.Parse(criterionElement.Attribute(Attributes.Id).Value); string name = criterionElement.Attribute(Attributes.Name).Value; string variableIdentifier = criterionElement.Attribute(Attributes.VariableIdentifier).Value; CriterionType criterionType = EnumExtensions.Parse <CriterionType>(criterionElement.Attribute(Attributes.Type).Value); int weight = Convert.ToInt32(criterionElement.Attribute(Attributes.Weight).Value); string expression = criterionElement.Attribute(Attributes.Expression).Value; Criterion criterion = new Criterion(id, name, variableIdentifier, criterionType, expression) { Weight = weight }; ReadPropertyCollection(criterion.Properties, ReadCollectionElement(Elements.Properties, criterionElement, false)); model.Criteria.Add(criterion); } }
private static void ReadPromotableCriteria(ModelDraft modelDraft, XElement promotableCriterionCollectionElement) { IEnumerable <XElement> promotableCriterionElements = promotableCriterionCollectionElement.Descendants(Elements.PromotableCriterion); foreach (XElement promotableCriterionElement in promotableCriterionElements) { TId id = TId.Parse(promotableCriterionElement.Attribute(Attributes.Id).Value); string name = promotableCriterionElement.Attribute(Attributes.Name).Value; string variableIdentifier = promotableCriterionElement.Attribute(Attributes.VariableIdentifier).Value; bool isPromoted = Convert.ToBoolean(promotableCriterionElement.Attribute(Attributes.IsPromoted).Value); CriterionType criterionType = EnumExtensions.Parse <CriterionType>(promotableCriterionElement.Attribute(Attributes.Type).Value); Relation constraintRelation = EnumExtensions.Parse <Relation>(promotableCriterionElement.Attribute(Attributes.Relation).Value); double value = ConvertExtensions.ToDoubleInvariant(promotableCriterionElement.Attribute(Attributes.Value).Value); PromotableCriterion promotableCriterion = new PromotableCriterion(id, name, variableIdentifier, criterionType) { ConstraintRelation = constraintRelation, IsPromoted = isPromoted, Value = value }; modelDraft.PromotableCriteria.Add(promotableCriterion); } }
private static void ReadPromotableConstants(ModelDraft modelDraft, XElement promotableConstantCollectionElement) { IEnumerable <XElement> promotableConstantElements = promotableConstantCollectionElement.Descendants(Elements.PromotableConstant); foreach (XElement promotableConstantElement in promotableConstantElements) { TId id = TId.Parse(promotableConstantElement.Attribute(Attributes.Id).Value); string name = promotableConstantElement.Attribute(Attributes.Name).Value; string variableIdentifier = promotableConstantElement.Attribute(Attributes.VariableIdentifier).Value; bool isPromoted = Convert.ToBoolean(promotableConstantElement.Attribute(Attributes.IsPromoted).Value); double value = ConvertExtensions.ToDoubleInvariant(promotableConstantElement.Attribute(Attributes.Value).Value); double minValue = ConvertExtensions.ToDoubleInvariant(promotableConstantElement.Attribute(Attributes.MinValue).Value); double maxValue = ConvertExtensions.ToDoubleInvariant(promotableConstantElement.Attribute(Attributes.MaxValue).Value); PromotableConstant promotableConstant = new PromotableConstant(id, name, variableIdentifier, value) { IsPromoted = isPromoted, MaxValue = maxValue, MinValue = minValue }; modelDraft.PromotableConstants.Add(promotableConstant); } }
private void dgvData_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { TId expIndex = (TId)this.dgvData[0, e.RowIndex].Value; TId colIndex = TId.Parse(this.dgvData.Columns[e.ColumnIndex].Name); string colType = this.dgvData.Columns[e.ColumnIndex].Tag.ToString(); // Проверим на пустую строку if (string.IsNullOrEmpty(e.FormattedValue.ToString())) { string message = string.Empty; switch (colType) { case "parameter": message = "Ошибка: на этом этапе нельзя редактировать значения оптимизируемых параметров"; break; case "criterion": message = "Значение критерия '" + this._model.Criteria[colIndex].Name + "' для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " должно быть задано"; break; case "constraint": message = "Значение функционального ограничения '" + this._model.FunctionalConstraints[colIndex].Name + "' для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " должно быть задано"; break; } MessageBoxHelper.ShowExclamation(message); e.Cancel = true; return; } // Проверим, число ли это и можно ли его преобразовать в double double newValue = Double.NaN; try { newValue = Convert.ToDouble(e.FormattedValue); } catch (Exception) { string message = string.Empty; switch (colType) { case "parameter": message = "Ошибка: на этом этапе нельзя редактировать значения оптимизируемых параметров"; break; case "criterion": message = "Невозможно преобразовать введенное для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " значение критерия '" + this._model.Criteria[colIndex].Name + "' в число"; break; case "constraint": message = "Невозможно преобразовать введенное для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " значение функционального ограничения '" + this._model.FunctionalConstraints[colIndex].Name + "' в число"; break; } MessageBoxHelper.ShowError(message); e.Cancel = true; return; } // Это число и все ок? if (newValue != Double.NaN) { // Проверять, вписывается ли введенное значение в допустимый диапазон // для оптимизируемого параметра, не будем, потому что на этом этапе // нельзя изменять значения оптимизируемых параметров // Наконец-то сохраним введенное значение! switch (colType) { case "criterion": if (this._model.Experiments[expIndex].CriterionValues.ContainsKey(colIndex)) { this._model.Experiments[expIndex].CriterionValues[colIndex] = newValue; } else { this._model.Experiments[expIndex].CriterionValues.Add( colIndex, newValue); } break; case "constraint": if (this._model.Experiments[expIndex].ConstraintValues.ContainsKey(colIndex)) { this._model.Experiments[expIndex].ConstraintValues[colIndex] = newValue; } else { this._model.Experiments[expIndex].ConstraintValues.Add( colIndex, newValue); } break; } } }
private void dgvParameterValues_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { TId expIndex = (TId)this.dgvParameterValues[0, e.RowIndex].Value; TId parIndex = TId.Parse(this.dgvParameterValues.Columns[e.ColumnIndex].Name); // Проверим на пустую строку if (string.IsNullOrEmpty(e.FormattedValue.ToString())) { MessageBoxHelper.ShowExclamation( "Значение параметра '" + this._model.Parameters[parIndex].Name + "' для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " должно быть задано"); e.Cancel = true; return; } // Проверим, число ли это и можно ли его преобразовать в double double newValue = Double.NaN; try { newValue = Convert.ToDouble(e.FormattedValue); } catch (Exception) { MessageBoxHelper.ShowError( "Невозможно преобразовать введенное для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " значение параметра '" + this._model.Parameters[parIndex].Name + "' в число"); e.Cancel = true; return; } // Это число и все ок? if (newValue != Double.NaN) { // Вписывается ли оно в допустимый диапазон if (newValue > this._model.Parameters[parIndex].MaxValue || newValue < this._model.Parameters[parIndex].MinValue) { MessageBoxHelper.ShowError( "Введенное для эксперимента №" + this._model.Experiments[expIndex].Number.ToString() + " значение параметра '" + this._model.Parameters[parIndex].Name + "' выходит за границы допустимого диапазона\n" + "Минимально возможное значение параметра '" + this._model.Parameters[parIndex].Name + "': " + this._model.Parameters[parIndex].MinValue.ToString(SettingsManager.Instance.DoubleStringFormat) + "\nМаксимально возможное значение параметра '" + this._model.Parameters[parIndex].Name + "': " + this._model.Parameters[parIndex].MaxValue.ToString(SettingsManager.Instance.DoubleStringFormat)); e.Cancel = true; return; } // Наконец-то сохраним введенное значение! if (this._model.Experiments[expIndex].ParameterValues.ContainsKey(parIndex)) { this._model.Experiments[expIndex].ParameterValues[parIndex] = newValue; } else { this._model.Experiments[expIndex].ParameterValues.Add( parIndex, newValue); } } }