private void DisplayTree_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { var position = e.GetPosition(this.DisplayTree); var hitItem = this.DisplayTree.InputHitTest(position) as DependencyObject; if (hitItem != null && hitItem != this.DisplayTree) { var item = FindAncestor <TreeViewItem>(hitItem); // we might have been clicked without having a treeview item selected if (item == null) { return; } var structure = item.Header as ModelSystemDisplayStructure; this.SelectedModule = structure.Structure; if (structure.Structure != null) { if (structure.Structure.IsCollection) { ((UMSIContextMenu)this.ContextMenu).SetData( this.RootModule, ModelSystemStructure.GetParent(this.RootModule, structure.Structure), structure.Structure); } else { ((UMSIContextMenu)this.ContextMenu).SetData( ModelSystemStructure.CheckForRootModule(this.RootModule, structure.Structure, ModelSystemStructure.GetRootRequirement(structure.Structure.Type)), ModelSystemStructure.GetParent(this.RootModule, structure.Structure), structure.Structure); } } } }
public XTMFRunRemoteClient(Configuration configuration, string runName, string runDirectory, string modelSystemString) : base(runName, runDirectory, configuration) { using (var memStream = new MemoryStream()) { try { Project temp = new Project(Path.GetFileName(runDirectory), configuration, true); ((ProjectRepository)(configuration.ProjectRepository)).SetActiveProject(temp); temp.ExternallySaved += (o, e) => { SendProjectSaved(null); }; var msAsBytes = Encoding.Unicode.GetBytes(modelSystemString); memStream.Write(msAsBytes, 0, msAsBytes.Length); memStream.Position = 0; var mss = ModelSystemStructure.Load(memStream, configuration); memStream.Position = 0; _Root = (ModelSystemStructure)mss; temp.AddModelSystem(_Root, LoadLinkedParameters(_Root, memStream), String.Empty); } catch (Exception e) { Console.WriteLine(e.Message); } } }
private bool IsAssignable(IModelSystemStructure rootStructure, IModelSystemStructure parentStructure, IModelSystemStructure selectedElement) { // 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 (this.CopyBuffer.IsCollection) { // Make sure that we are doing collection to collection and that they are of the right types if (!selectedElement.IsCollection || !selectedElement.ParentFieldType.IsAssignableFrom(this.CopyBuffer.ParentFieldType)) { return(false); } // now make sure that every new element is alright with the parent and root var parentType = selectedElement.ParentFieldType; var arguements = parentType.IsArray ? parentType.GetElementType() : parentType.GetGenericArguments()[0]; foreach (var member in this.CopyBuffer.Children) { var t = member.Type; if (arguements.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, selectedElement, t) != null) { return(true); } } } else { var t = this.CopyBuffer.Type; rootStructure = ModelSystemStructure.CheckForRootModule(rootStructure, selectedElement, t); if (selectedElement.IsCollection) { var parentType = selectedElement.ParentFieldType; var arguements = parentType.IsArray ? parentType.GetElementType() : parentType.GetGenericArguments()[0]; if (arguements.IsAssignableFrom(t) && (ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, selectedElement, t) != null) { return(true); } } else { if (selectedElement.ParentFieldType.IsAssignableFrom(t) && (parent == null || ModelSystemStructure.CheckForParent(parent, t)) && ModelSystemStructure.CheckForRootModule(rootStructure, selectedElement, t) != null) { return(true); } } } } catch { return(false); } return(false); }
private void LoadAndSignalModelSystem(BinaryReader reader) { try { var length = (int)reader.ReadInt64(); byte[] msText = new byte[length]; var soFar = 0; while (soFar < length) { soFar += reader.Read(msText, soFar, length - soFar); } using (var stream = new MemoryStream(msText)) { var mss = ModelSystemStructure.Load(stream, Configuration); SendProjectSaved(mss as ModelSystemStructure); } } catch (Exception e) { SendRunMessage(e.Message + "\r\n" + e.StackTrace); } }
public void SetActive(object data) { var result = data as QuestionResult; if (result == null && data is string) { this.RunDirectory = System.IO.Path.GetDirectoryName((string)data); this.ModelInterface.LoadRoot(ModelSystemStructure.Load((string)data, this.XTMF.XTMF.Configuration)); this.ModelInterface.EditMode = false; } else if (result != null && result.Data is IModelSystem) { // save here IModelSystem ms = result.Data as IModelSystem; ms.ModelSystemStructure = this.ModelInterface.Root.Clone(); string error = null; if (!ms.Save(ref error)) { MessageBox.Show("Unable to save Model System!\r\n" + error, "Unable to save!", MessageBoxButton.OK, MessageBoxImage.Error); } this.XTMF.XTMF.Configuration.ModelSystemRepository.Add(ms); } }
private List <ILinkedParameter> LoadLinkedParameters(ModelSystemStructure root, Stream stream) { var ret = new List <ILinkedParameter>(); string error = null; using (XmlReader reader = XmlReader.Create(stream)) { bool skipRead = false; while (!reader.EOF && (skipRead || reader.Read())) { skipRead = false; if (reader.NodeType != XmlNodeType.Element) { continue; } switch (reader.LocalName) { case "LinkedParameter": { string linkedParameterName = "Unnamed"; string valueRepresentation = null; var startingDepth = reader.Depth; while (reader.MoveToNextAttribute()) { if (reader.NodeType == XmlNodeType.Attribute) { if (reader.LocalName == "Name") { linkedParameterName = reader.ReadContentAsString(); } else if (reader.LocalName == "Value") { valueRepresentation = reader.ReadContentAsString(); } } } LinkedParameter lp = new LinkedParameter(linkedParameterName); lp.SetValue(valueRepresentation, ref error); ret.Add(lp); skipRead = true; while (reader.Read()) { if (reader.Depth <= startingDepth) { break; } if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == "Reference") { string variableLink = null; while (reader.MoveToNextAttribute()) { if (reader.Name == "Name") { variableLink = reader.ReadContentAsString(); } } if (variableLink != null) { IModuleParameter param = GetParameterFromLink(variableLink, root); if (param != null) { // in any case if there is a type error, just throw it out lp.Add(param, ref error); } } } } } break; } } } return(ret); }
private IModuleParameter GetParameterFromLink(string variableLink, ModelSystemStructure root) { // we need to search the space now return(GetParameterFromLink(ParseLinkedParameterName(variableLink), 0, root)); }
private bool ProcessMessage(BinaryWriter writer, BinaryFormatter outputFormat, Message message) { switch (message.Type) { case MessageType.PostComplete: { writer.Write((Int32)MessageType.PostComplete); } break; case MessageType.PostResource: { var data = message.Data as Result; writer.Write(data.Name); outputFormat.Serialize(writer.BaseStream, data.Data); } break; case MessageType.PostProgess: { writer.Write((Int32)MessageType.PostProgess); writer.Write(_Progress); writer.Flush(); } break; case MessageType.RequestResource: { var dr = message.Data as DelayedResult; ResourceRequests.AddLast(dr); writer.Write((Int32)MessageType.RequestResource); writer.Write(dr.Name); writer.Flush(); } break; case MessageType.ReturningResource: { var result = message.Data as Result; DelayedResult toRemove = null; foreach (var delayed in ResourceRequests) { if (delayed.Name == result.Name) { toRemove = delayed; delayed.Data = result.Data; delayed.Lock.Release(); break; } } if (toRemove != null) { ResourceRequests.Remove(toRemove); } } break; case MessageType.PostCancel: { var ms = _CurrentRunningModelSystem; // if we don't have a model system then we are done if (ms == null) { break; } try { // try to cancel the model system ms.ExitRequest(); } catch { } } break; case MessageType.SendModelSystem: try { var mssBuff = message.Data as byte[]; IModelSystemStructure mss = null; using (MemoryStream memory = new MemoryStream()) { memory.Write(mssBuff, 0, mssBuff.Length); memory.Position = 0; mss = ModelSystemStructure.Load(memory, _Configuration); } if (_ModelSystemThread != null && _ModelSystemThread.IsAlive) { try { _ModelSystemThread.Abort(); } catch { } } // now that the other thread is going to end // we can now go and start generating ourselves // in another run thread (_ModelSystemThread = new Thread(ModelSystemStartup)).Start(mss); } catch (Exception e) { Console.WriteLine(e.ToString()); return(true); } break; case MessageType.WriteToHostConsole: writer.Write((int)MessageType.WriteToHostConsole); writer.Write(message.Data as string); break; case MessageType.SendCustomMessage: { var msg = message.Data as SendCustomMessageMessage; int msgNumber = msg.CustomMessageNumber; int length = 0; var failed = false; byte[] buffer = null; Action <object, Stream> customConverter; bool getConverter = false; lock (this) { getConverter = _CustomSenders.TryGetValue(msgNumber, out customConverter); } if (getConverter) { using (MemoryStream mem = new MemoryStream(0x100)) { try { customConverter(msg.Data, mem); mem.Position = 0; buffer = mem.ToArray(); length = buffer.Length; } catch { failed = true; } } } writer.Write((Int32)MessageType.SendCustomMessage); writer.Write((Int32)msg.CustomMessageNumber); writer.Write((Int32)length); if (!failed) { writer.Write(buffer, 0, length); buffer = null; } } break; case MessageType.ReceiveCustomMessage: { var msg = message.Data as ReceiveCustomMessageMessage; var customNumber = msg.CustomMessageNumber; Func <Stream, object> customConverter; bool getConverted = false; lock (this) { getConverted = _CustomReceivers.TryGetValue(customNumber, out customConverter); } if (getConverted) { using (var stream = msg.Stream) { try { object output = customConverter(stream); if (_CustomHandlers.TryGetValue(msg.CustomMessageNumber, out List <Action <object> > handlers)) { foreach (var handler in handlers) { try { handler(output); } catch (Exception e) { Console.WriteLine(e.Message + "\r\n" + e.StackTrace); } } } } catch { } } } } break; default: // FAIL! Console.WriteLine("Processing a message of type " + message.Type + " and we didn't know what to do with it."); return(true); } return(false); }
private IModelSystemStructure GetRoot(IModelSystemStructure rootFor) { return(ModelSystemStructure.CheckForRootModule(this.Root, rootFor, ModelSystemStructure.GetRootRequirement(rootFor.Type))); }
private IModelSystemStructure GetParent(IModelSystemStructure toFind) { return(ModelSystemStructure.GetParent(this.Root, toFind)); }