/// <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 = ConvertExtensions.ToDoubleInvariant(valueElement.Attribute(Attributes.Value).Value); collection.Add(id, value); } }
/// <summary> /// Reads a collection of <see cref="Parameter"/> from XML. /// </summary> /// <param name="model"><see cref="Model"/> instance to be read from XML.</param> /// <param name="parametersCollectionElement"><see cref="XElement"/> to read a collection from.</param> private static void ReadParameters(Model model, XElement parametersCollectionElement) { IEnumerable <XElement> parameterElements = parametersCollectionElement.Descendants(Elements.Parameter); 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 = ConvertExtensions.ToDoubleInvariant(parameterElement.Attribute(Attributes.MinValue).Value); double maxValue = ConvertExtensions.ToDoubleInvariant(parameterElement.Attribute(Attributes.MaxValue).Value); Parameter parameter = new Parameter(id, name, variableIdentifier, minValue, maxValue); ReadPropertyCollection(parameter.Properties, ReadCollectionElement(Elements.Properties, parameterElement, false)); model.Parameters.Add(parameter); } }
/// <summary> /// Reads a collection of <see cref="Constraint"/> from XML. /// </summary> /// <param name="model"><see cref="Model"/> instance to be read from XML.</param> /// <param name="constraintsCollectionElement"><see cref="XElement"/> to read a collection from.</param> private static void ReadConstraints(Model model, 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 = EnumExtensions.Parse <Relation>(constraintElement.Attribute(Attributes.Relation).Value); double value = ConvertExtensions.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)); model.FunctionalConstraints.Add(constraint); } }
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); } }
/// <summary> /// Reads <see cref="Double"/> values of entities represented by <typeparamref name="T"/> from <paramref name="streamReader"/> /// to the <paramref name="valuesCollection"/> /// </summary> /// <typeparam name="T">Type of the entity which value should be read</typeparam> /// <param name="entitiesCollection">Collection of entities to read values of</param> /// <param name="valuesCollection">A collection to read values to</param> /// <param name="streamReader"><see cref="StreamReader"/> instance used to read values</param> private static void ReadValues <T>(IEnumerable <T> entitiesCollection, IDictionary <TId, double> valuesCollection, StreamReader streamReader) where T : ModelEntity { if (entitiesCollection == null) { throw new ArgumentNullException("entitiesCollection"); } if (valuesCollection == null) { throw new ArgumentNullException("valuesCollection"); } if (streamReader == null) { throw new ArgumentNullException("streamReader"); } foreach (T entity in entitiesCollection) { string valueString = streamReader.ReadLine(); if (string.IsNullOrEmpty(valueString)) { throw new InvalidOperationException("Cannot read Double value from file stream"); } double value = ConvertExtensions.ToDoubleInvariant(valueString); if (valuesCollection.ContainsKey(entity.Id)) { valuesCollection[entity.Id] = value; } else { valuesCollection.Add(entity.Id, value); } } }