示例#1
0
        /// <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;
        }
示例#2
0
        /// <summary>
        /// Creates the tree based on element sets and quantities in exchange items
        /// passed with <see cref="PopulateExchangeItemTree">PopulateExchangeItemTree</see> method.
        /// </summary>
        public void CreateTree()
        {
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();


            string lastQuantityID = null;

            // fill tree with exchange items (if there are any because DrawTree could be called
            // before _exchangeItems was assigned )
            if (_exchangeItems != null)
            {
                foreach (IExchangeItem exchangeItem in _exchangeItems)
                {
                    TreeNode elementSetNode = null;                     // newly added node (if any)

                    // apply filters (if any)
                    if (_filterDimension != null &&
                        !Utils.CompareDimensions(_filterDimension, exchangeItem.Quantity.Dimension))
                    {
                        continue;
                    }

                    if (_filterElementSet != null &&
                        _filterElementSet.ElementType != ElementType.IDBased &&
                        (exchangeItem.ElementSet.ElementType != ElementType.IDBased ||
                         exchangeItem.ElementSet.ElementCount != _filterElementSet.ElementCount))
                    {
                        continue;
                    }


                    TreeNode quantityNode;

                    if (lastQuantityID != exchangeItem.Quantity.ID)
                    {
                        // adding new quantity node
                        quantityNode     = treeView1.Nodes.Add(exchangeItem.Quantity.ID);
                        quantityNode.Tag = exchangeItem.Quantity;
                        if (exchangeItem.Quantity.ValueType == global::OpenMI.Standard.ValueType.Scalar)
                        {
                            quantityNode.ImageIndex = quantityNode.SelectedImageIndex = 0;
                        }
                        else
                        {
                            quantityNode.ImageIndex = quantityNode.SelectedImageIndex = 1;
                        }

                        lastQuantityID = exchangeItem.Quantity.ID;
                    }
                    else
                    {
                        // last node corresponds to quantity with same ID
                        quantityNode = treeView1.Nodes[treeView1.Nodes.Count - 1];
                    }

                    // Add ElementSet node
                    elementSetNode = quantityNode.Nodes.Add(exchangeItem.ElementSet.ID);

                    Debug.Assert(0 <= (int)exchangeItem.ElementSet.ElementType && (int)exchangeItem.ElementSet.ElementType <= 9);
                    elementSetNode.ImageIndex = elementSetNode.SelectedImageIndex = (int)exchangeItem.ElementSet.ElementType + 2;

                    elementSetNode.Tag = exchangeItem.ElementSet;


                    // add DataOperation subnodes only if newly added node is IOutputExchangeItem
                    if (exchangeItem is IOutputExchangeItem)
                    {
                        IOutputExchangeItem item = (IOutputExchangeItem)exchangeItem;
                        for (int j = 0; j < item.DataOperationCount; j++)
                        {
                            TreeNode dataOperationNode = elementSetNode.Nodes.Add(item.GetDataOperation(j).ID);
                            dataOperationNode.ImageIndex = dataOperationNode.SelectedImageIndex = 12;
                            dataOperationNode.Tag        = item.GetDataOperation(j);
                        }
                    }
                }
            }

            treeView1.CollapseAll();
            treeView1.EndUpdate();

            _checkedQuantity       = null;
            _checkedElementSet     = null;
            _checkedDataOperations = new ArrayList();
        }