예제 #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private static IReadOnlyList <MatrixLabel> ReadLabels(EmberReader reader)
        {
            reader.AssertInnerNumber(GlowLabelCollection.InnerNumber);
            var result = new List <MatrixLabel>();

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                switch (reader.GetContextSpecificOuterNumber())
                {
                case GlowLabelCollection.Label.OuterNumber:
                    reader.AssertInnerNumber(GlowLabel.InnerNumber);

                    int[]  basePath    = null;
                    string description = null;

                    while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
                    {
                        switch (reader.GetContextSpecificOuterNumber())
                        {
                        case GlowLabel.BasePath.OuterNumber:
                            basePath = reader.AssertAndReadContentsAsInt32Array();
                            break;

                        case GlowLabel.Description.OuterNumber:
                            description = reader.AssertAndReadContentsAsString();
                            break;

                        default:
                            reader.Skip();
                            break;
                        }
                    }

                    if ((basePath != null) && (description != null))
                    {
                        result.Add(new MatrixLabel(basePath, description));
                    }

                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(result);
        }
예제 #2
0
        internal sealed override RetrievalState ReadAdditionalField(EmberReader reader, int contextSpecificOuterNumber)
        {
            this.isProviderChangeInProgress = true;

            try
            {
                switch (contextSpecificOuterNumber)
                {
                case GlowMatrix.Targets.OuterNumber:
                    reader.AssertInnerNumber(GlowTargetCollection.InnerNumber);
                    this.Targets = this.ReadSignals(
                        reader,
                        this.Targets,
                        GlowTargetCollection.Target.OuterNumber,
                        GlowTarget.InnerNumber,
                        GlowTarget.Number.OuterNumber,
                        GlowTarget.Number.Name);

                    break;

                case GlowMatrix.Sources.OuterNumber:
                    reader.AssertInnerNumber(GlowSourceCollection.InnerNumber);
                    this.Sources = this.ReadSignals(
                        reader,
                        this.Sources,
                        GlowSourceCollection.Source.OuterNumber,
                        GlowSource.InnerNumber,
                        GlowSource.Number.OuterNumber,
                        GlowSource.Number.Name);
                    break;

                case GlowMatrix.Connections.OuterNumber:
                    this.ReadConnections(reader);
                    this.connectionsRead = true;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }
            finally
            {
                this.isProviderChangeInProgress = false;
            }

            return(this.RetrievalState);
        }
        private void ReadChildContents(
            EmberReader reader,
            ElementType actualType,
            int number,
            ref Element child,
            out RetrievalState childRetrievalState)
        {
            reader.AssertInnerNumber(InnerNumber.Set);

            if (child != null)
            {
                childRetrievalState = child.ReadContents(reader, actualType);
            }
            else
            {
                using (var stream = new MemoryStream())
                    using (var writer = new EmberWriter(stream))
                    {
                        // Since EmberReader checks that every end of a container is matched by a start, we need to write
                        // this dummy here.
                        writer.WriteStartSet(GlowNode.Contents.OuterId);
                        var identifier = reader.CopyToEndContainer(writer, GlowNodeContents.Identifier.OuterId) as string;

                        if (identifier != null)
                        {
                            writer.Flush();
                            stream.Position = 0;

                            using (var contentsReader = new EmberReader(stream))
                            {
                                contentsReader.Read(); // Read what we have written with WriteStartSet above

                                var newPolicy = this.childrenRetrievalPolicy == ChildrenRetrievalPolicy.All ?
                                                ChildrenRetrievalPolicy.All : ChildrenRetrievalPolicy.None;
                                var context = new Context(this, number, identifier, newPolicy);
                                child = this.ReadNewChildContents(contentsReader, actualType, context, out childRetrievalState);

                                if (child != null)
                                {
                                    this.children.Add(number, child);
                                }
                            }
                        }
                        else
                        {
                            childRetrievalState = RetrievalState.Complete;
                            child = null;
                        }
                    }
            }
        }
예제 #4
0
        private List <KeyValuePair <string, int> > ReadEnumMap(EmberReader reader)
        {
            reader.AssertInnerNumber(GlowStringIntegerCollection.InnerNumber);
            var result = new List <KeyValuePair <string, int> >();

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                reader.AssertInnerNumber(GlowStringIntegerPair.InnerNumber);
                reader.ReadAndAssertOuter(GlowStringIntegerPair.EntryString.OuterId);
                var entryString = reader.AssertAndReadContentsAsString();
                reader.ReadAndAssertOuter(GlowStringIntegerPair.EntryInteger.OuterId);
                var entryInteger = this.ReadInt(reader, GlowStringIntegerPair.EntryInteger.Name);

                while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
                {
                    reader.Skip();
                }

                result.Add(new KeyValuePair <string, int>(entryString, entryInteger));
            }

            return(result);
        }
예제 #5
0
        private StreamDescription ReadStreamDescriptor(EmberReader reader)
        {
            reader.AssertInnerNumber(GlowStreamDescription.InnerNumber);
            reader.ReadAndAssertOuter(GlowStreamDescription.Format.OuterId);
            var streamFormat = this.ReadEnum <StreamFormat>(reader, GlowStreamDescription.Format.Name);

            reader.ReadAndAssertOuter(GlowStreamDescription.Offset.OuterId);
            var offset = this.ReadInt(reader, GlowStreamDescription.Offset.Name);

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                reader.Skip();
            }

            return(new StreamDescription(streamFormat, offset));
        }
