Ejemplo n.º 1
0
        private void LeoComplete(ITransitionState transitionState, IState completed, int k)
        {
            var earleySet           = _chart.EarleySets[transitionState.Index];
            var rootTransitionState = earleySet.FindTransitionState(
                transitionState.PreDotSymbol);

            if (rootTransitionState == null)
            {
                rootTransitionState = transitionState;
            }

            var virtualParseNode = CreateVirtualParseNode(completed, k, rootTransitionState);

            var topmostItem = StateFactory.NewState(
                transitionState.DottedRule.Production,
                transitionState.DottedRule.Position,
                transitionState.Origin);

            topmostItem.ParseNode = virtualParseNode;

            if (_chart.Enqueue(k, topmostItem))
            {
                Log("Complete", k, topmostItem);
            }
        }
Ejemplo n.º 2
0
 private bool EnqueueTransition(ITransitionState transitionState)
 {
     if (_transitions == null)
     {
         _transitions = new UniqueList <ITransitionState>();
     }
     return(_transitions.AddUnique(transitionState));
 }
Ejemplo n.º 3
0
        public bool TryGetExistingVirtualNode(
            int location,
            ITransitionState transitionState,
            out VirtualForestNode node)
        {
            var targetState = transitionState.GetTargetState();
            var hash        = ComputeHashCode(targetState.DottedRule.Production.LeftHandSide, targetState.Origin, location);

            return(_virtualNodes.TryGetValue(hash, out node));
        }
Ejemplo n.º 4
0
        private void OptimizeReductionPath(ISymbol searchSymbol, int k)
        {
            IState           t_rule = null;
            ITransitionState previousTransitionState = null;

            var visited = SharedPools.Default <HashSet <IState> >().AllocateAndClear();

            OptimizeReductionPathRecursive(searchSymbol, k, ref t_rule, ref previousTransitionState, visited);
            SharedPools.Default <HashSet <IState> >().ClearAndFree(visited);
        }
Ejemplo n.º 5
0
 public VirtualForestNode(
     int location,
     ITransitionState transitionState,
     IForestNode completedParseNode)
     : this(location,
           transitionState, 
           completedParseNode,
           transitionState.GetTargetState())
 {
 }
Ejemplo n.º 6
0
 public VirtualNode(
     int location, 
     ITransitionState transitionState, 
     INode completedParseNode)
 {
     _transitionState = transitionState;
     _completedParseNode = completedParseNode;
     _children = new ReadWriteList<IAndNode>();
     Location = location;
     Initialize(transitionState);
 }
Ejemplo n.º 7
0
 public VirtualForestNode(
     int location,
     ITransitionState transitionState,
     IForestNode completedParseNode)
     : this(
         location,
         transitionState,
         completedParseNode,
         transitionState.GetTargetState())
 {
 }
Ejemplo n.º 8
0
        protected VirtualForestNode(
            int location,
            ITransitionState transitionState,
            IForestNode completedParseNode,
            IState targetState)
            : base(targetState.Origin, location)
        {
            _paths = new List<VirtualForestNodePath>();

            Symbol = targetState.Production.LeftHandSide;
            _hashCode = ComputeHashCode();
            var path = new VirtualForestNodePath(transitionState, completedParseNode);
            AddUniquePath(path);
        }
Ejemplo n.º 9
0
        protected VirtualForestNode(
            int location,
            ITransitionState transitionState,
            IForestNode completedParseNode,
            IState targetState)
            : base(targetState.Origin, location)
        {
            _paths = new List <VirtualForestNodePath>();

            Symbol    = targetState.DottedRule.Production.LeftHandSide;
            _hashCode = ComputeHashCode();
            var path = new VirtualForestNodePath(transitionState, completedParseNode);

            AddUniquePath(path);
        }
Ejemplo n.º 10
0
        private VirtualForestNode CreateVirtualParseNode(IState completed, int k, ITransitionState rootTransitionState)
        {
            if (_nodeSet.TryGetExistingVirtualNode(
                    k,
                    rootTransitionState,
                    out VirtualForestNode virtualParseNode))
            {
                virtualParseNode.AddUniquePath(
                    new VirtualForestNodePath(rootTransitionState, completed.ParseNode));
            }
            else
            {
                virtualParseNode = new VirtualForestNode(k, rootTransitionState, completed.ParseNode);
                _nodeSet.AddNewVirtualNode(virtualParseNode);
            }

            return(virtualParseNode);
        }
