/// <summary> /// Copies <see cref="NamedModelEntity"/> instances or instances of the derived classes from /// <paramref name="source"/> collection to the <paramref name="destination"/> collection /// </summary> /// <typeparam name="TSource">Type of the source items</typeparam> /// <typeparam name="TDestination">Type of the destination items</typeparam> /// <param name="source">Source collection (copy from)</param> /// <param name="destination">Destination collection (copy to)</param> private static void CopyModelEntites <TSource, TDestination>(NamedModelEntityCollection <TSource> source, NamedModelEntityCollection <TDestination> destination) where TSource : TDestination where TDestination : NamedModelEntity, ICloneable { foreach (TSource item in source.Values) { destination.Add((TDestination)item.Clone()); } }
/// <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); } }
private static void CopyEntities <TEntity>(IEnumerable <TEntity> source, NamedModelEntityCollection <TEntity> destination) where TEntity : NamedModelEntity { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } destination.Clear(); foreach (TEntity entity in source) { destination.Add(entity); } }
/// <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 <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> /// Copies references to <see cref="Parameter"/> instances from <paramref name="source"/> collection to the /// <paramref name="destination"/> collection, optionally - with IDs shift /// </summary> /// <param name="source">Source collection (copy from)</param> /// <param name="destination">Destination collection (copy to)</param> /// <param name="shiftIds">If True, method will create a deep copy of each <see cref="Parameter"/> instance /// and assign it new free ID from <paramref name="destination"/> collection</param> /// <returns>If <paramref name="shiftIds"/> is True, method will return a map of IDs: key is an ID of /// <see cref="Parameter"/> instance in <paramref name="source"/> and value is this instance's ID in the /// <paramref name="destination"/></returns> private static Dictionary <TId, TId> CopyParameters(NamedModelEntityCollection <Parameter> source, NamedModelEntityCollection <Parameter> destination, bool shiftIds = false) { Dictionary <TId, TId> idMap = new Dictionary <TId, TId>(source.Count); foreach (Parameter parameter in source.Values) { Parameter toAdd = parameter; if (shiftIds) { TId newId = destination.GetFreeConsequentId(); // Here we are leaving Custom Properties behind, but that should be fine for now toAdd = new Parameter(newId, parameter.Name, parameter.VariableIdentifier, parameter.MinValue, parameter.MaxValue); idMap.Add(parameter.Id, newId); } destination.Add(toAdd); } return(idMap); }