예제 #6
0
        private static void ReadStreamEntry(
            EmberReader reader, IReadOnlyDictionary <int, IEnumerable <IStreamedParameter> > streamedParameters)
        {
            reader.AssertInnerNumber(GlowStreamEntry.InnerNumber);
            int?   identifier = null;
            object rawValue   = null;

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                switch (reader.GetContextSpecificOuterNumber())
                {
                case GlowStreamEntry.StreamIdentifier.OuterNumber:
                    identifier = reader.AssertAndReadContentsAsInt32();
                    break;

                case GlowStreamEntry.StreamValue.OuterNumber:
                    rawValue = reader.ReadContentsAsObject();
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            IEnumerable <IStreamedParameter> group;

            if (identifier.HasValue && streamedParameters.TryGetValue(identifier.Value, out group) &&
                (rawValue != null))
            {
                foreach (var parameter in group)
                {
                    var value = ExtractValue(parameter, rawValue);

                    try
                    {
                        parameter.SetProviderValue(value);
                    }
                    catch (ArgumentException ex)
                    {
                        const string Format = "Read unexpected stream value {0} for the parameter with the path {1}.";
                        throw new ModelException(
                                  string.Format(CultureInfo.InvariantCulture, Format, value, parameter.GetPath()), ex);
                    }
                }
            }
        }
        private void ReadChild(EmberReader reader, ElementType actualType, int number)
        {
            var     childRetrievalState = RetrievalState.Complete;
            Element child;

            this.children.TryGetValue(number, out child);

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                var contextSpecificOuterNumber = reader.GetContextSpecificOuterNumber();

                if (contextSpecificOuterNumber == GlowNode.Contents.OuterNumber)
                {
                    this.ReadChildContents(reader, actualType, number, ref child, out childRetrievalState);
                }
                else
                {
                    if (child == null)
                    {
                        reader.Skip();
                    }
                    else
                    {
                        if (contextSpecificOuterNumber == GlowNode.Children.OuterNumber)
                        {
                            reader.AssertInnerNumber(GlowElementCollection.InnerNumber);
                            childRetrievalState = child.ReadChildren(reader);
                        }
                        else
                        {
                            childRetrievalState = child.ReadAdditionalField(reader, contextSpecificOuterNumber);
                        }
                    }
                }
            }

            if (child != null)
            {
                child.RetrievalState = childRetrievalState;
            }

            this.RetrievalState =
                (this.children.Count == 0 ? RetrievalState.Complete : this.RetrievalState) & childRetrievalState;
        }
