Esempio n. 1
0
        public void TestGetComponentTypes()
        {
            // Create the component types
            var com1 = new IOComponentType("component1");
            var com2 = new IOComponentType("collection1", "component2");
            var com3 = new IOComponentType("collection1", "component3");

            // Create the test document
            IODocument doc = new IODocument();

            // Add instances of each component
            doc.Components.Add(new IOComponent("1", null, null, null, null, com1));
            doc.Components.Add(new IOComponent("1", null, null, null, null, com2));
            doc.Components.Add(new IOComponent("1", null, null, null, null, com3));

            // Add a duplicate of component 3
            doc.Components.Add(new IOComponent("1", null, null, null, null, com3));

            // Call the method being tested
            var comTypes = doc.GetComponentTypes();

            // There should be a "special:unknown" collection,
            // and a "collection1" collection
            Assert.AreEqual(2, comTypes.Count);
            Assert.IsTrue(comTypes.ContainsKey(IODocument.UnknownCollection));
            Assert.IsTrue(comTypes.ContainsKey("collection1"));

            // "special:unknown" should contain "component1"
            Assert.AreEqual(1, comTypes[IODocument.UnknownCollection].Count);
            Assert.IsTrue(comTypes[IODocument.UnknownCollection].Contains(com1));

            // "collection1" should contain "component2" and "component3"
            Assert.AreEqual(2, comTypes["collection1"].Count);
            Assert.IsTrue(comTypes["collection1"].Contains(com2));
            Assert.IsTrue(comTypes["collection1"].Contains(com3));
        }
        /// <summary>
        /// Converts a <see cref="CircuitDocument"/> to an <see cref="IODocument"/>.
        /// </summary>
        /// <param name="document">The CircuitDocument to convert.</param>
        /// <returns>An IODocument constructed from the CircuitDocument.</returns>
        public static IODocument ToIODocument(this CircuitDocument document, out IDictionary<IOComponentType, EmbedComponentData> embedComponents)
        {
            IODocument ioDocument = new IODocument();
            ioDocument.Size = document.Size;

            // Set metadata
            ioDocument.Metadata = document.Metadata;

            // Get connections
            Dictionary<Component, Dictionary<string, string>> connections = ConnectionHelper.RemoveWires(document);

            // Generate types
            Dictionary<ComponentIdentifier, IOComponentType> componentTypes = new Dictionary<ComponentIdentifier,IOComponentType>();
            foreach(Component component in document.Components)
            {
                if (ComponentHelper.IsWire(component))
                    continue; // Skip wires

                ComponentIdentifier identifier = new ComponentIdentifier(component.Description, component.Configuration());
                if (!componentTypes.ContainsKey(identifier))
                {
                    IOComponentType ioType = new IOComponentType(component.Description.Metadata.ImplementSet,
                        (identifier.Configuration != null && !String.IsNullOrEmpty(identifier.Configuration.ImplementationName) ? identifier.Configuration.ImplementationName : component.Description.Metadata.ImplementItem));
                    ioType.Name = component.Description.ComponentName;
                    ioType.GUID = component.Description.Metadata.GUID;
                    componentTypes.Add(identifier, ioType);
                }
            }

            // Add visible components
            int idCounter = 0; // generate component IDs
            foreach (IComponentElement component in document.Elements.Where(component => component is IComponentElement && !ComponentHelper.IsWire(component)))
            {
                IOComponent ioComponent = new IOComponent();
                ioComponent.ID = idCounter.ToString();
                ioComponent.Size = component.Size;
                ioComponent.Location = new Point(component.Location.X, component.Location.Y);
                ioComponent.IsFlipped = component.IsFlipped;
                ioComponent.Orientation = component.Orientation;
                IOComponentType ioType = new IOComponentType(component.ImplementationCollection, component.ImplementationItem);

                if (component is Component)
                {
                    Component cComponent = component as Component;
                    ioType.Name = cComponent.Description.ComponentName;
                    ioType.GUID = cComponent.Description.Metadata.GUID;
                    ioComponent.Type = ioType;

                    // Set connections
                    if (connections.ContainsKey(cComponent))
                        foreach (var connection in connections[cComponent])
                            ioComponent.Connections.Add(connection.Key, connection.Value);
                }

                // Set properties
                foreach (var property in component.Properties)
                    ioComponent.Properties.Add(new IOComponentProperty(property.Key, property.Value, true)); // TODO: implement IsStandard

                ioDocument.Components.Add(ioComponent);

                idCounter++;
            }

            // Add unavailable components
            foreach (DisabledComponent component in document.DisabledComponents)
            {
                Point? location = null;
                if (component.Location.HasValue)
                    location = new Point(component.Location.Value.X, component.Location.Value.Y);

                IOComponent ioComponent = new IOComponent();
                ioComponent.ID = idCounter.ToString();
                ioComponent.Location = location;
                ioComponent.Size = component.Size;
                ioComponent.IsFlipped = component.IsFlipped;
                ioComponent.Orientation = component.Orientation;

                IOComponentType ioType = new IOComponentType(component.ImplementationCollection, component.ImplementationItem);

                // Set name
                if (!String.IsNullOrEmpty(component.Name))
                    ioType.Name = component.Name;

                // Set GUID
                if (component.GUID.HasValue)
                    ioType.GUID = component.GUID.Value;

                ioComponent.Type = ioType;

                // Set properties
                foreach (var property in component.Properties)
                    ioComponent.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));

                ioDocument.Components.Add(ioComponent);

                idCounter++;
            }

            // Add wires
            foreach (IComponentElement wire in document.Components.Where(component => ComponentHelper.IsWire(component)))
            {
                IOWire ioWire = new IOWire(new Point(wire.Location.X, wire.Location.Y), wire.Size, wire.Orientation);
                ioDocument.Wires.Add(ioWire);
            }

            // Embed components
            embedComponents = new Dictionary<IOComponentType, EmbedComponentData>();
            foreach (EmbedDescription embedItem in document.Metadata.EmbedComponents.Where(item => item.IsEmbedded == true))
            {
                if (!String.IsNullOrEmpty(embedItem.Description.Source.Path) && System.IO.File.Exists(embedItem.Description.Source.Path))
                {
                    EmbedComponentData embedData = new EmbedComponentData();
                    embedData.Stream = System.IO.File.OpenRead(embedItem.Description.Source.Path);
                    embedData.FileExtension = System.IO.Path.GetExtension(embedItem.Description.Source.Path);

                    switch (embedData.FileExtension)
                    {
                        case ".xml":
                            embedData.ContentType = "application/xml";
                            break;
                        case ".cdcom":
                            embedData.ContentType = IO.CDDX.ContentTypeNames.BinaryComponent;
                            break;
                    }

                    List<IOComponentType> associatedTypes = new List<IOComponentType>();
                    foreach (var item in componentTypes)
                    {
                        if (item.Key.Description == embedItem.Description && !embedComponents.ContainsKey(item.Value))
                            embedComponents.Add(item.Value, embedData);
                    }
                }
            }

            return ioDocument;
        }