/// <summary> /// Removes connection between two models. /// </summary> /// <param name="connection">Connection to be removed.</param> public void RemoveConnection(UIConnection connection) { // remove ILinks from both connected components if (!_runPrepareForComputationStarted) { foreach (ILink link in connection.Links) { string linkID = link.ID; ILinkableComponent sourceComponent = link.SourceComponent, targetComponent = link.TargetComponent; sourceComponent.RemoveLink(linkID); targetComponent.RemoveLink(linkID); } } _connections.Remove(connection); _shouldBeSaved = true; }
/// <summary> /// Creates a new instance of <see cref="ConnectionDialog">ConnectionDialog</see> dialog. /// </summary> public ConnectionDialog() { InitializeComponent(); _uilink = null; _propertyManagerCache = new Hashtable(); _startingLinkID = 0; _shouldBeSaved = false; _elementSetViewer = new ElementSetViewer(); }
/// <summary> /// Populates this <see cref="ConnectionDialog">ConnectionDialog</see> with specific connection. /// </summary> /// <param name="uilink"></param> /// <param name="startingLinkID"></param> public void PopulateDialog( UIConnection uilink, int startingLinkID ) { _uilink = uilink; _startingLinkID = startingLinkID; _propertyManagerCache = new Hashtable(); _shouldBeSaved = false; ElementTypeFilterCheckBox.Checked = false; DimensionFilterCheckBox.Checked = false; int count; ILinkableComponent component; component = uilink.ProvidingModel.LinkableComponent; IExchangeItem[] outputExchangeItems = new IExchangeItem[ component.OutputExchangeItemCount ]; count = component.OutputExchangeItemCount; for( int i = 0; i < count; i++ ) outputExchangeItems[i] = component.GetOutputExchangeItem(i); providerExchangeItemSelector.PopulateExchangeItemTree( outputExchangeItems, true ); component = uilink.AcceptingModel.LinkableComponent; IExchangeItem[] inputExchangeItems = new IExchangeItem[ component.InputExchangeItemCount ]; count = component.InputExchangeItemCount; for( int i = 0; i < count; i++ ) inputExchangeItems[i] = component.GetInputExchangeItem(i); acceptorExchangeItemSelector.PopulateExchangeItemTree( inputExchangeItems, true ); UpdateListLinks(); labelInfo.Text = "Connection "+uilink.ProvidingModel.ModelID+" => "+uilink.AcceptingModel.ModelID; }
private void ShowLinkDialog( UIConnection link ) { if (_composition.EditorMode == CompositionManager.EEditorMode.RunButNotReloaded) return; // find maximum link ID of all existing links int maxID = 0; foreach( UIConnection uiLink in _composition.Connections ) foreach( ILink iLink in uiLink.Links ) maxID = Math.Max( int.Parse(iLink.ID), maxID ); _connectionDialog.PopulateDialog( link, maxID+1 ); if( _connectionDialog.ShowDialog(this) == DialogResult.OK ) _composition.ShouldBeSaved = true; UpdateTitle(); }
/// <summary> /// Loads composition from XML document. /// </summary> /// <param name="omiRelativeDirectory">Directory the OMI files are relative to.</param> /// <param name="xmlDocument">XML document</param> private void LoadFromXmlDocument(string omiRelativeDirectory, XmlDocument xmlDocument) { // once you choose to load new file, all previously opened models are closed Release(); CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo(""); XmlElement xmlRoot = (XmlElement)xmlDocument.ChildNodes[0]; XmlElement xmlModels = (XmlElement)xmlRoot.ChildNodes[0]; XmlElement xmlLinks = (XmlElement)xmlRoot.ChildNodes[1]; // run properties aren't mandatory XmlElement xmlRunProperties = null; if (xmlRoot.ChildNodes.Count > 2) xmlRunProperties = (XmlElement)xmlRoot.ChildNodes[2]; // check if (xmlRoot.GetAttribute("version") != "1.0") throw (new FormatException("Version of file not supported. Currently supported only version '1.0'")); if (xmlModels.Name != "models" || xmlLinks.Name != "links") throw (new FormatException("Unknown file format ('models' or 'links' tag not present where expected).")); if (xmlRunProperties != null) if (xmlRunProperties.Name != "runproperties") throw (new FormatException("Unknown file format ('runproperties' tag not present where expected).")); // read UIModels foreach (XmlElement xmlUiModel in xmlModels.ChildNodes) { try { UIModel uiModel = AddModel(omiRelativeDirectory, xmlUiModel.GetAttribute("omi")); uiModel.Rect.X = Int32.Parse(xmlUiModel.GetAttribute("rect_x")); uiModel.Rect.Y = Int32.Parse(xmlUiModel.GetAttribute("rect_y")); uiModel.Rect.Width = Int32.Parse(xmlUiModel.GetAttribute("rect_width")); uiModel.Rect.Height = Int32.Parse(xmlUiModel.GetAttribute("rect_height")); } catch (Exception e) { throw (new Exception( "Model cannot be added to composition due to exception.\n" + "OMI filename: " + xmlUiModel.GetAttribute("omi") + "\n" + "Exception: " + e.ToString())); } } // read UILinks foreach (XmlElement xmlUiLink in xmlLinks.ChildNodes) { // find models corresponding to this UIConnection UIModel providingModel = null, acceptingModel = null; foreach (UIModel uiModel in _models) if (uiModel.ModelID == xmlUiLink.GetAttribute("model_providing")) { providingModel = uiModel; break; } foreach (UIModel uiModel in _models) if (uiModel.ModelID == xmlUiLink.GetAttribute("model_accepting")) { acceptingModel = uiModel; break; } if (providingModel == null || acceptingModel == null) { throw (new Exception( "One model (or both) corresponding to this link cannot be found...\n" + "Providing model: " + xmlUiLink.GetAttribute("model_providing") + "\n" + "Accepting model: " + xmlUiLink.GetAttribute("model_accepting"))); } // construct UIConnection UIConnection uiLink = new UIConnection(providingModel, acceptingModel); // read OpenMI Links foreach (XmlElement xmlLink in xmlUiLink.ChildNodes) { // find corresponding exchange items IOutputExchangeItem outputExchangeItem = null; IInputExchangeItem inputExchangeItem = null; int count = providingModel.LinkableComponent.OutputExchangeItemCount; string sourceElementSetID = xmlLink.GetAttribute("source_elementset"); string sourceQuantityID = xmlLink.GetAttribute("source_quantity"); for (int i = 0; i < count; i++) if (sourceElementSetID == providingModel.LinkableComponent.GetOutputExchangeItem(i).ElementSet.ID && sourceQuantityID == providingModel.LinkableComponent.GetOutputExchangeItem(i).Quantity.ID) { outputExchangeItem = providingModel.LinkableComponent.GetOutputExchangeItem(i); break; } for (int i = 0; i < acceptingModel.LinkableComponent.InputExchangeItemCount; i++) if (xmlLink.GetAttribute("target_elementset") == acceptingModel.LinkableComponent.GetInputExchangeItem(i).ElementSet.ID && xmlLink.GetAttribute("target_quantity") == acceptingModel.LinkableComponent.GetInputExchangeItem(i).Quantity.ID) { inputExchangeItem = acceptingModel.LinkableComponent.GetInputExchangeItem(i); break; } if (outputExchangeItem == null || inputExchangeItem == null) throw (new Exception( "Cannot find exchange item\n" + "Providing model: " + providingModel.ModelID + "\n" + "Accepting model: " + acceptingModel.ModelID + "\n" + "Source ElementSet: " + xmlLink.GetAttribute("source_elementset") + "\n" + "Source Quantity: " + xmlLink.GetAttribute("source_quantity") + "\n" + "Target ElementSet: " + xmlLink.GetAttribute("target_elementset") + "\n" + "Target Quantity: " + xmlLink.GetAttribute("target_quantity"))); // read selected DataOperation's IDs, find their equivalents // in outputExchangeItem, and add these to link ArrayList dataOperationsToAdd = new ArrayList(); foreach (XmlElement xmlDataOperation in xmlLink.ChildNodes) for (int i = 0; i < outputExchangeItem.DataOperationCount; i++) { IDataOperation dataOperation = outputExchangeItem.GetDataOperation(i); if (dataOperation.ID == xmlDataOperation.GetAttribute("id")) { // set data operation's arguments if any foreach (XmlElement xmlArgument in xmlDataOperation.ChildNodes) { string argumentKey = xmlArgument.GetAttribute("key"); for (int j = 0; j < dataOperation.ArgumentCount; j++) { IArgument argument = dataOperation.GetArgument(j); if (argument.Key == argumentKey && !argument.ReadOnly) argument.Value = xmlArgument.GetAttribute("value"); } } dataOperationsToAdd.Add(dataOperation); break; } } // now construct the Link... Link link = new Link( providingModel.LinkableComponent, outputExchangeItem.ElementSet, outputExchangeItem.Quantity, acceptingModel.LinkableComponent, inputExchangeItem.ElementSet, inputExchangeItem.Quantity, "No description available.", xmlLink.GetAttribute("id"), dataOperationsToAdd); // ...add the link to the list uiLink.Links.Add(link); // and add it to both LinkableComponents uiLink.AcceptingModel.LinkableComponent.AddLink(link); uiLink.ProvidingModel.LinkableComponent.AddLink(link); } // add new UIConnection to list of connections _connections.Add(uiLink); } // read run properties (if present) if (xmlRunProperties != null) { string str = xmlRunProperties.GetAttribute("listenedeventtypes"); if (str.Length != (int)EventType.NUM_OF_EVENT_TYPES) throw (new FormatException("Invalid number of event types in 'runproperties' tag, expected " + EventType.NUM_OF_EVENT_TYPES + ", but only " + str.Length + " found.")); for (int i = 0; i < (int)EventType.NUM_OF_EVENT_TYPES; i++) switch (str[i]) { case '1': _listenedEventTypes[i] = true; break; case '0': _listenedEventTypes[i] = false; break; default: throw (new FormatException("Unknown format of 'listenedeventtypes' attribute in 'runproperties' tag.")); } _triggerInvokeTime = DateTime.Parse(xmlRunProperties.GetAttribute("triggerinvoke")); _logFileName = xmlRunProperties.GetAttribute("logfilename"); if (_logFileName != null) { _logFileName = _logFileName.Trim(); if (_logFileName == "") _logFileName = null; // if not set, logfile isn't used } str = xmlRunProperties.GetAttribute("showeventsinlistbox"); if (str == null || str == "" || str == "1") _showEventsInListbox = true; // if not set, value is true else _showEventsInListbox = false; str = xmlRunProperties.GetAttribute("runinsamethread"); if (str == "1") _runInSameThread = true; } Thread.CurrentThread.CurrentCulture = currentCulture; }
/// <summary> /// Removes connection between two models. /// </summary> /// <param name="connection">Connection to be removed.</param> public void RemoveConnection(UIConnection connection) { // remove ILinks from both connected components if (!_runPrepareForComputationStarted) foreach (ILink link in connection.Links) { string linkID = link.ID; ILinkableComponent sourceComponent = link.SourceComponent, targetComponent = link.TargetComponent; sourceComponent.RemoveLink(linkID); targetComponent.RemoveLink(linkID); } _connections.Remove(connection); _shouldBeSaved = true; }
/// <summary> /// Loads composition from XML document. /// </summary> /// <param name="omiRelativeDirectory">Directory the OMI files are relative to.</param> /// <param name="xmlDocument">XML document</param> private void LoadFromXmlDocument(string omiRelativeDirectory, XmlDocument xmlDocument) { // once you choose to load new file, all previously opened models are closed Release(); CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo(""); XmlElement xmlRoot = (XmlElement)xmlDocument.ChildNodes[0]; XmlElement xmlModels = (XmlElement)xmlRoot.ChildNodes[0]; XmlElement xmlLinks = (XmlElement)xmlRoot.ChildNodes[1]; // run properties aren't mandatory XmlElement xmlRunProperties = null; if (xmlRoot.ChildNodes.Count > 2) { xmlRunProperties = (XmlElement)xmlRoot.ChildNodes[2]; } // check if (xmlRoot.GetAttribute("version") != "1.0") { throw (new FormatException("Version of file not supported. Currently supported only version '1.0'")); } if (xmlModels.Name != "models" || xmlLinks.Name != "links") { throw (new FormatException("Unknown file format ('models' or 'links' tag not present where expected).")); } if (xmlRunProperties != null) { if (xmlRunProperties.Name != "runproperties") { throw (new FormatException("Unknown file format ('runproperties' tag not present where expected).")); } } // read UIModels foreach (XmlElement xmlUiModel in xmlModels.ChildNodes) { try { UIModel uiModel = AddModel(omiRelativeDirectory, xmlUiModel.GetAttribute("omi")); uiModel.Rect.X = Int32.Parse(xmlUiModel.GetAttribute("rect_x")); uiModel.Rect.Y = Int32.Parse(xmlUiModel.GetAttribute("rect_y")); uiModel.Rect.Width = Int32.Parse(xmlUiModel.GetAttribute("rect_width")); uiModel.Rect.Height = Int32.Parse(xmlUiModel.GetAttribute("rect_height")); } catch (Exception e) { throw (new Exception( "Model cannot be added to composition due to exception.\n" + "OMI filename: " + xmlUiModel.GetAttribute("omi") + "\n" + "Exception: " + e.ToString())); } } // read UILinks foreach (XmlElement xmlUiLink in xmlLinks.ChildNodes) { // find models corresponding to this UIConnection UIModel providingModel = null, acceptingModel = null; foreach (UIModel uiModel in _models) { if (uiModel.ModelID == xmlUiLink.GetAttribute("model_providing")) { providingModel = uiModel; break; } } foreach (UIModel uiModel in _models) { if (uiModel.ModelID == xmlUiLink.GetAttribute("model_accepting")) { acceptingModel = uiModel; break; } } if (providingModel == null || acceptingModel == null) { throw (new Exception( "One model (or both) corresponding to this link cannot be found...\n" + "Providing model: " + xmlUiLink.GetAttribute("model_providing") + "\n" + "Accepting model: " + xmlUiLink.GetAttribute("model_accepting"))); } // construct UIConnection UIConnection uiLink = new UIConnection(providingModel, acceptingModel); // read OpenMI Links foreach (XmlElement xmlLink in xmlUiLink.ChildNodes) { // find corresponding exchange items IOutputExchangeItem outputExchangeItem = null; IInputExchangeItem inputExchangeItem = null; int count = providingModel.LinkableComponent.OutputExchangeItemCount; string sourceElementSetID = xmlLink.GetAttribute("source_elementset"); string sourceQuantityID = xmlLink.GetAttribute("source_quantity"); for (int i = 0; i < count; i++) { if (sourceElementSetID == providingModel.LinkableComponent.GetOutputExchangeItem(i).ElementSet.ID && sourceQuantityID == providingModel.LinkableComponent.GetOutputExchangeItem(i).Quantity.ID) { outputExchangeItem = providingModel.LinkableComponent.GetOutputExchangeItem(i); break; } } for (int i = 0; i < acceptingModel.LinkableComponent.InputExchangeItemCount; i++) { if (xmlLink.GetAttribute("target_elementset") == acceptingModel.LinkableComponent.GetInputExchangeItem(i).ElementSet.ID && xmlLink.GetAttribute("target_quantity") == acceptingModel.LinkableComponent.GetInputExchangeItem(i).Quantity.ID) { inputExchangeItem = acceptingModel.LinkableComponent.GetInputExchangeItem(i); break; } } if (outputExchangeItem == null || inputExchangeItem == null) { throw (new Exception( "Cannot find exchange item\n" + "Providing model: " + providingModel.ModelID + "\n" + "Accepting model: " + acceptingModel.ModelID + "\n" + "Source ElementSet: " + xmlLink.GetAttribute("source_elementset") + "\n" + "Source Quantity: " + xmlLink.GetAttribute("source_quantity") + "\n" + "Target ElementSet: " + xmlLink.GetAttribute("target_elementset") + "\n" + "Target Quantity: " + xmlLink.GetAttribute("target_quantity"))); } // read selected DataOperation's IDs, find their equivalents // in outputExchangeItem, and add these to link ArrayList dataOperationsToAdd = new ArrayList(); foreach (XmlElement xmlDataOperation in xmlLink.ChildNodes) { for (int i = 0; i < outputExchangeItem.DataOperationCount; i++) { IDataOperation dataOperation = outputExchangeItem.GetDataOperation(i); if (dataOperation.ID == xmlDataOperation.GetAttribute("id")) { // set data operation's arguments if any foreach (XmlElement xmlArgument in xmlDataOperation.ChildNodes) { string argumentKey = xmlArgument.GetAttribute("key"); for (int j = 0; j < dataOperation.ArgumentCount; j++) { IArgument argument = dataOperation.GetArgument(j); if (argument.Key == argumentKey && !argument.ReadOnly) { argument.Value = xmlArgument.GetAttribute("value"); } } } dataOperationsToAdd.Add(dataOperation); break; } } } // now construct the Link... Link link = new Link( providingModel.LinkableComponent, outputExchangeItem.ElementSet, outputExchangeItem.Quantity, acceptingModel.LinkableComponent, inputExchangeItem.ElementSet, inputExchangeItem.Quantity, "No description available.", xmlLink.GetAttribute("id"), dataOperationsToAdd); // ...add the link to the list uiLink.Links.Add(link); // and add it to both LinkableComponents uiLink.AcceptingModel.LinkableComponent.AddLink(link); uiLink.ProvidingModel.LinkableComponent.AddLink(link); } // add new UIConnection to list of connections _connections.Add(uiLink); } // read run properties (if present) if (xmlRunProperties != null) { string str = xmlRunProperties.GetAttribute("listenedeventtypes"); if (str.Length != (int)EventType.NUM_OF_EVENT_TYPES) { throw (new FormatException("Invalid number of event types in 'runproperties' tag, expected " + EventType.NUM_OF_EVENT_TYPES + ", but only " + str.Length + " found.")); } for (int i = 0; i < (int)EventType.NUM_OF_EVENT_TYPES; i++) { switch (str[i]) { case '1': _listenedEventTypes[i] = true; break; case '0': _listenedEventTypes[i] = false; break; default: throw (new FormatException("Unknown format of 'listenedeventtypes' attribute in 'runproperties' tag.")); } } _triggerInvokeTime = DateTime.Parse(xmlRunProperties.GetAttribute("triggerinvoke")); _logFileName = xmlRunProperties.GetAttribute("logfilename"); if (_logFileName != null) { _logFileName = _logFileName.Trim(); if (_logFileName == "") { _logFileName = null; // if not set, logfile isn't used } } str = xmlRunProperties.GetAttribute("showeventsinlistbox"); if (str == null || str == "" || str == "1") { _showEventsInListbox = true; // if not set, value is true } else { _showEventsInListbox = false; } str = xmlRunProperties.GetAttribute("runinsamethread"); if (str == "1") { _runInSameThread = true; } } Thread.CurrentThread.CurrentCulture = currentCulture; }