Ejemplo n.º 11
0
        private void OptimizeReductionPathRecursive(
            ISymbol searchSymbol,
            int k,
            ref IState t_rule,
            ref ITransitionState previousTransitionState,
            HashSet <IState> visited)
        {
            var earleySet = _chart.EarleySets[k];

            // if Ii contains a transitive item of the for [B -> b., A, k]
            var transitionState = earleySet.FindTransitionState(searchSymbol);

            if (transitionState != null)
            {
                // then t_rule := B-> b.; t_pos = k;
                previousTransitionState = transitionState;
                t_rule = transitionState;
                return;
            }

            // else if Ii contains exactly one item of the form [B -> a.Ab, k]
            var sourceState = earleySet.FindSourceState(searchSymbol);

            if (sourceState == null)
            {
                return;
            }

            if (!visited.Add(sourceState))
            {
                return;
            }

            // and [B-> aA.b, k] is quasi complete (is b null)
            if (!IsNextStateQuasiComplete(sourceState))
            {
                return;
            }

            // then t_rule := [B->aAb.]; t_pos=k;
            t_rule = StateFactory.NextState(sourceState);

            if (sourceState.Origin != k)
            {
                visited.Clear();
            }

            // T_Update(I0...Ik, B);
            OptimizeReductionPathRecursive(
                sourceState.DottedRule.Production.LeftHandSide,
                sourceState.Origin,
                ref t_rule,
                ref previousTransitionState,
                visited);

            if (t_rule == null)
            {
                return;
            }

            ITransitionState currentTransitionState = null;

            if (previousTransitionState != null)
            {
                currentTransitionState = new TransitionState(
                    searchSymbol,
                    t_rule,
                    sourceState,
                    previousTransitionState.Index);

                previousTransitionState.NextTransition = currentTransitionState;
            }
            else
            {
                currentTransitionState = new TransitionState(
                    searchSymbol,
                    t_rule,
                    sourceState,
                    k);
            }

            if (_chart.Enqueue(k, currentTransitionState))
            {
                Log("Transition", k, currentTransitionState);
            }

            previousTransitionState = currentTransitionState;
        }
Ejemplo n.º 12
0
 private void Initialize(ITransitionState transitionState)
 {
     if (transitionState.ParseNode != null)
     {
         Symbol = transitionState.Production.LeftHandSide;
         Origin = transitionState.Origin;
     }
     else
     {
         Symbol = _transitionState.Reduction.Production.LeftHandSide;
         Origin = _transitionState.Reduction.Origin;
     }
 }
Ejemplo n.º 13
0
 public VirtualForestNodePath(ITransitionState transitionState, IForestNode forestNode)
 {
     TransitionState = transitionState;
     ForestNode      = forestNode;
     _hashCode       = ComputeHashCode(TransitionState, ForestNode);
 }
Ejemplo n.º 14
0
 public bool TryGetExistingVirtualNode(
     int location,
     ITransitionState transitionState,
     out VirtualForestNode node)
 {
     var targetState = transitionState.GetTargetState();
     var hash = ComputeHashCode(targetState.Production.LeftHandSide, targetState.Origin, location);
     return _virtualNodes.TryGetValue(hash, out node);
 }
Ejemplo n.º 15
0
        private void OptimizeReductionPathRecursive(
            ISymbol searchSymbol,
            int k,
            ref IState t_rule,
            ref ITransitionState previousTransitionState)
        {
            var earleySet = _chart.EarleySets[k];

            // if Ii contains a transitive item of the for [B -> b., A, k]
            var transitionState = earleySet.FindTransitionState(searchSymbol);
            if (transitionState != null)
            {
                // then t_rule := B-> b.; t_pos = k;
                previousTransitionState = transitionState;
                t_rule = transitionState;
                return;
            }

            // else if Ii contains exactly one item of the form [B -> a.Ab, k]
            var sourceState = earleySet.FindSourceState(searchSymbol);
            if (sourceState == null)
                return;

            // and [B-> aA.b, k] is quasi complete (is b null)
            if (!IsNextStateQuasiComplete(sourceState))
                return;

            // then t_rule := [B->aAb.]; t_pos=k;
            t_rule = sourceState.NextState();

            // T_Update(I0...Ik, B);
            OptimizeReductionPathRecursive(
                sourceState.Production.LeftHandSide,
                sourceState.Origin,
                ref t_rule,
                ref previousTransitionState);

            if (t_rule == null)
                return;

            ITransitionState currentTransitionState = null;
            if (previousTransitionState != null)
            {
                currentTransitionState = new TransitionState(
                  searchSymbol,
                  t_rule,
                  sourceState,
                  previousTransitionState.Index);

                previousTransitionState.NextTransition = currentTransitionState;
            }
            else
                currentTransitionState = new TransitionState(
                  searchSymbol,
                  t_rule,
                  sourceState,
                  k);

            if (_chart.Enqueue(k, currentTransitionState))
                Log("Transition", k, currentTransitionState);

            previousTransitionState = currentTransitionState;
        }
