/// <summary> /// Validate the correctness of this Model System /// </summary> /// <param name="error">A description of the error, if one is found</param> /// <returns>If an error was found</returns> public bool Validate(ref string error) { if (ModelSystemStructure == null) { error = "No model system structure is present in this model system!"; return(false); } else if (string.IsNullOrEmpty(Name)) { error = "This model system does not have a name!"; return(false); } // Since we are going to be storing these things, we need to make sure that invalid characters are not // included in the model system names foreach (var invalidChar in Path.GetInvalidFileNameChars()) { if (Name.Contains(invalidChar)) { error = string.Format("The character {0} is not allowed in a Model System's name.", invalidChar); return(false); } } // Make sure that the structure itself is valid if (!ModelSystemStructure.Validate(ref error)) { return(false); } return(true); }
public ModelSystemStructureModel(ModelSystemEditingSession session, ModelSystemStructure realModelSystemStructure) { Session = session; RealModelSystemStructure = realModelSystemStructure; Parameters = new ParametersModel(this, session); Children = CreateChildren(Session, RealModelSystemStructure); }
private static List <Type> GatherAllTypes(ModelSystemStructure start) { List <Type> ret = new List <Type>(); GatherAllTypes(start, ret); return(ret); }
public IModelSystemStructure Clone() { ModelSystemStructure cloneUs = new ModelSystemStructure(this.Configuration); cloneUs.Name = this.Name; cloneUs.Description = this.Description; cloneUs.Module = this.Module; if (this.Parameters != null) { if ((cloneUs.Parameters = this.Parameters.Clone()) != null) { (cloneUs.Parameters as ModuleParameters).BelongsTo = cloneUs; foreach (var p in cloneUs.Parameters) { (p as ModuleParameter).BelongsTo = cloneUs; } } } cloneUs.Required = this.Required; cloneUs.ParentFieldName = this.ParentFieldName; cloneUs.ParentFieldType = this.ParentFieldType; cloneUs._Type = this._Type; cloneUs.IsCollection = this.IsCollection; if (this.Children != null) { foreach (var child in this.Children) { cloneUs.Add(child.Clone()); } } return(cloneUs); }
/// <summary> /// Create a clone of this model system /// </summary> /// <param name="linkedParameters">The linked parameters</param> /// <returns>A cloned model system that can be used for editing.</returns> internal ModelSystemStructure CreateEditingClone(out List <ILinkedParameter> linkedParameters) { var ourClone = ModelSystemStructure.Clone(); linkedParameters = LinkedParameters.Count > 0 ? LinkedParameter.MapLinkedParameters(LinkedParameters, ourClone, ModelSystemStructure) : new List <ILinkedParameter>(); return(ourClone as ModelSystemStructure); }
private bool IsAssignable(ModelSystemStructure rootStructure, ModelSystemStructure parentStructure, ModelSystemStructure copyBuffer) { // This will update what module we are using for the root as per the Re-rootable extension for XTMF try { var parent = parentStructure == null ? typeof(IModelSystemTemplate) : parentStructure.Type; if (copyBuffer.IsCollection) { // Make sure that we are doing collection to collection and that they are of the right types if (!this.IsCollection || !this.RealModelSystemStructure.ParentFieldType.IsAssignableFrom(copyBuffer.ParentFieldType)) { return(false); } // now make sure that every new element is alright with the parent and root var parentType = this.RealModelSystemStructure.ParentFieldType; var arguements = parentType.IsArray ? parentType.GetElementType() : parentType.GetGenericArguments()[0]; foreach (var member in copyBuffer.Children) { var t = member.Type; if (arguements.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) != null) { return(true); } } } else { var t = copyBuffer.Type; rootStructure = ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) as ModelSystemStructure; if (this.IsCollection) { var parentType = Session.GetParent(this).Type; var arguements = ParentFieldType.IsArray ? ParentFieldType.GetElementType() : ParentFieldType.GetGenericArguments()[0]; if (arguements.IsAssignableFrom(t) && (ModelSystemStructure.CheckForParent(parentType, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, this.RealModelSystemStructure, t) != null) { return(true); } } else { if (this.RealModelSystemStructure.ParentFieldType.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) != null) { return(true); } } } } catch { return(false); } return(false); }
/// <summary> /// Create a clone of this model system /// </summary> /// <param name="linkedParameters">The linked parameters</param> /// <returns>A cloned model system that can be used for editing.</returns> internal ModelSystemStructure CreateEditingClone(out List <ILinkedParameter> linkedParameters, out List <IRegionDisplay> regionDisplays) { var ourClone = ModelSystemStructure.Clone(); linkedParameters = LinkedParameters.Count > 0 ? LinkedParameter.MapLinkedParameters(LinkedParameters, ourClone, ModelSystemStructure) : new List <ILinkedParameter>(); regionDisplays = RegionDisplay.MapRegionDisplays(this._regionDisplays, ourClone); return(ourClone as ModelSystemStructure); }
public bool Save(string fileName, ref string error) { string tempFileName = Path.GetTempFileName(); try { using (XmlWriter writer = XmlWriter.Create(tempFileName, new XmlWriterSettings() { Indent = true, Encoding = Encoding.Unicode })) { writer.WriteStartDocument(); writer.WriteStartElement("Root"); writer.Flush(); ModelSystemStructure.Save(writer); if (Description != null) { writer.WriteStartElement("Description"); writer.WriteString(Description); writer.WriteEndElement(); } if (LinkedParameters != null) { foreach (var lp in LinkedParameters) { writer.WriteStartElement("LinkedParameter"); writer.WriteAttributeString("Name", lp.Name); if (lp.Value != null) { writer.WriteAttributeString("Value", lp.Value.ToString()); } foreach (var reference in lp.Parameters) { writer.WriteStartElement("Reference"); writer.WriteAttributeString("Name", LookupName(reference)); writer.WriteEndElement(); } writer.WriteEndElement(); } } writer.WriteEndDocument(); } } catch (Exception e) { Description = string.Empty; error = e.Message; return(false); } File.Copy(tempFileName, fileName, true); File.Delete(tempFileName); return(true); }
public static IModelSystemStructure Load(Stream stream, IConfiguration config) { ModelSystemStructure root = new ModelSystemStructure(config); root.Description = "The Model System Template that the project is based on"; root.Required = true; root.ParentFieldType = typeof(IModelSystemTemplate); root.ParentFieldName = "Root"; XmlDocument doc = new XmlDocument(); doc.Load(stream); LoadRoot(config, root, doc["Root"].ChildNodes); return(root); }
private static void LoadCollection(IModelSystemStructure parent, XmlNode child, IConfiguration config, Dictionary <int, Type> lookUp) { var paramNameAttribute = child.Attributes["ParentFieldName"]; var paramTIndexAttribute = child.Attributes["ParentTIndex"]; var paramTypeAttribute = child.Attributes["ParentFieldType"]; var NameAttribute = child.Attributes["Name"]; IModelSystemStructure us = null; if (paramNameAttribute != null && (paramTIndexAttribute != null || paramTypeAttribute != null)) { if (parent.Children == null) { return; } for (int i = 0; i < parent.Children.Count; i++) { if (parent.Children[i].ParentFieldName == paramNameAttribute.InnerText) { us = parent.Children[i]; break; } } if (us != null) { us.ParentFieldType = AquireTypeFromField(parent, us.ParentFieldName); if (NameAttribute != null) { us.Name = NameAttribute.InnerText; } us.ParentFieldName = paramNameAttribute.InnerText; // now load the children if (child.HasChildNodes) { foreach (XmlNode element in child.ChildNodes) { XTMF.ModelSystemStructure ps = new ModelSystemStructure(config); Load(ps, us, element, config, lookUp); if (ps.ParentFieldType == null || ps.ParentFieldName == null) { ps.ParentFieldName = us.ParentFieldName; ps.ParentFieldType = us.Type; } us.Children.Add(ps); } } } } }
public IModelSystemStructure CreateCollectionMember(Type newType) { if (this.IsCollection) { if (this.Children == null) { this.Children = new List <IModelSystemStructure>(); } ModelSystemStructure p = new ModelSystemStructure(this.Configuration); Type innerType = this.ParentFieldType.IsArray ? this.ParentFieldType.GetElementType() : this.ParentFieldType.GetGenericArguments()[0]; p.Type = newType; p.ParentFieldType = innerType; p.ParentFieldName = this.ParentFieldName; p.Name = CreateModuleName(newType.Name); return(p); } return(null); }
private ModelSystemStructureModel GetModelFor(ModelSystemStructure realStructure, ModelSystemStructureModel current) { if (current.RealModelSystemStructure == realStructure) { return(current); } var children = current.Children; if (children != null) { for (int i = 0; i < children.Count; i++) { ModelSystemStructureModel ret; if ((ret = GetModelFor(realStructure, children[i])) != null) { return(ret); } } } return(null); }
public static IModelSystemStructure Load(string fileName, IConfiguration config) { ModelSystemStructure root = new ModelSystemStructure(config); root.Description = "The Model System Template that the project is based on"; root.Required = true; root.ParentFieldType = typeof(IModelSystemTemplate); root.ParentFieldName = "Root"; if (!File.Exists(fileName)) { return(root); } XmlDocument doc = new XmlDocument(); doc.Load(fileName); var list = doc["Root"].ChildNodes; LoadRoot(config, root, list); return(root); }
private static void LoadRoot(IConfiguration config, ModelSystemStructure root, XmlNodeList list) { if (list != null) { var lookUp = new Dictionary <int, Type>(20); for (int i = 0; i < list.Count; i++) { var child = list[i]; if (child.LocalName == "TypeDefinitions") { LoadDefinitions(child, lookUp); } } for (int i = 0; i < list.Count; i++) { var child = list[i]; if (child.LocalName == "Module") { Load(root, null, list[i], config, lookUp); } } } }
/// <summary> /// Check to see if a type is valid for a module. /// </summary> /// <param name="type">The type to check for.</param> /// <param name="topLevelModule">The top level module</param> /// <param name="error"></param> /// <returns></returns> internal bool CheckPossibleModule(Type type, ModelSystemStructure topLevelModule, ref string error) { var rootRequirement = GetRootRequirement(type); var parent = GetParent(topLevelModule, this); if (this.IsCollection) { var arguements = this.ParentFieldType.IsArray ? this.ParentFieldType.GetElementType() : this.ParentFieldType.GetGenericArguments()[0]; if(!(arguements.IsAssignableFrom(type) && (CheckForParent(parent.Type, type)) && CheckForRootModule(topLevelModule, this, rootRequirement) != null)) { if(!arguements.IsAssignableFrom(type)) { error = "The type is not valid for the collection!"; } else if(!CheckForParent(parent.Type, type)) { error = "This type requires a different parent type!"; } else if(CheckForRootModule(topLevelModule, this, rootRequirement) == null) { error = "There is no root module that can support this type at this position!"; } return false; } } else { if(!(this.ParentFieldType.IsAssignableFrom(type) && (parent == null || CheckForParent(parent.Type, type)) && CheckForRootModule(topLevelModule, this, rootRequirement) != null)) { if(!this.ParentFieldType.IsAssignableFrom(type)) { error = "This type does not meet the requirements of the parent!"; } else if(!(parent == null || CheckForParent(parent.Type, type))) { error = "The type does not support the parent as a valid option!"; } else if(CheckForRootModule(topLevelModule, this, rootRequirement) == null) { error = "There is no root module that can support this type at this position!"; } return false; } } return true; }
internal static ModelSystemStructure Load(XmlNode modelSystemNode, IConfiguration config) { XTMF.ModelSystemStructure structure = new ModelSystemStructure(config); LoadRoot(config, structure, modelSystemNode.ChildNodes); return structure; }
/// <summary> /// Load a model system structure from file. /// </summary> /// <param name="runFile">The file to load from.</param> /// <returns>The model system structure located at that file.</returns> public ModelSystemStructure LoadFromRunFile(string runFile) { return(ModelSystemStructure.Load(runFile, Runtime.Configuration) as ModelSystemStructure); }
/// <summary> /// /// </summary> /// <param name="directory"></param> /// <param name="guid"></param> /// <param name="pms"></param> /// <returns></returns> private bool LoadDetachedModelSystem(string directory, string guid, out ProjectModelSystem pms) { var msPath = Path.Combine(_DirectoryLocation, "._ModelSystems", $"Project.ms-{guid}.xml"); if (!File.Exists(msPath)) { msPath = Path.Combine(_DirectoryLocation, $"Project.ms-{guid}.xml"); if (!File.Exists(msPath)) { pms = null; return(false); } else { var newMsPath = Path.Combine(EnsureModelSystemDirectoryExists(_DirectoryLocation), $"Project.ms-{guid}.xml"); File.Move(msPath, newMsPath); msPath = newMsPath; } } XmlDocument msDoc = new XmlDocument(); msDoc.Load(msPath); pms = new ProjectModelSystem() { GUID = guid }; var child = msDoc["Root"] ?? msDoc["AdvancedModelSystem"]; bool hasDescription = false; var attributes = child.Attributes; if (attributes != null) { foreach (XmlAttribute attribute in attributes) { if (attribute.Name == "Description") { hasDescription = true; pms.Description = attribute.InnerText; break; } } } if (child.HasChildNodes) { ModelSystemStructure ms = XTMF.ModelSystemStructure.Load(child, _Configuration); if (ms != null) { pms.Root = ms; } } if (pms.Root == null) { return(false); } if (!hasDescription) { pms.Description = pms.Root.Description; } // now do a second pass for Linked parameters, since we need the current model system to actually link things for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "LastModified": { var result = DateTime.TryParse(child.ChildNodes[i].Attributes?["Time"]?.InnerText, out var modified); pms.LastModified = result ? modified : DateTime.Now; break; } case "LinkedParameters": { pms.LinkedParameters = LoadLinkedParameters(child.ChildNodes[i], pms.Root); break; } case "Regions": { pms.RegionDisplays = LoadRegionDisplays(child.ChildNodes[i], pms.Root); } break; } } return(true); }
private static List<Type> GatherAllTypes(ModelSystemStructure start) { List<Type> ret = new List<Type>(); GatherAllTypes(start, ret); return ret; }
private static void LoadCollection(IModelSystemStructure parent, XmlNode child, IConfiguration config, Dictionary<int, Type> lookUp) { var paramNameAttribute = child.Attributes["ParentFieldName"]; var paramTIndexAttribute = child.Attributes["ParentTIndex"]; var paramTypeAttribute = child.Attributes["ParentFieldType"]; var NameAttribute = child.Attributes["Name"]; IModelSystemStructure us = null; if(paramNameAttribute != null && (paramTIndexAttribute != null || paramTypeAttribute != null)) { if(parent.Children == null) { return; } for(int i = 0; i < parent.Children.Count; i++) { if(parent.Children[i].ParentFieldName == paramNameAttribute.InnerText) { us = parent.Children[i]; break; } } if(us != null) { us.ParentFieldType = AquireTypeFromField(parent, us.ParentFieldName); if(NameAttribute != null) { us.Name = NameAttribute.InnerText; } us.ParentFieldName = paramNameAttribute.InnerText; // now load the children if(child.HasChildNodes) { foreach(XmlNode element in child.ChildNodes) { XTMF.ModelSystemStructure ps = new ModelSystemStructure(config); Load(ps, us, element, config, lookUp); if(ps.ParentFieldType == null || ps.ParentFieldName == null) { ps.ParentFieldName = us.ParentFieldName; ps.ParentFieldType = us.Type; } us.Children.Add(ps); } } } } }
private ModelSystemStructure GetModelSystemStructureFromXML(XmlNode rootMSChild) { return(ModelSystemStructure.Load(rootMSChild, Session.Configuration)); }
public bool Paste(string buffer, ref string error) { ModelSystemStructure copiedStructure; List <TempLinkedParameter> linkedParameters; // Get the data using (MemoryStream backing = new MemoryStream()) { StreamWriter writer = new StreamWriter(backing); writer.Write(buffer); writer.Flush(); backing.Position = 0; try { XmlDocument doc = new XmlDocument(); doc.Load(backing); copiedStructure = GetModelSystemStructureFromXML(doc["CopiedModule"]["CopiedModules"]); linkedParameters = GetLinkedParametersFromXML(doc["CopiedModule"]["LinkedParameters"]); } catch (Exception e) { error = "Unable to decode the copy buffer.\r\n" + e.Message; return(false); } } if (copiedStructure.IsCollection) { if (!IsCollection) { error = "The copied model system is not pasteable at this location."; return(false); } foreach (var child in copiedStructure.Children) { if (!IsAssignable(Session.ModelSystemModel.Root.RealModelSystemStructure, IsCollection ? RealModelSystemStructure : Session.GetParent(this).RealModelSystemStructure, child as ModelSystemStructure)) { error = "The copied model system is not pasteable at this location."; return(false); } } } else { // validate the modules contained if (!IsAssignable(Session.ModelSystemModel.Root.RealModelSystemStructure, IsCollection ? RealModelSystemStructure : Session.GetParent(this).RealModelSystemStructure, copiedStructure)) { error = "The copied model system is not pasteable at this location."; return(false); } } List <LinkedParameterModel> newLinkedParameters = new List <LinkedParameterModel>(); var additions = new List <Tuple <ParameterModel, LinkedParameterModel> >(); var oldReal = RealModelSystemStructure; return(Session.RunCommand(XTMFCommand.CreateCommand( (ref string e) => { ModelSystemStructureModel beingAdded; int indexOffset = 0; if (IsCollection) { if (copiedStructure.IsCollection) { indexOffset = RealModelSystemStructure.Children != null ? RealModelSystemStructure.Children.Count : 0; foreach (var child in copiedStructure.Children) { RealModelSystemStructure.Add(child); } UpdateChildren(); beingAdded = this; } else { RealModelSystemStructure.Add(copiedStructure); UpdateChildren(); beingAdded = Children[Children.Count - 1]; } } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if (modelSystemRoot == RealModelSystemStructure) { copiedStructure.Required = RealModelSystemStructure.Required; copiedStructure.ParentFieldType = RealModelSystemStructure.ParentFieldType; copiedStructure.ParentFieldName = RealModelSystemStructure.ParentFieldName; Session.ModelSystemModel.Root.RealModelSystemStructure = copiedStructure; } else { var parent = ModelSystemStructure.GetParent(modelSystemRoot, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); copiedStructure.Required = RealModelSystemStructure.Required; copiedStructure.ParentFieldType = RealModelSystemStructure.ParentFieldType; copiedStructure.ParentFieldName = RealModelSystemStructure.ParentFieldName; RealModelSystemStructure = copiedStructure; parent.Children[index] = copiedStructure; } UpdateAll(); beingAdded = this; } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; var realLinkedParameters = linkedParameterModel.GetLinkedParameters(); var missing = from lp in linkedParameters where !realLinkedParameters.Any(rlp => rlp.Name == lp.Name) select lp; var matching = linkedParameters.Join(realLinkedParameters, (p) => p.Name, (p) => p.Name, (t, r) => new { Real = r, Temp = t }); // add links for the ones we've matched foreach (var lp in matching) { foreach (var containedParameters in GetParametersFromTemp(lp.Temp, beingAdded, indexOffset)) { lp.Real.AddParameterWithoutCommand(containedParameters); containedParameters.SignalIsLinkedChanged(); additions.Add(new Tuple <ParameterModel, LinkedParameterModel>(containedParameters, lp.Real)); } } // add links for the ones that didn't match foreach (var missingLp in missing) { var newLP = linkedParameterModel.AddWithoutCommand(missingLp.Name, missingLp.Value); newLinkedParameters.Add(newLP); foreach (var containedParameters in GetParametersFromTemp(missingLp, beingAdded, indexOffset)) { newLP.AddParameterWithoutCommand(containedParameters); containedParameters.SignalIsLinkedChanged(); } } return true; }, (ref string e) => { if (IsCollection) { if (copiedStructure.IsCollection) { foreach (var child in copiedStructure.Children) { RealModelSystemStructure.Children.Remove(child); } } else { RealModelSystemStructure.Children.Remove(copiedStructure); } UpdateChildren(); } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if (modelSystemRoot == RealModelSystemStructure) { RealModelSystemStructure = oldReal; Session.ModelSystemModel.Root.RealModelSystemStructure = oldReal; } else { var parent = ModelSystemStructure.GetParent(Session.ModelSystemModel.Root.RealModelSystemStructure, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); RealModelSystemStructure = oldReal; parent.Children[index] = RealModelSystemStructure; } UpdateAll(); } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; foreach (var newLP in newLinkedParameters) { linkedParameterModel.RemoveWithoutCommand(newLP); } foreach (var addition in additions) { addition.Item2.RemoveParameterWithoutCommand(addition.Item1); } return true; }, (ref string e) => { if (IsCollection) { if (copiedStructure.IsCollection) { foreach (var child in copiedStructure.Children) { RealModelSystemStructure.Add(child); } } else { RealModelSystemStructure.Add(copiedStructure); } UpdateChildren(); } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if (modelSystemRoot == RealModelSystemStructure) { RealModelSystemStructure = copiedStructure; Session.ModelSystemModel.Root.RealModelSystemStructure = RealModelSystemStructure; } else { var parent = ModelSystemStructure.GetParent(Session.ModelSystemModel.Root.RealModelSystemStructure, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); RealModelSystemStructure = copiedStructure; parent.Children[index] = RealModelSystemStructure; } UpdateAll(); } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; foreach (var newLP in newLinkedParameters) { linkedParameterModel.AddWithoutCommand(newLP); } foreach (var addition in additions) { addition.Item2.AddParameterWithoutCommand(addition.Item1); } return true; }), ref error)); }
private ObservableCollection <ModelSystemStructureModel> CreateChildren(ModelSystemEditingSession session, ModelSystemStructure realModelSystemStructure) { if (realModelSystemStructure.Children == null) { return(new ObservableCollection <ModelSystemStructureModel>()); } ObservableCollection <ModelSystemStructureModel> ret; if (Children == null) { ret = new ObservableCollection <ModelSystemStructureModel>(); for (int i = 0; i < realModelSystemStructure.Children.Count; i++) { ret.Add(new ModelSystemStructureModel(session, realModelSystemStructure.Children[i] as ModelSystemStructure)); } } else { ret = Children; if (realModelSystemStructure.Children == null) { ret.Clear(); } else { // remove children var removedChildren = (from child in Children where !realModelSystemStructure.Children.Any(r => r == child.RealModelSystemStructure) select child).ToArray(); // new children go to the end var newChildren = (from child in realModelSystemStructure.Children where !Children.Any(c => c.RealModelSystemStructure == child) select child).ToArray(); foreach (var child in removedChildren) { ret.Remove(child); } foreach (var child in newChildren) { ret.Add(new ModelSystemStructureModel(session, child as ModelSystemStructure)); } bool repeat = false; do { // now search for children that have moved indexes after adds and deleted have been performed var indexes = (from child in Children select realModelSystemStructure.Children.IndexOf(child.RealModelSystemStructure)).ToArray(); for (int i = 0; i < indexes.Length; i++) { // if a child has moved if (indexes[i] != i) { Children.Move(i, indexes[i]); repeat = true; break; } } } while (repeat); } } return(ret); }
private void LoadCollection(IModelSystemStructure projectStructure, XmlNode child) { var paramNameAttribute = child.Attributes["ParentFieldName"]; var paramTypeAttribute = child.Attributes["ParentFieldType"]; IModelSystemStructure us = null; if (paramNameAttribute != null && paramTypeAttribute != null) { if (projectStructure.Children != null) { for (int i = 0; i < projectStructure.Children.Count; i++) { if (projectStructure.Children[i].ParentFieldName == paramNameAttribute.InnerText) { us = projectStructure.Children[i]; break; } } } if (us != null) { us.ParentFieldType = Type.GetType(paramTypeAttribute.InnerText); us.ParentFieldName = paramNameAttribute.InnerText; // now load the children if (child.HasChildNodes) { foreach (XmlNode element in child.ChildNodes) { ModelSystemStructure ps = new ModelSystemStructure(Configuration); Load(ps, element); if (ps.ParentFieldType == null || ps.ParentFieldName == null) { ps.ParentFieldName = us.ParentFieldName; ps.ParentFieldType = us.Type; } us.Children.Add(ps); } } } } }
/// <summary> /// /// </summary> /// <param name="child"></param> /// <param name="index"></param> /// <param name="guid"></param> /// <param name="pms"></param> /// <returns></returns> private bool LoadAdvancedModelSystem(XmlNode child, int index, string guid, out ProjectModelSystem pms) { pms = new ProjectModelSystem() { GUID = guid }; bool hasDescription = false; var attributes = child.Attributes; if (attributes != null) { foreach (XmlAttribute attribute in attributes) { if (attribute.Name == "Description") { hasDescription = true; pms.Description = attribute.InnerText; break; } } } if (!hasDescription) { pms.Description = "No Description"; } if (child.HasChildNodes) { for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "ModelSystem": { if (pms.Root == null) { if (child.ChildNodes[i].FirstChild != null) { ModelSystemStructure ms = XTMF.ModelSystemStructure.Load(child.ChildNodes[i], _Configuration); if (ms != null) { pms.Root = ms; } } } } break; } } if (pms.Root == null) { return(false); } // now do a second pass for Linked parameters, since we need the current model system to actually link things for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "LastModified": { var result = DateTime.TryParse(child.ChildNodes[i].Attributes?["Time"]?.InnerText, out var modified); pms.LastModified = result ? modified : DateTime.Now; break; } case "LinkedParameters": { pms.LinkedParameters = LoadLinkedParameters(child.ChildNodes[i], pms.Root); break; } case "Regions": { pms.RegionDisplays = LoadRegionDisplays(child.ChildNodes[i], pms.Root); } break; } } } return(true); }
internal ModelSystemStructure GetParent(ModelSystemStructure realModelSystemStructure) { return ModelSystemStructure.GetParent(realModelSystemStructure, this) as ModelSystemStructure; }
public bool Paste(string buffer, ref string error) { ModelSystemStructure copiedStructure; List<TempLinkedParameter> linkedParameters; // Get the data using (MemoryStream backing = new MemoryStream()) { StreamWriter writer = new StreamWriter(backing); writer.Write(buffer); writer.Flush(); backing.Position = 0; try { XmlDocument doc = new XmlDocument(); doc.Load(backing); copiedStructure = GetModelSystemStructureFromXML(doc["CopiedModule"]["CopiedModules"]); linkedParameters = GetLinkedParametersFromXML(doc["CopiedModule"]["LinkedParameters"]); } catch { error = "Unable to decode the copy buffer."; return false; } } if(copiedStructure.IsCollection) { if(!IsCollection) { error = "The copied model system is not pasteable at this location."; return false; } foreach(var child in copiedStructure.Children) { if(!IsAssignable(Session.ModelSystemModel.Root.RealModelSystemStructure, IsCollection ? RealModelSystemStructure : Session.GetParent(this).RealModelSystemStructure, child as ModelSystemStructure)) { error = "The copied model system is not pasteable at this location."; return false; } } } else { // validate the modules contained if(!IsAssignable(Session.ModelSystemModel.Root.RealModelSystemStructure, IsCollection ? RealModelSystemStructure : Session.GetParent(this).RealModelSystemStructure, copiedStructure)) { error = "The copied model system is not pasteable at this location."; return false; } } List<LinkedParameterModel> newLinkedParameters = new List<LinkedParameterModel>(); var additions = new List<Tuple<ParameterModel, LinkedParameterModel>>(); var oldReal = RealModelSystemStructure; return Session.RunCommand(XTMFCommand.CreateCommand( (ref string e) => { ModelSystemStructureModel beingAdded; int indexOffset = 0; if(IsCollection) { if(copiedStructure.IsCollection) { indexOffset = RealModelSystemStructure.Children != null ? RealModelSystemStructure.Children.Count : 0; foreach(var child in copiedStructure.Children) { RealModelSystemStructure.Add(child); } UpdateChildren(); beingAdded = this; } else { RealModelSystemStructure.Add(copiedStructure); UpdateChildren(); beingAdded = Children[Children.Count - 1]; } } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if(modelSystemRoot == RealModelSystemStructure) { copiedStructure.Required = RealModelSystemStructure.Required; copiedStructure.ParentFieldType = RealModelSystemStructure.ParentFieldType; copiedStructure.ParentFieldName = RealModelSystemStructure.ParentFieldName; Session.ModelSystemModel.Root.RealModelSystemStructure = copiedStructure; } else { var parent = ModelSystemStructure.GetParent(modelSystemRoot, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); copiedStructure.Required = RealModelSystemStructure.Required; copiedStructure.ParentFieldType = RealModelSystemStructure.ParentFieldType; copiedStructure.ParentFieldName = RealModelSystemStructure.ParentFieldName; RealModelSystemStructure = copiedStructure; parent.Children[index] = copiedStructure; } UpdateAll(); beingAdded = this; } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; var realLinkedParameters = linkedParameterModel.GetLinkedParameters(); var missing = from lp in linkedParameters where !realLinkedParameters.Any(rlp => rlp.Name == lp.Name) select lp; var matching = linkedParameters.Join(realLinkedParameters, (p) => p.Name, (p) => p.Name, (t, r) => new { Real = r, Temp = t }); // add links for the ones we've matched foreach(var lp in matching) { foreach(var containedParameters in GetParametersFromTemp(lp.Temp, beingAdded, indexOffset)) { lp.Real.AddParameterWithoutCommand(containedParameters); containedParameters.SignalIsLinkedChanged(); additions.Add(new Tuple<ParameterModel, LinkedParameterModel>(containedParameters, lp.Real)); } } // add links for the ones that didn't match foreach(var missingLp in missing) { var newLP = linkedParameterModel.AddWithoutCommand(missingLp.Name, missingLp.Value); newLinkedParameters.Add(newLP); foreach(var containedParameters in GetParametersFromTemp(missingLp, beingAdded, indexOffset)) { newLP.AddParameterWithoutCommand(containedParameters); containedParameters.SignalIsLinkedChanged(); } } return true; }, (ref string e) => { if(IsCollection) { if(copiedStructure.IsCollection) { foreach(var child in copiedStructure.Children) { RealModelSystemStructure.Children.Remove(child); } } else { RealModelSystemStructure.Children.Remove(copiedStructure); } UpdateChildren(); } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if(modelSystemRoot == RealModelSystemStructure) { RealModelSystemStructure = oldReal; Session.ModelSystemModel.Root.RealModelSystemStructure = oldReal; } else { var parent = ModelSystemStructure.GetParent(Session.ModelSystemModel.Root.RealModelSystemStructure, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); RealModelSystemStructure = oldReal; parent.Children[index] = RealModelSystemStructure; } UpdateAll(); } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; foreach(var newLP in newLinkedParameters) { linkedParameterModel.RemoveWithoutCommand(newLP); } foreach(var addition in additions) { addition.Item2.RemoveParameterWithoutCommand(addition.Item1); } return true; }, (ref string e) => { if(IsCollection) { if(copiedStructure.IsCollection) { foreach(var child in copiedStructure.Children) { RealModelSystemStructure.Add(child); } } else { RealModelSystemStructure.Add(copiedStructure); } UpdateChildren(); } else { var modelSystemRoot = Session.ModelSystemModel.Root.RealModelSystemStructure; // if we are the root of the model system if(modelSystemRoot == RealModelSystemStructure) { RealModelSystemStructure = copiedStructure; Session.ModelSystemModel.Root.RealModelSystemStructure = RealModelSystemStructure; } else { var parent = ModelSystemStructure.GetParent(Session.ModelSystemModel.Root.RealModelSystemStructure, RealModelSystemStructure); var index = parent.Children.IndexOf(RealModelSystemStructure); RealModelSystemStructure = copiedStructure; parent.Children[index] = RealModelSystemStructure; } UpdateAll(); } var linkedParameterModel = Session.ModelSystemModel.LinkedParameters; foreach(var newLP in newLinkedParameters) { linkedParameterModel.AddWithoutCommand(newLP); } foreach(var addition in additions) { addition.Item2.AddParameterWithoutCommand(addition.Item1); } return true; }), ref error); }
internal ModelSystemStructure GetRoot(ModelSystemStructure modelSystemRoot) { return ModelSystemStructure.CheckForRootModule(modelSystemRoot, this, Type) as ModelSystemStructure; }
private ObservableCollection<ModelSystemStructureModel> CreateChildren(ModelSystemEditingSession session, ModelSystemStructure realModelSystemStructure) { if(realModelSystemStructure.Children == null) return null; ObservableCollection<ModelSystemStructureModel> ret; if(Children == null) { ret = new ObservableCollection<ModelSystemStructureModel>(); for(int i = 0; i < realModelSystemStructure.Children.Count; i++) { ret.Add(new ModelSystemStructureModel(session, realModelSystemStructure.Children[i] as ModelSystemStructure)); } } else { ret = Children; if(realModelSystemStructure.Children == null) { ret.Clear(); } else { // remove children var removedChildren = (from child in Children where !realModelSystemStructure.Children.Any(r => r == child.RealModelSystemStructure) select child).ToArray(); // new children go to the end var newChildren = (from child in realModelSystemStructure.Children where !Children.Any(c => c.RealModelSystemStructure == child) select child).ToArray(); foreach(var child in removedChildren) { ret.Remove(child); } foreach(var child in newChildren) { ret.Add(new ModelSystemStructureModel(session, child as ModelSystemStructure)); } bool repeat = false; do { // now search for children that have moved indexes after adds and deleted have been performed var indexes = (from child in Children select realModelSystemStructure.Children.IndexOf(child.RealModelSystemStructure)).ToArray(); for(int i = 0; i < indexes.Length; i++) { // if a child has moved if(indexes[i] != i) { Children.Move(i, indexes[i]); repeat = true; break; } } } while(repeat); } } return ret; }
private static IModelSystemStructure GenerateChildren(IModelSystemStructure element, Type type, object[] attributes, IConfiguration config) { Type iModel = typeof(IModule); if(type.IsArray) { var argument = type.GetElementType(); if(iModel.IsAssignableFrom(argument)) { ModelSystemStructure child = new ModelSystemStructure(config); child.IsCollection = true; child.Children = new List<IModelSystemStructure>(); foreach(var at in attributes) { if(at is DoNotAutomate) { return null; } else if(at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } if(child.Description == null) { child.Description = "No description available"; child.Required = false; } } return child; } } if(type.IsGenericType) { var arguements = type.GetGenericArguments(); if(arguements != null && arguements.Length == 1) { // if the type of this generic is assignable to IModel.. if(iModel.IsAssignableFrom(arguements[0])) { Type iCollection = typeof(ICollection<>).MakeGenericType(arguements[0]); if(iCollection.IsAssignableFrom(type)) { ModelSystemStructure child = new ModelSystemStructure(config); child.IsCollection = true; child.Children = new List<IModelSystemStructure>(); foreach(var at in attributes) { if(at is DoNotAutomate) { return null; } else if(at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } if(child.Description == null) { child.Description = "No description available"; child.Required = false; } } return child; } } } } if(iModel.IsAssignableFrom(type)) { ModelSystemStructure child = new ModelSystemStructure(config); foreach(var at in attributes) { if(at is ParentModel || at is DoNotAutomate || at is RootModule) { return null; } if(at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } } if(child.Description == null) { child.Description = "No description available"; child.Required = false; } return child; } return null; }
public ModelSystemStructureModel GetModelFor(ModelSystemStructure realStructure) { return(GetModelFor(realStructure, Root)); }
public ModelSystemStructureModel GetModelFor(ModelSystemStructure realStructure) { return GetModelFor(realStructure, Root); }
private ModelSystemStructureModel GetModelFor(ModelSystemStructure realStructure, ModelSystemStructureModel current) { if(current.RealModelSystemStructure == realStructure) { return current; } var children = current.Children; if(children != null) { for(int i = 0; i < children.Count; i++) { ModelSystemStructureModel ret; if((ret = GetModelFor(realStructure, children[i])) != null) { return ret; } } } return null; }
private bool IsAssignable(ModelSystemStructure rootStructure, ModelSystemStructure parentStructure, ModelSystemStructure copyBuffer) { // This will update what module we are using for the root as per the Re-rootable extension for XTMF try { var parent = parentStructure == null ? typeof(IModelSystemTemplate) : parentStructure.Type; if(copyBuffer.IsCollection) { // Make sure that we are doing collection to collection and that they are of the right types if(!this.IsCollection || !this.RealModelSystemStructure.ParentFieldType.IsAssignableFrom(copyBuffer.ParentFieldType)) { return false; } // now make sure that every new element is alright with the parent and root var parentType = this.RealModelSystemStructure.ParentFieldType; var arguements = parentType.IsArray ? parentType.GetElementType() : parentType.GetGenericArguments()[0]; foreach(var member in copyBuffer.Children) { var t = member.Type; if(arguements.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) != null) { return true; } } } else { var t = copyBuffer.Type; rootStructure = ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) as ModelSystemStructure; if(this.IsCollection) { var parentType = this.RealModelSystemStructure.ParentFieldType; var arguements = parentType.IsArray ? parentType.GetElementType() : parentType.GetGenericArguments()[0]; if(arguements.IsAssignableFrom(t) && (ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, this.RealModelSystemStructure, t) != null) { return true; } } else { if(this.RealModelSystemStructure.ParentFieldType.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, RealModelSystemStructure, t) != null) { return true; } } } } catch { return false; } return false; }
public ModelSystemStructureModel GetRoot(ModelSystemStructure currentModule) { return(ModelSystemModel.GetModelFor(currentModule.GetRoot(ModelSystemModel.Root.RealModelSystemStructure))); }
/// <summary> /// /// </summary> /// <param name="original"></param> /// <param name="cloneRoot"></param> /// <returns></returns> private IModelSystemStructure GetSiblingModule(IModelSystemStructure original, IModelSystemStructure cloneRoot) { var path = ModelSystemStructure.GetModuleReferencePath(original, new List <string>()); return(ModelSystemStructure.GetModuleFromReference(path, cloneRoot)); }
public IModelSystemStructure CreateCollectionMember(string name, Type newType) { if(this.IsCollection) { if(this.Children == null) { this.Children = new List<IModelSystemStructure>(); } ModelSystemStructure p = new ModelSystemStructure(this.Configuration); Type innerType = this.ParentFieldType.IsArray ? this.ParentFieldType.GetElementType() : this.ParentFieldType.GetGenericArguments()[0]; p.Type = newType; p.ParentFieldType = innerType; p.ParentFieldName = this.ParentFieldName; p.Name = name; return p; } return null; }
private static void LoadRoot(IConfiguration config, ModelSystemStructure root, XmlNodeList list) { if(list != null) { var lookUp = new Dictionary<int, Type>(20); for(int i = 0; i < list.Count; i++) { var child = list[i]; if(child.LocalName == "TypeDefinitions") { LoadDefinitions(child, lookUp); } } for(int i = 0; i < list.Count; i++) { var child = list[i]; if(child.LocalName == "Module") { Load(root, null, list[i], config, lookUp); } else if(child.LocalName == "Collection") { root.IsCollection = true; root.Children = new List<IModelSystemStructure>(); Load(root, null, list[i], config, lookUp); } } } }
protected void SendProjectSaved(ModelSystemStructure mss) { ProjectSavedByRun?.Invoke(this, mss); }
public static IModelSystemStructure Load(Stream stream, IConfiguration config) { ModelSystemStructure root = new ModelSystemStructure(config); root.Description = "The Model System Template that the project is based on"; root.Required = true; root.ParentFieldType = typeof(IModelSystemTemplate); root.ParentFieldName = "Root"; XmlDocument doc = new XmlDocument(); doc.Load(stream); LoadRoot(config, root, doc["Root"].ChildNodes); return root; }
public static IModelSystemStructure Load(string fileName, IConfiguration config) { ModelSystemStructure root = new ModelSystemStructure(config); root.Description = "The Model System Template that the project is based on"; root.Required = true; root.ParentFieldType = typeof(IModelSystemTemplate); root.ParentFieldName = "Root"; if(!File.Exists(fileName)) { return root; } XmlDocument doc = new XmlDocument(); doc.Load(fileName); var list = doc["Root"].ChildNodes; LoadRoot(config, root, list); return root; }
internal static ModelSystemStructure Load(XmlNode modelSystemNode, IConfiguration config) { XTMF.ModelSystemStructure structure = new ModelSystemStructure(config); LoadRoot(config, structure, modelSystemNode.ChildNodes); return(structure); }
public void Add(string name, Type type) { if(this.Children == null) { this.Children = new List<IModelSystemStructure>(); } var newChild = new ModelSystemStructure(this.Configuration, name, ParentFieldType); newChild.Type = type; this.Children.Add(newChild); }
private static IModelSystemStructure GenerateChildren(IModelSystemStructure element, Type type, object[] attributes, IConfiguration config) { Type iModel = typeof(IModule); if (type.IsArray) { var argument = type.GetElementType(); if (iModel.IsAssignableFrom(argument)) { ModelSystemStructure child = new ModelSystemStructure(config); child.IsCollection = true; child.Children = new List <IModelSystemStructure>(); foreach (var at in attributes) { if (at is DoNotAutomate) { return(null); } else if (at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } if (child.Description == null) { child.Description = "No description available"; child.Required = false; } } return(child); } } if (type.IsGenericType) { var arguements = type.GetGenericArguments(); if (arguements != null && arguements.Length == 1) { // if the type of this generic is assignable to IModel.. if (iModel.IsAssignableFrom(arguements[0])) { Type iCollection = typeof(ICollection <>).MakeGenericType(arguements[0]); if (iCollection.IsAssignableFrom(type)) { ModelSystemStructure child = new ModelSystemStructure(config); child.IsCollection = true; child.Children = new List <IModelSystemStructure>(); foreach (var at in attributes) { if (at is DoNotAutomate) { return(null); } else if (at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } if (child.Description == null) { child.Description = "No description available"; child.Required = false; } } return(child); } } } } if (iModel.IsAssignableFrom(type)) { ModelSystemStructure child = new ModelSystemStructure(config); foreach (var at in attributes) { if (at is ParentModel || at is DoNotAutomate || at is RootModule) { return(null); } if (at is SubModelInformation) { SubModelInformation info = at as SubModelInformation; child.Description = info.Description; child.Required = info.Required; } } if (child.Description == null) { child.Description = "No description available"; child.Required = false; } return(child); } return(null); }
public IModelSystemStructure Clone() { ModelSystemStructure cloneUs = new ModelSystemStructure(this.Configuration); cloneUs.Name = this.Name; cloneUs.Description = this.Description; cloneUs.Module = this.Module; if(this.Parameters != null) { if((cloneUs.Parameters = this.Parameters.Clone()) != null) { (cloneUs.Parameters as ModuleParameters).BelongsTo = cloneUs; foreach(var p in cloneUs.Parameters) { (p as ModuleParameter).BelongsTo = cloneUs; } } } cloneUs.Required = this.Required; cloneUs.ParentFieldName = this.ParentFieldName; cloneUs.ParentFieldType = this.ParentFieldType; cloneUs._Type = this._Type; cloneUs.IsCollection = this.IsCollection; if(this.Children != null) { foreach(var child in this.Children) { cloneUs.Add(child.Clone()); } } return cloneUs; }
public ModelSystemStructureModel GetModelSystemStructureModel(ModelSystemStructure modelSystemStructure) { return(ModelSystemModel.GetModelFor(modelSystemStructure)); }
public ModelSystemStructureModel GetModelFor(ModelSystemStructure realStructure) => GetModelFor(realStructure, Root);