示例#1
0
        private void Gather(T element)
        {
            this.opened.Remove(element);
            this.closed.Add(element);

            if (element.OwnerObject is BinaryOperation)
            {
                BinaryOperation bin = element.OwnerObject as BinaryOperation;
                int             h   = bin.LeftOperand.OwnerWeight.HashCode;
                T next = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    bin[leftTermName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        bin[leftTermName] = next.OwnerObject;
                    }
                }
                h    = bin.RightOperand.OwnerWeight.HashCode;
                next = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    bin[rightTermName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        bin[rightTermName] = next.OwnerObject;
                    }
                }
            }
            else if (element.OwnerObject is UnaryOperation)
            {
                UnaryOperation u = element.OwnerObject as UnaryOperation;
                int            h = u.InnerOperand.OwnerWeight.HashCode;
                T next           = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    u[innerOperandName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        u[innerOperandName] = next.OwnerObject;
                    }
                }
            }
            else if (element.OwnerObject is Term)
            {
                Term t    = element.OwnerObject as Term;
                int  h    = t.Constant.OwnerWeight.HashCode;
                T    next = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    t[constantName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        t[constantName] = next.OwnerObject;
                    }
                }

                h    = t.Coefficient.OwnerWeight.HashCode;
                next = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    t[coefName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        t[coefName] = next.OwnerObject;
                    }
                }

                h    = t.Unknown.OwnerWeight.HashCode;
                next = this.opened.Find(x => x.HashCode == h);
                if (next != null)
                {
                    this.Gather(next);
                    t[unknownName] = next.OwnerObject;
                }
                else
                {
                    next = this.closed.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        t[unknownName] = next.OwnerObject;
                    }
                }
            }
            else if (element.OwnerObject is Sum)
            {
                Sum s = element.OwnerObject as Sum;
                for (int index = 0; index < s.Items.Count(); ++index)
                {
                    int h    = s.Items.ElementAt(index).OwnerWeight.HashCode;
                    T   next = this.opened.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        this.Gather(next);
                        s.Replace(s.Items.ElementAt(index), next.OwnerObject);
                    }
                    else
                    {
                        next = this.closed.Find(x => x.HashCode == h);
                        if (next != null)
                        {
                            s.Replace(s.Items.ElementAt(index), next.OwnerObject);
                        }
                    }
                }
            }
            else if (element.OwnerObject is Product)
            {
                Product p = element.OwnerObject as Product;
                for (int index = 0; index < p.Items.Count(); ++index)
                {
                    int h    = p.Items.ElementAt(index).OwnerWeight.HashCode;
                    T   next = this.opened.Find(x => x.HashCode == h);
                    if (next != null)
                    {
                        this.Gather(next);
                        p.Replace(p.Items.ElementAt(index), next.OwnerObject);
                    }
                    else
                    {
                        next = this.closed.Find(x => x.HashCode == h);
                        if (next != null)
                        {
                            p.Replace(p.Items.ElementAt(index), next.OwnerObject);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Computes a weight from a unary operator
 /// </summary>
 /// <param name="op">unary term object</param>
 /// <returns>weight of this object</returns>
 public static Weight ComputeWeight(UnaryOperation op)
 {
     return(new Weight(UnaryOperatorValueType, op.Operator, new Weight[] { op.InnerOperand.OwnerWeight as Weight }, op));
 }