예제 #8
0
        private IReadOnlyList <int> ReadSignals(
            EmberReader reader,
            IReadOnlyList <int> signals,
            int outerNumber,
            int innerNumber,
            int numberOuterNumber,
            string numberName)
        {
            List <int> result = new List <int>();

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                if (reader.GetContextSpecificOuterNumber() == outerNumber)
                {
                    reader.AssertInnerNumber(innerNumber);

                    while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
                    {
                        if (reader.GetContextSpecificOuterNumber() == numberOuterNumber)
                        {
                            result.Add(this.ReadInt(reader, numberName));
                        }
                        else
                        {
                            reader.Skip();
                        }
                    }
                }
                else
                {
                    reader.Skip();
                }
            }

            if (signals.Count != result.Count)
            {
                throw new ModelException("Inconsistent source or target counts in matrix.");
            }

            return(result);
        }
예제 #9
0
        internal int ReadTupleDescription(
            EmberReader reader,
            KeyValuePair <string, ParameterType>[] expectedTypes,
            Action <int, KeyValuePair <string, ParameterType> > addItem)
        {
            reader.AssertInnerNumber(GlowTupleDescription.InnerNumber);
            int index;

            for (index = 0; reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer);)
            {
                if (reader.GetContextSpecificOuterNumber() == GlowTupleDescription.TupleItemDescription.OuterNumber)
                {
                    addItem(index, this.ReadTupleItemDescription(reader, expectedTypes, index));
                    ++index;
                }
                else
                {
                    reader.Skip();
                }
            }

            return(index);
        }
예제 #10
0
        internal virtual KeyValuePair <string, ParameterType> ReadTupleItemDescription(
            EmberReader reader, KeyValuePair <string, ParameterType>[] expectedTypes, int index)
        {
            reader.AssertInnerNumber(GlowTupleItemDescription.InnerNumber);
            reader.ReadAndAssertOuter(GlowTupleItemDescription.Type.OuterId);
            var    type = this.ReadEnum <ParameterType>(reader, GlowTupleItemDescription.Type.Name);
            string name = null;

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                switch (reader.GetContextSpecificOuterNumber())
                {
                case GlowTupleItemDescription.TheName.OuterNumber:
                    name = reader.AssertAndReadContentsAsString();
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new KeyValuePair <string, ParameterType>(name, type));
        }
예제 #11
0
        private void ReadConnections(EmberReader reader)
        {
            reader.AssertInnerNumber(GlowConnectionCollection.InnerNumber);

            while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
            {
                switch (reader.GetContextSpecificOuterNumber())
                {
                case GlowConnectionCollection.Connection.OuterNumber:
                    reader.AssertInnerNumber(GlowConnection.InnerNumber);
                    int?  target                      = null;
                    int[] connectedSources            = new int[0];
                    ConnectionOperation   operation   = ConnectionOperation.Absolute;
                    ConnectionDisposition disposition = ConnectionDisposition.Tally;

                    while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer))
                    {
                        switch (reader.GetContextSpecificOuterNumber())
                        {
                        case GlowConnection.Target.OuterNumber:
                            target = this.ReadInt(reader, GlowConnection.Target.Name);
                            break;

                        case GlowConnection.Sources.OuterNumber:
                            connectedSources = reader.AssertAndReadContentsAsInt32Array();
                            break;

                        case GlowConnection.Operation.OuterNumber:
                            operation =
                                this.ReadEnum <ConnectionOperation>(reader, GlowConnection.Operation.Name);
                            break;

                        case GlowConnection.Disposition.OuterNumber:
                            disposition =
                                this.ReadEnum <ConnectionDisposition>(reader, GlowConnection.Disposition.Name);
                            break;

                        default:
                            reader.Skip();
                            break;
                        }
                    }

                    if (target.HasValue && (disposition != ConnectionDisposition.Pending) && !this.HasChanges)
                    {
                        var existingConnectedSources = this.Connections[target.Value];

                        switch (operation)
                        {
                        case ConnectionOperation.Absolute:
                            Insert(existingConnectedSources, connectedSources, true);
                            break;

                        case ConnectionOperation.Connect:
                            Insert(existingConnectedSources, connectedSources, false);
                            break;

                        case ConnectionOperation.Disconnect:
                            foreach (var source in connectedSources)
                            {
                                existingConnectedSources.Remove(source);
                            }

                            break;
                        }
                    }

                    break;

                default:
                    reader.Skip();
                    break;
                }
            }
        }