示例#1
0
        protected IEnumerable <T> GetAllEvents <T>(BeamNode node, bool ignoreCloseEvents = false)
            where T : EventBase
        {
            var closedEvents = new HashSet <T>();

            var currentNode = node;

            while (currentNode != null)
            {
                var evt = currentNode.Evt;
                if (!ignoreCloseEvents && evt is CloseEvent closingEvent)
                {
                    if (closingEvent.ClosedEvent is T closedEvent)
                    {
                        closedEvents.Add(closedEvent);
                    }
                }


                if (evt is T searchedEvent && !closedEvents.Contains(searchedEvent))
                {
                    yield return(searchedEvent);
                }

                currentNode = currentNode.ParentNode;
            }
        }
示例#2
0
        protected IEnumerable <T> GetPrecedingEvents <T>(BeamNode node, T startingEvt, bool turnLimited)
            where T : EventBase
        {
            var currentNode = node;

            while (currentNode != null)
            {
                if (currentNode.Evt == startingEvt)
                {
                    break;
                }

                currentNode = currentNode.ParentNode;
            }
            currentNode = currentNode.ParentNode;
            while (currentNode != null)
            {
                var evt = currentNode.Evt;
                if (turnLimited && evt is TurnStartEvent)
                {
                    yield break;
                }

                if (evt is T searchedEvent)
                {
                    yield return(searchedEvent);
                }

                currentNode = currentNode.ParentNode;
            }
        }
示例#3
0
        protected FrameEvent GetOpenGoal(BeamNode node)
        {
            var currentNode  = node;
            var closedEvents = new HashSet <EventBase>();

            while (currentNode != null)
            {
                if (currentNode.Evt is CloseEvent closeEvt)
                {
                    closedEvents.Add(closeEvt.ClosedEvent);
                }

                if (currentNode.Evt is FrameEvent goalEvent)
                {
                    if (closedEvents.Contains(goalEvent))
                    {
                        //TODO think more about goal closing semantic
                        return(null);
                    }

                    return(goalEvent);
                }

                currentNode = currentNode.ParentNode;
            }

            return(null);
        }
示例#4
0
        public void PushSelf()
        {
            var currentNode = getCurrentNode();
            var newNode     = new BeamNode(currentNode.ParentNode, currentNode.Evt);

            _beam.Remove(currentNode);
            _beam.Add(newNode);
        }
示例#5
0
        protected IEnumerable <T> GetFrameEvents <T>(BeamNode node, bool turnLimited)
            where T : EventBase
        {
            var closedEvents = new HashSet <T>();
            var closedFrames = new HashSet <FrameEvent>();

            var hasTurnStart = false;
            var currentNode  = node;

            while (currentNode != null)
            {
                var evt = currentNode.Evt;
                if (evt is CloseEvent closingEvent)
                {
                    if (closingEvent.ClosedEvent is T closedEvent)
                    {
                        closedEvents.Add(closedEvent);
                    }

                    if (closingEvent.ClosedEvent is FrameEvent cfEvt)
                    {
                        closedFrames.Add(cfEvt);
                    }
                }

                if (hasTurnStart && evt is TurnEndEvent)
                {
                    //read all events till end of the previous
                    break;
                }

                if (turnLimited && evt is TurnStartEvent)
                {
                    hasTurnStart = true;
                }

                if (evt is T searchedEvent && !closedEvents.Contains(searchedEvent))
                {
                    yield return(searchedEvent);
                }

                if (evt is FrameEvent fEvt && !closedFrames.Contains(fEvt))
                {
                    //we found boundary of an active frame
                    break;
                }

                currentNode = currentNode.ParentNode;
            }
        }
示例#6
0
        public static double GetScore(TracedScoreEventBase scoreEvt, BeamNode node)
        {
            //TODO recognition learning belongs here
            var featureScore = 0.0;

            foreach (var feature in scoreEvt.GenerateFeatures(node))
            {
                if (_featureScores.TryGetValue(feature, out var score))
                {
                    featureScore += score;
                    break; //TODO think more about feature semantic
                }
            }
            return(scoreEvt.GetDefaultScore(node) + featureScore);
        }
示例#7
0
        internal IEnumerable <EventBase> GetPreviousTurnEvents(BeamNode node)
        {
            var currentNode = node;

            while (currentNode != null)
            {
                if (currentNode.Evt is TurnStartEvent)
                {
                    break;
                }

                currentNode = currentNode.ParentNode;
            }

            return(GetTurnEvents(currentNode.ParentNode));
        }
示例#8
0
        internal BeamNode GetBestNode()
        {
            var      bestScore = double.NegativeInfinity;
            BeamNode bestNode  = null;

            foreach (var node in _beam)
            {
                var score = GetScore(node);
                if (bestNode == null || score > bestScore)
                {
                    bestScore = score;
                    bestNode  = node;
                }
            }

            return(bestNode);
        }
示例#9
0
        protected double GetScore(BeamNode node)
        {
            var accumulator = 0.0;
            var currentNode = node;

            while (currentNode != null)
            {
                if (currentNode.Evt is TracedScoreEventBase scoreEvt)
                {
                    accumulator += GetScore(scoreEvt, node);
                }

                currentNode = currentNode.ParentNode;
            }

            return(accumulator);
        }
示例#10
0
        internal static IEnumerable <string> GetDescriptions(Concept2 concept, BeamNode node)
        {
            var result      = new List <string>();
            var currentNode = node;

            while (currentNode != null)
            {
                var descriptionEvt = currentNode.Evt as ConceptDescriptionEvent;
                if (descriptionEvt?.Concept == concept)
                {
                    result.Add(descriptionEvt.Description);
                }

                currentNode = currentNode.ParentNode;
            }

            return(result);
        }
