private static Func<ISequenceNode> BuildAliasCreator(ParseTreeNode node, Context context)
        {
            ParseTreeNode nameNode = FindNode(node, SpeechExpression.NameLiteralName);
            ParseTreeNode argsNode = FindNode(node, SpeechExpression.ArgumentListLiteralName);

            string aliasName = nameNode.Token.Text;
            Func<ISequenceNode>[] args;

            if (argsNode != null)
            {
                ParseTreeNodeList argsChildNodes = argsNode.ChildNodes;
                args = new Func<ISequenceNode>[argsChildNodes.Count];

                for (int i = 0; i < argsChildNodes.Count; i++)
                {
                    args[i] = BuildExpressionCreator(argsChildNodes[i], context);
                }
            }
            else
            {
                args = new Func<ISequenceNode>[0];
            }

            Alias alias = ResourceManager.Instance.GetAlias(aliasName);

            return () =>
            {
                return alias.CreateNode(context, args);
            };
        }
        private static Func<ISequenceNode> BuildExpressionCreator(ParseTreeNode tree, Context context)
        {
            List<Func<ISequenceNode>> funcList = new List<Func<ISequenceNode>>();

            foreach (ParseTreeNode node in tree.ChildNodes)
            {
                switch (node.Term.Name)
                {
                    case SpeechExpression.TextLiteralName:
                        funcList.Add(BuildTextCreator(node, context));
                        break;
                    case SpeechExpression.AliasLiteralName:
                        funcList.Add(BuildAliasCreator(node, context));
                        break;
                    case SpeechExpression.VariableLiteralName:
                        funcList.Add(BuildVariableCreator(node, context));
                        break;
                }
            }

            return () =>
            {
                List<ISequenceNode> nodes = new List<ISequenceNode>();

                foreach (Func<ISequenceNode> creator in funcList)
                {
                    nodes.Add(creator());
                }

                return SequenceFactory.AssembleResultNode(nodes);
            };
        }
        protected override void OnInitNewState(Context context)
        {
            base.OnInitNewState(context);

            m_audio = m_sequence.ToAudio();
            m_audio.InitNewState(context);
        }
        public override string GetValue(XmlElement element, Context context)
        {
            if(element != null)
            {
                if (element.HasAttribute(Name, string.Empty))
                {
                    return element.GetAttribute(Name);
                }
                else if (element.HasAttribute(Name, ResourceManager.VariableNamespace))
                {
                    IValueNode value = (IValueNode)context.GetVariable(element.GetAttribute(Name, ResourceManager.VariableNamespace));
                    value.InitNewState(context);
                    return value.Value;
                }
                else
                {
                    XmlElement valueNode = element[element.Name + "." + Name];

                    if (valueNode != null)
                    {
                        IValueNode value = (IValueNode)SequenceFactory.Instance.CreateChildrenAsSequence(valueNode, context);
                        value.InitNewState(context);
                        return value.Value;
                    }
                }
            }

            return null;
        }
        public static Func<ISequenceNode> ParseExpression(string expression, Context context)
        {
            ParseTree tree = ParseExpressionToTree(expression);

            if(tree.Status == ParseTreeStatus.Error)
            {
                throw new FormatException("Invalid Expression " + tree.ParserMessages[0].Message);
            }

            return BuildExpressionCreator(tree.Root, context);
        }
        public static void BindValues(object instance, XmlElement element, Context context)
        {
            foreach(AbstractAttributeField property in instance.GetType().GetAttributeProperties())
            {
                string value = property.GetValue(element, context);

                if(value != null)
                {
                    property.SetValue(instance, value);
                }
            }
        }
