/// <summary> /// Writes <paramref name="modelDraft"/> to XML file /// </summary> /// <param name="modelDraft"><see cref="ModelDraft"/> instance to be written to XML</param> /// <param name="filePath">Full path to target XML file</param> public static void Save(ModelDraft modelDraft, string filePath) { if (modelDraft == null) { throw new ArgumentNullException("modelDraft"); } if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); } XDocument modelDraftDocument = new XDocument(new XDeclaration("1.0", "UTF-8", null)); WriteModelDraftElement(modelDraftDocument); WritePromotableConstants(modelDraft, modelDraftDocument.Root); WritePromotableCriteria(modelDraft, modelDraftDocument.Root); FileInfo modelDraftFileInfo = new FileInfo(filePath); using (FileStream modelDraftFileStream = modelDraftFileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.None)) using (XmlWriter modelDraftFileWriter = XmlWriter.Create(modelDraftFileStream, modelFileWriterSettings)) { modelDraftDocument.Save(modelDraftFileWriter); } }
/// <summary> /// Converts instance of <see cref="ModelDraft"/> into a new instance of <see cref="Model"/> /// </summary> /// <param name="modelDraft"><see cref="ModelDraft"/> instance to be converted</param> /// <returns>New instance of <see cref="Model"/> created based on the data from <paramref name="modelDraft"/></returns> public static Model Convert(ModelDraft modelDraft) { if (modelDraft == null) { throw new ArgumentNullException("modelDraft"); } Model model = new Model(); CopyParameters(modelDraft.PromotableConstants, model.Parameters); CopyCriteria(modelDraft.PromotableCriteria, model.Criteria); CopyConstraints(modelDraft.PromotableCriteria, model.FunctionalConstraints); return(model); }
private static void WriteConstants(ModelDraft modelDraft, XElement modelConstantsDocumentRoot) { IEnumerable <PromotableConstant> constants = modelDraft.PromotableConstants.Values.Where(constant => !constant.IsPromoted); foreach (PromotableConstant constant in constants) { XElement constantElement = new XElement(Elements.Constant, new XAttribute(Attributes.Id, constant.Id.ToString()), new XAttribute(Attributes.Name, constant.Name), new XAttribute(Attributes.VariableIdentifier, constant.VariableIdentifier), new XAttribute(Attributes.Value, constant.Value.ToStringInvariant()) ); modelConstantsDocumentRoot.Add(constantElement); } }
/// <summary> /// Reads <see cref="ModelDraft"/> from XML file /// </summary> /// <param name="filePath">Full path to model XML file</param> /// <returns><see cref="ModelDraft"/> instance read from <paramref name="filePath"/></returns> public static ModelDraft Open(string filePath) { ModelDraft modelDraft = new ModelDraft(); using (FileStream modelDraftFileStream = File.OpenRead(filePath)) using (XmlReader modelDraftFileReader = XmlReader.Create(filePath, modelFileReaderSettings)) { XDocument modelDraftFileDocument = XDocument.Load(modelDraftFileReader, LoadOptions.None); XElement promotableConstantCollectionElement = ReadCollectionElement(Elements.PromotableConstants, modelDraftFileDocument.Root); XElement promotableCriterionCollectionElement = ReadCollectionElement(Elements.PromotableCriteria, modelDraftFileDocument.Root); ReadPromotableConstants(modelDraft, promotableConstantCollectionElement); ReadPromotableCriteria(modelDraft, promotableCriterionCollectionElement); } return(modelDraft); }
private static void WritePromotableCriteria(ModelDraft modelDraft, XElement modelDraftDocumentRoot) { XElement promotableCriterionCollectionElement = new XElement(Elements.PromotableCriteria); foreach (PromotableCriterion promotableCriterion in modelDraft.PromotableCriteria.Values) { XElement criterionElement = new XElement(Elements.PromotableCriterion, new XAttribute(Attributes.Id, promotableCriterion.Id.ToString()), new XAttribute(Attributes.Name, promotableCriterion.Name), new XAttribute(Attributes.VariableIdentifier, promotableCriterion.VariableIdentifier), new XAttribute(Attributes.IsPromoted, promotableCriterion.IsPromoted.ToString()), new XAttribute(Attributes.Type, promotableCriterion.Type.ToString()), new XAttribute(Attributes.Relation, promotableCriterion.ConstraintRelation.ToString()), new XAttribute(Attributes.Value, promotableCriterion.Value.ToStringInvariant()) ); promotableCriterionCollectionElement.Add(criterionElement); } modelDraftDocumentRoot.Add(promotableCriterionCollectionElement); }
private static void WritePromotableConstants(ModelDraft modelDraft, XElement modelDraftDocumentRoot) { XElement promotableConstantsCollectionElement = new XElement(Elements.PromotableConstants); foreach (PromotableConstant promotableConstant in modelDraft.PromotableConstants.Values) { XElement promotableConstantElement = new XElement(Elements.PromotableConstant, new XAttribute(Attributes.Id, promotableConstant.Id.ToString()), new XAttribute(Attributes.Name, promotableConstant.Name), new XAttribute(Attributes.VariableIdentifier, promotableConstant.VariableIdentifier), new XAttribute(Attributes.IsPromoted, promotableConstant.IsPromoted.ToString()), new XAttribute(Attributes.Value, promotableConstant.Value.ToStringInvariant()), new XAttribute(Attributes.MinValue, promotableConstant.MinValue.ToStringInvariant()), new XAttribute(Attributes.MaxValue, promotableConstant.MaxValue.ToStringInvariant()) ); promotableConstantsCollectionElement.Add(promotableConstantElement); } modelDraftDocumentRoot.Add(promotableConstantsCollectionElement); }
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); } }
public void NewDraft() { Draft = new ModelDraft(); IsChanged = false; }
public ModelDraftController() { Draft = new ModelDraft(); IsChanged = false; }