示例#11
0
        /// <summary>
        /// Pushes to currently active branch
        /// </summary>
        /// <param name="evt"></param>
        public void Push(EventBase evt)
        {
            var currentLayer = _layers.Peek();

            var newLayer = new List <BeamNode>();

            foreach (var pushNode in currentLayer)
            {
                var oldBeam = new HashSet <BeamNode>(_beam);
                var newNode = new BeamNode(pushNode, evt);
                _beam.Remove(pushNode);
                _beam.Add(newNode);

                try
                {
                    _layers.Push(new List <BeamNode>()
                    {
                        newNode
                    });
                    evt.Accept(this);
                }
                finally
                {
                    newLayer.AddRange(_beam.Except(oldBeam));

                    //auto pop layers so Pops within accept works consistently
                    //the popped layers are not interesting - we care only about the top ones
                    while (_layers.Peek() != currentLayer)
                    {
                        _layers.Pop();
                    }
                }
            }

            _layers.Push(newLayer);
        }
示例#12
0
        internal static Dictionary <Concept2, ConceptInstance> GetPropertyValues(ConceptInstance instance, BeamNode node, bool includeInheritedProps = true, bool includeInstanceProps = true)
        {
            var result = new Dictionary <Concept2, ConceptInstance>();

            var currentNode = node;

            while (currentNode != null)
            {
                var propertySetEvt = currentNode.Evt as PropertySetEvent;
                if (((propertySetEvt?.Target.Instance == instance && includeInstanceProps) || (propertySetEvt?.Target.Concept == instance.Concept && includeInheritedProps)) && !result.ContainsKey(propertySetEvt.Target.Property))
                {
                    //reflect only last value (the freshest one)
                    result.Add(propertySetEvt.Target.Property, propertySetEvt.SubstitutedValue);
                }

                currentNode = currentNode.ParentNode;
            }

            return(result);
        }
示例#13
0
        internal static ConceptInstance GetValue(ConceptInstance instance, Concept2 property, BeamNode node)
        {
            var currentNode = node;

            while (currentNode != null)
            {
                var propertySetEvt = currentNode.Evt as PropertySetEvent;
                if ((propertySetEvt?.Target.Instance == instance || propertySetEvt?.Target.Concept == instance.Concept) && propertySetEvt.Target.Property == property)
                {
                    return(propertySetEvt.SubstitutedValue);
                }

                currentNode = currentNode.ParentNode;
            }

            return(null);
        }
示例#14
0
        internal static ConceptInstance GetValue(ConceptInstance instance, ParamDefinedEvent parameter, BeamNode node)
        {
            var currentNode = node;

            while (currentNode != null)
            {
                var substitutionEvent = currentNode.Evt as PropertySetEvent;
                var propertyMatch     = substitutionEvent?.Target.Property == parameter.Property || parameter.Property == Concept2.Something;

                if (substitutionEvent?.Target.Instance == instance && propertyMatch)
                {
                    return(substitutionEvent.SubstitutedValue);
                }

                currentNode = currentNode.ParentNode;
            }

            return(null);
        }
示例#15
0
        internal static InstanceActivationRequestEvent GetInstanceActivationRequest(ConceptInstance instance, BeamNode node)
        {
            var currentNode = node;

            while (currentNode != null)
            {
                var activationEvt = currentNode.Evt as InstanceActivationRequestEvent;
                if (activationEvt?.Instance == instance)
                {
                    return(activationEvt);
                }

                currentNode = currentNode.ParentNode;
            }

            return(null);
        }
示例#16
0
        protected IEnumerable <ParamDefinedEvent> GetParameterDefinitions(ConceptInstance instance, BeamNode node)
        {
            var result      = new List <ParamDefinedEvent>();
            var currentNode = node;

            while (currentNode != null)
            {
                var tagetDefinedEvt = currentNode.Evt as ParamDefinedEvent;
                if (tagetDefinedEvt?.Concept == instance.Concept)
                {
                    result.Add(tagetDefinedEvt);
                }

                currentNode = currentNode.ParentNode;
            }

            return(result);
        }
示例#17
0
        internal static InputPhraseEvent[] GetPrefixPhrases(InputPhraseEvent phrase, int prefixLength, BeamNode node)
        {
            var result      = new List <InputPhraseEvent>();
            var currentNode = node;

            //find node where prefix starts
            while (currentNode != null && currentNode.Evt != phrase)
            {
                currentNode = currentNode.ParentNode;
            }

            while (currentNode != null)
            {
                if (currentNode.Evt is TurnStartEvent)
                {
                    break;
                }

                if (currentNode.Evt is InputPhraseEvent prefixPhrase)
                {
                    result.Add(prefixPhrase);
                    if (result.Count >= prefixLength)
                    {
                        break;
                    }
                }

                currentNode = currentNode.ParentNode;
            }

            result.Reverse();
            return(result.ToArray());
        }
示例#18
0
 internal static InputPhraseEvent[] GetSufixPhrases(InputPhraseEvent phrase, int ngramLimitCount, BeamNode node)
 {
     throw new NotImplementedException();
 }
示例#19
0
 internal IEnumerable <EventBase> GetTurnEvents(BeamNode node)
 {
     return(GetFrameEvents <EventBase>(node, true));
 }
示例#20
0
 public BeamNode(BeamNode parentNode, EventBase evt)
 {
     ParentNode = parentNode;
     Evt        = evt;
 }
示例#21
0
 public IEnumerable <string> GenerateFeatures(BeamNode node)
 {
     throw new NotImplementedException();
 }