Example #7
0
        public ISequenceNode CreateNode(Context context, params Func<ISequenceNode>[] arguments)
        {
            ISequenceNode sequence = m_sequenceFactory(context);

            if(m_variableInfos.Count != 0)
            {
                for(int i = 0; i < m_variableInfos.Count; i++)
                {
                    VariableInfo info = m_variableInfos[i];
                    Func<ISequenceNode> variable = (i < arguments.Length) ? arguments[i] : () => info.defaultCreator(context);
                    sequence.OverrideVariable(info.name, variable);
                }
            }

            return sequence;
        }
        protected override ISampleProvider CreateProvider(string value, Context context)
        {
            WebRequest request = WebRequest.Create(string.Format(c_apiSearch, ApiKey, value));
            WebResponse response = request.GetResponse();
            XmlDocument document = new XmlDocument();
            document.Load(response.GetResponseStream());
            XmlElement element = (XmlElement)document.SelectSingleNode(".//audio/url");

            if (element != null)
            {
                return LoadAudio(element.InnerText);
            }
            else
            {
                IAudioNode audio = ("Не удалось найти аудио по запросу " + value).WrapStringAsSpeech();
                audio.InitNewState(context);
                return audio;
            }
        }
        public void InitNewState(Context context)
        {
            LocalContext = new Context(context);

            if(m_variables != null)
            {
                foreach (KeyValuePair<string, Func<ISequenceNode>> variable in m_variables)
                {
                    LocalContext.SetVariableFactory(variable.Key, variable.Value);
                }
            }

            ValueBinder.BindValues(this, XmlData, LocalContext);

            if (XmlData != null)
            {
                LoadDataFromXml(XmlData, LocalContext);
            }

            OnInitNewState(LocalContext);
        }
 protected override string InitValue(Context context)
 {
     ChildValue.InitNewState(context);
     return ProcessValue(ChildValue.Value);
 }
 protected override void OnInitNewState(Context context)
 {
     base.OnInitNewState(context);
     m_value = (IValueNode) m_sequence;
     m_value.InitNewState(context);
 }
 protected virtual void OnInitNewState(Context context)
 {
 }
 protected virtual void LoadDataFromXml(XmlElement element, Context context)
 {
 }
        public ISequenceNode DecorateNode(ISequenceNode node, XmlElement element, Context context)
        {
            if(node is IAudioNode)
            {
                return DecorateAudioNode((IAudioNode) node, element, context);
            }

            return node;
        }
        private static Func<ISequenceNode> BuildTextCreator(ParseTreeNode node, Context context)
        {
            string text = node.Token.Text;

            return () =>
            {
                return new TextValueNode(text);
            };
        }
 protected override string InitValue(Context context)
 {
     return Value;
 }
        public ISequenceNode CreateSequence(XmlElement element, Context context)
        {
            ISequenceNode node = CreateSequence(element.LocalName);
            node.XmlData = element;

            return DecorateNode(node, element, context);
        }
 protected override ISampleProvider CreateProvider(string value, Context context)
 {
     return new AudioFileReader(ValueHolder.Value);
 }
Example #19
0
 public Context(Context context)
 {
     m_contextRandom = context.m_contextRandom;
     m_variables = new Dictionary<string, Func<ISequenceNode>>(context.m_variables);
 }
 public abstract string GetValue(XmlElement element, Context context);
 protected override void LoadDataFromXml(XmlElement element, Context context)
 {
     base.LoadDataFromXml(element, context);
     ChildValue = (IValueNode) SequenceFactory.Instance.CreateChildrenAsSequence(element, context);
 }
 public override string GetValue(XmlElement element, Context context)
 {
     return ResourceManager.Instance.GetConstant(Name);
 }
Example #23
0
 protected override ISampleProvider CreateProvider(string value, Context context)
 {
     return SpeechManager.Instance.SpeakText(value, Voice);
 }
        private static Func<ISequenceNode> BuildVariableCreator(ParseTreeNode node, Context context)
        {
            ParseTreeNode nameNode = FindNode(node, SpeechExpression.NameLiteralName);

            string varName = nameNode.Token.Text;

            return () =>
            {
                return context.GetVariable(varName);
            };
        }
 protected override void LoadDataFromXml(XmlElement element, Context context)
 {
     Value = element.InnerText.Trim();
     base.LoadDataFromXml(element, context);
 }
 protected abstract ISampleProvider CreateProvider(string value, Context context);
        public IAudioNode DecorateAudioNode(IAudioNode node, XmlElement element, Context context)
        {
            HashSet<string> existingAttrs = new HashSet<string>();

            for (int i = 0; i < element.Attributes.Count; i++)
            {
                existingAttrs.Add(element.Attributes[i].LocalName);
            }

            foreach (XmlNode child in element.ChildNodes)
            {
                int attrIndex = child.LocalName.IndexOf('.');

                if (attrIndex != -1)
                {
                    existingAttrs.Add(child.LocalName.Substring(attrIndex + 1));
                }
            }

            foreach (DecoratorInfo decorator in m_audioDecorators)
            {
                if (decorator.triggeredProperties.Overlaps(existingAttrs))
                {
                    IAudioDecoratorNode decoratorNode = decorator.ctor();
                    decoratorNode.XmlData = element;
                    decoratorNode.DecoratedNode = node;
                    node = decoratorNode;
                }
            }

            return node;
        }
 protected override void OnInitNewState(Context context)
 {
     base.OnInitNewState(context);
     ValueHolder.InitNewState(context);
     m_provider = CreateProvider(ValueHolder.Value, context);
 }
        public ISequenceNode CreateChildrenAsSequence(XmlElement element, Context context)
        {
            List<ISequenceNode> nodes = new List<ISequenceNode>();

            EnumerateChildren(element, (e) =>
            {
                nodes.Add(CreateSequence(e, context));
            });

            return AssembleResultNode(nodes);
        }
 protected override void OnInitNewState(Context context)
 {
     base.OnInitNewState(context);
     DecoratedNode.InitNewState(LocalContext);
 }