Exemple #1
0
        /// <summary>
        /// <see cref="M:System.Object.Equals(object)"/>
        /// </summary>
        /// <param name="obj">The object to compare with the current object.</param>
        /// <returns>true if the specified object is equal to the current object;
        /// otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (obj.GetType() != this.GetType())
            {
                return(false);
            }

            TagAssociation other = obj as TagAssociation;

            bool ok = this.Operation == other.Operation;

            if (ok)
            {
                ok = this.SourceTag == null ? other.SourceTag == null : this.SourceTag.Equals(other.SourceTag);
            }

            if (ok)
            {
                ok = this.TargetTag == null ? other.TargetTag == null : this.TargetTag.Equals(other.TargetTag);
            }

            return(ok);
        }
        /// <summary>
        /// If the tag alignment suggests action "Change", but the ED can't find this,
        /// we need to patch the corresponding ED item for the corresponding start or end tag as well.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="tagAlignment"></param>
        private void FixTagActions(IList <Core.Tokenization.Token> sourceTokens,
                                   IList <Core.Tokenization.Token> targetTokens,
                                   Core.EditDistance.EditDistance result, TagAssociations tagAlignment)
        {
            // not yet working
            return;

            // NOTE a single ED item may suggest "D" or "I" for a tag. This does not necessarily conflict
            //  with a "C" suggested by the alignment, as a following ED item may suggest
            //  a compensating "I" or "D" which would result in a "M", which is still
            //  compatible with the alignment's "C".

            TagAssociation srcAssoc = null;
            TagAssociation trgAssoc = null;

            foreach (EditDistanceItem edi in result.Items)
            {
                switch (edi.Operation)
                {
                case EditOperation.Identity:
                case EditOperation.Change:
                    srcAssoc = tagAlignment.GetBySourcePosition(edi.Source);
                    trgAssoc = tagAlignment.GetByTargetPosition(edi.Target);
                    if (srcAssoc != null || trgAssoc != null)
                    {
                        // assignment is only valid between tags
                        System.Diagnostics.Debug.Assert(srcAssoc != null && trgAssoc != null);
                        // should also be the same association, otherwise incompatible tags are
                        //  associated
                        System.Diagnostics.Debug.Assert(object.ReferenceEquals(srcAssoc, trgAssoc));
                        System.Diagnostics.Debug.Assert(srcAssoc.SourceTag != null && srcAssoc.TargetTag != null);

                        if (edi.Source == srcAssoc.SourceTag.Start)
                        {
                            System.Diagnostics.Debug.Assert(srcAssoc.SourceTag.StartTagOperation == EditOperation.Undefined);
                            System.Diagnostics.Debug.Assert(srcAssoc.TargetTag.StartTagOperation == EditOperation.Undefined);
                            // source tag start position
                            srcAssoc.SourceTag.StartTagOperation = edi.Operation;
                            srcAssoc.TargetTag.StartTagOperation = edi.Operation;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(edi.Source == srcAssoc.SourceTag.End);
                            System.Diagnostics.Debug.Assert(srcAssoc.SourceTag.EndTagOperation == EditOperation.Undefined);
                            System.Diagnostics.Debug.Assert(srcAssoc.TargetTag.EndTagOperation == EditOperation.Undefined);

                            srcAssoc.SourceTag.EndTagOperation = edi.Operation;
                            srcAssoc.TargetTag.EndTagOperation = edi.Operation;
                        }
                    }
                    break;

                case EditOperation.Insert:
                    trgAssoc = tagAlignment.GetByTargetPosition(edi.Target);
                    if (trgAssoc != null)
                    {
                        if (edi.Target == trgAssoc.TargetTag.Start)
                        {
                            System.Diagnostics.Debug.Assert(trgAssoc.TargetTag.StartTagOperation == EditOperation.Undefined);
                            trgAssoc.TargetTag.StartTagOperation = edi.Operation;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(edi.Target == trgAssoc.TargetTag.End);
                            System.Diagnostics.Debug.Assert(trgAssoc.TargetTag.EndTagOperation == EditOperation.Undefined);
                            trgAssoc.TargetTag.EndTagOperation = edi.Operation;
                        }
                    }
                    break;

                case EditOperation.Delete:
                    srcAssoc = tagAlignment.GetBySourcePosition(edi.Source);
                    if (srcAssoc != null)
                    {
                        if (edi.Source == srcAssoc.SourceTag.Start)
                        {
                            System.Diagnostics.Debug.Assert(srcAssoc.SourceTag.StartTagOperation == EditOperation.Undefined);
                            srcAssoc.SourceTag.StartTagOperation = edi.Operation;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(edi.Source == srcAssoc.SourceTag.End);
                            System.Diagnostics.Debug.Assert(srcAssoc.SourceTag.EndTagOperation == EditOperation.Undefined);
                            srcAssoc.SourceTag.EndTagOperation = edi.Operation;
                        }
                    }
                    break;

                case EditOperation.Move:
                case EditOperation.Undefined:
                default:
                    throw new Exception("Unexpected case");
                }
            }

            // phase 2: detect conflicts

            foreach (TagAssociation ta in tagAlignment)
            {
                EditOperation startOp = EditOperation.Undefined;
                EditOperation endOp   = EditOperation.Undefined;

                if ((ta.SourceTag.StartTagOperation == EditOperation.Insert) &&
                    (ta.TargetTag.StartTagOperation == EditOperation.Delete))
                {
                    startOp = EditOperation.Move;
                }
                else if ((ta.SourceTag.StartTagOperation == EditOperation.Delete) &&
                         (ta.TargetTag.StartTagOperation == EditOperation.Insert))
                {
                    startOp = EditOperation.Move;
                }
                else if (ta.SourceTag.StartTagOperation == ta.TargetTag.StartTagOperation)
                {
                    startOp = ta.SourceTag.StartTagOperation;
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false, "Conflicting start tag operations");
                    startOp = EditOperation.Undefined;
                }

                if ((ta.SourceTag.EndTagOperation == EditOperation.Insert) &&
                    (ta.TargetTag.EndTagOperation == EditOperation.Delete))
                {
                    endOp = EditOperation.Move;
                }
                else if ((ta.SourceTag.EndTagOperation == EditOperation.Delete) &&
                         (ta.TargetTag.EndTagOperation == EditOperation.Insert))
                {
                    endOp = EditOperation.Move;
                }
                else if (ta.SourceTag.EndTagOperation == ta.TargetTag.EndTagOperation)
                {
                    endOp = ta.SourceTag.EndTagOperation;
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false, "Conflicting end tag operations");
                    endOp = EditOperation.Undefined;
                }

                if (startOp != endOp)
                {
                    System.Diagnostics.Debug.Assert(false, "Conflicting tag actions");
                }
            }
        }