Ejemplo n.º 16
0
        private void LeoComplete(ITransitionState transitionState, IState completed, int k)
        {
            var earleySet = _chart.EarleySets[transitionState.Index];
            var rootTransitionState = earleySet.FindTransitionState(
                transitionState.PreDotSymbol);

            if (rootTransitionState == null)
                rootTransitionState = transitionState;

            var virtualParseNode = CreateVirtualParseNode(completed, k, rootTransitionState);

            var topmostItem = new NormalState(
                transitionState.Production,
                transitionState.Position,
                transitionState.Origin);

            topmostItem.ParseNode = virtualParseNode;

            if (_chart.Enqueue(k, topmostItem))
                Log("Complete", k, topmostItem);
        }
Ejemplo n.º 17
0
        private VirtualForestNode CreateVirtualParseNode(IState completed, int k, ITransitionState rootTransitionState)
        {
            VirtualForestNode virtualParseNode = null;
            if (!_nodeSet.TryGetExistingVirtualNode(
                k,
                rootTransitionState,
                out virtualParseNode))
            {
                virtualParseNode = new VirtualForestNode(k, rootTransitionState, completed.ParseNode);
                _nodeSet.AddNewVirtualNode(virtualParseNode);
            }
            else
            {
                virtualParseNode.AddUniquePath(
                    new VirtualForestNodePath(rootTransitionState, completed.ParseNode));
            }

            return virtualParseNode;
        }
Ejemplo n.º 18
0
        private void OptimizeReductionPathRecursive(
            ISymbol searchSymbol,
            int k,
            ref IState t_rule,
            ref ITransitionState previousTransitionState)
        {
            var earleySet = _chart.EarleySets[k];
            var transitionState = earleySet.FindTransitionState(searchSymbol);
            if (transitionState != null)
            {
                previousTransitionState = transitionState;
                t_rule = transitionState;
                return;
            }
            var sourceState = earleySet.FindSourceState(searchSymbol);
            if (sourceState == null)
                return;

            if (!IsNextStateQuasiComplete(sourceState))
                return;

            t_rule = sourceState.NextState();

            OptimizeReductionPathRecursive(
                sourceState.Production.LeftHandSide,
                sourceState.Origin,
                ref t_rule,
                ref previousTransitionState);

            if (t_rule == null)
                return;

            ITransitionState currentTransitionState = null;
            if (previousTransitionState != null)
            {
                currentTransitionState = new TransitionState(
                  searchSymbol,
                  t_rule,
                  sourceState,
                  previousTransitionState.Position);

                previousTransitionState.NextTransition = currentTransitionState;
            }
            else
                currentTransitionState = new TransitionState(
                  searchSymbol,
                  t_rule,
                  sourceState,
                  k);

            if (_chart.Enqueue(k, currentTransitionState))
                Log("Transition", k, currentTransitionState);

            previousTransitionState = currentTransitionState;
        }
Ejemplo n.º 19
0
        private void LeoComplete(ITransitionState transitionState, IState completed, int k)
        {
            var earleySet = _chart.EarleySets[transitionState.Position];
            var rootTransitionState = earleySet.FindTransitionState(
                transitionState.PreDotSymbol);

            if (rootTransitionState == null)
                rootTransitionState = transitionState;

            var virtualParseNode = new VirtualNode(k, rootTransitionState, completed.ParseNode);

            var topmostItem = new State(
                transitionState.Production,
                transitionState.Length,
                transitionState.Origin,
                virtualParseNode);

            if (_chart.Enqueue(k, topmostItem))
                Log("Complete", k, topmostItem);
        }
Ejemplo n.º 20
0
 private bool EnqueueTransition(ITransitionState transitionState)
 {
     return _transitions.Enqueue(transitionState);
 }
Ejemplo n.º 21
0
 public VirtualForestNodePath(ITransitionState transitionState, IForestNode forestNode)
 {
     TransitionState = transitionState;
     ForestNode = forestNode;
     _hashCode = ComputeHashCode(TransitionState, ForestNode);
 }
Ejemplo n.º 22
0
 private static int ComputeHashCode(ITransitionState transitionState, IForestNode forestNode)
 {
     return(HashCode.Compute(
                transitionState.GetHashCode(),
                forestNode.GetHashCode()));
 }
Ejemplo n.º 23
0
 private static int ComputeHashCode(ITransitionState transitionState, IForestNode forestNode)
 {
     return HashCode.Compute(
         transitionState.GetHashCode(),
         forestNode.GetHashCode());
 }
Ejemplo n.º 24
0
 private bool EnqueueTransition(ITransitionState transitionState)
 {
     return(_transitions.AddUnique(transitionState));
 }