コード例 #1
0
ファイル: ContextualStore.cs プロジェクト: sbarski/cormo
        public IContextual GetContextual(ComponentIdentifier id)
        {
            if (id.Key.StartsWith(GeneratedIdPrefix))
            {
                return(_contextualsInverse.GetOrDefault(id));
            }

            var contextual = _passivationCapableContextuals.GetOrDefault(id);

            if (contextual != null)
            {
                return(contextual);
            }

            var seed = _passivationCapableContextuals.GetOrDefault(new ComponentIdentifier(id.Key)) as IWeldComponent;

            if (seed == null)
            {
                return(null);
            }

            contextual = seed.Resolve(seed.Type);
            if (contextual != null)
            {
                PutIfAbsent(contextual);
            }
            return(contextual);
        }
コード例 #2
0
        public SerializableContextual(IContextual contextual, IContextualStore contextualStore)
        {
            _cachedContextualStore = contextualStore;
            if (contextual.GetType().IsSerializable)
            {
                _serializable = contextual;
            }
            else
            {
                _id = contextualStore.PutIfAbsent(contextual);
            }

            _cached = contextual;
        }
コード例 #3
0
ファイル: ContextualStore.cs プロジェクト: sbarski/cormo
        /// <summary>
        /// Add a contextual (if not already present) to the store, and return it's
        /// id. If the contextual is passivation capable, it's id will be used,
        /// otherwise an id will be generated
        /// </summary>
        /// <param name="contextual">contextual the contexutal to add</param>
        /// <returns>the current id for the contextual</returns>
        public ComponentIdentifier PutIfAbsent(IContextual contextual)
        {
            var passivationCapable = contextual as IPassivationCapable <ComponentIdentifier>;

            if (passivationCapable != null)
            {
                var id = passivationCapable.Id;
                _passivationCapableContextuals.TryAdd(id, contextual);
                return(id);
            }

            return(_contextuals.GetOrAdd(contextual, _ =>
            {
                var id = new ComponentIdentifier(
                    string.Format("{0}{1}", GeneratedIdPrefix, Interlocked.Increment(ref _idIncrement)));
                _contextualsInverse[id] = contextual;
                return id;
            }));
        }
コード例 #4
0
 public IContextualInstance GetOrPut(ComponentIdentifier id, Func <ComponentIdentifier, IContextualInstance> create)
 {
     return(_instances.GetOrAdd(id, create));
 }
コード例 #5
0
 public IContextualInstance Get(ComponentIdentifier id)
 {
     return(_instances.GetOrDefault(id));
 }
コード例 #6
0
 public ComponentDependencySet(CID Client, IEnumerable <CID> Suppliers)
     : base(Suppliers)
 {
     this.Client = Client;
 }
コード例 #7
0
 public ComponentDependencySet(CID Client, params CID[] Suppliers)
     : base(Suppliers)
 {
     this.Client = Client;
 }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
        /// <summary>
        /// Converts a <see cref="IODocument"/> to a <see cref="CircuitDocument"/>.
        /// </summary>
        /// <param name="document">The IODocument to convert.</param>
        /// <returns>A CircuitDocument constructed from the IODocument.</returns>
        public static CircuitDocument ToCircuitDocument(this IODocument document, IDocumentReader reader, out List <IOComponentType> unavailableComponents)
        {
            CircuitDocument circuitDocument = new CircuitDocument();

            circuitDocument.Size = document.Size;

            // Set metadata
            circuitDocument.Metadata = new CircuitDocumentMetadata(null, null, document.Metadata);

            // Add components
            unavailableComponents = new List <IOComponentType>();
            foreach (IOComponent component in document.Components)
            {
                ComponentIdentifier identifier = null;

                // Find description
                if (component.Type.GUID != Guid.Empty && ComponentHelper.IsDescriptionAvailable(component.Type.GUID))
                {
                    identifier = new ComponentIdentifier(ComponentHelper.FindDescription(component.Type.GUID));
                }
                if (identifier == null && reader.IsDescriptionEmbedded(component.Type))
                {
                    identifier = LoadDescription(reader.GetEmbeddedDescription(component.Type), component.Type);
                }
                if (identifier == null && component.Type.IsStandard)
                {
                    identifier = ComponentHelper.GetStandardComponent(component.Type.Collection, component.Type.Item);
                }

                if (identifier != null)
                {
                    // Add full component

                    Dictionary <string, object> properties = new Dictionary <string, object>();
                    foreach (var property in component.Properties)
                    {
                        properties.Add(property.Key, property.Value);
                    }

                    Component addComponent = Component.Create(identifier, properties);
                    addComponent.Layout(component.Location.Value.X, component.Location.Value.Y, (component.Size.HasValue ? component.Size.Value : identifier.Description.MinSize), component.Orientation.Value, component.IsFlipped == true);
                    addComponent.ImplementMinimumSize(addComponent.Description.MinSize);
                    FlagOptions flagOptions = ComponentHelper.ApplyFlags(addComponent);
                    if ((flagOptions & FlagOptions.HorizontalOnly) == FlagOptions.HorizontalOnly && component.Orientation == Orientation.Vertical)
                    {
                        addComponent.Orientation = Orientation.Horizontal;
                    }
                    else if ((flagOptions & FlagOptions.VerticalOnly) == FlagOptions.VerticalOnly && component.Orientation == Orientation.Horizontal)
                    {
                        addComponent.Orientation = Orientation.Vertical;
                    }
                    circuitDocument.Elements.Add(addComponent);
                }
                else
                {
                    // Add disabled component

                    if (!unavailableComponents.Contains(component.Type))
                    {
                        unavailableComponents.Add(component.Type);
                    }

                    DisabledComponent addComponent = new DisabledComponent();

                    Dictionary <string, object> properties = new Dictionary <string, object>();
                    foreach (var property in component.Properties)
                    {
                        addComponent.Properties.Add(property.Key, property.Value);
                    }

                    addComponent.ImplementationCollection = component.Type.Collection;
                    addComponent.ImplementationItem       = component.Type.Item;
                    addComponent.Name = component.Type.Name;
                    addComponent.GUID = component.Type.GUID;
                    if (component.Location.HasValue)
                    {
                        addComponent.Location = new Vector(component.Location.Value.X, component.Location.Value.Y);
                    }
                    addComponent.Size        = component.Size;
                    addComponent.Orientation = component.Orientation;

                    circuitDocument.DisabledComponents.Add(addComponent);
                }
            }

            // Add wires
            IOComponentType wireType = new IOComponentType("wire");

            if (ComponentHelper.WireDescription == null)
            {
                unavailableComponents.Add(wireType);
            }
            else
            {
                foreach (IOWire wire in document.Wires)
                {
                    Dictionary <string, object> properties = new Dictionary <string, object>(4);
                    properties.Add("@x", wire.Location.X);
                    properties.Add("@y", wire.Location.Y);
                    properties.Add("@orientation", wire.Orientation == Orientation.Horizontal);
                    properties.Add("@size", wire.Size);

                    Component wireComponent = Component.Create(ComponentHelper.WireDescription, properties);
                    wireComponent.Layout(wire.Location.X, wire.Location.Y, wire.Size, wire.Orientation, false);
                    wireComponent.ApplyConnections(circuitDocument);
                    circuitDocument.Elements.Add(wireComponent);
                }
            }

            // Connections
            foreach (Component component in circuitDocument.Components)
            {
                component.ApplyConnections(circuitDocument);
            }

            return(circuitDocument);
        }